Merge branch 'master' of git://git.onelab.eu/sface
authorGiovanni Gherdovich <ggherdov@brentaal.inria.fr>
Mon, 20 Sep 2010 17:20:32 +0000 (19:20 +0200)
committerGiovanni Gherdovich <ggherdov@brentaal.inria.fr>
Mon, 20 Sep 2010 17:20:32 +0000 (19:20 +0200)
1  2 
sface/rspecwindow.py

diff --combined sface/rspecwindow.py
@@@ -24,6 -24,8 +24,8 @@@ class RSpecView(QTreeView)
          self.collapseAll()
          self.expandToDepth(0)
  
+         model = self.model()
          def recursiveExpand(index):
              parent = index.parent()
              if parent and parent.isValid():
              if index.data().toString() == txt:
                  recursiveExpand(index)
                  self.scrollTo(index, self.PositionAtCenter)
+                 return
              
              rows = model.rowCount(index)
              for r in range(rows):
                  child_index = index.child(r, 0)
                  search(child_index)
              
-         model = self.model()
          root_rows = model.rowCount()
          for r in range(root_rows):
              index = model.index(r, 0)
@@@ -61,8 -63,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 -77,9 +78,9 @@@
                  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:
@@@ -196,8 -197,6 +198,8 @@@ class RSpecWindow(QDialog)
          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)
          self.view.expand(self.model.index(0, 0)) #expand first level only
  
  
 +class RSpecDelegate(QAbstractItemDelegate):
 +    
 +    def __init__(self, parent=None):
 +        print "init-ing the delegate"
 +        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)