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)
36 self.rootItem = DomItem(document, 0);
38 def data(self, index, role = Qt.DisplayRole):
39 # for interesting nodes, returns a dict wrapped into a QVariant.
40 if not index.isValid():
42 if role != Qt.DisplayRole:
44 node = index.internalPointer().node()
45 attributeMap = node.attributes()
49 if node.nodeType() == QDomNode.ElementNode:
50 qslist = QStringList()
51 for i in range(attributeMap.count()):
52 attr = attributeMap.item(i)
53 elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue())
55 ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' '))
58 {'nodeType':QVariant(QString('element')),
59 'content':ElemNameAndAtts})
60 elif node.nodeType() == QDomNode.AttributeNode:
63 elif node.nodeType() == QDomNode.TextNode:
66 {'nodeType':QVariant(QString('text')),
67 'content':node.nodeValue()})
68 elif node.nodeType() == QDomNode.CDATASectionNode:
70 return QString('unsupported node type')
71 elif node.nodeType() == QDomNode.EntityReferenceNode:
73 return QString('unsupported node type')
74 elif node.nodeType() == QDomNode.EntityNode:
76 return QString('unsupported node type')
77 elif node.nodeType() == QDomNode.ProcessingInstructionNode:
80 elif node.nodeType() == QDomNode.CommentNode:
83 {'nodeType':QVariant(QString('comment')),
84 'content':node.nodeValue()})
85 elif node.nodeType() == QDomNode.DocumentNode:
87 return QString('unsupported node type')
88 elif node.nodeType() == QDomNode.DocumentTypeNode:
90 return QString('unsupported node type')
91 elif node.nodeType() == QDomNode.DocumentFragmentNode:
93 return QString('unsupported node type')
94 elif node.nodeType() == QDomNode.NotationNode:
96 return QString('unsupported node type')
97 elif node.nodeType() == QDomNode.BaseNode:
99 return QString('unsupported node type')
100 elif node.nodeType() == QDomNode.CharacterDataNode:
102 return QString('unsupported node type')
110 def flags(self, index):
111 if not index.isValid():
112 return Qt.ItemIsEnabled
113 return Qt.ItemIsEnabled | Qt.ItemIsSelectable
115 def headerData(self, section, orientation, role):
118 def index(self, row, column, parent=None):
119 if not parent or not parent.isValid():
120 parentItem = self.rootItem
122 parentItem = parent.internalPointer()
124 childItem = parentItem.child(row)
125 # childItem would be None to say "false"?
127 return self.createIndex(row, column, childItem)
131 def parent(self, child):
132 if not child.isValid():
134 childItem = child.internalPointer()
135 parentItem = childItem.parent()
137 if not parentItem or parentItem == self.rootItem:
139 return self.createIndex(parentItem.row(), 0, parentItem)
141 def rowCount(self, parent=None):
142 if not parent or not parent.isValid():
143 parentItem = self.rootItem
145 parentItem = parent.internalPointer()
147 return parentItem.node().childNodes().count()
149 def columnCount(self, parent):
150 # just one column we'll print tag name (and attributes) or the
156 # wrapper around PyQt4.QtXml.QDomNode it keeps an hash of
157 # childrens for performance reasons
159 def __init__(self, node, row, parent = 0):
160 # node is of type PyQt4.QtXml.QDomNode
162 self.parentItem = parent
167 if i in self.childItems:
168 return self.childItems[i]
169 if i >= 0 and i < self.domNode.childNodes().count():
170 childNode = self.domNode.childNodes().item(i)
171 childItem = DomItem(childNode, i, self)
172 self.childItems[i] = childItem
177 return self.parentItem
183 return self.rowNumber
185 class XmlView(QTreeView):
186 def __init__(self, parent=None):
187 QTreeView.__init__(self, parent)
189 delegate = XmlDelegate(self)
190 delegate.insertNodeDelegate('element', ElemNodeDelegate())
191 delegate.insertNodeDelegate('text', TextNodeDelegate())
192 delegate.insertNodeDelegate('comment', CommentNodeDelegate())
193 self.setItemDelegate(delegate)
195 self.setAnimated(True)
196 self.setItemsExpandable(True)
197 self.setRootIsDecorated(True)
198 self.setHeaderHidden(True)
199 self.setAttribute(Qt.WA_MacShowFocusRect, 0)
200 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
202 class XmlDelegate(QItemDelegate):
204 def __init__(self, parent=None):
205 QAbstractItemDelegate.__init__(self, parent)
208 def insertNodeDelegate(self, nodeType, delegate):
209 delegate.setParent(self)
210 self.delegates[nodeType] = delegate
212 def removeNodeDelegate(self, nodeType, delegate):
213 if nodeType in self.delegates:
214 del self.delegates[nodeType]
216 def paint(self, painter, option, index):
217 print "ASKING FOR DATA"
218 dataAsQVarMap = index.model().data(index)
220 nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
221 delegate = self.delegates.get(nodeType)
222 #print "DELEGS DICT:", self.delegates
223 #print "NODETYPE:", nodeType.toString()
224 if delegate is not None:
225 #print "WOW DELEG ISNT NONE"
226 delegate.paint(painter, option, index)
229 # not sure this will ever work. this delegate
230 # doesn't know about my QMap strategy.
231 QItemDelegate.paint(self, painter, option, index)
233 # def sizeHint(self, option, index):
234 # fm = option.fontMetrics
235 # print "TYPE:", str(type(index.model().data(index).convert(QObject)))
236 # text = "the fish doesn't talk"
237 # #text = str(index.model().data(index).property('content').toString())
238 # nodeType = str(index.model().data(index).property('nodeType').toString())
239 # if nodeType == 'element' or nodeType == 'comment':
241 # elif nodeType == 'text':
242 # numlines = text.count('\n')
243 # sys.__stdout__.write("TEXT: \n" + text)
246 # document = QTextDocument()
247 # document.setDefaultFont(option.font)
248 # document.setHtml(text)
249 # # the +5 is for margin. The +4 is voodoo;
250 # # fm.height just give it too small.
251 # return QSize(document.idealWidth() + 5, (fm.height() + 4) * numlines)
253 class ElemNodeDelegate(QAbstractItemDelegate):
254 def paint(self, painter, option, index):
255 palette = QApplication.palette()
256 document = QTextDocument()
257 document.setDefaultFont(option.font)
258 nonHighGlobPattern = '<<b><font color="#b42be2">%s</font></b>%s>'
259 nonHighAttPattern = ' <b>%s</b>="<font color="#1e90ff">%s</font>"'
260 highGlobPattern = '<<b>%s</b>%s>'
261 highAttPattern = ' <b>%s</b>="%s"'
262 def getHtmlText(plainText, globPattern, attPattern):
263 tmp = plainText.split(' ', 1)
267 # many elems don't have atts...
268 attList = tmp[1].split()
272 attValue = tmp[1][1:-1]
273 AttListHtml += (nonHighAttPattern % (attName, attValue))
274 html = (globPattern % (elemName, AttListHtml))
276 def colorize(color, text):
277 return '<font color=' + color + '>' + text + '</font>'
278 dataAsQVarMap = index.model().data(index)
279 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
280 if option.state & QStyle.State_Selected:
281 htmlText = colorize(palette.highlightedText().color().name(),
282 getHtmlText(text, highGlobPattern, highAttPattern))
283 document.setHtml(QString(htmlText))
285 htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern)
286 document.setHtml(QString(htmlText))
287 color = palette.highlight().color() \
288 if option.state & QStyle.State_Selected \
289 else palette.base().color()
291 # voodoo: if not highlighted, filling the rect
292 # with the base color makes no difference
293 painter.fillRect(option.rect, color)
294 painter.translate(option.rect.x(), option.rect.y())
295 document.drawContents(painter)
298 class TextNodeDelegate(QAbstractItemDelegate):
299 def paint(self, painter, option, index):
300 palette = QApplication.palette()
301 document = QTextDocument()
302 document.setDefaultFont(option.font)
303 def verbatimize(text):
304 text.replace('\n', '<br>')
305 return '<pre>' + text + '</pre'
306 def colorize(color, text):
307 return '<font color=' + color + '>' + text + '</font>'
308 dataAsQVarMap = index.model().data(index)
309 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
310 if option.state & QStyle.State_Selected:
311 htmlText = colorize(palette.highlightedText().color().name(),
313 document.setHtml(QString(htmlText))
315 htmlText = verbatimize(text)
316 document.setHtml(QString(htmlText))
317 color = palette.highlight().color() \
318 if option.state & QStyle.State_Selected \
319 else palette.base().color()
321 # voodoo: if not highlighted, filling the rect
322 # with the base color makes no difference
323 painter.fillRect(option.rect, color)
324 painter.translate(option.rect.x(), option.rect.y())
325 document.drawContents(painter)
328 class CommentNodeDelegate(QAbstractItemDelegate):
329 def paint(self, painter, option, index):
330 paint(self, painter, option, index)
332 def paint(self, painter, option, index):
333 text = index.model().data(index).property('content').toString()
334 palette = QApplication.palette()
335 document = QTextDocument()
336 document.setDefaultFont(option.font)
337 if option.state & QStyle.State_Selected:
338 rx = QRegExp(QString('<font .*>'))
340 # If selected, I remove the <font color="..."> by hand,
341 # and give the highlight color
342 document.setHtml(QString("<font color=%1>%2</font>") \
343 .arg(palette.highlightedText().color().name())\
344 .arg(text.replace(rx, QString('')).
345 replace(QString('</font>'),QString(''))))
347 document.setHtml(text)
348 color = palette.highlight().color() \
349 if option.state & QStyle.State_Selected \
350 else palette.base().color()
352 # voodoo: if not highlighted, filling the rect
353 # with the base color makes no difference
354 painter.fillRect(option.rect, color)
355 painter.translate(option.rect.x(), option.rect.y())
356 document.drawContents(painter)