Source

thg-qt-mq / htmllistview-qlabel-delegate.diff

# HG changeset patch
# Parent 9ac8e0e0c7acb9a3610610274d45a837517fc4a2
htmllistview: use QLabel for rich text rendering **EXPERIMENTAL**

diff --git a/tortoisehg/hgqt/htmllistview.py b/tortoisehg/hgqt/htmllistview.py
--- a/tortoisehg/hgqt/htmllistview.py
+++ b/tortoisehg/hgqt/htmllistview.py
@@ -100,27 +100,29 @@ class HTMLDelegate(QStyledItemDelegate):
         self.parent().style().drawControl(QStyle.CE_ItemViewItem, option, painter)
 
         # draw text
-        doc = QTextDocument()
         painter.save()
-        doc.setHtml(text)
         painter.setClipRect(option.rect)
-        painter.translate(QPointF(
-            option.rect.left(),
-            option.rect.top() + (option.rect.height() - doc.size().height()) / 2))
-        ctx = QAbstractTextDocumentLayout.PaintContext()
+        painter.translate(option.rect.topLeft())
+        label = QLabel(text, size=option.rect.size())
+        pal = label.palette()
+        pal.setColor(label.backgroundRole(), Qt.transparent)
         if option.state & QStyle.State_Selected:
             if option.state & QStyle.State_Active:
                 colgroup = QPalette.Active
             else:
                 colgroup = QPalette.Inactive
-            ctx.palette.setColor(QPalette.Text,
-                                 option.palette.color(colgroup,
-                                                      QPalette.HighlightedText))
-        doc.documentLayout().draw(painter, ctx)
+            pal.setColor(label.foregroundRole(),
+                         option.palette.color(colgroup,
+                                              QPalette.HighlightedText))
+        label.setPalette(pal)
+        # TODO: margin or inner rect?
+        # TODO: elide mode
+        label.render(painter)
         painter.restore()
 
     def sizeHint(self, option, index):
         text = index.model().data(index, Qt.DisplayRole).toString()
+        # TODO: use QLabel?
         doc = QTextDocument()
         doc.setDefaultStyleSheet(qtlib.thgstylesheet)
         doc.setDefaultFont(option.font)