From 79442c9e30b92e048e0a8ce78b74915b5d8b5881 Mon Sep 17 00:00:00 2001 From: Giovanni Gherdovich Date: Mon, 20 Sep 2010 19:20:23 +0200 Subject: [PATCH 1/1] syntax highlighting to XML, the HTML way --- sface/rspecwindow.py | 51 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/sface/rspecwindow.py b/sface/rspecwindow.py index 17bef6a..da70b1c 100644 --- a/sface/rspecwindow.py +++ b/sface/rspecwindow.py @@ -61,7 +61,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: @@ -75,9 +76,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: @@ -195,6 +196,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) @@ -226,3 +229,45 @@ class RSpecWindow(QDialog): 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('')) + 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) -- 2.43.0