* export PYTHONPATH to sfi.py
[sface.git] / sface / rspecwindow.py
index 45c2508..a0741c4 100644 (file)
@@ -36,6 +36,7 @@ class RSpecView(QTreeView):
             if index.data().toString() == txt:
                 recursiveExpand(index)
                 self.scrollTo(index, self.PositionAtCenter)
+                return
             
             rows = model.rowCount(index)
             for r in range(rows):
@@ -62,7 +63,8 @@ class DomModel(QAbstractItemModel):
                 break
         self.rootItem = DomItem(document, 0);
 
-    def data(self, index, role):
+    def data(self, index, role = Qt.DisplayRole):
+        # sometimes it return a QString, sometimes a QVariant. not good.
         if not index.isValid():
             return QVariant()
         if role != Qt.DisplayRole:
@@ -76,9 +78,9 @@ class DomModel(QAbstractItemModel):
                 qslist = QStringList()
                 for i in range(attributeMap.count()):
                     attr = attributeMap.item(i)
-                    elem = '%s="%s"' % (attr.nodeName(), attr.nodeValue())
+                    elem = ' <b>%s</b>="<font color="#1e90ff">%s</font>"' % (attr.nodeName(), attr.nodeValue())
                     qslist.append(elem)
-                return QString("<%s %s>" % (node.nodeName(), qslist.join(' ')))
+                return QString('&lt;<b><font color="#b42be2">%s</font></b>%s&gt;'% (node.nodeName(), qslist.join(' ')))
             elif node.nodeType() == QDomNode.AttributeNode:
                 return QString('Whozat?!')
             elif node.nodeType() == QDomNode.TextNode:
@@ -190,12 +192,14 @@ class DomItem:
 class RSpecWindow(QDialog):
     def __init__(self, parent=None):
         QDialog.__init__(self, parent)
-        self.setWindowTitle("RSPec View")
+        self.setWindowTitle("RSpec View")
 
         self.document = None
         self.model = None
 
         self.view = RSpecView(self)
+        self.delegate = RSpecDelegate(self)
+        self.view.setItemDelegate(self.delegate)
         layout = QVBoxLayout()
         layout.addWidget(self.view)
         self.setLayout(layout)
@@ -227,3 +231,44 @@ class RSpecWindow(QDialog):
         self.view.expand(self.model.index(0, 0)) #expand first level only
 
 
+class RSpecDelegate(QAbstractItemDelegate):
+    
+    def __init__(self, parent=None):
+        QAbstractItemDelegate.__init__(self, parent)
+
+    def paint(self, painter, option, index):
+        text = index.model().data(index)
+        palette = QApplication.palette()
+        document = QTextDocument()
+        document.setDefaultFont(option.font)
+        if option.state & QStyle.State_Selected:
+            rx = QRegExp(QString('<font .*>'))
+            rx.setMinimal(True)
+            # If selected, I remove the <font color="..."> by hand,
+            # and give the highlight color
+            document.setHtml(QString("<font color=%1>%2</font>") \
+                                 .arg(palette.highlightedText().color().name())\
+                                 .arg(text.replace(rx, QString('')).
+                                      replace(QString('</font>'),QString(''))))
+        else:
+            document.setHtml(text)
+        color = palette.highlight().color() \
+            if option.state & QStyle.State_Selected \
+            else palette.base().color()
+        painter.save()
+        # voodoo: if not highlighted, filling the rect
+        # with the base color makes no difference
+        painter.fillRect(option.rect, color)
+        painter.translate(option.rect.x(), option.rect.y())
+        document.drawContents(painter)
+        painter.restore()
+
+    def sizeHint(self, option, index):
+        fm = option.fontMetrics
+        text = index.model().data(index)
+        document = QTextDocument()
+        document.setDefaultFont(option.font)
+        document.setHtml(text)
+        # the +5 is for margin. The +4 is voodoo;
+        # fm.height just give it too small.
+        return QSize(document.idealWidth() + 5, fm.height() + 4)