From 486e0a9d06e43f3b8669471bef13a5eeedbda4c6 Mon Sep 17 00:00:00 2001
From: Seth Hitefield <sdhitefield@gmail.com>
Date: Wed, 3 May 2017 07:06:54 -0700
Subject: grc: gtk3: Converted actions to Gio.Action instead of Gtk.Action

---
 grc/gui/DrawingArea.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

(limited to 'grc/gui/DrawingArea.py')

diff --git a/grc/gui/DrawingArea.py b/grc/gui/DrawingArea.py
index 94dfcf1370..2403fa2844 100644
--- a/grc/gui/DrawingArea.py
+++ b/grc/gui/DrawingArea.py
@@ -23,6 +23,7 @@ from gi.repository import Gtk, Gdk
 
 from .canvas.colors import FLOWGRAPH_BACKGROUND_COLOR
 from . import Constants
+from . import Actions
 
 
 class DrawingArea(Gtk.DrawingArea):
@@ -42,6 +43,7 @@ class DrawingArea(Gtk.DrawingArea):
         Gtk.DrawingArea.__init__(self)
 
         self._flow_graph = flow_graph
+        self.set_property('can_focus', True)
 
         self.zoom_factor = 1.0
         self._update_after_zoom = False
@@ -66,6 +68,11 @@ class DrawingArea(Gtk.DrawingArea):
             # Gdk.EventMask.FOCUS_CHANGE_MASK
         )
 
+        # This may not be the correct place to be handling the user events
+        # Should this be in the page instead?
+        # Or should more of the page functionality move here?
+        self.connect('key_press_event', self._handle_key_press)
+
         # setup drag and drop
         self.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
         self.connect('drag-data-received', self._handle_drag_data_received)
@@ -78,12 +85,14 @@ class DrawingArea(Gtk.DrawingArea):
 
         def _handle_notify_event(widget, event, focus_flag):
             self._focus_flag = focus_flag
+
         self.connect('leave-notify-event', _handle_notify_event, False)
         self.connect('enter-notify-event', _handle_notify_event, True)
         # todo: fix
 #        self.set_flags(Gtk.CAN_FOCUS)  # self.set_can_focus(True)
 #        self.connect('focus-out-event', self._handle_focus_lost_event)
 
+
     ##########################################################################
     # Handlers
     ##########################################################################
@@ -155,6 +164,41 @@ class DrawingArea(Gtk.DrawingArea):
             coordinate=self._translate_event_coords(event),
         )
 
+    def _handle_key_press(self, widget, event):
+        """
+        Handle specific keypresses when the drawing area has focus that
+        triggers actions by the user.
+        """
+        key = event.keyval
+        mod = event.state
+
+        # Setup a map of the accelerator keys to the action to trigger
+        accels = {
+            Gtk.accelerator_parse('d'): Actions.BLOCK_DISABLE,
+            Gtk.accelerator_parse('e'): Actions.BLOCK_ENABLE,
+            Gtk.accelerator_parse('b'): Actions.BLOCK_BYPASS,
+            Gtk.accelerator_parse('c'): Actions.BLOCK_CREATE_HIER,
+            Gtk.accelerator_parse('Up'): Actions.BLOCK_DEC_TYPE,
+            Gtk.accelerator_parse('Down'): Actions.BLOCK_INC_TYPE,
+            Gtk.accelerator_parse('Left'): Actions.BLOCK_ROTATE_CCW,
+            Gtk.accelerator_parse('Right'): Actions.BLOCK_ROTATE_CW,
+            Gtk.accelerator_parse('minus'): Actions.PORT_CONTROLLER_DEC,
+            Gtk.accelerator_parse('plus'): Actions.PORT_CONTROLLER_INC,
+            Gtk.accelerator_parse('Add'): Actions.PORT_CONTROLLER_INC,
+            Gtk.accelerator_parse('Subtract'): Actions.PORT_CONTROLLER_DEC,
+            Gtk.accelerator_parse('Return'): Actions.BLOCK_PARAM_MODIFY,
+            Gtk.accelerator_parse('<Shift>t'): Actions.BLOCK_VALIGN_TOP,
+            Gtk.accelerator_parse('<Shift>m'): Actions.BLOCK_VALIGN_MIDDLE,
+            Gtk.accelerator_parse('<Shift>b'): Actions.BLOCK_VALIGN_BOTTOM,
+            Gtk.accelerator_parse('<Shift>l'): Actions.BLOCK_HALIGN_LEFT,
+            Gtk.accelerator_parse('<Shift>c'): Actions.BLOCK_HALIGN_CENTER,
+            Gtk.accelerator_parse('<Shift>r'): Actions.BLOCK_HALIGN_RIGHT,
+        }
+        # Not sold on this.
+        if (key, mod) in accels:
+            accels[(key, mod)]()
+            return True
+
     def _update_size(self):
         w, h = self._flow_graph.get_extents()[2:]
         self.set_size_request(w * self.zoom_factor + 100, h * self.zoom_factor + 100)
-- 
cgit v1.2.3