very first draft for dmg packaging - all set in script, no option
[sface.git] / sface / xmlwidget.py
index da6563e..b11f1ed 100644 (file)
@@ -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 = '&lt;<b><font color="#b42be2">%s</font></b>%s&gt;'
+        nonHighAttPattern = ' <b>%s</b>="<font color="#1e90ff">%s</font>"'
+        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)
+#            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 '<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))
+            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):