restore baris tree expansion feature. Removed >print< statements.
[sface.git] / sface / xmlwidget.py
index 2aa7238..2c0f53c 100644 (file)
@@ -8,18 +8,16 @@ from PyQt4.QtXml import *
 from sface.config import config
 from sface.screens.sfascreen import SfaScreen
 
-def QVarMapAccess(qv, key):
-    # helper function. qv is a dict wrapped into a QVariant
-    print 10*'='
-    print "DICT:", qv.toMap()
-    if len(qv.toMap().keys()) == 0:
-        print "EMPTY!"
-        import traceback
-        traceback.print_stack()
-        return None
-    else:
-        return qv.toMap()[QString(key)].toString()
+class nodeData(QVariant):
+    def __init__(self, *args, **kws):
+        QVariant.__init__(self, *args, **kws)
+        self.type = None
 
+    def setType(self, typ):
+        self.type = typ
+
+    def getType(self):
+        return self.type
 
 class DomModel(QAbstractItemModel):
     def __init__(self, document, parent = 0):
@@ -53,58 +51,42 @@ class DomModel(QAbstractItemModel):
                     elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue())
                     qslist.append(elem)
                 ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' '))
-                print "1"
-                return QVariant(
-                    {'nodeType':QVariant(QString('element')),
-                     'content':ElemNameAndAtts})
+                answer = nodeData(ElemNameAndAtts)
+                answer.setType('element')
+                return answer
             elif node.nodeType() == QDomNode.AttributeNode:
-                print "2"
                 return QVariant()
             elif node.nodeType() == QDomNode.TextNode:
-                print "3"
-                return QVariant(
-                    {'nodeType':QVariant(QString('text')),
-                     'content':node.nodeValue()})
+                answer = nodeData(node.nodeValue())
+                answer.setType('text')
+                return answer
             elif node.nodeType() == QDomNode.CDATASectionNode:
-                print "4"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.EntityReferenceNode:
-                print "5"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.EntityNode:
-                print "6"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.ProcessingInstructionNode:
-                print "7"
                 return QVariant()
             elif node.nodeType() == QDomNode.CommentNode:
-                print "8"
-                return QVariant(
-                    {'nodeType':QVariant(QString('comment')),
-                     'content':node.nodeValue()})
+                answer = nodeData(node.nodeValue())
+                answer.setType('comment')
+                return answer
             elif node.nodeType() == QDomNode.DocumentNode:
-                print "9"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.DocumentTypeNode:
-                print "10"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.DocumentFragmentNode:
-                print "12"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.NotationNode:
-                print "13"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.BaseNode:
-                print "14"
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.CharacterDataNode:
-                print "15"
                 return QString('unsupported node type')
             else:
-                print "16"
                 return QVariant()
         else:
-            print "17"
             return QVariant()
 
     def flags(self, index):
@@ -214,41 +196,32 @@ class XmlDelegate(QItemDelegate):
             del self.delegates[nodeType]
     
     def paint(self, painter, option, index):
-        print "ASKING FOR DATA"
-        dataAsQVarMap = index.model().data(index)
-        print "GOT DATA"
-        nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
+        nodeData = index.model().data(index)
+        nodeType = nodeData.getType()
         delegate = self.delegates.get(nodeType)
-        #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 QMap strategy.
             QItemDelegate.paint(self, painter, option, index)
 
-#    def sizeHint(self, option, index):
-#        fm = option.fontMetrics
-#        print "TYPE:", str(type(index.model().data(index).convert(QObject)))
-#        text = "the fish doesn't talk"
-#        #text = str(index.model().data(index).property('content').toString())
-#        nodeType = str(index.model().data(index).property('nodeType').toString())
-#        if nodeType == 'element' or nodeType == 'comment':
-#            numlines = 1
-#        elif nodeType == 'text':
-#            numlines = text.count('\n')
-#            sys.__stdout__.write("TEXT: \n" + text)
-#        else:
-#            numlines = 1
-#        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) * numlines)    
+    def sizeHint(self, option, index):
+        fm = option.fontMetrics
+        nodeData = index.model().data(index)
+        nodeType = nodeData.getType()
+        text = nodeData.toString()
+        if nodeType == 'element' or nodeType == 'comment':
+            numlines = 1
+        elif nodeType == 'text':
+            nl = text.count('\n')
+            numlines = nl if nl > 0 else 1
+        else:
+            numlines = 1
+        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) * numlines)    
 
 class ElemNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
@@ -270,13 +243,15 @@ class ElemNodeDelegate(QAbstractItemDelegate):
                     tmp = att.split('=')
                     attName = tmp[0]
                     attValue = tmp[1][1:-1]
-                    AttListHtml += (nonHighAttPattern % (attName, attValue))
+                    AttListHtml += (attPattern % (attName, attValue))
             html = (globPattern % (elemName, AttListHtml))
             return html
         def colorize(color, text):
             return '<font color=' + color + '>' + text + '</font>'
-        dataAsQVarMap = index.model().data(index)
-        text = str(QVarMapAccess(dataAsQVarMap, 'content'))
+        nodeData = index.model().data(index)
+        nodeType = nodeData.getType()
+        # Uff... QString Vs string...
+        text = str(nodeData.toString())
         if option.state & QStyle.State_Selected:
             htmlText = colorize(palette.highlightedText().color().name(),
                                 getHtmlText(text, highGlobPattern, highAttPattern))
@@ -305,8 +280,9 @@ class TextNodeDelegate(QAbstractItemDelegate):
             return '<pre>' + text + '</pre'
         def colorize(color, text):
             return '<font color=' + color + '>' + text + '</font>'
-        dataAsQVarMap = index.model().data(index)
-        text = str(QVarMapAccess(dataAsQVarMap, 'content'))
+        nodeData = index.model().data(index)
+        nodeType = nodeData.getType()
+        text = nodeData.toString()
         if option.state & QStyle.State_Selected:
             htmlText = colorize(palette.highlightedText().color().name(),
                                 verbatimize(text))