X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sface%2Fxmlwidget.py;h=34578df222e363df253941b376a68c1d555794d9;hb=5c18c5b0393cb0970ccc6b839d056a504e4177d7;hp=a2d3ec2312d6a9a24c484e843ba4c68df92e5779;hpb=5ca19693a97294cff7be4a2e3cbb92ce5a039794;p=sface.git
diff --git a/sface/xmlwidget.py b/sface/xmlwidget.py
index a2d3ec2..34578df 100644
--- a/sface/xmlwidget.py
+++ b/sface/xmlwidget.py
@@ -23,7 +23,7 @@ class DomModel(QAbstractItemModel):
self.rootItem = DomItem(document, 0);
def data(self, index, role = Qt.DisplayRole):
- # sometimes it return a QString, sometimes a QVariant. not good.
+ # for interesting nodes, returns a dict wrapped into a QVariant.
if not index.isValid():
return QVariant()
if role != Qt.DisplayRole:
@@ -40,17 +40,15 @@ class DomModel(QAbstractItemModel):
elem = ' %s="%s"' % (attr.nodeName(), attr.nodeValue())
qslist.append(elem)
ElemNameAndAtts = '%s%s'% (node.nodeName(), qslist.join(' '))
- obj = QObject()
- obj.setProperty('nodeType', QString('element'))
- obj.setProperty('content', ElemNameAndAtts)
- return obj
+ return QVariant(
+ {QString('nodeType'):QVariant(QString('element')),
+ QString('content'):ElemNameAndAtts})
elif node.nodeType() == QDomNode.AttributeNode:
return QVariant()
elif node.nodeType() == QDomNode.TextNode:
- obj = QObject()
- obj.setProperty('nodeType', QString('text'))
- obj.setProperty('content', node.nodeValue())
- return obj
+ return QVariant(
+ {QString('nodeType'):QVariant(QString('text')),
+ QString('content'):node.nodeValue()})
elif node.nodeType() == QDomNode.CDATASectionNode:
return QString('unsupported node type')
elif node.nodeType() == QDomNode.EntityReferenceNode:
@@ -60,10 +58,9 @@ class DomModel(QAbstractItemModel):
elif node.nodeType() == QDomNode.ProcessingInstructionNode:
return QVariant()
elif node.nodeType() == QDomNode.CommentNode:
- obj = QObject()
- obj.setProperty('nodeType', QString('comment'))
- obj.setProperty('content', node.nodeValue())
- return obj
+ return QVariant(
+ {QString('nodeType'):QVariant(QString('comment')),
+ QString('content'):node.nodeValue()})
elif node.nodeType() == QDomNode.DocumentNode:
return QString('unsupported node type')
elif node.nodeType() == QDomNode.DocumentTypeNode:
@@ -228,9 +225,12 @@ class XmlDelegate(QItemDelegate):
del self.delegates[nodeType]
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())))
+ QVarMapAccess = lambda qv, key: qv.toMap()[QString(key)].toString()
+ dataAsQVarMap = index.model().data(index)
+ nodeType = str(QVarMapAccess(dataAsQVarMap, 'nodeType'))
+ delegate = self.delegates.get(nodeType)
+ print "WTF IS THIS TYPE:", type(index.model().data(index))
+ print "NODE TYPE:", nodeType
#print "DELEGS DICT:", self.delegates
#print "NODETYPE:", nodeType.toString()
if delegate is not None:
@@ -242,19 +242,28 @@ class XmlDelegate(QItemDelegate):
# 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)
+# def sizeHint(self, option, index):
+# fm = option.fontMetrics
+# print "TYPE:", str(type(index.model().data(index).convert(QObject)))
+# text = "the fish doesn't talk"
+# #text = str(index.model().data(index).property('content').toString())
+# nodeType = str(index.model().data(index).property('nodeType').toString())
+# if nodeType == 'element' or nodeType == 'comment':
+# numlines = 1
+# elif nodeType == 'text':
+# numlines = text.count('\n')
+# sys.__stdout__.write("TEXT: \n" + text)
+# else:
+# numlines = 1
+# 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) * numlines)
class ElemNodeDelegate(QAbstractItemDelegate):
def paint(self, painter, option, index):
- text = index.model().data(index)
palette = QApplication.palette()
document = QTextDocument()
document.setDefaultFont(option.font)
@@ -263,9 +272,7 @@ class ElemNodeDelegate(QAbstractItemDelegate):
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:
@@ -280,8 +287,9 @@ class ElemNodeDelegate(QAbstractItemDelegate):
return html
def colorize(color, text):
return '' + text + ''
- text = str(index.model().data(index).property('content').toString())
- print "TEXT:", text
+ QVarMapAccess = lambda qv, key: qv.toMap()[QString(key)].toString()
+ dataAsQVarMap = index.model().data(index)
+ text = str(QVarMapAccess(dataAsQVarMap, 'content'))
if option.state & QStyle.State_Selected:
htmlText = colorize(palette.highlightedText().color().name(),
getHtmlText(text, highGlobPattern, highAttPattern))
@@ -293,7 +301,6 @@ class ElemNodeDelegate(QAbstractItemDelegate):
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)
@@ -301,20 +308,39 @@ class ElemNodeDelegate(QAbstractItemDelegate):
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"
- paint(self, painter, option, index)
-
- def sizeHint(self, option, index):
- sizeHint(self, option, index)
+ palette = QApplication.palette()
+ document = QTextDocument()
+ document.setDefaultFont(option.font)
+ def verbatimize(text):
+ text.replace('\n', '
')
+ return '
' + text + '' + text + '' + QVarMapAccess = lambda qv, key: qv.toMap()[QString(key)].toString() + dataAsQVarMap = index.model().data(index) + text = str(QVarMapAccess(dataAsQVarMap, 'content')) + if option.state & QStyle.State_Selected: + htmlText = colorize(palette.highlightedText().color().name(), + verbatimize(text)) + document.setHtml(QString(htmlText)) + else: + htmlText = verbatimize(text) + document.setHtml(QString(htmlText)) + color = palette.highlight().color() \ + if option.state & QStyle.State_Selected \ + else palette.base().color() + painter.save() + # 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() class CommentNodeDelegate(QAbstractItemDelegate): def paint(self, painter, option, index): - #print "TEXT DELEG CALLED" paint(self, painter, option, index) def paint(self, painter, option, index):