Rev 172: Implement support for WTModel.data(), I believe this makes a complete model. in http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

John Arbash Meinel john at arbash-meinel.com
Tue Jul 7 23:18:57 BST 2009


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

------------------------------------------------------------
revno: 172
revision-id: john at arbash-meinel.com-20090707221849-17waznmkhccielpg
parent: john at arbash-meinel.com-20090707220759-bu83yqbq43eqkodg
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: wt_model
timestamp: Tue 2009-07-07 17:18:49 -0500
message:
  Implement support for WTModel.data(), I believe this makes a complete model.
-------------- next part --------------
=== modified file 'lib/wt_model.py'
--- a/lib/wt_model.py	2009-07-07 22:07:59 +0000
+++ b/lib/wt_model.py	2009-07-07 22:18:49 +0000
@@ -44,6 +44,7 @@
         #       instead
         self.parent = parent
         self.children = []
+        self.status = None
 
     def row(self):
         """Return the row for this object in the parents children list."""
@@ -91,6 +92,7 @@
         'inv_kind',
         'path',
         ]
+    _column_to_attribute = dict(enumerate(_headers))
 
     def __init__(self, wt, parent=None):
         QtCore.QAbstractItemModel.__init__(self, parent)
@@ -162,7 +164,14 @@
             return 0
         return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)
 
-    # def data(self, ...):
+    def data(self, index, role):
+        if index is None or not index.isValid():
+            return QtCore.QVariant()
+        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))
 
     # implement def setData(self, ...) if we want to be editable
 

=== modified file 'tests/test_wt_model.py'
--- a/tests/test_wt_model.py	2009-07-07 22:07:59 +0000
+++ b/tests/test_wt_model.py	2009-07-07 22:18:49 +0000
@@ -42,6 +42,43 @@
         self.assertEqual(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable,
                          model.flags(index))
 
+    def test_data_invalid(self):
+        wt = self.make_branch_and_tree('.')
+        self.build_tree(['a_file'])
+        model = wt_model.WorkingTreeModel(wt)
+        res = model.data(QtCore.QModelIndex(), QtCore.Qt.DisplayRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertFalse(res.isValid())
+        index = model.index(0, 0, None)
+        # TODO: We probably could implement ToolTipRole, perhaps returning the
+        #       full path?
+        res = model.data(QtCore.QModelIndex(), QtCore.Qt.ToolTipRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertFalse(res.isValid())
+
+    def test_data(self):
+        wt = self.make_branch_and_tree('.')
+        self.build_tree(['dir/', 'dir/a_file', 'b_file'])
+        wt.add(['dir', 'dir/a_file', 'b_file'],
+               ['dir-id', 'a-id', 'b-id'])
+        model = wt_model.WorkingTreeModel(wt)
+        index = model.index(1, 0, None)
+        res = model.data(index, QtCore.Qt.DisplayRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertEqual('dir', res.toString())
+        index = model.index(1, 1, None)
+        res = model.data(index, QtCore.Qt.DisplayRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertEqual('dir-id', res.toString())
+        a_index = model.index(0, 0, index)
+        res = model.data(a_index, QtCore.Qt.DisplayRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertEqual('a_file', res.toString())
+        a_index = model.index(0, 5, index)
+        res = model.data(a_index, QtCore.Qt.DisplayRole)
+        self.assertIsInstance(res, QtCore.QVariant)
+        self.assertEqual('dir/a_file', res.toString())
+
     def test_index_invalid(self):
         wt = self.make_branch_and_tree('.')
         model = wt_model.WorkingTreeModel(wt)



More information about the bazaar-commits mailing list