From 16fda85a3620dcd2a198a2ba5b9f17be0cc72049 Mon Sep 17 00:00:00 2001 From: Giovanni Gherdovich Date: Tue, 12 Oct 2010 14:42:12 +0200 Subject: [PATCH] Uh-uh. textDelegate came to birth. Still bad rendering, but good multiline verbatim is on its way. --- sface/xmlwidget.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py index a2d3ec2..633c95a 100644 --- a/sface/xmlwidget.py +++ b/sface/xmlwidget.py @@ -254,7 +254,6 @@ class XmlDelegate(QItemDelegate): class ElemNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - text = index.model().data(index) palette = QApplication.palette() document = QTextDocument() document.setDefaultFont(option.font) @@ -263,9 +262,7 @@ class ElemNodeDelegate(QAbstractItemDelegate): 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: @@ -281,7 +278,6 @@ class ElemNodeDelegate(QAbstractItemDelegate): 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)) @@ -293,7 +289,6 @@ class ElemNodeDelegate(QAbstractItemDelegate): if option.state & QStyle.State_Selected \ else palette.base().color() painter.save() - print "COLOR:", color.name() # voodoo: if not highlighted, filling the rect # with the base color makes no difference painter.fillRect(option.rect, color) @@ -306,15 +301,40 @@ class ElemNodeDelegate(QAbstractItemDelegate): class TextNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - #print "TEXT DELEG CALLED" - paint(self, painter, option, index) + palette = QApplication.palette() + document = QTextDocument() + document.setDefaultFont(option.font) + def verbatimize(text): + text.replace('\n', '
') + return '
' + text + '' + text + ''
+        text = str(index.model().data(index).property('content').toString())
+        if option.state & QStyle.State_Selected:
+            htmlText = colorize(palette.highlightedText().color().name(),
+                                verbatimize(text))
+            document.setHtml(QString(htmlText))
+        else:
+            htmlText = verbatimize(text)
+            document.setHtml(QString(htmlText))
+        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):
         sizeHint(self, option, index)
 
 class CommentNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
-        #print "TEXT DELEG CALLED"
+
+
         paint(self, painter, option, index)
 
 def paint(self, painter, option, index):
-- 
2.43.0