Rev 176: Start trying to add pixmaps for the WTModel. in http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

John Arbash Meinel john at arbash-meinel.com
Wed Jul 8 20:39:26 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

------------------------------------------------------------
revno: 176
revision-id: john at arbash-meinel.com-20090708193921-d1nkn5u02z9ug89x
parent: john at arbash-meinel.com-20090707223742-48caj3t2uacieyc4
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: wt_model
timestamp: Wed 2009-07-08 14:39:21 -0500
message:
  Start trying to add pixmaps for the WTModel.
-------------- next part --------------
=== modified file 'lib/wt_model.py'
--- a/lib/wt_model.py	2009-07-07 22:37:13 +0000
+++ b/lib/wt_model.py	2009-07-08 19:39:21 +0000
@@ -102,6 +102,7 @@
     def __init__(self, wt, parent=None):
         QtCore.QAbstractItemModel.__init__(self, parent)
         self._wt = wt
+        self._dirmodel = QtGui.QDirModel()
         self._root_wt_item = _WTItem.create_from_wt(self._wt)
 
     def index(self, row, column, parent=None):
@@ -172,9 +173,15 @@
     def data(self, index, role):
         if index is None or not index.isValid():
             return QtCore.QVariant()
+        # TODO: implement uspport for Qt.DecorationRole, if we return a
+        #       QPixmap, then we will get the displayed icon.
+        item = index.internalPointer()
+        if role == QtCore.Qt.DecorationRole:
+            # Return the decoration from an QDirModel
+            dir_index = self._dirmodel.index(item.path)
+            return self._dirmodel.data(dir_index, role)
         if role != QtCore.Qt.DisplayRole:
             return QtCore.QVariant()
-        item = index.internalPointer()
         attr = self._column_to_attribute[index.column()]
         return QtCore.QVariant(getattr(item, attr, None))
 
@@ -187,33 +194,3 @@
     # IterChangesModel
     # def fetchMore(self, ...)
     # def canFetchmore(self, ...)
-
-
-# class WorkingTreeModel(QtGui.QDirModel):
-#     """A model that starts to know information about a given directory.
-#     """
-# 
-#     def __init__(self, parent=None):
-#         QtGui.QDirModel.__init__(self, parent)
-#         self._dir_model_columns = QtGui.QDirModel.columnCount(self)
-#         self._extra_data = {}
-# 
-#     def columnCount(self, ignored=None):
-#         return self._dir_model_columns + 1
-# 
-#     def data(self, index, role):
-#         if not index.isValid():
-#             return QtCore.QVariant()
-#         column = index.column() - self._dir_model_columns
-#         if column < 0:
-#             return QtGui.QDirModel.data(self, index, role)
-#         if role != QtCore.Qt.DisplayRole:
-#             return QtCore.QVariant()
-#         return QtCore.QVariant('this is my str')
-# 
-#     def headerData(self, section, orientation, role):
-#         column = section - self._dir_model_columns
-#         if column < 0:
-#             return QtGui.QDirModel.headerData(self, section, orientation, role)
-#         return QtCore.QVariant('my section')
-# 

=== modified file 'tests/test_wt_model.py'
--- a/tests/test_wt_model.py	2009-07-07 22:37:13 +0000
+++ b/tests/test_wt_model.py	2009-07-08 19:39:21 +0000
@@ -80,6 +80,19 @@
         self.assertIsInstance(res, QtCore.QVariant)
         self.assertEqual('dir/a_file', res.toString())
 
+    def test_data_decoraterole(self):
+        wt = self.make_branch_and_tree('.')
+        self.build_tree(['a_file.txt'])
+        wt.add(['a_file.txt'], ['a-id'])
+        model = wt_model.WorkingTreeModel(wt)
+        root_index = model.index(0, 0, None)
+        a_index = model.index(0, 0, root_index)
+        self.assertEqual('a_file.txt', a_index.internalPointer().name)
+        res = model.data(a_index, QtCore.Qt.DecorationRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertTrue(res.isValid())
+        self.assertTrue(res.canConvert(QtGui.QPixmap))
+
     def test_index_invalid(self):
         wt = self.make_branch_and_tree('.')
         model = wt_model.WorkingTreeModel(wt)



More information about the bazaar-commits mailing list