r23431 - gnucash/trunk/src - Register2: improve sort functionality

Geert Janssens gjanssens at code.gnucash.org
Sun Nov 24 11:28:25 EST 2013


Author: gjanssens
Date: 2013-11-24 11:28:25 -0500 (Sun, 24 Nov 2013)
New Revision: 23431
Trac: http://svn.gnucash.org/trac/changeset/23431

Modified:
   gnucash/trunk/src/gnome-utils/gnc-tree-control-split-reg.c
   gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.c
   gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.h
   gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c
   gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.h
   gnucash/trunk/src/gnome-utils/gnc-tree-view.c
   gnucash/trunk/src/gnome/gnc-split-reg2.c
Log:
Register2: improve sort functionality

- reduce state file access to one location (tree view)
- at runtime, let model keep track of the sort state (it needs it
  the most for its query)
- the view code can read the sort state from the model when needed
- reuse state handling code in gnc_tree_view where possible to avoid
  unneeded code duplication

Modified: gnucash/trunk/src/gnome/gnc-split-reg2.c
===================================================================
--- gnucash/trunk/src/gnome/gnc-split-reg2.c	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome/gnc-split-reg2.c	2013-11-24 16:28:25 UTC (rev 23431)
@@ -293,31 +293,25 @@
 
     gnc_ledger_display2_set_split_view_register (gsr->ledger, view);
 
-    /* Setting the view's state-section above already restores most
-     * of the saved state automatically. There are a few register specific
-     * settings though that will be restored here. */
+    /* Synchronize model state with view state
+     * (needed to properly set up the internal query) */
 
     /* Restore the sort depth from saved state */
-    view->sort_depth = g_key_file_get_integer (state_file, state_section, "sort_depth", NULL);
+    model->sort_depth = g_key_file_get_integer (state_file, state_section, "sort_depth", NULL);
 
-    /* Restore the sort order from saved state */
-    sort_string = g_key_file_get_string (state_file, state_section, "sort_order", NULL);
-    if (g_strcmp0 ("descending", sort_string) == 0)
-        view->sort_direction = -1;
-    else
-        view->sort_direction = 1;
+    s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
+    if (s_model)
+    {
+        gint sort_col;
+        GtkSortType   type;
 
-    /* Restore the sort column from saved state */
-    view->sort_col = g_key_file_get_integer (state_file, state_section, "sort_col", NULL);
-    if (view->sort_col == 0)
-        view->sort_col = 1;
+        if (gtk_tree_sortable_get_sort_column_id (GTK_TREE_SORTABLE (s_model), &sort_col, &type))
+        {
+            model->sort_col = sort_col;
+            model->sort_direction = type;
+        }
+    }
 
-    /* Synchronize model state with view state
-     * (needed to properly set up the internal query) */
-    model->sort_col = view->sort_col;
-    model->sort_depth = view->sort_depth;
-    model->sort_direction = view->sort_direction;
-
     gnc_tree_view_configure_columns (GNC_TREE_VIEW (view));
 
     if (ledger_type == LD2_GL && model->type == GENERAL_LEDGER2)
@@ -371,8 +365,6 @@
     g_signal_connect (gsr->scroll_bar, "button-release-event",
                       G_CALLBACK (gsr2_scroll_button_event_cb), gsr);
 
-    s_model = gtk_tree_view_get_model (GTK_TREE_VIEW (view));
-
     // Connect a call back to update the sort settings.
     g_signal_connect (GTK_TREE_SORTABLE (s_model), "sort-column-changed",
           G_CALLBACK (gnc_split_reg2_sort_changed_cb), gsr);
@@ -816,29 +808,16 @@
 
     sort_depth = gnc_tree_view_reg_get_selected_row_depth (view);
     if (sort_depth != 0)
-        view->sort_depth = sort_depth;
+        model->sort_depth = sort_depth;
 
-    view->sort_col = sortcol;
     model->sort_col = sortcol;
+    model->sort_direction = type;
 
-    if (type == GTK_SORT_DESCENDING)
-    {
-        view->sort_direction = -1;
-        model->sort_direction = -1;
-    }
-    else
-    {
-        view->sort_direction = 1;
-        model->sort_direction = 1;
-    }
-
     /* Save the sort depth state */
     state_section = gnc_tree_view_get_state_section (GNC_TREE_VIEW (view));
-    g_key_file_set_integer (state_file, state_section, "sort_depth", view->sort_depth);
-    g_key_file_set_integer (state_file, state_section, "sort_col", view->sort_col);
-    /* NOTE sort_column is handled directly by the treeview */
+    g_key_file_set_integer (state_file, state_section, "sort_depth", model->sort_depth);
 
-    LEAVE("v_sort_col %d, v_sort_direction is %d  v_sort_depth is %d", view->sort_col, view->sort_direction, view->sort_depth);
+    LEAVE("m_sort_col %d, m_sort_direction is %d  m_sort_depth is %d", model->sort_col, model->sort_direction, model->sort_depth);
 
     if (sortcol != -1)
         gnc_ledger_display2_refresh (gsr->ledger);

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-control-split-reg.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-control-split-reg.c	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-control-split-reg.c	2013-11-24 16:28:25 UTC (rev 23431)
@@ -737,7 +737,7 @@
     GncTreeModelSplitReg *model;
     GtkTreePath *mpath, *spath;
     GtkTreePath  *new_mpath, *new_spath;
-    gint *indices;
+    gint *indices, sort_direction;
     gchar *sstring;
 
     ENTER("Move relative, view is %p, relative is %d", view, relative);
@@ -752,8 +752,13 @@
 
     indices = gtk_tree_path_get_indices (spath);
 
-    new_spath = gtk_tree_path_new_from_indices (indices[0] + (relative * view->sort_direction), -1);
+    if (model->sort_direction == GTK_SORT_DESCENDING)
+        sort_direction = -1;
+    else
+        sort_direction = 1;
 
+    new_spath = gtk_tree_path_new_from_indices (indices[0] + (relative * sort_direction), -1);
+
     // if relative == 0 we block all selection changes
     gnc_tree_view_split_reg_block_selection (view, TRUE);
     gtk_tree_selection_unselect_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (view)), spath);
@@ -1427,18 +1432,18 @@
 
     ENTER("");
 
-    if (view->sort_col != COL_DATE)
-    {
-        LEAVE("Not sorted by date - no up/down move available");
-        return FALSE;
-    }
-
     // The allocated memory references will all be cleaned up in the
     // updown_finish: label.
 
     model = gnc_tree_view_split_reg_get_model_from_view (view);
     g_return_val_if_fail(model, FALSE);
 
+    if (model->sort_col != COL_DATE)
+    {
+        LEAVE("Not sorted by date - no up/down move available");
+        return FALSE;
+    }
+
     mpath = gnc_tree_view_split_reg_get_current_path (view);
     if (!mpath)
     {

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.c	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.c	2013-11-24 16:28:25 UTC (rev 23431)
@@ -478,7 +478,7 @@
 
     model->sort_col = 1;
     model->sort_depth = 1;
-    model->sort_direction = 1;
+    model->sort_direction = GTK_SORT_ASCENDING;
 
     model->current_trans = NULL;
     model->current_row = -1;
@@ -631,7 +631,7 @@
     if (model->current_trans == NULL)
         model->current_trans = priv->btrans;
 
-    if (model->sort_direction == 1) // ascending
+    if (model->sort_direction == GTK_SORT_ASCENDING)
     {
         /* Get a list of Unique Transactions from an slist */
         priv->full_tlist = xaccSplitListGetUniqueTransactions (slist);
@@ -2276,7 +2276,7 @@
 
     /* This is really a dummy sort function, it leaves the list as is. */
 
-    if (model->sort_direction == 1) // Ascending
+    if (model->sort_direction == GTK_SORT_ASCENDING)
         return gtk_tree_path_compare (gnc_tree_model_split_reg_get_path (tm, a),
                                       gnc_tree_model_split_reg_get_path (tm, b));
     else

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.h	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-model-split-reg.h	2013-11-24 16:28:25 UTC (rev 23431)
@@ -142,7 +142,7 @@
 
     gint                         sort_depth;            /**< This is the row the sort direction is based on. */
     gint                         sort_col;              /**< This is the column the sort direction is based on. */
-    gint                         sort_direction;        /**< This is the direction of sort, 1 for ascending or -1 rest */
+    GtkSortType                  sort_direction;        /**< This is the direction of sort */
 
     gboolean                     use_accounting_labels; /**< whether to use accounting Labels */
     gboolean                     separator_changed;     /**< whether the separator has changed */ 

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.c	2013-11-24 16:28:25 UTC (rev 23431)
@@ -466,8 +466,6 @@
     view->priv->current_depth = 0;
     view->reg_closing = FALSE;
     view->priv->fo_handler_id = 0;
-    view->sort_depth = 1;
-    view->sort_direction = 1;
     view->priv->auto_complete = FALSE;
     view->priv->trans_confirm = RESET;
     view->priv->single_button_press = 0;
@@ -2783,9 +2781,9 @@
     RowDepth temp_depth;
     gboolean is_template;
 
-    ENTER("title depth is %d and sort_depth %d, sort_col is %d", depth, view->sort_depth, view->sort_col);
-
     model = gnc_tree_view_split_reg_get_model_from_view (view);
+    ENTER("title depth is %d and sort_depth %d, sort_col is %d", depth, model->sort_depth, model->sort_col);
+
     columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (view));
 
     is_template = gnc_tree_model_split_reg_get_template (model);
@@ -2813,7 +2811,7 @@
             {
             default: //FIXME These if statements may not be required
                 /* Display arrows if we are sorting on this row */
-                if (view->sort_depth == depth && view->sort_col == viewcol)
+                if (model->sort_depth == depth && model->sort_col == viewcol)
                     gtk_tree_view_column_set_sort_indicator (tvc, TRUE);
                 else
                     gtk_tree_view_column_set_sort_indicator (tvc, FALSE);
@@ -2858,7 +2856,7 @@
 
             default:
                 /* Display arrows if we are sorting on this row */
-                if (view->sort_depth == depth && view->sort_col == viewcol)
+                if (model->sort_depth == depth && model->sort_col == viewcol)
                     gtk_tree_view_column_set_sort_indicator (tvc, TRUE);
                 else
                     gtk_tree_view_column_set_sort_indicator (tvc, FALSE);
@@ -2905,7 +2903,7 @@
 
             default:
                 /* Display arrows if we are sorting on this row */
-                if (view->sort_depth == depth && view->sort_col == viewcol)
+                if (model->sort_depth == depth && model->sort_col == viewcol)
                     gtk_tree_view_column_set_sort_indicator (tvc, TRUE);
                 else
                     gtk_tree_view_column_set_sort_indicator (tvc, FALSE);
@@ -2939,7 +2937,7 @@
 
             default:
                 /* Display arrows if we are sorting on this row */
-                if (view->sort_depth == depth && view->sort_col == viewcol)
+                if (model->sort_depth == depth && model->sort_col == viewcol)
                     gtk_tree_view_column_set_sort_indicator (tvc, TRUE);
                 else
                     gtk_tree_view_column_set_sort_indicator (tvc, FALSE);
@@ -5939,7 +5937,7 @@
     mpath = gnc_tree_view_split_reg_get_current_path (view);
     spath = gnc_tree_view_split_reg_get_sort_path_from_model_path (view, mpath);
 
-    if (view->sort_direction == -1)
+    if (model->sort_direction == GTK_SORT_DESCENDING)
     {
         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (view), spath, NULL, TRUE, 0.5, 0.0); //0.0
     }

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.h	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-view-split-reg.h	2013-11-24 16:28:25 UTC (rev 23431)
@@ -57,9 +57,6 @@
     gpointer                    uiupdate_cb_data;            // Used for page gui update
 
     gchar                      *help_text;                // This is the help text to be displayed.
-    gint                        sort_depth;               // This is the row the sort direction is based on.
-    gint                        sort_col;                 // This is the column the sort direction is based on.
-    gint                        sort_direction;           // This is the direction of sort, 1 for ascending or -1 rest
     gboolean                    reg_closing;              // This is set when closing the register.
     gboolean                    change_allowed;           // This is set when we allow the reconciled split to change.
     gboolean                    editing_now;              // This is set while editing of a cell.

Modified: gnucash/trunk/src/gnome-utils/gnc-tree-view.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-tree-view.c	2013-11-24 16:28:12 UTC (rev 23430)
+++ gnucash/trunk/src/gnome-utils/gnc-tree-view.c	2013-11-24 16:28:25 UTC (rev 23431)
@@ -340,7 +340,7 @@
 
     if (priv->state_section)
     {
-        /* Save state */
+        /* Save state. Only store non-default values when possible. */
         GList *column_list, *tmp;
         GKeyFile *state_file = gnc_state_get_current();
         gsize num_cols = 0;
@@ -348,15 +348,22 @@
         gchar *sort_order = gnc_tree_view_get_sort_order (view);
         gchar **col_order = gnc_tree_view_get_column_order (view, &num_cols);
 
-        if (sort_column)
+        /* Default sort column is the first column */
+        if (sort_column &&
+                (gnc_tree_view_find_column_by_name (view, sort_column) !=
+                        gtk_tree_view_get_column (GTK_TREE_VIEW (view), 0)))
             g_key_file_set_string (state_file, priv->state_section, STATE_KEY_SORT_COLUMN, sort_column);
         else if (g_key_file_has_key (state_file, priv->state_section, STATE_KEY_SORT_COLUMN, NULL))
             g_key_file_remove_key (state_file, priv->state_section, STATE_KEY_SORT_COLUMN, NULL);
+        g_free (sort_column);
 
-        if (sort_order)
+
+        /* Default sort order is "ascending" */
+        if (g_strcmp0 (sort_order, "descending") == 0)
             g_key_file_set_string (state_file, priv->state_section, STATE_KEY_SORT_ORDER, sort_order);
         else if (g_key_file_has_key (state_file, priv->state_section, STATE_KEY_SORT_ORDER, NULL))
             g_key_file_remove_key (state_file, priv->state_section, STATE_KEY_SORT_ORDER, NULL);
+        g_free (sort_order);
 
         if (col_order && (num_cols > 0))
             g_key_file_set_string_list (state_file, priv->state_section, STATE_KEY_COLUMN_ORDER,



More information about the gnucash-changes mailing list