[meld] treehelpers: New module for non-Meld-specific tree model helpers



commit 00f369e79d8e19df1121905f3f300a00aa30d17b
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Oct 23 07:52:54 2016 +1000

    treehelpers: New module for non-Meld-specific tree model helpers

 meld/tree.py        | 64 +-----------------------------------------
 meld/treehelpers.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 63 deletions(-)
---
diff --git a/meld/tree.py b/meld/tree.py
index bf39748d..1784db90 100644
--- a/meld/tree.py
+++ b/meld/tree.py
@@ -27,6 +27,7 @@ COL_TYPES = (str, str, str, str, str, Gdk.RGBA, Pango.Style,
              Pango.Weight, bool)
 
 from meld.misc import colour_lookup_with_fallback
+from meld.treehelpers import SearchableTreeStore
 from meld.vc._vc import \
     STATE_IGNORED, STATE_NONE, STATE_NORMAL, STATE_NOCHANGE, \
     STATE_ERROR, STATE_EMPTY, STATE_NEW, \
@@ -36,69 +37,6 @@ from meld.vc._vc import \
     CONFLICT_MERGED, CONFLICT_OTHER, CONFLICT_THIS
 
 
-class SearchableTreeStore(Gtk.TreeStore):
-
-    def inorder_search_down(self, it):
-        while it:
-            child = self.iter_children(it)
-            if child:
-                it = child
-            else:
-                next_it = self.iter_next(it)
-                if next_it:
-                    it = next_it
-                else:
-                    while True:
-                        it = self.iter_parent(it)
-                        if not it:
-                            raise StopIteration()
-                        next_it = self.iter_next(it)
-                        if next_it:
-                            it = next_it
-                            break
-            yield it
-
-    def inorder_search_up(self, it):
-        while it:
-            path = self.get_path(it)
-            if path[-1]:
-                path = path[:-1] + [path[-1] - 1]
-                it = self.get_iter(path)
-                while 1:
-                    nc = self.iter_n_children(it)
-                    if nc:
-                        it = self.iter_nth_child(it, nc - 1)
-                    else:
-                        break
-            else:
-                up = self.iter_parent(it)
-                if up:
-                    it = up
-                else:
-                    raise StopIteration()
-            yield it
-
-    def get_previous_next_paths(self, path, match_func):
-        prev_path, next_path = None, None
-        try:
-            start_iter = self.get_iter(path)
-        except ValueError:
-            # Invalid tree path
-            return None, None
-
-        for it in self.inorder_search_up(start_iter):
-            if match_func(it):
-                prev_path = self.get_path(it)
-                break
-
-        for it in self.inorder_search_down(start_iter):
-            if match_func(it):
-                next_path = self.get_path(it)
-                break
-
-        return prev_path, next_path
-
-
 class DiffTreeStore(SearchableTreeStore):
 
     def __init__(self, ntree, types):
diff --git a/meld/treehelpers.py b/meld/treehelpers.py
new file mode 100644
index 00000000..cdae8d37
--- /dev/null
+++ b/meld/treehelpers.py
@@ -0,0 +1,80 @@
+# Copyright (C) 2002-2006 Stephen Kennedy <stevek gnome org>
+# Copyright (C) 2011-2016 Kai Willadsen <kai willadsen gmail com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from gi.repository import Gtk
+
+
+class SearchableTreeStore(Gtk.TreeStore):
+
+    def inorder_search_down(self, it):
+        while it:
+            child = self.iter_children(it)
+            if child:
+                it = child
+            else:
+                next_it = self.iter_next(it)
+                if next_it:
+                    it = next_it
+                else:
+                    while True:
+                        it = self.iter_parent(it)
+                        if not it:
+                            raise StopIteration()
+                        next_it = self.iter_next(it)
+                        if next_it:
+                            it = next_it
+                            break
+            yield it
+
+    def inorder_search_up(self, it):
+        while it:
+            path = self.get_path(it)
+            if path[-1]:
+                path = path[:-1] + [path[-1] - 1]
+                it = self.get_iter(path)
+                while 1:
+                    nc = self.iter_n_children(it)
+                    if nc:
+                        it = self.iter_nth_child(it, nc - 1)
+                    else:
+                        break
+            else:
+                up = self.iter_parent(it)
+                if up:
+                    it = up
+                else:
+                    raise StopIteration()
+            yield it
+
+    def get_previous_next_paths(self, path, match_func):
+        prev_path, next_path = None, None
+        try:
+            start_iter = self.get_iter(path)
+        except ValueError:
+            # Invalid tree path
+            return None, None
+
+        for it in self.inorder_search_up(start_iter):
+            if match_func(it):
+                prev_path = self.get_path(it)
+                break
+
+        for it in self.inorder_search_down(start_iter):
+            if match_func(it):
+                next_path = self.get_path(it)
+                break
+
+        return prev_path, next_path


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]