done with sizeHint. Next: it a bit slow in changing the orientation of the arrow...
[sface.git] / sface / xmlwidget.py
index a6c36b3..2a57587 100644 (file)
@@ -32,6 +32,7 @@ class DomModel(QAbstractItemModel):
             currElem = childList.item(i)
             if (currElem.nodeType() == QDomNode.ProcessingInstructionNode):
                 document.removeChild(currElem)
+                print "REMOVED!"
                 break
         self.rootItem = DomItem(document, 0);
 
@@ -183,9 +184,15 @@ class DomItem:
         return self.rowNumber
 
 class XmlView(QTreeView):
-    def __init__(self, parent):
+    def __init__(self, parent=None):
         QTreeView.__init__(self, parent)
 
+        delegate = XmlDelegate(self)
+        delegate.insertNodeDelegate('element', ElemNodeDelegate())
+        delegate.insertNodeDelegate('text', TextNodeDelegate())
+        delegate.insertNodeDelegate('comment', CommentNodeDelegate())
+        self.setItemDelegate(delegate)
+
         self.setAnimated(True)
         self.setItemsExpandable(True)
         self.setRootIsDecorated(True)
@@ -193,52 +200,6 @@ class XmlView(QTreeView):
         self.setAttribute(Qt.WA_MacShowFocusRect, 0)
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
 
-class XmlWindow(QDialog):
-    def __init__(self, parent=None, title='XML Window'):
-        QDialog.__init__(self, parent)
-        self.setWindowTitle(title)
-
-        self.document = None
-        self.model = None
-        self.title = title
-
-        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)
-
-        self.updateView()
-
-    def show(self):
-        self.updateView()
-        QDialog.show(self)
-        
-    def updateView(self):
-        del self.document
-        del self.model
-        self.document = None
-        self.model = None
-
-        self.document = QDomDocument(self.title)
-        self.model = DomModel(self.document, self)
-
-        self.view.setModel(self.model)
-        self.view.expand(self.model.index(0, 0)) #expand first level only
-
-        #move the code below to rspec window
-        rspec_file = config.getSliceRSpecFile()
-        if not os.path.exists(rspec_file):
-            return
-
-        self.document.setContent(open(rspec_file,'r').read())
-
-
-
 class XmlDelegate(QItemDelegate):
     
     def __init__(self, parent=None):
@@ -270,25 +231,25 @@ class XmlDelegate(QItemDelegate):
             # 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
+        text = "the fish doesn't talk"
+        dataAsQVarMap = index.model().data(index)
+        nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
+        if nodeType == 'element' or nodeType == 'comment':
+            numlines = 1
+        elif nodeType == 'text':
+            nl = text.count('\n')
+            numlines = nl if nl > 0 else 1
+            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)    
 
 class ElemNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
@@ -310,7 +271,7 @@ 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):
@@ -320,6 +281,8 @@ class ElemNodeDelegate(QAbstractItemDelegate):
         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)