very first draft for dmg packaging - all set in script, no option
[sface.git] / sface / xmlwidget.py
index a52522a..b11f1ed 100644 (file)
@@ -37,13 +37,20 @@ class DomModel(QAbstractItemModel):
                 qslist = QStringList()
                 for i in range(attributeMap.count()):
                     attr = attributeMap.item(i)
-                    elem = ' <b>%s</b>="<font color="#1e90ff">%s</font>"' % (attr.nodeName(), attr.nodeValue())
+                    elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue())
                     qslist.append(elem)
-                return QString('&lt;<b><font color="#b42be2">%s</font></b>%s&gt;'% (node.nodeName(), qslist.join(' ')))
+                ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' '))
+                obj = QObject()
+                obj.setProperty('nodeType', QString('element'))
+                obj.setProperty('content', ElemNameAndAtts)
+                return obj
             elif node.nodeType() == QDomNode.AttributeNode:
-                return QString('Whozat?!')
+                return QVariant()
             elif node.nodeType() == QDomNode.TextNode:
-                return node.nodeValue()
+                obj = QObject()
+                obj.setProperty('nodeType', QString('text'))
+                obj.setProperty('content', node.nodeValue())
+                return obj
             elif node.nodeType() == QDomNode.CDATASectionNode:
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.EntityReferenceNode:
@@ -52,9 +59,11 @@ class DomModel(QAbstractItemModel):
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.ProcessingInstructionNode:
                 return QVariant()
-                #return node.nodeName()
             elif node.nodeType() == QDomNode.CommentNode:
-                return QString('#').append(node.nodeValue())
+                obj = QObject()
+                obj.setProperty('nodeType', QString('comment'))
+                obj.setProperty('content', node.nodeValue())
+                return obj
             elif node.nodeType() == QDomNode.DocumentNode:
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.DocumentTypeNode:
@@ -170,6 +179,9 @@ class XmlWindow(QDialog):
         self.view = XmlView(self)
         self.delegate = XmlDelegate(self)
         self.view.setItemDelegate(self.delegate)
+        self.delegate.insertNodeDelegate('element', ElemNodeDelegate())
+        self.delegate.insertNodeDelegate('text', TextNodeDelegate())
+        self.delegate.insertNodeDelegate('comment', CommentNodeDelegate())
         layout = QVBoxLayout()
         layout.addWidget(self.view)
         self.setLayout(layout)
@@ -201,31 +213,87 @@ class XmlWindow(QDialog):
 
 
 
-class XmlDelegate(QAbstractItemDelegate):
+class XmlDelegate(QItemDelegate):
     
     def __init__(self, parent=None):
         QAbstractItemDelegate.__init__(self, parent)
+        self.delegates = {}
+
+    def insertNodeDelegate(self, nodeType, delegate):
+        delegate.setParent(self)
+        self.delegates[nodeType] = delegate
 
+    def removeNodeDelegate(self, nodeType, delegate):
+        if nodeType in self.delegates:
+            del self.delegates[nodeType]
+    
     def paint(self, painter, option, index):
+        nodeType = index.model().data(index).property('nodeType')
+        delegate = self.delegates.get(str(nodeType.toString()))
+        #print "TYPE:", str(type(str(nodeType.toString())))
+        #print "DELEGS DICT:", self.delegates
+        #print "NODETYPE:", nodeType.toString()
+        if delegate is not None:
+            #print "WOW DELEG ISNT NONE"
+            delegate.paint(painter, option, index)
+        else:
+            #print "ELSE BRANCH"
+            # not sure this will ever work. this delegate
+            # doesn't know about my QObject strategy.
+            QItemDelegate.paint(self, painter, option, index)
+
+    def sizeHint(self, option, index):
+        fm = option.fontMetrics
+        text = index.model().data(index).property('content').toString()
+        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)    
+
+class ElemNodeDelegate(QAbstractItemDelegate):
+    def paint(self, painter, option, index): 
         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:
-            rx = QRegExp(QString('<font .*>'))
-            rx.setMinimal(True)
-            # If selected, I remove the <font color="..."> by hand,
-            # and give the highlight color
-            document.setHtml(QString("<font color=%1>%2</font>") \
-                                 .arg(palette.highlightedText().color().name())\
-                                 .arg(text.replace(rx, QString('')).
-                                      replace(QString('</font>'),QString(''))))
+            htmlText = colorize(palette.highlightedText().color().name(),
+                                getHtmlText(text, highGlobPattern, highAttPattern))
+            document.setHtml(QString(htmlText))
         else:
-            document.setHtml(text)
+            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()
+#        print "COLOR:", color.name()
         # voodoo: if not highlighted, filling the rect
         # with the base color makes no difference
         painter.fillRect(option.rect, color)
@@ -234,11 +302,46 @@ class XmlDelegate(QAbstractItemDelegate):
         painter.restore()
 
     def sizeHint(self, option, index):
-        fm = option.fontMetrics
-        text = index.model().data(index)
-        document = QTextDocument()
-        document.setDefaultFont(option.font)
+        sizeHint(self, option, index)
+
+class TextNodeDelegate(QAbstractItemDelegate):
+    def paint(self, painter, option, index): 
+        #print "TEXT DELEG CALLED"
+        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):
+    text = index.model().data(index).property('content').toString()
+    palette = QApplication.palette()
+    document = QTextDocument()
+    document.setDefaultFont(option.font)
+    if option.state & QStyle.State_Selected:
+        rx = QRegExp(QString('<font .*>'))
+        rx.setMinimal(True)
+        # If selected, I remove the <font color="..."> by hand,
+        # and give the highlight color
+        document.setHtml(QString("<font color=%1>%2</font>") \
+                             .arg(palette.highlightedText().color().name())\
+                             .arg(text.replace(rx, QString('')).
+                                  replace(QString('</font>'),QString(''))))
+    else:
         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)
+    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()
+
+