[gtranslator] Translation memory keybindings



commit 074c9e2dc6dcb969095df4ac6991e00a1769570d
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Thu Sep 20 10:13:20 2018 +0200

    Translation memory keybindings
    
    See #20

 src/gtr-application.c | 32 +++++++++++++++++++++++
 src/gtr-tab.c         | 15 +++++++++++
 src/gtr-tab.h         |  2 ++
 src/gtr-window.c      | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/gtr-window.h      |  2 ++
 5 files changed, 122 insertions(+), 1 deletion(-)
---
diff --git a/src/gtr-application.c b/src/gtr-application.c
index 16ffbf92..29017ef6 100644
--- a/src/gtr-application.c
+++ b/src/gtr-application.c
@@ -378,6 +378,17 @@ build_tm_activated (GSimpleAction *action,
   gtr_window_show_tm_dialog (w);
 }
 
+static void
+tm_activated (GSimpleAction *action,
+              GVariant      *parameter,
+              gpointer       user_data)
+{
+  GtrApplication *app = GTR_APPLICATION (user_data);
+  GtrApplicationPrivate *priv = gtr_application_get_instance_private (app);
+  GtrWindow *w = GTR_WINDOW (priv->active_window);
+  gtr_window_tm_keybind (w, action);
+}
+
 static void
 toggle_fuzzy_activated (GSimpleAction *action,
                         GVariant      *parameter,
@@ -406,6 +417,16 @@ static GActionEntry app_entries[] = {
   { "fuzzy", toggle_fuzzy_activated, NULL, NULL, NULL },
 
   { "build_tm", build_tm_activated, NULL, NULL, NULL },
+  { "tm_1", tm_activated, NULL, NULL, NULL },
+  { "tm_2", tm_activated, NULL, NULL, NULL },
+  { "tm_3", tm_activated, NULL, NULL, NULL },
+  { "tm_4", tm_activated, NULL, NULL, NULL },
+  { "tm_5", tm_activated, NULL, NULL, NULL },
+  { "tm_6", tm_activated, NULL, NULL, NULL },
+  { "tm_7", tm_activated, NULL, NULL, NULL },
+  { "tm_8", tm_activated, NULL, NULL, NULL },
+  { "tm_9", tm_activated, NULL, NULL, NULL },
+
   { "new_window", new_window_activated, NULL, NULL, NULL },
   { "preferences", preferences_activated, NULL, NULL, NULL },
   { "help", help_activated, NULL, NULL, NULL },
@@ -455,6 +476,17 @@ gtr_application_startup (GApplication *application)
 
   set_kb (application, "app.fuzzy", "<Ctrl>u");
 
+  set_kb (application, "app.build_tm", "<Ctrl>plus");
+  set_kb (application, "app.tm_1", "<Ctrl>1");
+  set_kb (application, "app.tm_2", "<Ctrl>2");
+  set_kb (application, "app.tm_3", "<Ctrl>3");
+  set_kb (application, "app.tm_4", "<Ctrl>4");
+  set_kb (application, "app.tm_5", "<Ctrl>5");
+  set_kb (application, "app.tm_6", "<Ctrl>6");
+  set_kb (application, "app.tm_7", "<Ctrl>7");
+  set_kb (application, "app.tm_8", "<Ctrl>8");
+  set_kb (application, "app.tm_9", "<Ctrl>9");
+
   builder = gtk_builder_new ();
   gtk_builder_add_from_resource (builder, "/org/gnome/translator/gtranslator-menu.ui", NULL);
   gtk_application_set_app_menu (GTK_APPLICATION (application),
diff --git a/src/gtr-tab.c b/src/gtr-tab.c
index 12461bc8..ea3716fb 100644
--- a/src/gtr-tab.c
+++ b/src/gtr-tab.c
@@ -1708,3 +1708,18 @@ gtr_tab_set_info_bar (GtrTab * tab, GtkWidget * infobar)
   g_object_add_weak_pointer (G_OBJECT (priv->infobar),
                              (gpointer *) & priv->infobar);
 }
+
+GtrMsg *
+gtr_tab_get_msg (GtrTab *tab)
+{
+  GtrTabPrivate *priv;
+  GList *msg_aux;
+  GtrMsg *msg;
+
+  priv = gtr_tab_get_instance_private (tab);
+  msg_aux = gtr_po_get_current_message (priv->po);
+  msg = msg_aux->data;
+
+  return msg;
+}
+
diff --git a/src/gtr-tab.h b/src/gtr-tab.h
index 63d59e92..33de3bee 100644
--- a/src/gtr-tab.h
+++ b/src/gtr-tab.h
@@ -150,6 +150,8 @@ void gtr_tab_go_to_number (GtrTab * tab, gint number);
 
 void gtr_tab_set_info_bar (GtrTab * tab, GtkWidget * infobar);
 
+GtrMsg * gtr_tab_get_msg (GtrTab *tab);
+
 /* Semi-public methods */
 
 gchar *_gtr_tab_get_name (GtrTab * tab);
diff --git a/src/gtr-window.c b/src/gtr-window.c
index 709c8628..665f9777 100644
--- a/src/gtr-window.c
+++ b/src/gtr-window.c
@@ -103,6 +103,15 @@ static void          profile_combo_changed            (GtrStatusComboBox *combo,
 
 static void update_saved_state (GtrPo *po, GParamSpec *param, gpointer window);
 
+static void
+free_match (gpointer data)
+{
+  GtrTranslationMemoryMatch *match = (GtrTranslationMemoryMatch *) data;
+
+  g_free (match->match);
+  g_slice_free (GtrTranslationMemoryMatch, match);
+}
+
 static void
 update_undo_state (GtrTab     *tab,
                    GtrMsg     *msg,
@@ -110,7 +119,7 @@ update_undo_state (GtrTab     *tab,
 {
   GtrWindowPrivate *priv = gtr_window_get_instance_private(window);
   GtrView *active_view = gtr_window_get_active_view (window);
-  gtr_notebook_update_undo_buttons (priv->notebook, active_view);
+  gtr_notebook_update_undo_buttons (GTR_NOTEBOOK (priv->notebook), active_view);
 }
 
 /*
@@ -1193,3 +1202,64 @@ gtr_window_get_tm (GtrWindow *window) {
   GtrWindowPrivate *priv = gtr_window_get_instance_private (window);
   return priv->translation_memory;
 }
+
+void
+gtr_window_tm_keybind (GtrWindow *window,
+                       GSimpleAction *action)
+{
+  GtrWindowPrivate *priv = gtr_window_get_instance_private (window);
+  GtrTranslationMemory *tm = priv->translation_memory;
+  GList *tm_list;
+  const gchar *msgid;
+  GtrTab *tab = gtr_window_get_active_tab (window);
+  GtrMsg *msg;
+  const gchar *action_name;
+  GtrPo *po;
+  GtrView *view;
+  GtkTextBuffer *buffer;
+  gint index = 0;
+  GtrTranslationMemoryMatch *match = NULL;
+
+  if (!tab)
+    return;
+
+  view = gtr_tab_get_active_view (tab);
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
+  po = gtr_tab_get_po (tab);
+  msg = gtr_tab_get_msg (tab);
+  msgid = gtr_msg_get_msgid (msg);
+  tm_list = gtr_translation_memory_lookup (tm, msgid);
+
+  action_name = g_action_get_name (G_ACTION (action));
+  if (g_strcmp0 (action_name, "tm_1") == 0)
+    index = 0;
+  else if (g_strcmp0 (action_name, "tm_2") == 0)
+    index = 1;
+  else if (g_strcmp0 (action_name, "tm_2") == 0)
+    index = 1;
+  else if (g_strcmp0 (action_name, "tm_3") == 0)
+    index = 2;
+  else if (g_strcmp0 (action_name, "tm_4") == 0)
+    index = 3;
+  else if (g_strcmp0 (action_name, "tm_5") == 0)
+    index = 4;
+  else if (g_strcmp0 (action_name, "tm_6") == 0)
+    index = 5;
+  else if (g_strcmp0 (action_name, "tm_7") == 0)
+    index = 6;
+  else if (g_strcmp0 (action_name, "tm_8") == 0)
+    index = 7;
+  else if (g_strcmp0 (action_name, "tm_9") == 0)
+    index = 8;
+
+  match = (GtrTranslationMemoryMatch *) g_list_nth_data (tm_list, index);
+  if (match)
+    {
+      gtr_msg_set_msgstr (msg, match->match);
+      gtk_text_buffer_set_text (buffer, match->match, -1);
+      gtr_po_set_state (po, GTR_PO_STATE_MODIFIED);
+    }
+
+  g_list_free_full (tm_list, free_match);
+}
+
diff --git a/src/gtr-window.h b/src/gtr-window.h
index 8e38b60a..63ea2051 100644
--- a/src/gtr-window.h
+++ b/src/gtr-window.h
@@ -86,6 +86,8 @@ void gtr_window_show_tm_dialog (GtrWindow *window);
 
 void gtr_window_remove_all_pages (GtrWindow *window);
 
+void gtr_window_tm_keybind (GtrWindow *window, GSimpleAction *action);
+
 /** stack app states **/
 void gtr_window_show_projects (GtrWindow *window);
 void gtr_window_show_poeditor (GtrWindow *window);


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