X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fxmlwidget.py;h=84e1e6129028e22aef53f76b1c8cf7a782960114;hp=be53182d91fa5f68f82995548256686beab6bffe;hb=83a8227f531598385af1b6b9e3fa8ef56904b32b;hpb=8d71fde88896701be7c908195b6bd83f7bf6d93a diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py index be53182..84e1e61 100644 --- a/sface/xmlwidget.py +++ b/sface/xmlwidget.py @@ -13,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): @@ -59,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')) @@ -195,7 +190,10 @@ class XmlWindow(QDialog): def show(self): self.updateView() QDialog.show(self) - + + def readContent(self): + raise ValueError("readContent needs to be implemented in the subclass") + def updateView(self): del self.document del self.model @@ -206,14 +204,20 @@ 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() - if not os.path.exists(rspec_file): - return + self.document.setContent(self.readContent()) - 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 tag, so expand the second + self.view.expand(self.model.index(1,0)) + else: + # the document didn't start with an 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)) @@ -258,7 +262,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): @@ -362,4 +366,3 @@ def paint(self, painter, option, index): document.drawContents(painter) painter.restore() -