Skip to content

Instantly share code, notes, and snippets.

@yuja
Created April 17, 2013 15:04
Show Gist options
  • Save yuja/5405044 to your computer and use it in GitHub Desktop.
Save yuja/5405044 to your computer and use it in GitHub Desktop.
use ItemDelegate for graph rendering
# HG changeset patch
# Parent cbd303484003454f8ab1eb04f01f2ede6092cb7d
diff --git a/tortoisehg/hgqt/repomodel.py b/tortoisehg/hgqt/repomodel.py
--- a/tortoisehg/hgqt/repomodel.py
+++ b/tortoisehg/hgqt/repomodel.py
@@ -500,7 +500,7 @@ class HgRepoListModel(QAbstractTableMode
def _roleoffsets(self):
return {Qt.DisplayRole : 0,
Qt.ForegroundRole : len(self._columns),
- Qt.DecorationRole : len(self._columns) * 2}
+ 32 : len(self._columns) * 2} # should define role name
def data(self, index, role):
if not index.isValid():
@@ -532,12 +532,10 @@ class HgRepoListModel(QAbstractTableMode
self._cache.extend([None,] * (graphlen-cachelen))
data = self._cache[row]
if data is None:
- data = [None,] * (self._roleoffsets[Qt.DecorationRole]+1)
+ data = [None,] * (self._roleoffsets[32]+1)
column = self._columns[index.column()]
offset = self._roleoffsets[role]
- if role == Qt.DecorationRole:
- if column != 'Graph':
- return nullvariant
+ if role == 32:
if data[offset] is None:
gnode = self.graph[row]
ctx = self.repo.changectx(gnode.rev)
@@ -593,10 +591,7 @@ class HgRepoListModel(QAbstractTableMode
if not self.revset:
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | dragflags
if ctx.rev() not in self.revset:
- if self._columns[index.column()] == 'Graph':
- return Qt.ItemIsEnabled
- else:
- return Qt.ItemFlags(0)
+ return Qt.ItemFlags(0)
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | dragflags
def mimeTypes(self):
diff --git a/tortoisehg/hgqt/repoview.py b/tortoisehg/hgqt/repoview.py
--- a/tortoisehg/hgqt/repoview.py
+++ b/tortoisehg/hgqt/repoview.py
@@ -24,6 +24,14 @@ from tortoisehg.hgqt.logcolumns import C
from PyQt4.QtCore import *
from PyQt4.QtGui import *
+class GraphDelegate(QStyledItemDelegate):
+ def paint(self, painter, option, index):
+ super(GraphDelegate, self).paint(painter, option, index)
+ # Maybe GraphDelegate should have responsibility for rendering?
+ # i.e. move model.graphctx() here?
+ graph = QPixmap(index.data(32))
+ painter.drawPixmap(option.rect.left(), option.rect.top(), graph)
+
class HgRepoView(QTableView):
revisionClicked = pyqtSignal(object)
@@ -56,6 +64,7 @@ class HgRepoView(QTableView):
self.standardDelegate = self.itemDelegate()
self.htmlDelegate = htmldelegate.HTMLDelegate(self)
+ self.graphDelegate = GraphDelegate(self)
self.setAcceptDrops(True)
if PYQT_VERSION >= 0x40700:
@@ -140,6 +149,8 @@ class HgRepoView(QTableView):
for c in range(model.columnCount(QModelIndex())):
if model._columns[c] in ['Description', 'Changes']:
self.setItemDelegateForColumn(c, self.htmlDelegate)
+ elif model._columns[c] == 'Graph':
+ self.setItemDelegateForColumn(c, self.graphDelegate)
else:
self.setItemDelegateForColumn(c, self.standardDelegate)
@yuja
Copy link
Author

yuja commented Apr 17, 2013

標準の QStyledItemDelegate で下地を描いて、その上にグラフを描画させてみた。
QStyledItemDelegate はグラフを描画すべきでないので、専用のロールでグラフデータを受け渡してる。

ここまでやるなら、グラフの基礎データを data(xxx) で得て、描画処理は全て GraphDelegate がすべきかもしれない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment