horizontal scrollbar working in XML views
[sface.git] / sface / xmlwidget.py
index 84e1e61..13c26e0 100644 (file)
@@ -142,7 +142,7 @@ class DomItem:
             self.childItems[i] = childItem
             return childItem
         return None
-            
+
     def parent(self):
         return self.parentItem
 
@@ -162,6 +162,7 @@ class XmlView(QTreeView):
         self.setHeaderHidden(True)
         self.setAttribute(Qt.WA_MacShowFocusRect, 0)
         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
+        self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
 
 class XmlWindow(QDialog):
     def __init__(self, parent=None, title='XML Window'):
@@ -182,6 +183,7 @@ class XmlWindow(QDialog):
         layout.addWidget(self.view)
         self.setLayout(layout)
 
+        self.connect(self.view, SIGNAL('expanded(QModelIndex)'), self.onItemExpanded)
         self.updateView()
 
     def initView(self):
@@ -219,7 +221,12 @@ class XmlWindow(QDialog):
             # tag.
             self.view.expand(self.model.index(0,0))
 
+        self.view.header().resizeSection(0,0)
+        self.view.resizeColumnToContents(0)
 
+    def onItemExpanded(self, index):
+        self.view.header().resizeSection(0,0)
+        self.view.resizeColumnToContents(0)
 
 class XmlDelegate(QItemDelegate):
     
@@ -256,24 +263,34 @@ class XmlDelegate(QItemDelegate):
         fm = option.fontMetrics
         if isinstance(index.model().data(index),QVariant):
             return QSize(0, 0)
-        text = index.model().data(index).property('content').toString()
+
+        nodeType = index.model().data(index).property('nodeType')
+        delegate = self.delegates.get(str(nodeType.toString()), None)
+
+        # Use the text from the appropriate delegate if we have it, to
+        # compute the size. Should probably finish the sizeHint() methods
+        # of the delegates at some point, and call them instead.
+        if delegate!=None and hasattr(delegate, "getItemText"):
+            text = delegate.getItemText(option, index)
+        else:
+            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): 
+    def getItemText(self, 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)
@@ -300,20 +317,33 @@ class ElemNodeDelegate(QAbstractItemDelegate):
                     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:
+            palette = QApplication.palette()
             htmlText = colorize(palette.highlightedText().color().name(),
                                 getHtmlText(text, highGlobPattern, highAttPattern))
-            document.setHtml(QString(htmlText))
         else:
             htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern)
-            document.setHtml(QString(htmlText))
+
+        return htmlText
+
+    def paint(self, painter, option, index):
+        palette = QApplication.palette()
+        document = QTextDocument()
+        document.setDefaultFont(option.font)
+
+        htmlText = self.getItemText(option, index)
+        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
@@ -323,46 +353,40 @@ class ElemNodeDelegate(QAbstractItemDelegate):
         document.drawContents(painter)
         painter.restore()
 
-    def sizeHint(self, option, index):
-        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)
-    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()
+    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)
+        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()