[gtranslator] Sort action in headerbar



commit 93bcbfef3f50a86a25d7e489750e067d1f5b2f51
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Sat Oct 13 12:56:15 2018 +0200

    Sort action in headerbar
    
    See #25

 src/gtr-application.c         |  32 ++++++++++
 src/gtr-message-table-model.h |  14 +++++
 src/gtr-message-table.c       |  64 ++++++++++++++++++--
 src/gtr-message-table.h       |   3 +
 src/gtr-notebook.c            |   8 +++
 src/gtr-notebook.h            |   2 +
 src/gtr-notebook.ui           | 136 ++++++++++++++++++++++++++++++++++++++++++
 src/gtr-tab.c                 |  10 ++++
 src/gtr-tab.h                 |   3 +
 src/gtr-window.c              |   6 +-
 10 files changed, 270 insertions(+), 8 deletions(-)
---
diff --git a/src/gtr-application.c b/src/gtr-application.c
index fe352431..765999f4 100644
--- a/src/gtr-application.c
+++ b/src/gtr-application.c
@@ -456,6 +456,29 @@ toggle_fuzzy_activated (GSimpleAction *action,
   gtr_message_status_toggle_fuzzy (NULL, w);
 }
 
+static void
+sort_by_activated (GSimpleAction *action,
+                   GVariant      *parameter,
+                   gpointer       user_data)
+{
+  GtrApplication *app = GTR_APPLICATION (user_data);
+  GtrApplicationPrivate *priv = gtr_application_get_instance_private (app);
+  GtrWindow *w;
+  GtrTab *tab;
+
+  GVariant *st;
+  gint sortby = 0;
+
+  g_object_get (G_OBJECT (action), "state", &st, NULL);
+  sortby = g_variant_get_int32 (st);
+
+  w = GTR_WINDOW (priv->active_window);
+  g_return_if_fail (w != NULL);
+  tab = gtr_window_get_active_tab (w);
+  g_return_if_fail (tab != NULL);
+  gtr_tab_sort_by (tab, (GtrMessageTableSortBy)sortby);
+}
+
 static GActionEntry app_entries[] = {
   { "save", save_activated, NULL, NULL, NULL },
   { "saveas", saveas_activated, NULL, NULL, NULL },
@@ -472,6 +495,15 @@ static GActionEntry app_entries[] = {
 
   { "fuzzy", toggle_fuzzy_activated, NULL, NULL, NULL },
 
+  // sort actions
+  { "sort_by_id", sort_by_activated, NULL, "0", NULL },
+  { "sort_by_status", sort_by_activated, NULL, "1", NULL },
+  { "sort_by_status_desc", sort_by_activated, NULL, "2", NULL },
+  { "sort_by_msgid", sort_by_activated, NULL, "3", NULL },
+  { "sort_by_msgid_desc", sort_by_activated, NULL, "4", NULL },
+  { "sort_by_translated", sort_by_activated, NULL, "5", NULL },
+  { "sort_by_translated_desc", sort_by_activated, NULL, "6", NULL },
+
   { "build_tm", build_tm_activated, NULL, NULL, NULL },
   { "tm_1", tm_activated, NULL, NULL, NULL },
   { "tm_2", tm_activated, NULL, NULL, NULL },
diff --git a/src/gtr-message-table-model.h b/src/gtr-message-table-model.h
index 0e2817d5..5b7dcdb2 100644
--- a/src/gtr-message-table-model.h
+++ b/src/gtr-message-table-model.h
@@ -64,6 +64,20 @@ enum _GtrMessageTableModelColumn
   GTR_MESSAGE_TABLE_MODEL_N_COLUMNS
 };
 
+typedef enum _GtrMessageTableSortBy GtrMessageTableSortBy;
+
+enum _GtrMessageTableSortBy
+{
+  GTR_MESSAGE_TABLE_SORT_ID,
+  GTR_MESSAGE_TABLE_SORT_STATUS,
+  GTR_MESSAGE_TABLE_SORT_STATUS_DESC,
+  GTR_MESSAGE_TABLE_SORT_MSGID,
+  GTR_MESSAGE_TABLE_SORT_MSGID_DESC,
+  GTR_MESSAGE_TABLE_SORT_TRANSLATED,
+  GTR_MESSAGE_TABLE_SORT_TRANSLATED_DESC,
+  GTR_MESSAGE_TABLE_SORT_N_COLUMNS
+};
+
 GType                   gtr_message_table_model_get_type      (void) G_GNUC_CONST;
 
 GtrMessageTableModel   *gtr_message_table_model_new           (GtrMessageContainer  *container);
diff --git a/src/gtr-message-table.c b/src/gtr-message-table.c
index 8140b508..291526cf 100644
--- a/src/gtr-message-table.c
+++ b/src/gtr-message-table.c
@@ -51,6 +51,7 @@ typedef struct
   GtkTreeModel *sort_model;
 
   GtrTab *tab;
+  GtrMessageTableSortBy sort_status;
 } GtrMessageTablePrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtrMessageTable, gtr_message_table, GTK_TYPE_BOX)
@@ -240,6 +241,7 @@ gtr_message_table_init (GtrMessageTable * table)
   colors->untranslated = gdk_rgba_copy (&untranslated);
 
   priv = gtr_message_table_get_instance_private (table);
+  priv->sort_status = GTR_MESSAGE_TABLE_SORT_ID;
 
   gtk_orientable_set_orientation (GTK_ORIENTABLE (table),
                                   GTK_ORIENTATION_VERTICAL);
@@ -416,19 +418,14 @@ gtr_message_table_populate (GtrMessageTable * table, GtrMessageContainer * conta
   priv->sort_model =
     gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (priv->store));
 
-  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
-                                        (priv->sort_model),
-                                        GTR_MESSAGE_TABLE_MODEL_ID_COLUMN,
-                                        GTK_SORT_ASCENDING);
-
   gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE
                                            (priv->sort_model),
                                            NULL, NULL, NULL);
-
   gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (priv->sort_model),
                                    GTR_MESSAGE_TABLE_MODEL_STATUS_COLUMN,
                                    model_compare_by_status, NULL, NULL);
 
+  gtr_message_table_sort_by (table, priv->sort_status);
   gtk_tree_view_set_model (GTK_TREE_VIEW (priv->treeview),
                            priv->sort_model);
 }
@@ -532,3 +529,58 @@ gtr_message_table_navigate (GtrMessageTable * table,
 
   return msg;
 }
+
+void
+gtr_message_table_sort_by (GtrMessageTable *table,
+                           GtrMessageTableSortBy sort)
+{
+  GtrMessageTablePrivate *priv;
+  priv = gtr_message_table_get_instance_private (table);
+  priv->sort_status = sort;
+
+  switch (sort)
+    {
+    case GTR_MESSAGE_TABLE_SORT_STATUS:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_STATUS_COLUMN,
+                                            GTK_SORT_ASCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_STATUS_DESC:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_STATUS_COLUMN,
+                                            GTK_SORT_DESCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_MSGID:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_ORIGINAL_COLUMN,
+                                            GTK_SORT_ASCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_MSGID_DESC:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_ORIGINAL_COLUMN,
+                                            GTK_SORT_DESCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_TRANSLATED:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_TRANSLATION_COLUMN,
+                                            GTK_SORT_ASCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_TRANSLATED_DESC:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_TRANSLATION_COLUMN,
+                                            GTK_SORT_DESCENDING);
+      break;
+    case GTR_MESSAGE_TABLE_SORT_ID:
+    default:
+      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE
+                                            (priv->sort_model),
+                                            GTR_MESSAGE_TABLE_MODEL_ID_COLUMN,
+                                            GTK_SORT_ASCENDING);
+    }
+}
diff --git a/src/gtr-message-table.h b/src/gtr-message-table.h
index 65cb4faa..27f30a7b 100644
--- a/src/gtr-message-table.h
+++ b/src/gtr-message-table.h
@@ -90,5 +90,8 @@ gtr_message_table_get_type (void)
                                              GtrMsg * msg,
                                              gchar * translation);
 
+     void gtr_message_table_sort_by (GtrMessageTable *table,
+                                     GtrMessageTableSortBy sort);
+
 G_END_DECLS
 #endif /* __MESSAGE_TABLE_H__ */
diff --git a/src/gtr-notebook.c b/src/gtr-notebook.c
index c0c88872..5c92ad97 100644
--- a/src/gtr-notebook.c
+++ b/src/gtr-notebook.c
@@ -34,6 +34,7 @@ typedef struct
 {
   GtkWidget *titlebar;
   GtkWidget *save;
+  GtkWidget *sort_id;
 
   GtkWidget *undo;
   GtkWidget *redo;
@@ -139,6 +140,7 @@ gtr_notebook_class_init (GtrNotebookClass * klass)
                                                "/org/gnome/translator/gtr-notebook.ui");
 
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, titlebar);
+  gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, sort_id);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, undo);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, redo);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, save);
@@ -281,3 +283,9 @@ gtr_notebook_update_undo_buttons (GtrNotebook *notebook,
   gtk_widget_set_sensitive (priv->redo, can_redo);
 }
 
+void
+gtr_notebook_reset_sort (GtrNotebook *notebook)
+{
+  GtrNotebookPrivate *priv = gtr_notebook_get_instance_private (notebook);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->sort_id), TRUE);
+}
diff --git a/src/gtr-notebook.h b/src/gtr-notebook.h
index 72c74429..38b85a28 100644
--- a/src/gtr-notebook.h
+++ b/src/gtr-notebook.h
@@ -79,5 +79,7 @@ void gtr_notebook_enable_save (GtrNotebook *notebook, gboolean enable);
 
 void gtr_notebook_update_undo_buttons (GtrNotebook *notebook, GtrView *view);
 
+void gtr_notebook_reset_sort (GtrNotebook *notebook);
+
 G_END_DECLS
 #endif /* __NOTEBOOK_H__ */
diff --git a/src/gtr-notebook.ui b/src/gtr-notebook.ui
index 259d5eda..45278cc9 100644
--- a/src/gtr-notebook.ui
+++ b/src/gtr-notebook.ui
@@ -6,6 +6,116 @@
     <property name="visible">True</property>
     <property name="can_focus">True</property>
   </template>
+
+  <object class="GtkPopoverMenu" id="order_menu_popover">
+    <property name="can_focus">False</property>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="margin_left">6</property>
+        <property name="margin_right">6</property>
+        <property name="margin_top">6</property>
+        <property name="margin_bottom">6</property>
+        <property name="orientation">vertical</property>
+
+        <child>
+          <object class="GtkLabel">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes">Order messages by</property>
+            <property name="xalign">0.5</property>
+            <property name="margin_top">3</property>
+            <property name="margin_bottom">3</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+
+        <child>
+          <object class="GtkSeparator">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="margin_top">3</property>
+            <property name="margin_bottom">3</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+
+        <child>
+          <object class="GtkRadioButton" id="sort_id">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="action_name">app.sort_by_id</property>
+            <property name="label" translatable="yes">Appearance in the original file</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkRadioButton" id="sort_status">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="action_name">app.sort_by_status</property>
+            <property name="label" translatable="yes">Message status</property>
+            <property name="group">sort_id</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">3</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkRadioButton" id="sort_msgid">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="action_name">app.sort_by_msgid</property>
+            <property name="label" translatable="yes">Original message</property>
+            <property name="group">sort_id</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">5</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkRadioButton" id="sort_translated">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="action_name">app.sort_by_translated</property>
+            <property name="label" translatable="yes">Translated message</property>
+            <property name="group">sort_id</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">7</property>
+          </packing>
+        </child>
+      </object>
+      <packing>
+        <property name="submenu">main</property>
+        <property name="position">1</property>
+      </packing>
+    </child>
+  </object>
+
   <object class="GtkPopoverMenu" id="main_menu_popover">
     <property name="can_focus">False</property>
     <child>
@@ -342,6 +452,31 @@
         <property name="position">10</property>
       </packing>
     </child>
+
+    <child>
+      <object class="GtkMenuButton" id="sortby_menu">
+        <property name="visible">True</property>
+        <property name="can_focus">True</property>
+        <property name="receives_default">True</property>
+        <property name="popover">order_menu_popover</property>
+        <child>
+          <object class="GtkImage">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="icon_name">view-sort-ascending-symbolic</property>
+          </object>
+        </child>
+        <child internal-child="accessible">
+          <object class="AtkObject" id="sortby_menu-atkobject">
+            <property name="AtkObject::accessible-name" translatable="yes">Order by menu</property>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="position">11</property>
+      </packing>
+    </child>
+
     <child>
       <object class="GtkMenuButton" id="main_menu">
         <property name="visible">True</property>
@@ -419,6 +554,7 @@
         <property name="position">6</property>
       </packing>
     </child>
+
     <style>
       <class name="titlebar"/>
     </style>
diff --git a/src/gtr-tab.c b/src/gtr-tab.c
index 0eaeef6b..092eac12 100644
--- a/src/gtr-tab.c
+++ b/src/gtr-tab.c
@@ -1798,3 +1798,13 @@ gtr_tab_set_progress (GtrTab      *tab,
   g_free (fuzzy_text);
   g_free (untrans_text);
 }
+
+void
+gtr_tab_sort_by (GtrTab *tab,
+                 GtrMessageTableSortBy sort)
+{
+  GtrTabPrivate *priv;
+  priv = gtr_tab_get_instance_private (tab);
+  gtr_message_table_sort_by (GTR_MESSAGE_TABLE (priv->message_table), sort);
+}
+
diff --git a/src/gtr-tab.h b/src/gtr-tab.h
index 519ec05d..d151f2ae 100644
--- a/src/gtr-tab.h
+++ b/src/gtr-tab.h
@@ -31,6 +31,7 @@
 #include "gtr-msg.h"
 #include "gtr-po.h"
 #include "gtr-view.h"
+#include "gtr-message-table-model.h"
 
 G_BEGIN_DECLS
 /*
@@ -154,6 +155,8 @@ GtrMsg * gtr_tab_get_msg (GtrTab *tab);
 
 void gtr_tab_set_progress (GtrTab *tab, gint trans, gint untrans, gint fuzzy);
 
+void gtr_tab_sort_by (GtrTab *tab, GtrMessageTableSortBy sort);
+
 /* Semi-public methods */
 
 gchar *_gtr_tab_get_name (GtrTab * tab);
diff --git a/src/gtr-window.c b/src/gtr-window.c
index ffe4b219..c1f7ef24 100644
--- a/src/gtr-window.c
+++ b/src/gtr-window.c
@@ -879,6 +879,7 @@ gtr_window_create_tab (GtrWindow * window, GtrPo * po)
   gtk_widget_show (GTK_WIDGET (tab));
 
   gtr_notebook_add_page (GTR_NOTEBOOK (priv->notebook), tab);
+  gtr_notebook_reset_sort (GTR_NOTEBOOK (priv->notebook));
 
   g_signal_connect_after (po,
                           "notify::state",
@@ -900,7 +901,9 @@ gtr_window_create_tab (GtrWindow * window, GtrPo * po)
 GtrTab *
 gtr_window_get_active_tab (GtrWindow * window)
 {
-  GtrWindowPrivate *priv = gtr_window_get_instance_private(window);
+  GtrWindowPrivate *priv = gtr_window_get_instance_private (window);
+  g_return_val_if_fail (priv != NULL, NULL);
+  g_return_val_if_fail (priv->notebook != NULL, NULL);
 
   return gtr_notebook_get_page (GTR_NOTEBOOK (priv->notebook));
 }
@@ -1264,4 +1267,3 @@ gtr_window_tm_keybind (GtrWindow *window,
 
   g_list_free_full (tm_list, free_match);
 }
-


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