fix bug when submitting changes
[sface.git] / sface / xmlwidget.py
index a803868..47dedeb 100644 (file)
@@ -1,4 +1,5 @@
 import os
+import shlex
 import sys
 
 from PyQt4.QtCore import *
@@ -12,14 +13,6 @@ class DomModel(QAbstractItemModel):
     def __init__(self, document, parent = 0):
         QAbstractItemModel.__init__(self, parent)
         self.domDocument = document
-        # one of the children of the rootItem is the 'xml' thing.
-        # here I delete it.
-        childList = document.childNodes()
-        for i in range(childList.count()):
-            currElem = childList.item(i)
-            if (currElem.nodeType() == QDomNode.ProcessingInstructionNode):
-                document.removeChild(currElem)
-                break
         self.rootItem = DomItem(document, 0);
 
     def data(self, index, role = Qt.DisplayRole):
@@ -58,7 +51,10 @@ class DomModel(QAbstractItemModel):
             elif node.nodeType() == QDomNode.EntityNode:
                 return QString('unsupported node type')
             elif node.nodeType() == QDomNode.ProcessingInstructionNode:
-                return QVariant()
+                obj = QObject()
+                obj.setProperty('nodeType', QString('element'))
+                obj.setProperty('content', node.nodeName() + " " + node.nodeValue())
+                return obj
             elif node.nodeType() == QDomNode.CommentNode:
                 obj = QObject()
                 obj.setProperty('nodeType', QString('comment'))
@@ -194,7 +190,7 @@ class XmlWindow(QDialog):
     def show(self):
         self.updateView()
         QDialog.show(self)
-        
+
     def updateView(self):
         del self.document
         del self.model
@@ -205,7 +201,6 @@ class XmlWindow(QDialog):
         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()
@@ -214,6 +209,18 @@ class XmlWindow(QDialog):
 
         self.document.setContent(open(rspec_file,'r').read())
 
+        if self.document.childNodes().count() == 0:
+            # empty document - do nothing
+            pass
+        elif self.document.childNodes().item(0).nodeType() == QDomNode.ProcessingInstructionNode:
+            # the first item is the <xml> tag, so expand the second
+            self.view.expand(self.model.index(1,0))
+        else:
+            # the document didn't start with an <xml> tag; let's try to expand
+            # the first item on the assumption that it is our root level xml
+            # tag.
+            self.view.expand(self.model.index(0,0))
+
 
 
 class XmlDelegate(QItemDelegate):
@@ -257,7 +264,7 @@ class XmlDelegate(QItemDelegate):
         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)    
+        return QSize(document.idealWidth() + 5, fm.height() + 4)
 
 class ElemNodeDelegate(QAbstractItemDelegate):
     def paint(self, painter, option, index): 
@@ -277,11 +284,21 @@ class ElemNodeDelegate(QAbstractItemDelegate):
             AttListHtml = ''
             if len(tmp) > 1:
                 # many elems don't have atts...
-                attList = tmp[1].split()
+                # use shlex.split so we can handle quoted strings with spaces
+                # in them, like <link enpoints="foo bar">. Note that there are
+                # documented problems with shlex.split and unicode, so we
+                # convert any potential unicode to a string first.
+                attList = shlex.split(str(tmp[1]))
                 for att in attList:
-                    tmp = att.split('=')
-                    attName = tmp[0]
-                    attValue = tmp[1][1:-1]
+                    tmp = att.split('=',1)
+                    if len(tmp)>=2:
+                        attName = tmp[0]
+                        attValue = tmp[1]
+                    else:
+                        # this shouldn't happen, but if it does, pretend the
+                        # attribute value is blank.
+                        attName = tmp[0]
+                        attValue = ""
                     AttListHtml += (nonHighAttPattern % (attName, attValue))
             html = (globPattern % (elemName, AttListHtml))
             return html