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