From 5ca19693a97294cff7be4a2e3cbb92ce5a039794 Mon Sep 17 00:00:00 2001 From: Giovanni Gherdovich Date: Tue, 12 Oct 2010 11:32:33 +0200 Subject: [PATCH] elemNode delegate working properly. Gotta test with an RSpec, XML with atts. --- sface/xmlwidget.py | 53 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py index da6563e..a2d3ec2 100644 --- a/sface/xmlwidget.py +++ b/sface/xmlwidget.py @@ -252,18 +252,53 @@ class XmlDelegate(QItemDelegate): # fm.height just give it too small. return QSize(document.idealWidth() + 5, fm.height() + 4) - - - class ElemNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - text = index.model().data(index).property('content').toString() + text = index.model().data(index) + palette = QApplication.palette() + document = QTextDocument() + document.setDefaultFont(option.font) + nonHighGlobPattern = '<%s%s>' + nonHighAttPattern = ' %s="%s"' + highGlobPattern = '<%s%s>' + highAttPattern = ' %s="%s"' + def getHtmlText(plainText, globPattern, attPattern): + print "PLAIN TEXT:", plainText + tmp = plainText.split(' ', 1) + print "TMP:", tmp + elemName = tmp[0] + AttListHtml = '' + if len(tmp) > 1: + # many elems don't have atts... + attList = tmp[1].split() + for att in attList: + tmp = att.split('=') + attName = tmp[0] + attValue = tmp[1][1:-1] + AttListHtml += (nonHighAttPattern % (attName, attValue)) + html = (globPattern % (elemName, AttListHtml)) + return html + def colorize(color, text): + return '' + text + '' + text = str(index.model().data(index).property('content').toString()) + print "TEXT:", text + if option.state & QStyle.State_Selected: + htmlText = colorize(palette.highlightedText().color().name(), + getHtmlText(text, highGlobPattern, highAttPattern)) + document.setHtml(QString(htmlText)) + else: + htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern) + document.setHtml(QString(htmlText)) + color = palette.highlight().color() \ + if option.state & QStyle.State_Selected \ + else palette.base().color() painter.save() - font = option.font - font.setBold(True) - painter.setFont(font) - painter.drawText(option.rect, Qt.AlignLeft, QString(text)) - #print "ELEM DELEG CALLED" + print "COLOR:", color.name() + # 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): -- 2.43.0