Commits

Yuya Nishihara committed 20b1973

labeleddelegate: render revision/changes labels on top of default painting

This allows "unfiltered" rendering of labels on selected rows as it was in 3.0.
_renderlabels() will be moved to repoview module later.

Rendering on CentOS 6.3 (PyQt < 4.7.1) is temporarily broken because of the
following PyQt bug, which should be addressed later.

http://www.riverbankcomputing.com/pipermail/pyqt/2010-March/026021.html

  • Participants
  • Parent commits 5b72e59

Comments (0)

Files changed (2)

tortoisehg/hgqt/repomodel.py

                 return nullvariant
             if column in (GraphColumn, BranchColumn):
                 return QVariant(QColor(self.namedbranch_color(ctx.branch())))
-        elif role == Qt.DecorationRole and column == DescColumn:
-            return QVariant(self._renderrevlabels(ctx))
-        elif role == Qt.DecorationRole and column == ChangesColumn:
-            return QVariant(self._renderchanges(ctx))
         elif role == LabelsRole and column == DescColumn:
             return QVariant(self._getrevlabels(ctx))
         elif role == LabelsRole and column == ChangesColumn:
 
         return labels
 
-    def _renderrevlabels(self, ctx):
-        labels = self._getrevlabels(ctx)
-        return _renderlabels(labels)
-
     def _getchanges(self, ctx):
         """Return the MAR status for the given ctx."""
         labels = []
             labels.append((str(len(R)), 'log.removed'))
         return labels
 
-    def _renderchanges(self, ctx):
-        labels = self._getchanges(ctx)
-        return _renderlabels(labels, margin=0)
-
     def getconv(self, ctx):
         if ctx.rev() is not None:
             extra = ctx.extra()
         + [(Qt.ForegroundRole, GraphColumn),
            (Qt.ForegroundRole, BranchColumn),
            (Qt.ForegroundRole, AuthorColumn),
-           (Qt.DecorationRole, DescColumn),
-           (Qt.DecorationRole, ChangesColumn),
            (LabelsRole, DescColumn),
            (LabelsRole, ChangesColumn)]))
 

tortoisehg/hgqt/repoview.py

         self.createActions()
         self.setItemDelegateForColumn(repomodel.GraphColumn,
                                       GraphDelegate(self))
+        self.setItemDelegateForColumn(repomodel.DescColumn,
+                                      LabeledDelegate(self))
+        self.setItemDelegateForColumn(repomodel.ChangesColumn,
+                                      LabeledDelegate(self, margin=0))
 
         self.setAcceptDrops(True)
         if PYQT_VERSION >= 0x40700:
             return QSize(0, 0)
 
 
+class LabeledDelegate(QStyledItemDelegate):
+    """Render text labels in place of icon/pixmap decoration"""
+
+    def __init__(self, parent=None, margin=2):
+        super(LabeledDelegate, self).__init__(parent)
+        self._margin = margin
+
+    def initStyleOption(self, option, index):
+        super(LabeledDelegate, self).initStyleOption(option, index)
+        labels = index.data(repomodel.LabelsRole).toPyObject()
+        if not labels:
+            return
+        pix = self._renderlabels(labels)
+        option.decorationSize = pix.size()
+        if isinstance(option, QStyleOptionViewItemV2):
+            option.features |= QStyleOptionViewItemV2.HasDecoration
+
+    def paint(self, painter, option, index):
+        super(LabeledDelegate, self).paint(painter, option, index)
+        labels = index.data(repomodel.LabelsRole).toPyObject()
+        if not labels:
+            return
+
+        option = QStyleOptionViewItemV4(option)
+        self.initStyleOption(option, index)
+        if option.widget:
+            style = option.widget.style()
+        else:
+            style = QApplication.style()
+        rect = style.subElementRect(QStyle.SE_ItemViewItemDecoration, option,
+                                    option.widget)
+
+        # for maximum readability, use vivid color regardless of option.state
+        pix = self._renderlabels(labels)
+        painter.save()
+        try:
+            painter.setClipRect(option.rect)
+            painter.drawPixmap(rect, pix)
+        finally:
+            painter.restore()
+
+    def _renderlabels(self, labels):
+        return repomodel._renderlabels(labels, self._margin)
+
+
 class ColumnSelectDialog(QDialog):
     def __init__(self, name, model, curcolumns, parent=None):
         QDialog.__init__(self, parent)