4 from PyQt4.QtCore import *
5 from PyQt4.QtGui import *
6 from PyQt4.QtXml import *
8 from sface.config import config
9 from sface.screens.sfascreen import SfaScreen
11 def QVarMapAccess(qv, key):
12 # helper function. qv is a dict wrapped into a QVariant
14 print "DICT:", qv.toMap()
15 if len(qv.toMap().keys()) == 0:
18 traceback.print_stack()
21 return qv.toMap()[QString(key)].toString()
24 class DomModel(QAbstractItemModel):
25 def __init__(self, document, parent = 0):
26 QAbstractItemModel.__init__(self, parent)
27 self.domDocument = document
28 # one of the children of the rootItem is the 'xml' thing.
30 childList = document.childNodes()
31 for i in range(childList.count()):
32 currElem = childList.item(i)
33 if (currElem.nodeType() == QDomNode.ProcessingInstructionNode):
34 document.removeChild(currElem)
37 self.rootItem = DomItem(document, 0);
39 def data(self, index, role = Qt.DisplayRole):
40 # for interesting nodes, returns a dict wrapped into a QVariant.
41 if not index.isValid():
43 if role != Qt.DisplayRole:
45 node = index.internalPointer().node()
46 attributeMap = node.attributes()
50 if node.nodeType() == QDomNode.ElementNode:
51 qslist = QStringList()
52 for i in range(attributeMap.count()):
53 attr = attributeMap.item(i)
54 elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue())
56 ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' '))
59 {'nodeType':QVariant(QString('element')),
60 'content':ElemNameAndAtts})
61 elif node.nodeType() == QDomNode.AttributeNode:
64 elif node.nodeType() == QDomNode.TextNode:
67 {'nodeType':QVariant(QString('text')),
68 'content':node.nodeValue()})
69 elif node.nodeType() == QDomNode.CDATASectionNode:
71 return QString('unsupported node type')
72 elif node.nodeType() == QDomNode.EntityReferenceNode:
74 return QString('unsupported node type')
75 elif node.nodeType() == QDomNode.EntityNode:
77 return QString('unsupported node type')
78 elif node.nodeType() == QDomNode.ProcessingInstructionNode:
81 elif node.nodeType() == QDomNode.CommentNode:
84 {'nodeType':QVariant(QString('comment')),
85 'content':node.nodeValue()})
86 elif node.nodeType() == QDomNode.DocumentNode:
88 return QString('unsupported node type')
89 elif node.nodeType() == QDomNode.DocumentTypeNode:
91 return QString('unsupported node type')
92 elif node.nodeType() == QDomNode.DocumentFragmentNode:
94 return QString('unsupported node type')
95 elif node.nodeType() == QDomNode.NotationNode:
97 return QString('unsupported node type')
98 elif node.nodeType() == QDomNode.BaseNode:
100 return QString('unsupported node type')
101 elif node.nodeType() == QDomNode.CharacterDataNode:
103 return QString('unsupported node type')
111 def flags(self, index):
112 if not index.isValid():
113 return Qt.ItemIsEnabled
114 return Qt.ItemIsEnabled | Qt.ItemIsSelectable
116 def headerData(self, section, orientation, role):
119 def index(self, row, column, parent=None):
120 if not parent or not parent.isValid():
121 parentItem = self.rootItem
123 parentItem = parent.internalPointer()
125 childItem = parentItem.child(row)
126 # childItem would be None to say "false"?
128 return self.createIndex(row, column, childItem)
132 def parent(self, child):
133 if not child.isValid():
135 childItem = child.internalPointer()
136 parentItem = childItem.parent()
138 if not parentItem or parentItem == self.rootItem:
140 return self.createIndex(parentItem.row(), 0, parentItem)
142 def rowCount(self, parent=None):
143 if not parent or not parent.isValid():
144 parentItem = self.rootItem
146 parentItem = parent.internalPointer()
148 return parentItem.node().childNodes().count()
150 def columnCount(self, parent):
151 # just one column we'll print tag name (and attributes) or the
157 # wrapper around PyQt4.QtXml.QDomNode it keeps an hash of
158 # childrens for performance reasons
160 def __init__(self, node, row, parent = 0):
161 # node is of type PyQt4.QtXml.QDomNode
163 self.parentItem = parent
168 if i in self.childItems:
169 return self.childItems[i]
170 if i >= 0 and i < self.domNode.childNodes().count():
171 childNode = self.domNode.childNodes().item(i)
172 childItem = DomItem(childNode, i, self)
173 self.childItems[i] = childItem
178 return self.parentItem
184 return self.rowNumber
186 class XmlView(QTreeView):
187 def __init__(self, parent=None):
188 QTreeView.__init__(self, parent)
190 delegate = XmlDelegate(self)
191 delegate.insertNodeDelegate('element', ElemNodeDelegate())
192 delegate.insertNodeDelegate('text', TextNodeDelegate())
193 delegate.insertNodeDelegate('comment', CommentNodeDelegate())
194 self.setItemDelegate(delegate)
196 self.setAnimated(True)
197 self.setItemsExpandable(True)
198 self.setRootIsDecorated(True)
199 self.setHeaderHidden(True)
200 self.setAttribute(Qt.WA_MacShowFocusRect, 0)
201 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
203 class XmlDelegate(QItemDelegate):
205 def __init__(self, parent=None):
206 QAbstractItemDelegate.__init__(self, parent)
209 def insertNodeDelegate(self, nodeType, delegate):
210 delegate.setParent(self)
211 self.delegates[nodeType] = delegate
213 def removeNodeDelegate(self, nodeType, delegate):
214 if nodeType in self.delegates:
215 del self.delegates[nodeType]
217 def paint(self, painter, option, index):
218 print "ASKING FOR DATA"
219 dataAsQVarMap = index.model().data(index)
221 nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
222 delegate = self.delegates.get(nodeType)
223 #print "DELEGS DICT:", self.delegates
224 #print "NODETYPE:", nodeType.toString()
225 if delegate is not None:
226 #print "WOW DELEG ISNT NONE"
227 delegate.paint(painter, option, index)
230 # not sure this will ever work. this delegate
231 # doesn't know about my QMap strategy.
232 QItemDelegate.paint(self, painter, option, index)
234 # def sizeHint(self, option, index):
235 # fm = option.fontMetrics
236 # print "TYPE:", str(type(index.model().data(index).convert(QObject)))
237 # text = "the fish doesn't talk"
238 # #text = str(index.model().data(index).property('content').toString())
239 # nodeType = str(index.model().data(index).property('nodeType').toString())
240 # if nodeType == 'element' or nodeType == 'comment':
242 # elif nodeType == 'text':
243 # numlines = text.count('\n')
244 # sys.__stdout__.write("TEXT: \n" + text)
247 # document = QTextDocument()
248 # document.setDefaultFont(option.font)
249 # document.setHtml(text)
250 # # the +5 is for margin. The +4 is voodoo;
251 # # fm.height just give it too small.
252 # return QSize(document.idealWidth() + 5, (fm.height() + 4) * numlines)
254 class ElemNodeDelegate(QAbstractItemDelegate):
255 def paint(self, painter, option, index):
256 palette = QApplication.palette()
257 document = QTextDocument()
258 document.setDefaultFont(option.font)
259 nonHighGlobPattern = '<<b><font color="#b42be2">%s</font></b>%s>'
260 nonHighAttPattern = ' <b>%s</b>="<font color="#1e90ff">%s</font>"'
261 highGlobPattern = '<<b>%s</b>%s>'
262 highAttPattern = ' <b>%s</b>="%s"'
263 def getHtmlText(plainText, globPattern, attPattern):
264 tmp = plainText.split(' ', 1)
268 # many elems don't have atts...
269 attList = tmp[1].split()
273 attValue = tmp[1][1:-1]
274 AttListHtml += (attPattern % (attName, attValue))
275 html = (globPattern % (elemName, AttListHtml))
277 def colorize(color, text):
278 return '<font color=' + color + '>' + text + '</font>'
279 dataAsQVarMap = index.model().data(index)
280 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
281 if option.state & QStyle.State_Selected:
282 htmlText = colorize(palette.highlightedText().color().name(),
283 getHtmlText(text, highGlobPattern, highAttPattern))
285 print getHtmlText(text, highGlobPattern, highAttPattern)
286 document.setHtml(QString(htmlText))
288 htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern)
289 document.setHtml(QString(htmlText))
290 color = palette.highlight().color() \
291 if option.state & QStyle.State_Selected \
292 else palette.base().color()
294 # voodoo: if not highlighted, filling the rect
295 # with the base color makes no difference
296 painter.fillRect(option.rect, color)
297 painter.translate(option.rect.x(), option.rect.y())
298 document.drawContents(painter)
301 class TextNodeDelegate(QAbstractItemDelegate):
302 def paint(self, painter, option, index):
303 palette = QApplication.palette()
304 document = QTextDocument()
305 document.setDefaultFont(option.font)
306 def verbatimize(text):
307 text.replace('\n', '<br>')
308 return '<pre>' + text + '</pre'
309 def colorize(color, text):
310 return '<font color=' + color + '>' + text + '</font>'
311 dataAsQVarMap = index.model().data(index)
312 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
313 if option.state & QStyle.State_Selected:
314 htmlText = colorize(palette.highlightedText().color().name(),
316 document.setHtml(QString(htmlText))
318 htmlText = verbatimize(text)
319 document.setHtml(QString(htmlText))
320 color = palette.highlight().color() \
321 if option.state & QStyle.State_Selected \
322 else palette.base().color()
324 # voodoo: if not highlighted, filling the rect
325 # with the base color makes no difference
326 painter.fillRect(option.rect, color)
327 painter.translate(option.rect.x(), option.rect.y())
328 document.drawContents(painter)
331 class CommentNodeDelegate(QAbstractItemDelegate):
332 def paint(self, painter, option, index):
333 paint(self, painter, option, index)
335 def paint(self, painter, option, index):
336 text = index.model().data(index).property('content').toString()
337 palette = QApplication.palette()
338 document = QTextDocument()
339 document.setDefaultFont(option.font)
340 if option.state & QStyle.State_Selected:
341 rx = QRegExp(QString('<font .*>'))
343 # If selected, I remove the <font color="..."> by hand,
344 # and give the highlight color
345 document.setHtml(QString("<font color=%1>%2</font>") \
346 .arg(palette.highlightedText().color().name())\
347 .arg(text.replace(rx, QString('')).
348 replace(QString('</font>'),QString(''))))
350 document.setHtml(text)
351 color = palette.highlight().color() \
352 if option.state & QStyle.State_Selected \
353 else palette.base().color()
355 # voodoo: if not highlighted, filling the rect
356 # with the base color makes no difference
357 painter.fillRect(option.rect, color)
358 painter.translate(option.rect.x(), option.rect.y())
359 document.drawContents(painter)