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

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@23431 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Geert Janssens 2013-11-24 16:28:25 +00:00
parent d3b7d454b5
commit 2fa8a82c43
7 changed files with 54 additions and 68 deletions

View File

@ -737,7 +737,7 @@ gnc_tree_control_split_reg_goto_rel_trans_row (GncTreeViewSplitReg *view, gint r
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,7 +752,12 @@ gnc_tree_control_split_reg_goto_rel_trans_row (GncTreeViewSplitReg *view, gint r
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);
@ -1427,18 +1432,18 @@ static gboolean gtcsr_move_current_entry_updown(GncTreeViewSplitReg *view,
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)
{

View File

@ -478,7 +478,7 @@ gnc_tree_model_split_reg_new (SplitRegisterType2 reg_type, SplitRegisterStyle2 s
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 @@ gnc_tree_model_split_reg_load (GncTreeModelSplitReg *model, GList *slist, Accoun
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 @@ gnc_tree_model_split_reg_sort_iter_compare_func (GtkTreeModel *tm,
/* 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

View File

@ -142,7 +142,7 @@ typedef struct
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 */

View File

@ -466,8 +466,6 @@ gnc_tree_view_split_reg_init (GncTreeViewSplitReg *view)
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 @@ gtv_sr_titles (GncTreeViewSplitReg *view, RowDepth depth)
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 @@ gtv_sr_titles (GncTreeViewSplitReg *view, RowDepth depth)
{
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 @@ gtv_sr_titles (GncTreeViewSplitReg *view, RowDepth depth)
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 @@ gtv_sr_titles (GncTreeViewSplitReg *view, RowDepth depth)
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 @@ gtv_sr_titles (GncTreeViewSplitReg *view, RowDepth depth)
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 @@ gnc_tree_view_split_reg_scroll_to_cell (GncTreeViewSplitReg *view)
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
}

View File

@ -57,9 +57,6 @@ typedef struct
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.

View File

@ -340,7 +340,7 @@ gnc_tree_view_destroy (GtkObject *object)
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 @@ gnc_tree_view_destroy (GtkObject *object)
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,

View File

@ -293,30 +293,24 @@ gsr2_create_table (GNCSplitReg2 *gsr)
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. */
/* Restore the sort depth from saved state */
view->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;
/* 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;
/* 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;
/* Restore the sort depth from saved state */
model->sort_depth = g_key_file_get_integer (state_file, state_section, "sort_depth", NULL);
s_model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
if (s_model)
{
gint sort_col;
GtkSortType type;
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;
}
}
gnc_tree_view_configure_columns (GNC_TREE_VIEW (view));
@ -371,8 +365,6 @@ gsr2_create_table (GNCSplitReg2 *gsr)
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 @@ gnc_split_reg2_sort_changed_cb (GtkTreeSortable *sortable, gpointer user_data)
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;
if (type == GTK_SORT_DESCENDING)
{
view->sort_direction = -1;
model->sort_direction = -1;
}
else
{
view->sort_direction = 1;
model->sort_direction = 1;
}
model->sort_direction = type;
/* 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);