+ text = index.model().data(index)
+ palette = QApplication.palette()
+ document = QTextDocument()
+ document.setDefaultFont(option.font)
+ nonHighGlobPattern = '<<b><font color="#b42be2">%s</font></b>%s>'
+ nonHighAttPattern = ' <b>%s</b>="<font color="#1e90ff">%s</font>"'
+ highGlobPattern = '<<b>%s</b>%s>'
+ highAttPattern = ' <b>%s</b>="%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...
+ # 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('=',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
+ 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:
+ 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()