html_elems.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """
  2. /***************************************************************************
  3. Name : DB Manager
  4. Description : Database manager plugin for QGIS
  5. Date : May 23, 2011
  6. copyright : (C) 2011 by Giuseppe Sucameli
  7. email : brush.tyler@gmail.com
  8. ***************************************************************************/
  9. /***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************/
  17. """
  18. class HtmlContent:
  19. def __init__(self, data):
  20. self.data = data if not isinstance(data, HtmlContent) else data.data
  21. def toHtml(self):
  22. if isinstance(self.data, list) or isinstance(self.data, tuple):
  23. html = ''
  24. for item in self.data:
  25. html += HtmlContent(item).toHtml()
  26. return html
  27. if hasattr(self.data, 'toHtml'):
  28. return self.data.toHtml()
  29. html = str(self.data).replace("\n", "<br>")
  30. return html
  31. def hasContents(self):
  32. if isinstance(self.data, list) or isinstance(self.data, tuple):
  33. empty = True
  34. for item in self.data:
  35. if item.hasContents():
  36. empty = False
  37. break
  38. return not empty
  39. if hasattr(self.data, 'hasContents'):
  40. return self.data.hasContents()
  41. return len(self.data) > 0
  42. class HtmlElem:
  43. def __init__(self, tag, data, attrs=None):
  44. self.tag = tag
  45. self.data = data if isinstance(data, HtmlContent) else HtmlContent(data)
  46. self.attrs = attrs if attrs is not None else dict()
  47. if 'tag' in self.attrs:
  48. self.setTag(self.attrs['tag'])
  49. del self.attrs['tag']
  50. def setTag(self, tag):
  51. self.tag = tag
  52. def getOriginalData(self):
  53. return self.data.data
  54. def setAttr(self, name, value):
  55. self.attrs[name] = value
  56. def getAttrsHtml(self):
  57. html = ''
  58. for k, v in self.attrs.items():
  59. html += ' %s="%s"' % (k, v)
  60. return html
  61. def openTagHtml(self):
  62. return "<%s%s>" % (self.tag, self.getAttrsHtml())
  63. def closeTagHtml(self):
  64. return "</%s>" % self.tag
  65. def toHtml(self):
  66. return "%s%s%s" % (self.openTagHtml(), self.data.toHtml(), self.closeTagHtml())
  67. def hasContents(self):
  68. return self.data.toHtml() != ""
  69. class HtmlParagraph(HtmlElem):
  70. def __init__(self, data, attrs=None):
  71. HtmlElem.__init__(self, 'p', data, attrs)
  72. class HtmlListItem(HtmlElem):
  73. def __init__(self, data, attrs=None):
  74. HtmlElem.__init__(self, 'li', data, attrs)
  75. class HtmlList(HtmlElem):
  76. def __init__(self, items, attrs=None):
  77. # make sure to have HtmlListItem items
  78. items = list(items)
  79. for i, item in enumerate(items):
  80. if not isinstance(item, HtmlListItem):
  81. items[i] = HtmlListItem(item)
  82. HtmlElem.__init__(self, 'ul', items, attrs)
  83. class HtmlTableCol(HtmlElem):
  84. def __init__(self, data, attrs=None):
  85. HtmlElem.__init__(self, 'td', data, attrs)
  86. def closeTagHtml(self):
  87. # FIX INVALID BEHAVIOR: an empty cell as last table's cell break margins
  88. return "&nbsp;%s" % HtmlElem.closeTagHtml(self)
  89. class HtmlTableRow(HtmlElem):
  90. def __init__(self, cols, attrs=None):
  91. # make sure to have HtmlTableCol items
  92. cols = list(cols)
  93. for i, c in enumerate(cols):
  94. if not isinstance(c, HtmlTableCol):
  95. cols[i] = HtmlTableCol(c)
  96. HtmlElem.__init__(self, 'tr', cols, attrs)
  97. class HtmlTableHeader(HtmlTableRow):
  98. def __init__(self, cols, attrs=None):
  99. HtmlTableRow.__init__(self, cols, attrs)
  100. for c in self.getOriginalData():
  101. c.setTag('th')
  102. class HtmlTable(HtmlElem):
  103. def __init__(self, rows, attrs=None):
  104. # make sure to have HtmlTableRow items
  105. rows = list(rows)
  106. for i, r in enumerate(rows):
  107. if not isinstance(r, HtmlTableRow):
  108. rows[i] = HtmlTableRow(r)
  109. HtmlElem.__init__(self, 'table', rows, attrs)
  110. class HtmlWarning(HtmlContent):
  111. def __init__(self, data):
  112. data = ['<img src=":/icons/warning-20px.png">&nbsp;&nbsp; ', data]
  113. HtmlContent.__init__(self, data)
  114. class HtmlSection(HtmlContent):
  115. def __init__(self, title, content=None):
  116. data = ['<div class="section"><h2>', title, '</h2>']
  117. if content is not None:
  118. data.extend(['<div>', content, '</div>'])
  119. data.append('</div>')
  120. HtmlContent.__init__(self, data)