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
12 class RSpecView(QTreeView):
13 def __init__(self, parent):
14 QTreeView.__init__(self, parent)
16 self.setAnimated(True)
17 self.setItemsExpandable(True)
18 self.setRootIsDecorated(True)
19 self.setHeaderHidden(True)
20 self.setAttribute(Qt.WA_MacShowFocusRect, 0)
21 self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
23 def expandMatchingText(self, txt):
27 def recursiveExpand(index):
28 parent = index.parent()
29 if parent and parent.isValid():
30 recursiveExpand(parent)
34 if index.data().toString() == txt:
35 recursiveExpand(index)
36 self.scrollTo(index, self.PositionAtCenter)
38 rows = model.rowCount(index)
40 child_index = index.child(r, 0)
44 root_rows = model.rowCount()
45 for r in range(root_rows):
46 index = model.index(r, 0)
50 class DomModel(QAbstractItemModel):
51 def __init__(self, document, parent = 0):
52 QAbstractItemModel.__init__(self, parent)
53 self.domDocument = document
54 # one of the children of the rootItem is the 'xml' thing.
56 childList = document.childNodes()
57 for i in range(childList.count()):
58 currElem = childList.item(i)
59 if (currElem.nodeType() == QDomNode.ProcessingInstructionNode):
60 document.removeChild(currElem)
62 self.rootItem = DomItem(document, 0);
64 def data(self, index, role = Qt.DisplayRole):
65 # sometimes it return a QString, sometimes a QVariant. not good.
66 if not index.isValid():
68 if role != Qt.DisplayRole:
70 node = index.internalPointer().node()
71 attributeMap = node.attributes()
75 if node.nodeType() == QDomNode.ElementNode:
76 qslist = QStringList()
77 for i in range(attributeMap.count()):
78 attr = attributeMap.item(i)
79 elem = ' <b>%s</b>="<font color="#1e90ff">%s</font>"' % (attr.nodeName(), attr.nodeValue())
81 return QString('<<b><font color="#b42be2">%s</font></b>%s>'% (node.nodeName(), qslist.join(' ')))
82 elif node.nodeType() == QDomNode.AttributeNode:
83 return QString('Whozat?!')
84 elif node.nodeType() == QDomNode.TextNode:
85 return node.nodeValue()
86 elif node.nodeType() == QDomNode.CDATASectionNode:
87 return QString('unsupported node type')
88 elif node.nodeType() == QDomNode.EntityReferenceNode:
89 return QString('unsupported node type')
90 elif node.nodeType() == QDomNode.EntityNode:
91 return QString('unsupported node type')
92 elif node.nodeType() == QDomNode.ProcessingInstructionNode:
94 #return node.nodeName()
95 elif node.nodeType() == QDomNode.CommentNode:
96 return QString('#').append(node.nodeValue())
97 elif node.nodeType() == QDomNode.DocumentNode:
98 return QString('unsupported node type')
99 elif node.nodeType() == QDomNode.DocumentTypeNode:
100 return QString('unsupported node type')
101 elif node.nodeType() == QDomNode.DocumentFragmentNode:
102 return QString('unsupported node type')
103 elif node.nodeType() == QDomNode.NotationNode:
104 return QString('unsupported node type')
105 elif node.nodeType() == QDomNode.BaseNode:
106 return QString('unsupported node type')
107 elif node.nodeType() == QDomNode.CharacterDataNode:
108 return QString('unsupported node type')
114 def flags(self, index):
115 if not index.isValid():
116 return Qt.ItemIsEnabled
117 return Qt.ItemIsEnabled | Qt.ItemIsSelectable
119 def headerData(self, section, orientation, role):
122 def index(self, row, column, parent=None):
123 if not parent or not parent.isValid():
124 parentItem = self.rootItem
126 parentItem = parent.internalPointer()
128 childItem = parentItem.child(row)
129 # childItem would be None to say "false"?
131 return self.createIndex(row, column, childItem)
135 def parent(self, child):
136 if not child.isValid():
138 childItem = child.internalPointer()
139 parentItem = childItem.parent()
141 if not parentItem or parentItem == self.rootItem:
143 return self.createIndex(parentItem.row(), 0, parentItem)
145 def rowCount(self, parent=None):
146 if not parent or not parent.isValid():
147 parentItem = self.rootItem
149 parentItem = parent.internalPointer()
151 return parentItem.node().childNodes().count()
153 def columnCount(self, parent):
154 # just one column we'll print tag name (and attributes) or the
160 # wrapper around PyQt4.QtXml.QDomNode it keeps an hash of
161 # childrens for performance reasons
163 def __init__(self, node, row, parent = 0):
164 # node is of type PyQt4.QtXml.QDomNode
166 self.parentItem = parent
171 if i in self.childItems:
172 return self.childItems[i]
173 if i >= 0 and i < self.domNode.childNodes().count():
174 childNode = self.domNode.childNodes().item(i)
175 childItem = DomItem(childNode, i, self)
176 self.childItems[i] = childItem
181 return self.parentItem
187 return self.rowNumber
190 class RSpecWindow(QDialog):
191 def __init__(self, parent=None):
192 QDialog.__init__(self, parent)
193 self.setWindowTitle("RSPec View")
198 self.view = RSpecView(self)
199 self.delegate = RSpecDelegate(self)
200 self.view.setItemDelegate(self.delegate)
201 layout = QVBoxLayout()
202 layout.addWidget(self.view)
203 self.setLayout(layout)
211 def showNode(self, hostname):
212 self.view.expandMatchingText(hostname)
214 def updateView(self):
220 rspec_file = config.getSliceRSpecFile()
221 if not os.path.exists(rspec_file):
224 self.document = QDomDocument("RSpec")
225 self.document.setContent(open(rspec_file,'r').read())
226 self.model = DomModel(self.document, self)
228 self.view.setModel(self.model)
229 self.view.expand(self.model.index(0, 0)) #expand first level only
232 class RSpecDelegate(QAbstractItemDelegate):
234 def __init__(self, parent=None):
235 print "init-ing the delegate"
236 QAbstractItemDelegate.__init__(self, parent)
238 def paint(self, painter, option, index):
239 text = index.model().data(index)
240 palette = QApplication.palette()
241 document = QTextDocument()
242 document.setDefaultFont(option.font)
243 if option.state & QStyle.State_Selected:
244 rx = QRegExp(QString('<font .*>'))
246 # If selected, I remove the <font color="..."> by hand,
247 # and give the highlight color
248 document.setHtml(QString("<font color=%1>%2</font>") \
249 .arg(palette.highlightedText().color().name())\
250 .arg(text.replace(rx, QString('')).
251 replace(QString('</font>'),QString(''))))
253 document.setHtml(text)
254 color = palette.highlight().color() \
255 if option.state & QStyle.State_Selected \
256 else palette.base().color()
258 # voodoo: if not highlighted, filling the rect
259 # with the base color makes no difference
260 painter.fillRect(option.rect, color)
261 painter.translate(option.rect.x(), option.rect.y())
262 document.drawContents(painter)
265 def sizeHint(self, option, index):
266 fm = option.fontMetrics
267 text = index.model().data(index)
268 document = QTextDocument()
269 document.setDefaultFont(option.font)
270 document.setHtml(text)
271 # the +5 is for margin. The +4 is voodoo;
272 # fm.height just give it too small.
273 return QSize(document.idealWidth() + 5, fm.height() + 4)