Rev 170: Implement _WTItem.row() and WorkingTreeModel.parent() in http://bazaar.launchpad.net/~jameinel/bzr-explorer/wt_model

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


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

------------------------------------------------------------
revno: 170
revision-id: john at arbash-meinel.com-20090707220101-81fmz8kk5bn47sqa
parent: john at arbash-meinel.com-20090707214157-hcuif5aamnls23yr
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: wt_model
timestamp: Tue 2009-07-07 17:01:01 -0500
message:
  Implement _WTItem.row() and WorkingTreeModel.parent()
-------------- next part --------------
=== modified file 'lib/wt_model.py'
--- a/lib/wt_model.py	2009-07-07 21:41:57 +0000
+++ b/lib/wt_model.py	2009-07-07 22:01:01 +0000
@@ -45,6 +45,12 @@
         self.parent = parent
         self.children = []
 
+    def row(self):
+        """Return the row for this object in the parents children list."""
+        if self.parent is None:
+            return 0
+        return self.parent.children.index(self)
+
     @staticmethod
     def create_from_wt(wt):
         """Create the _WTItem tree structure from an inventory."""
@@ -116,7 +122,14 @@
         child = parent_item.children[row]
         return self.createIndex(row, column, child)
 
-    # def parent(self, ...):
+    def parent(self, index):
+        if index is None or not index.isValid():
+            return QtCore.QModelIndex()
+
+        item = index.internalPointer()
+        if item.parent is self._root_wt_item:
+            return QtCore.QModelIndex()
+        return self.createIndex(item.parent.row(), 0, item.parent)
 
     def rowCount(self, parent=None):
         if parent is None or not parent.isValid():
@@ -125,8 +138,9 @@
             parent_item = parent.internalPointer()
         return len(parent_item.children)
 
-
     # implement def hasChildren(self, ...) if rowCount is expensive
+    # We could do this, by looking at Item.kind
+    # For now, though, we don't lazily load anything, so leave it alone
 
     def columnCount(self, parent=None):
         # this can vary based on parent, but *doesn't*

=== modified file 'tests/test_wt_model.py'
--- a/tests/test_wt_model.py	2009-07-07 21:41:57 +0000
+++ b/tests/test_wt_model.py	2009-07-07 22:01:01 +0000
@@ -85,6 +85,37 @@
         self.assertEqual('dir', dir_index.internalPointer().name)
         self.assertEqual(1, model.rowCount(dir_index))
 
+    def test_parent_empty(self):
+        wt = self.make_branch_and_tree('.')
+        model = wt_model.WorkingTreeModel(wt)
+        res = model.parent(QtCore.QModelIndex())
+        self.assertIsInstance(res, QtCore.QModelIndex)
+        self.assertFalse(res.isValid())
+        res = model.parent(None)
+        self.assertIsInstance(res, QtCore.QModelIndex)
+        self.assertFalse(res.isValid())
+
+    def test_parent_with_subdirs(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)
+        self.assertEqual(2, model.rowCount())
+        dir_index = model.index(1, 0, None)
+        self.assertIsInstance(dir_index, QtCore.QModelIndex)
+        self.assertTrue(dir_index.isValid())
+        self.assertEqual('dir', dir_index.internalPointer().name)
+        res = model.parent(dir_index)
+        self.assertIsInstance(res, QtCore.QModelIndex)
+        self.assertFalse(res.isValid())
+        a_index = model.index(0, 0, dir_index)
+        self.assertEqual('a_file', a_index.internalPointer().name)
+        res = model.parent(a_index)
+        self.assertIsInstance(res, QtCore.QModelIndex)
+        self.assertTrue(res.isValid())
+        self.assertEqual('dir', res.internalPointer().name)
+
     def test_headerData(self):
         wt = self.make_branch_and_tree('.')
         model = wt_model.WorkingTreeModel(wt)
@@ -137,6 +168,7 @@
         self.assertEqual('file-id', child.file_id)
         self.assertEqual('file', child.inv_kind)
         self.assertEqual(root_item, child.parent)
+        self.assertEqual(0, child.row())
 
     def test_unversioned_from_wt(self):
         wt = self.make_branch_and_tree('.')
@@ -159,6 +191,7 @@
         self.assertEqual('file-id', child.file_id)
         self.assertEqual('file', child.inv_kind)
         self.assertEqual(root_item, child.parent)
+        self.assertEqual(0, child.row())
         child = root_item.children[1]
         self.assertEqual('unversioned-file', child.path)
         self.assertEqual('unversioned-file', child.name)
@@ -166,6 +199,7 @@
         self.assertEqual(None, child.file_id)
         self.assertEqual(None, child.inv_kind)
         self.assertEqual(root_item, child.parent)
+        self.assertEqual(1, child.row())
 
     def test_with_subdirs(self):
         wt = self.make_branch_and_tree('.')
@@ -186,6 +220,7 @@
         self.assertEqual('dir-id', child.file_id)
         self.assertEqual('directory', child.inv_kind)
         self.assertEqual(root_item, child.parent)
+        self.assertEqual(0, root_item.row())
         self.assertEqual(1, len(child.children))
         this_dir = child
         child = this_dir.children[0]
@@ -195,6 +230,7 @@
         self.assertEqual('subdir-id', child.file_id)
         self.assertEqual('directory', child.inv_kind)
         self.assertEqual(this_dir, child.parent)
+        self.assertEqual(0, child.row())
         self.assertEqual(1, len(child.children))
         this_dir = child
         child = this_dir.children[0]
@@ -204,3 +240,4 @@
         self.assertEqual('file-id', child.file_id)
         self.assertEqual('file', child.inv_kind)
         self.assertEqual(this_dir, child.parent)
+        self.assertEqual(0, child.row())



More information about the bazaar-commits mailing list