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):
187 QTreeView.__init__(self, parent)
189 self.setAnimated(True)
190 self.setItemsExpandable(True)
191 self.setRootIsDecorated(True)
192 self.setHeaderHidden(True)
193 self.setAttribute(Qt.WA_MacShowFocusRect, 0)
194 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
196 class XmlWindow(QDialog):
197 def __init__(self, parent=None, title='XML Window'):
198 QDialog.__init__(self, parent)
199 self.setWindowTitle(title)
205 self.view = XmlView(self)
206 self.delegate = XmlDelegate(self)
207 self.view.setItemDelegate(self.delegate)
208 self.delegate.insertNodeDelegate('element', ElemNodeDelegate())
209 self.delegate.insertNodeDelegate('text', TextNodeDelegate())
210 self.delegate.insertNodeDelegate('comment', CommentNodeDelegate())
211 layout = QVBoxLayout()
212 layout.addWidget(self.view)
213 self.setLayout(layout)
221 def updateView(self):
227 self.document = QDomDocument(self.title)
228 self.model = DomModel(self.document, self)
230 self.view.setModel(self.model)
231 self.view.expand(self.model.index(0, 0)) #expand first level only
233 #move the code below to rspec window
234 rspec_file = config.getSliceRSpecFile()
235 if not os.path.exists(rspec_file):
238 self.document.setContent(open(rspec_file,'r').read())
242 class XmlDelegate(QItemDelegate):
244 def __init__(self, parent=None):
245 QAbstractItemDelegate.__init__(self, parent)
248 def insertNodeDelegate(self, nodeType, delegate):
249 delegate.setParent(self)
250 self.delegates[nodeType] = delegate
252 def removeNodeDelegate(self, nodeType, delegate):
253 if nodeType in self.delegates:
254 del self.delegates[nodeType]
256 def paint(self, painter, option, index):
257 print "ASKING FOR DATA"
258 dataAsQVarMap = index.model().data(index)
260 nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
261 delegate = self.delegates.get(nodeType)
262 #print "DELEGS DICT:", self.delegates
263 #print "NODETYPE:", nodeType.toString()
264 if delegate is not None:
265 #print "WOW DELEG ISNT NONE"
266 delegate.paint(painter, option, index)
269 # not sure this will ever work. this delegate
270 # doesn't know about my QMap strategy.
271 QItemDelegate.paint(self, painter, option, index)
273 # def sizeHint(self, option, index):
274 # fm = option.fontMetrics
275 # print "TYPE:", str(type(index.model().data(index).convert(QObject)))
276 # text = "the fish doesn't talk"
277 # #text = str(index.model().data(index).property('content').toString())
278 # nodeType = str(index.model().data(index).property('nodeType').toString())
279 # if nodeType == 'element' or nodeType == 'comment':
281 # elif nodeType == 'text':
282 # numlines = text.count('\n')
283 # sys.__stdout__.write("TEXT: \n" + text)
286 # document = QTextDocument()
287 # document.setDefaultFont(option.font)
288 # document.setHtml(text)
289 # # the +5 is for margin. The +4 is voodoo;
290 # # fm.height just give it too small.
291 # return QSize(document.idealWidth() + 5, (fm.height() + 4) * numlines)
293 class ElemNodeDelegate(QAbstractItemDelegate):
294 def paint(self, painter, option, index):
295 palette = QApplication.palette()
296 document = QTextDocument()
297 document.setDefaultFont(option.font)
298 nonHighGlobPattern = '<<b><font color="#b42be2">%s</font></b>%s>'
299 nonHighAttPattern = ' <b>%s</b>="<font color="#1e90ff">%s</font>"'
300 highGlobPattern = '<<b>%s</b>%s>'
301 highAttPattern = ' <b>%s</b>="%s"'
302 def getHtmlText(plainText, globPattern, attPattern):
303 tmp = plainText.split(' ', 1)
307 # many elems don't have atts...
308 attList = tmp[1].split()
312 attValue = tmp[1][1:-1]
313 AttListHtml += (nonHighAttPattern % (attName, attValue))
314 html = (globPattern % (elemName, AttListHtml))
316 def colorize(color, text):
317 return '<font color=' + color + '>' + text + '</font>'
318 dataAsQVarMap = index.model().data(index)
319 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
320 if option.state & QStyle.State_Selected:
321 htmlText = colorize(palette.highlightedText().color().name(),
322 getHtmlText(text, highGlobPattern, highAttPattern))
323 document.setHtml(QString(htmlText))
325 htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern)
326 document.setHtml(QString(htmlText))
327 color = palette.highlight().color() \
328 if option.state & QStyle.State_Selected \
329 else palette.base().color()
331 # voodoo: if not highlighted, filling the rect
332 # with the base color makes no difference
333 painter.fillRect(option.rect, color)
334 painter.translate(option.rect.x(), option.rect.y())
335 document.drawContents(painter)
338 class TextNodeDelegate(QAbstractItemDelegate):
339 def paint(self, painter, option, index):
340 palette = QApplication.palette()
341 document = QTextDocument()
342 document.setDefaultFont(option.font)
343 def verbatimize(text):
344 text.replace('\n', '<br>')
345 return '<pre>' + text + '</pre'
346 def colorize(color, text):
347 return '<font color=' + color + '>' + text + '</font>'
348 dataAsQVarMap = index.model().data(index)
349 text = str(QVarMapAccess(dataAsQVarMap, 'content'))
350 if option.state & QStyle.State_Selected:
351 htmlText = colorize(palette.highlightedText().color().name(),
353 document.setHtml(QString(htmlText))
355 htmlText = verbatimize(text)
356 document.setHtml(QString(htmlText))
357 color = palette.highlight().color() \
358 if option.state & QStyle.State_Selected \
359 else palette.base().color()
361 # voodoo: if not highlighted, filling the rect
362 # with the base color makes no difference
363 painter.fillRect(option.rect, color)
364 painter.translate(option.rect.x(), option.rect.y())
365 document.drawContents(painter)
368 class CommentNodeDelegate(QAbstractItemDelegate):
369 def paint(self, painter, option, index):
370 paint(self, painter, option, index)
372 def paint(self, painter, option, index):
373 text = index.model().data(index).property('content').toString()
374 palette = QApplication.palette()
375 document = QTextDocument()
376 document.setDefaultFont(option.font)
377 if option.state & QStyle.State_Selected:
378 rx = QRegExp(QString('<font .*>'))
380 # If selected, I remove the <font color="..."> by hand,
381 # and give the highlight color
382 document.setHtml(QString("<font color=%1>%2</font>") \
383 .arg(palette.highlightedText().color().name())\
384 .arg(text.replace(rx, QString('')).
385 replace(QString('</font>'),QString(''))))
387 document.setHtml(text)
388 color = palette.highlight().color() \
389 if option.state & QStyle.State_Selected \
390 else palette.base().color()
392 # voodoo: if not highlighted, filling the rect
393 # with the base color makes no difference
394 painter.fillRect(option.rect, color)
395 painter.translate(option.rect.x(), option.rect.y())
396 document.drawContents(painter)