Commits

Yuya Nishihara committed 0ba0157

labeleddelegate: move _renderlabels() from repomodel

_LabelsLayout, which will do a similar job to QTextLayout, is the stub for
calculating size without rendering. The delegate needs two separate outputs,
painting and size, from single input.

Comments (0)

Files changed (2)

tortoisehg/hgqt/repomodel.py

 
 from tortoisehg.util import hglib
 from tortoisehg.util.i18n import _
-from tortoisehg.hgqt import filedata, graph, qtlib
+from tortoisehg.hgqt import filedata, graph
 
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
         colors.append((key, val))
     return colors
 
-def _renderlabels(labels, margin=2):
-    if not labels:
-        return
-    font = QApplication.font('QAbstractItemView')
-    fm = QFontMetrics(font, None)  # screen-compatible (i.e. QPixmap) metrics
-    twidths = [fm.width(t) for t, _s in labels]
-    th = fm.height()
-
-    padw = 2
-    padh = 1  # may overwrite horizontal frame to fit row
-
-    pix = QPixmap(sum(twidths) + len(labels) * (2 * padw + margin) - margin,
-                  th + 2 * padh)
-    pix.fill(Qt.transparent)
-    painter = QPainter(pix)
-    painter.setFont(font)
-    x = 0
-    for (text, style), tw in zip(labels, twidths):
-        lw = tw + 2 * padw
-        lh = th + 2 * padh
-        # draw bevel, background and text in order
-        bg = qtlib.getbgcoloreffect(style)
-        painter.fillRect(x, 0, lw, lh, bg.darker(110))
-        painter.fillRect(x + 1, 1, lw - 2, lh - 2, bg.lighter(110))
-        painter.fillRect(x + 2, 2, lw - 4, lh - 4, bg)
-        painter.setPen(qtlib.gettextcoloreffect(style))
-        painter.drawText(x + padw, padh, tw, th, 0, text)
-        x += lw + margin
-    painter.end()
-    return pix
-
 
 class HgRepoListModel(QAbstractTableModel):
     """

tortoisehg/hgqt/repoview.py

             return QSize(0, 0)
 
 
+class _LabelsLayout(object):
+    """Lay out and render text labels"""
+
+    def __init__(self, labels, margin=2):
+        self._labels = labels
+        self._margin = margin
+
+    def render(self):
+        font = QApplication.font('QAbstractItemView')
+        # screen-compatible (i.e. QPixmap) metrics
+        fm = QFontMetrics(font, None)
+        labels = self._labels
+        twidths = [fm.width(t) for t, _s in labels]
+        th = fm.height()
+
+        margin = self._margin
+        padw = 2
+        padh = 1  # may overwrite horizontal frame to fit row
+
+        pix = QPixmap(sum(twidths) + len(labels) * (2 * padw + margin) - margin,
+                      th + 2 * padh)
+        pix.fill(Qt.transparent)
+        painter = QPainter(pix)
+        painter.setFont(font)
+        x = 0
+        for (text, style), tw in zip(labels, twidths):
+            lw = tw + 2 * padw
+            lh = th + 2 * padh
+            # draw bevel, background and text in order
+            bg = qtlib.getbgcoloreffect(style)
+            painter.fillRect(x, 0, lw, lh, bg.darker(110))
+            painter.fillRect(x + 1, 1, lw - 2, lh - 2, bg.lighter(110))
+            painter.fillRect(x + 2, 2, lw - 4, lh - 4, bg)
+            painter.setPen(qtlib.gettextcoloreffect(style))
+            painter.drawText(x + padw, padh, tw, th, 0, text)
+            x += lw + margin
+        painter.end()
+        return pix
+
+
 class LabeledDelegate(QStyledItemDelegate):
     """Render text labels in place of icon/pixmap decoration"""
 
         super(LabeledDelegate, self).__init__(parent)
         self._margin = margin
 
+    def _makeLabelsLayout(self, labels):
+        return _LabelsLayout(labels, self._margin)
+
     def initStyleOption(self, option, index):
         super(LabeledDelegate, self).initStyleOption(option, index)
         labels = index.data(repomodel.LabelsRole).toPyObject()
             painter.restore()
 
     def _renderlabels(self, labels):
-        return repomodel._renderlabels(labels, self._margin)
+        return self._makeLabelsLayout(labels).render()
 
 
 class ColumnSelectDialog(QDialog):