Uh-uh. textDelegate came to birth. Still bad rendering, but good multiline verbatim...
authorGiovanni Gherdovich <ggherdov@brentaal.inria.fr>
Tue, 12 Oct 2010 12:42:12 +0000 (14:42 +0200)
committerGiovanni Gherdovich <ggherdov@brentaal.inria.fr>
Tue, 12 Oct 2010 12:42:12 +0000 (14:42 +0200)
sface/xmlwidget.py

index a2d3ec2..633c95a 100644 (file)
@@ -254,7 +254,6 @@ class XmlDelegate(QItemDelegate):
 
 class ElemNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
 
 class ElemNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
-        text = index.model().data(index)
         palette = QApplication.palette()
         document = QTextDocument()
         document.setDefaultFont(option.font)
         palette = QApplication.palette()
         document = QTextDocument()
         document.setDefaultFont(option.font)
@@ -263,9 +262,7 @@ class ElemNodeDelegate(QAbstractItemDelegate):
         highGlobPattern = '&lt;<b>%s</b>%s&gt;'
         highAttPattern = ' <b>%s</b>="%s"'
         def getHtmlText(plainText, globPattern, attPattern):
         highGlobPattern = '&lt;<b>%s</b>%s&gt;'
         highAttPattern = ' <b>%s</b>="%s"'
         def getHtmlText(plainText, globPattern, attPattern):
-            print "PLAIN TEXT:", plainText
             tmp = plainText.split(' ', 1)
             tmp = plainText.split(' ', 1)
-            print "TMP:", tmp
             elemName = tmp[0]
             AttListHtml = ''
             if len(tmp) > 1:
             elemName = tmp[0]
             AttListHtml = ''
             if len(tmp) > 1:
@@ -281,7 +278,6 @@ class ElemNodeDelegate(QAbstractItemDelegate):
         def colorize(color, text):
             return '<font color=' + color + '>' + text + '</font>'
         text = str(index.model().data(index).property('content').toString())
         def colorize(color, text):
             return '<font color=' + color + '>' + text + '</font>'
         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))
         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()
             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)
         # 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): 
 
 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', '<br>')
+            return '<pre>' + text + '</pre'
+        def colorize(color, text):
+            return '<font color=' + color + '>' + text + '</font>'
+        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): 
 
     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):
         paint(self, painter, option, index)
 
 def paint(self, painter, option, index):