X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fxmlwidget.py;h=a80386860c9eba455a7028a22834fce8c2b24a45;hp=da6563ed585007983b9d8f6b99fcebbf753a52b7;hb=ce4c16bdd5c07f69c612b7c9db8bbc182ee28f74;hpb=99a5613d5bf887070a67eb2208ba907091fe41fb diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py index da6563e..a803868 100644 --- a/sface/xmlwidget.py +++ b/sface/xmlwidget.py @@ -85,7 +85,7 @@ class DomModel(QAbstractItemModel): if not index.isValid(): return Qt.ItemIsEnabled return Qt.ItemIsEnabled | Qt.ItemIsSelectable - + def headerData(self, section, orientation, role): return QVariant() @@ -176,7 +176,7 @@ class XmlWindow(QDialog): self.model = None self.title = title - self.view = XmlView(self) + self.view = self.initView() self.delegate = XmlDelegate(self) self.view.setItemDelegate(self.delegate) self.delegate.insertNodeDelegate('element', ElemNodeDelegate()) @@ -188,6 +188,9 @@ class XmlWindow(QDialog): self.updateView() + def initView(self): + return XmlView(self) + def show(self): self.updateView() QDialog.show(self) @@ -228,6 +231,8 @@ class XmlDelegate(QItemDelegate): del self.delegates[nodeType] def paint(self, painter, option, index): + if isinstance(index.model().data(index),QVariant): + return nodeType = index.model().data(index).property('nodeType') delegate = self.delegates.get(str(nodeType.toString())) #print "TYPE:", str(type(str(nodeType.toString()))) @@ -244,6 +249,8 @@ class XmlDelegate(QItemDelegate): def sizeHint(self, option, index): fm = option.fontMetrics + if isinstance(index.model().data(index),QVariant): + return QSize(0, 0) text = index.model().data(index).property('content').toString() document = QTextDocument() document.setDefaultFont(option.font) @@ -252,18 +259,53 @@ class XmlDelegate(QItemDelegate): # fm.height just give it too small. return QSize(document.idealWidth() + 5, fm.height() + 4) - - - class ElemNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - text = index.model().data(index).property('content').toString() + text = index.model().data(index) + palette = QApplication.palette() + document = QTextDocument() + document.setDefaultFont(option.font) + nonHighGlobPattern = '<%s%s>' + nonHighAttPattern = ' %s="%s"' + highGlobPattern = '<%s%s>' + highAttPattern = ' %s="%s"' + def getHtmlText(plainText, globPattern, attPattern): +# print "PLAIN TEXT:", plainText + tmp = plainText.split(' ', 1) +# print "TMP:", tmp + elemName = tmp[0] + AttListHtml = '' + if len(tmp) > 1: + # many elems don't have atts... + attList = tmp[1].split() + for att in attList: + tmp = att.split('=') + attName = tmp[0] + attValue = tmp[1][1:-1] + AttListHtml += (nonHighAttPattern % (attName, attValue)) + html = (globPattern % (elemName, AttListHtml)) + return html + def colorize(color, text): + return '' + text + '' + text = str(index.model().data(index).property('content').toString()) +# print "TEXT:", text + if option.state & QStyle.State_Selected: + htmlText = colorize(palette.highlightedText().color().name(), + getHtmlText(text, highGlobPattern, highAttPattern)) + document.setHtml(QString(htmlText)) + else: + htmlText = getHtmlText(text, nonHighGlobPattern, nonHighAttPattern) + document.setHtml(QString(htmlText)) + color = palette.highlight().color() \ + if option.state & QStyle.State_Selected \ + else palette.base().color() painter.save() - font = option.font - font.setBold(True) - painter.setFont(font) - painter.drawText(option.rect, Qt.AlignLeft, QString(text)) - #print "ELEM DELEG CALLED" +# print "COLOR:", color.name() + # 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 sizeHint(self, option, index):