+class RSpecDelegate(QAbstractItemDelegate):
+
+ def __init__(self, parent=None):
+ print "init-ing the delegate"
+ QAbstractItemDelegate.__init__(self, parent)
+
+ def paint(self, painter, option, index):
+ text = index.model().data(index)
+ palette = QApplication.palette()
+ document = QTextDocument()
+ document.setDefaultFont(option.font)
+ if option.state & QStyle.State_Selected:
+ rx = QRegExp(QString('<font .*>'))
+ rx.setMinimal(True)
+ # If selected, I remove the <font color="..."> by hand,
+ # and give the highlight color
+ document.setHtml(QString("<font color=%1>%2</font>") \
+ .arg(palette.highlightedText().color().name())\
+ .arg(text.replace(rx, QString('')).
+ replace(QString('</font>'),QString(''))))
+ else:
+ document.setHtml(text)
+ 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()
+
+ 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)