I forgot to remove the >print<s
[sface.git] / sface / xmlwidget.py
index 865f276..39fe7ac 100644 (file)
@@ -1,5 +1,6 @@
 import os
 import sys
+import re
 
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
@@ -8,18 +9,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):
@@ -32,7 +31,6 @@ class DomModel(QAbstractItemModel):
             currElem = childList.item(i)
             if (currElem.nodeType() == QDomNode.ProcessingInstructionNode):
                 document.removeChild(currElem)
-                print "REMOVED!"
                 break
         self.rootItem = DomItem(document, 0);
 
@@ -54,58 +52,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):
@@ -215,41 +197,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): 
@@ -266,7 +239,8 @@ class ElemNodeDelegate(QAbstractItemDelegate):
             AttListHtml = ''
             if len(tmp) > 1:
                 # many elems don't have atts...
-                attList = tmp[1].split()
+                pttrnAtt = ' *?.+.?=".*?"'
+                attList = re.compile(pttrnAtt, re.DOTALL).findall(tmp[1])
                 for att in attList:
                     tmp = att.split('=')
                     attName = tmp[0]
@@ -276,13 +250,13 @@ class ElemNodeDelegate(QAbstractItemDelegate):
             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))
-            print htmlText
-            print getHtmlText(text, highGlobPattern, highAttPattern)
             document.setHtml(QString(htmlText))
         else:
             htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern)
@@ -308,8 +282,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))