X-Git-Url: http://git.onelab.eu/?p=sface.git;a=blobdiff_plain;f=sface%2Fxmlwidget.py;h=b11f1edcf9b4e613bbcbbe411e2db975bf909ed0;hp=512d35a1efb0f1e4239577c309f4bc92a167159d;hb=3dd195d09ecb9fed27e65d93cd77596c11977c0f;hpb=6ba5d90f9b2b0ba25aeb46d0d92f22b1f8d33e90 diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py index 512d35a..b11f1ed 100644 --- a/sface/xmlwidget.py +++ b/sface/xmlwidget.py @@ -39,7 +39,7 @@ class DomModel(QAbstractItemModel): attr = attributeMap.item(i) elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue()) qslist.append(elem) - ElemNameAndAtts = QString('<%s%s>'% (node.nodeName(), qslist.join(' '))) + ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' ')) obj = QObject() obj.setProperty('nodeType', QString('element')) obj.setProperty('content', ElemNameAndAtts) @@ -230,29 +230,83 @@ class XmlDelegate(QItemDelegate): def paint(self, painter, option, index): nodeType = index.model().data(index).property('nodeType') delegate = self.delegates.get(str(nodeType.toString())) - print "TYPE:", str(type(str(nodeType.toString()))) - print "DELEGS DICT:", self.delegates - print "NODETYPE:", nodeType.toString() + #print "TYPE:", str(type(str(nodeType.toString()))) + #print "DELEGS DICT:", self.delegates + #print "NODETYPE:", nodeType.toString() if delegate is not None: - print "WOW DELEG ISNT NONE" + #print "WOW DELEG ISNT NONE" delegate.paint(painter, option, index) else: - print "ELSE BRANCH" + #print "ELSE BRANCH" # not sure this will ever work. this delegate # doesn't know about my QObject strategy. QItemDelegate.paint(self, painter, option, index) + def sizeHint(self, option, index): + fm = option.fontMetrics + 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): - print "ELEM DELEG CALLED" - paint(self, painter, option, index) + 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() +# 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): sizeHint(self, option, index) class TextNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - print "TEXT DELEG CALLED" + #print "TEXT DELEG CALLED" paint(self, painter, option, index) def sizeHint(self, option, index): @@ -260,13 +314,9 @@ class TextNodeDelegate(QAbstractItemDelegate): class CommentNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - print "TEXT DELEG CALLED" + #print "TEXT DELEG CALLED" paint(self, painter, option, index) - def sizeHint(self, option, index): - sizeHint(self, option, index) - - def paint(self, painter, option, index): text = index.model().data(index).property('content').toString() palette = QApplication.palette() @@ -294,12 +344,4 @@ def paint(self, painter, option, index): document.drawContents(painter) painter.restore() -def sizeHint(self, option, index): - fm = option.fontMetrics - text = index.model().data(index) - 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) +