[pitivi] prefs: Move the already-used logic to the shortcuts manager



commit 185115e9c99906a066034f52e1ee5378ef02ecc1
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Thu Sep 15 22:27:40 2016 +0200

    prefs: Move the already-used logic to the shortcuts manager
    
    Reviewed-by: Jakub Brindza <jakub brindza gmail com>
    Differential Revision: https://phabricator.freedesktop.org/D1310

 pitivi/dialogs/prefs.py |   29 +++++------------------------
 pitivi/shortcuts.py     |   26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 24 deletions(-)
---
diff --git a/pitivi/dialogs/prefs.py b/pitivi/dialogs/prefs.py
index 091fd7b..3cb4b35 100644
--- a/pitivi/dialogs/prefs.py
+++ b/pitivi/dialogs/prefs.py
@@ -437,7 +437,6 @@ class CustomShortcutDialog(Gtk.Dialog):
         self.currently_used = Gtk.Label()
         self.invalid_used = Gtk.Label()
         self.conflicting_action = None
-        self.conflicting_action_name = None
         self.conflict_label = Gtk.Label()
         self.apply_button = Gtk.Button()
         self.replace_button = Gtk.Button()
@@ -493,16 +492,18 @@ class CustomShortcutDialog(Gtk.Dialog):
                                            "Try using Control, Shift or Alt "
                                            "with some other key, please."))
 
-        already_used = self.verify_already_used(custom_keyval, custom_mask)
-        self.valid_shortcut = valid and not already_used
+        self.conflicting_action = self.app.shortcuts.get_conflicting_action(
+            self.customised_item.action_name, custom_keyval, custom_mask)
+        self.valid_shortcut = valid and not self.conflicting_action
         if self.valid_shortcut:
             self.toggle_apply_accel_buttons(custom_keyval, custom_mask)
         else:
             if valid and not equal_accelerators:
                 self.toggle_conflict_buttons(custom_keyval, custom_mask)
+                title = self.app.shortcuts.titles[self.conflicting_action]
                 self.conflict_label.set_markup(_("This shortcut is already used for <b>"
                                                  "%s</b>.\nDo you want to replace it?")
-                                               % self.conflicting_action_name)
+                                               % title)
 
         # Set visibility according to the booleans set above.
         self.apply_button.set_visible(self.valid_shortcut)
@@ -513,26 +514,6 @@ class CustomShortcutDialog(Gtk.Dialog):
         self.currently_used.set_visible(equal_accelerators)
         self.invalid_used.set_visible(not valid)
 
-    def verify_already_used(self, keyval, mask):
-        """Checks if the customised accelerator is not already used for another action.
-
-        Compare the customised accelerator to other accelerators in the same group
-        of actions as well as actions in the 'win' and 'app' groups, because these
-        would get affected if identical accelerator were set to some other action in a
-        container.
-        """
-        customised_action = self.customised_item.action_name
-        group_name = customised_action.split(".")[0]
-        groups_to_check = set([group_name, "app", "win"])
-        for group in groups_to_check:
-            for action, title in self.app.shortcuts.group_actions[group]:
-                for accel in self.app.get_accels_for_action(action):
-                    if (keyval, mask) == Gtk.accelerator_parse(accel):
-                        self.conflicting_action = action
-                        self.conflicting_action_name = title
-                        return True
-        return False
-
     def check_equal_to_set(self, keyval, mask):
         """Checks if the customised accelerator is not already set for the action."""
         action = self.customised_item.action_name
diff --git a/pitivi/shortcuts.py b/pitivi/shortcuts.py
index 0794ebd..6ecf282 100644
--- a/pitivi/shortcuts.py
+++ b/pitivi/shortcuts.py
@@ -39,6 +39,7 @@ class ShortcutsManager(GObject.Object):
         self.group_titles = {}
         self.group_actions = {}
         self.default_accelerators = {}
+        self.titles = {}
         self.config_path = os.path.sep.join([xdg_config_home(),
                                              "shortcuts.conf"])
         self.__loaded = self.__load()
@@ -84,6 +85,7 @@ class ShortcutsManager(GObject.Object):
                 to be used instead of that extracted from `action`.
         """
         self.default_accelerators[action] = accelerators
+        self.titles[action] = title
         if not self.__loaded:
             self.app.set_accels_for_action(action, accelerators)
 
@@ -116,6 +118,30 @@ class ShortcutsManager(GObject.Object):
         accelerators = self.app.get_accels_for_action(action)
         return set(accelerators) != set(self.default_accelerators[action])
 
+    def get_conflicting_action(self, action, keyval, mask):
+        """Looks for a conflicting action using the specified accelerator.
+
+        If an accelerator is used by another action in the same group or
+        in the "win" and "app" global groups, it is not clear which of them
+        will trigger when the accelerator is pressed.
+
+        Args:
+            action (str): The "prefix.name" identifying the action for which
+                the accelerator will be set if there is no conflict.
+            keyval (int): The key value of the accelerator.
+            mask (int): The mask value of the accelerator.
+
+        Returns:
+            str: The name of the conflicting action using the accelerator, or None.
+        """
+        group_name = action.split(".")[0]
+        for group in {group_name, "app", "win"}:
+            for action, unused_title in self.group_actions[group]:
+                for accel in self.app.get_accels_for_action(action):
+                    if (keyval, mask) == Gtk.accelerator_parse(accel):
+                        return action
+        return None
+
     def register_group(self, action_prefix, title):
         """Registers a group of shortcuts to be displayed.
 


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