X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Frspecwindow.py;h=a0741c43877134060aec02fc89f5974a48d35534;hp=45c25088ccace1ffbbdd2cf54b4739c21a27fa6d;hb=ab40b6e95de331b183c426db56fbe26cd3bdced8;hpb=743e1ca6dc5c9d1aa98197e4fb5dfe40d13fb513 diff --git a/sface/rspecwindow.py b/sface/rspecwindow.py index 45c2508..a0741c4 100644 --- a/sface/rspecwindow.py +++ b/sface/rspecwindow.py @@ -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 = ' %s="%s"' % (attr.nodeName(), attr.nodeValue()) qslist.append(elem) - return QString("<%s %s>" % (node.nodeName(), qslist.join(' '))) + return QString('<%s%s>'% (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('')) + rx.setMinimal(True) + # If selected, I remove the by hand, + # and give the highlight color + document.setHtml(QString("%2") \ + .arg(palette.highlightedText().color().name())\ + .arg(text.replace(rx, QString('')). + replace(QString(''),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)