[meld/deprecation-cleanup: 9/48] tree: Move common popup handling code to new class



commit dcb867b7da536f37d1841e95c0aa72370aaa003d
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Fri Nov 30 09:00:36 2018 +1000

    tree: Move common popup handling code to new class

 data/ui/vcview.ui |  2 +-
 meld/dirdiff.py   | 48 +-----------------------------------------------
 meld/tree.py      | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 meld/vcview.py    | 44 +-------------------------------------------
 4 files changed, 56 insertions(+), 91 deletions(-)
---
diff --git a/data/ui/vcview.ui b/data/ui/vcview.ui
index b72fa595..8f2a77ca 100644
--- a/data/ui/vcview.ui
+++ b/data/ui/vcview.ui
@@ -504,7 +504,7 @@
                   <object class="GtkTreeView" id="treeview">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <signal name="button-press-event" handler="on_button_press_event" swapped="no"/>
+                    <signal name="button-press-event" handler="on_treeview_button_press_event" swapped="no"/>
                     <signal name="cursor-changed" handler="on_treeview_cursor_changed" swapped="no"/>
                     <signal name="row-activated" handler="on_row_activated" swapped="no"/>
                     <signal name="popup-menu" handler="on_treeview_popup_menu" swapped="no"/>
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index 2cbd8156..a786fa3f 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -285,7 +285,7 @@ class CanonicalListing:
         return element.lower()
 
 
-class DirDiff(MeldDoc, Component):
+class DirDiff(tree.TreeviewCommon, MeldDoc, Component):
     """Two or three way folder comparison"""
 
     __gtype_name__ = "DirDiff"
@@ -1465,52 +1465,6 @@ class DirDiff(MeldDoc, Component):
                 })
         return different
 
-    def on_treeview_popup_menu(self, treeview):
-        cursor_path, cursor_col = treeview.get_cursor()
-        if not cursor_path:
-            self.popup_menu.popup_at_pointer(None)
-            return True
-
-        # We always want to pop up to the right of the first column,
-        # ignoring the actual cursor column location.
-        rect = treeview.get_background_area(
-            cursor_path, treeview.get_column(0))
-
-        self.popup_menu.popup_at_rect(
-            treeview.get_bin_window(),
-            rect,
-            Gdk.Gravity.SOUTH_EAST,
-            Gdk.Gravity.NORTH_WEST,
-            None,
-        )
-        return True
-
-    def on_treeview_button_press_event(self, treeview, event):
-        # Unselect any selected files in other panes
-        for t in [v for v in self.treeview[:self.num_panes] if v != treeview]:
-            t.get_selection().unselect_all()
-
-        if (event.triggers_context_menu() and
-                event.type == Gdk.EventType.BUTTON_PRESS):
-
-            treeview.grab_focus()
-
-            path = treeview.get_path_at_pos(int(event.x), int(event.y))
-            if path is None:
-                return False
-
-            selection = treeview.get_selection()
-            model, rows = selection.get_selected_rows()
-
-            if path[0] not in rows:
-                selection.unselect_all()
-                selection.select_path(path[0])
-                treeview.set_cursor(path[0])
-
-            self.popup_menu.popup_at_pointer(event)
-            return True
-        return False
-
     def get_state_traversal(self, diffmapindex):
         def tree_state_iter():
             treeindex = (0, self.num_panes-1)[diffmapindex]
diff --git a/meld/tree.py b/meld/tree.py
index 3a04115e..8bca4fd8 100644
--- a/meld/tree.py
+++ b/meld/tree.py
@@ -220,6 +220,59 @@ class DiffTreeStore(SearchableTreeStore):
             self.set(treeiter, safe_keys_values)
 
 
+class TreeviewCommon:
+
+    def on_treeview_popup_menu(self, treeview):
+        cursor_path, cursor_col = treeview.get_cursor()
+        if not cursor_path:
+            self.popup_menu.popup_at_pointer(None)
+            return True
+
+        # We always want to pop up to the right of the first column,
+        # ignoring the actual cursor column location.
+        rect = treeview.get_background_area(
+            cursor_path, treeview.get_column(0))
+
+        self.popup_menu.popup_at_rect(
+            treeview.get_bin_window(),
+            rect,
+            Gdk.Gravity.SOUTH_EAST,
+            Gdk.Gravity.NORTH_WEST,
+            None,
+        )
+        return True
+
+    def on_treeview_button_press_event(self, treeview, event):
+
+        # If we have multiple treeviews, unselect clear other tree selections
+        num_panes = getattr(self, 'num_panes', 1)
+        if num_panes > 1:
+            for t in self.treeview[:self.num_panes]:
+                if t != treeview:
+                    t.get_selection().unselect_all()
+
+        if (event.triggers_context_menu() and
+                event.type == Gdk.EventType.BUTTON_PRESS):
+
+            treeview.grab_focus()
+
+            path = treeview.get_path_at_pos(int(event.x), int(event.y))
+            if path is None:
+                return False
+
+            selection = treeview.get_selection()
+            model, rows = selection.get_selected_rows()
+
+            if path[0] not in rows:
+                selection.unselect_all()
+                selection.select_path(path[0])
+                treeview.set_cursor(path[0])
+
+            self.popup_menu.popup_at_pointer(event)
+            return True
+        return False
+
+
 def treeview_search_cb(model, column, key, it, data):
     # If the key contains a path separator, search the whole path,
     # otherwise just use the filename. If the key is all lower-case, do a
diff --git a/meld/vcview.py b/meld/vcview.py
index 78697de0..63d2c985 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -119,7 +119,7 @@ class VcTreeStore(tree.DiffTreeStore):
         return self.get_value(it, self.column_index(tree.COL_PATH, 0))
 
 
-class VcView(MeldDoc, Component):
+class VcView(tree.TreeviewCommon, MeldDoc, Component):
 
     __gtype_name__ = "VcView"
 
@@ -493,48 +493,6 @@ class VcView(MeldDoc, Component):
         self.emit("create-diff",
                   [Gio.File.new_for_path(d) for d in diffs], kwargs)
 
-    def on_treeview_popup_menu(self, treeview):
-        cursor_path, cursor_col = treeview.get_cursor()
-        if not cursor_path:
-            self.popup_menu.popup_at_pointer(None)
-            return True
-
-        # We always want to pop up to the right of the first column,
-        # ignoring the actual cursor column location.
-        rect = treeview.get_background_area(
-            cursor_path, treeview.get_column(0))
-
-        self.popup_menu.popup_at_rect(
-            treeview.get_bin_window(),
-            rect,
-            Gdk.Gravity.SOUTH_EAST,
-            Gdk.Gravity.NORTH_WEST,
-            None,
-        )
-        return True
-
-    def on_button_press_event(self, treeview, event):
-        if (event.triggers_context_menu() and
-                event.type == Gdk.EventType.BUTTON_PRESS):
-
-            treeview.grab_focus()
-
-            path = treeview.get_path_at_pos(int(event.x), int(event.y))
-            if path is None:
-                return False
-
-            selection = treeview.get_selection()
-            model, rows = selection.get_selected_rows()
-
-            if path[0] not in rows:
-                selection.unselect_all()
-                selection.select_path(path[0])
-                treeview.set_cursor(path[0])
-
-            self.popup_menu.popup_at_pointer(event)
-            return True
-        return False
-
     def on_filter_state_toggled(self, button):
         active_filters = [
             k for k, (action_name, fn) in self.state_actions.items()


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