From 0e4898fe0a5dd6108ca9fa281789c4e27157aa9a Mon Sep 17 00:00:00 2001 From: Robert Fewell <14uBobIT@gmail.com> Date: Sun, 15 Jul 2018 16:44:50 +0100 Subject: [PATCH 1/2] Add the ability to cascade account colours in account tree Add the ability to select an account that has sub-accounts and use that accounts colour on all sub accounts. By default only sub accounts that do not have a colour set are updated but there is an option to over ride this. --- gnucash/gnome-utils/dialog-account.c | 116 ++++++++++++++ gnucash/gnome-utils/dialog-account.h | 2 + gnucash/gnome/gnc-plugin-page-account-tree.c | 24 +++ gnucash/gtkbuilder/dialog-account.glade | 150 ++++++++++++++++++ .../ui/gnc-plugin-page-account-tree-ui.xml | 2 + 5 files changed, 294 insertions(+) diff --git a/gnucash/gnome-utils/dialog-account.c b/gnucash/gnome-utils/dialog-account.c index ae697c3a84..5eb2cf71f7 100644 --- a/gnucash/gnome-utils/dialog-account.c +++ b/gnucash/gnome-utils/dialog-account.c @@ -2080,3 +2080,119 @@ gnc_account_renumber_create_dialog (GtkWidget *window, Account *account) gtk_widget_show_all(data->dialog); } + +static void +default_color_button_cb (GtkButton *button, gpointer user_data) +{ + GdkRGBA color; + + if (gdk_rgba_parse (&color, DEFAULT_COLOR)) + gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER(user_data), &color); +} + +static void +update_account_color (Account *acc, const gchar *old_color, const gchar *new_color, gboolean replace) +{ + // check to see if the color has been changed + if (g_strcmp0 (new_color, old_color) != 0) + { + if ((old_color == NULL) || (g_strcmp0 (old_color, "Not Set") == 0) || (replace == TRUE)) + { + xaccAccountBeginEdit (acc); + xaccAccountSetColor (acc, new_color); + xaccAccountCommitEdit (acc); + } + } +} + +void +gnc_account_cascade_color_dialog (GtkWidget *window, Account *account) +{ + GtkWidget *dialog; + GtkBuilder *builder; + GtkWidget *color_label, *color_button, *over_write, *color_button_default; + gchar *string; + const char *color_string; + gchar *old_color_string; + GdkRGBA color; + gint response; + + // check if we actualy do have sub accounts + g_return_if_fail (gnc_account_n_children (account) > 0); + + builder = gtk_builder_new(); + gnc_builder_add_from_file (builder, "dialog-account.glade", "account_cascade_color_dialog"); + dialog = GTK_WIDGET(gtk_builder_get_object (builder, "account_cascade_color_dialog")); + gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(window)); + + color_label = GTK_WIDGET(gtk_builder_get_object (builder, "color_label")); + over_write = GTK_WIDGET(gtk_builder_get_object (builder, "replace_check")); + color_button = GTK_WIDGET(gtk_builder_get_object (builder, "color_button")); + color_button_default = GTK_WIDGET(gtk_builder_get_object (builder, "color_button_default")); + + gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER(color_button), FALSE); + + g_signal_connect (G_OBJECT(color_button_default), "clicked", + G_CALLBACK(default_color_button_cb), (gpointer)color_button); + + string = g_strdup_printf(_( "Set the account color for account '%s' " + "including all sub-accounts to the selected color:"), + gnc_account_get_full_name(account)); + gtk_label_set_text (GTK_LABEL(color_label), string); + g_free (string); + + color_string = xaccAccountGetColor (account); // get existing account color + + old_color_string = g_strdup (color_string); // save the old color string + + if ((color_string == NULL) || (g_strcmp0 (color_string, "Not Set") == 0)) + color_string = DEFAULT_COLOR; + + // set the color chooser to account color + if (gdk_rgba_parse (&color, color_string)) + gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER(color_button), &color); + + /* default to cancel */ + gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL); + + gtk_builder_connect_signals (builder, dialog); + g_object_unref (G_OBJECT(builder)); + + gtk_widget_show_all (dialog); + + response = gtk_dialog_run (GTK_DIALOG(dialog)); + + if (response == GTK_RESPONSE_OK) + { + GList *accounts = gnc_account_get_descendants (account); + gboolean replace = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(over_write)); + GList *acct; + GdkRGBA new_color; + const gchar *new_color_string; + + gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER(color_button), &new_color); + new_color_string = gdk_rgba_to_string (&new_color); + + if (g_strcmp0 (new_color_string, DEFAULT_COLOR) == 0) + new_color_string = "Not Set"; + + // check/update selected account + update_account_color (account, old_color_string, new_color_string, replace); + + if (accounts != NULL) + { + for (acct = accounts; acct; acct = g_list_next(acct)) + { + const char *string = xaccAccountGetColor (acct->data); + + // check/update sub-account + update_account_color (acct->data, string, new_color_string, replace); + } + g_list_free (accounts); + } + } + if (old_color_string) + g_free (old_color_string); + + gtk_widget_destroy (dialog); +} diff --git a/gnucash/gnome-utils/dialog-account.h b/gnucash/gnome-utils/dialog-account.h index ffdb44b201..099f1a3bd0 100644 --- a/gnucash/gnome-utils/dialog-account.h +++ b/gnucash/gnome-utils/dialog-account.h @@ -161,6 +161,8 @@ void gnc_ui_register_account_destroy_callback (void (*cb)(Account *)); void gnc_account_renumber_create_dialog (GtkWidget *window, Account *account); +void gnc_account_cascade_color_dialog (GtkWidget *window, Account *account); + /** @} */ /** @} */ diff --git a/gnucash/gnome/gnc-plugin-page-account-tree.c b/gnucash/gnome/gnc-plugin-page-account-tree.c index 5913b7513e..80478f5fbe 100644 --- a/gnucash/gnome/gnc-plugin-page-account-tree.c +++ b/gnucash/gnome/gnc-plugin-page-account-tree.c @@ -153,6 +153,7 @@ static void gnc_plugin_page_account_tree_cmd_lots (GtkAction *action, GncPluginP static void gnc_plugin_page_account_tree_cmd_scrub (GtkAction *action, GncPluginPageAccountTree *page); static void gnc_plugin_page_account_tree_cmd_scrub_sub (GtkAction *action, GncPluginPageAccountTree *page); static void gnc_plugin_page_account_tree_cmd_scrub_all (GtkAction *action, GncPluginPageAccountTree *page); +static void gnc_plugin_page_account_tree_cmd_cascade_color_account (GtkAction *action, GncPluginPageAccountTree *page); /* Command callback for new Register Test */ static void gnc_plugin_page_account_tree_cmd_open2_account (GtkAction *action, GncPluginPageAccountTree *page); @@ -226,6 +227,11 @@ static GtkActionEntry gnc_plugin_page_account_tree_actions [] = N_("Delete selected account"), G_CALLBACK (gnc_plugin_page_account_tree_cmd_delete_account) }, + { + "ColorCascadeAccountAction", NULL, N_("_Cascade Account Color..."), NULL, + N_("Cascade selected account color"), + G_CALLBACK (gnc_plugin_page_account_tree_cmd_cascade_color_account) + }, { "EditFindAccountAction", "edit-find", N_("F_ind Account"), "i", N_("Find an account"), @@ -1179,6 +1185,24 @@ gnc_plugin_page_account_tree_cmd_find_account_popup (GtkAction *action, GncPlugi LEAVE(" "); } +static void +gnc_plugin_page_account_tree_cmd_cascade_color_account (GtkAction *action, GncPluginPageAccountTree *page) +{ + Account *account = NULL; + GtkWidget *window; + + ENTER("action %p, page %p", action, page); + + account = gnc_plugin_page_account_tree_get_current_account (page); + + window = gnc_plugin_page_get_window(GNC_PLUGIN_PAGE(page)); + + if (account != NULL) + gnc_account_cascade_color_dialog (window, account); + + LEAVE(" "); +} + /******************************************************************** * delete_account_helper * See if this account has any splits present. Set the user data diff --git a/gnucash/gtkbuilder/dialog-account.glade b/gnucash/gtkbuilder/dialog-account.glade index 399febe2ff..998d380176 100644 --- a/gnucash/gtkbuilder/dialog-account.glade +++ b/gnucash/gtkbuilder/dialog-account.glade @@ -2,6 +2,156 @@ + + False + Cascade Account Color + dialog + + + False + vertical + 2 + + + False + end + + + _Cancel + True + True + True + False + True + + + False + False + 0 + + + + + _OK + True + True + True + False + True + + + False + False + 1 + + + + + False + False + 0 + + + + + True + False + vertical + 12 + + + True + False + True + + + False + True + 0 + + + + + True + False + center + + + True + True + True + + + False + True + 0 + + + + + Default + True + True + True + + + False + True + 1 + + + + + False + False + 1 + + + + + True + False + If any account has an existing color it will not be replaced unless the following is ticked. + True + + + False + True + 2 + + + + + Replace any existing account colors + True + True + False + center + True + + + False + True + 3 + + + + + False + True + 1 + + + + + + cancelbutton3 + okbutton3 + + + + + False 6 diff --git a/gnucash/ui/gnc-plugin-page-account-tree-ui.xml b/gnucash/ui/gnc-plugin-page-account-tree-ui.xml index a1165b3203..19708176f8 100644 --- a/gnucash/ui/gnc-plugin-page-account-tree-ui.xml +++ b/gnucash/ui/gnc-plugin-page-account-tree-ui.xml @@ -6,6 +6,7 @@ + @@ -45,6 +46,7 @@ + From 36dc2acd4a6313f599c39a567ef8866892a750dd Mon Sep 17 00:00:00 2001 From: Robert Fewell <14uBobIT@gmail.com> Date: Sun, 15 Jul 2018 16:45:41 +0100 Subject: [PATCH 2/2] Remove some white space and replace tabs with spaces. --- gnucash/gnome-utils/dialog-account.c | 49 ++++++++++--------- .../ui/gnc-plugin-page-account-tree-ui.xml | 42 ++++++++-------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/gnucash/gnome-utils/dialog-account.c b/gnucash/gnome-utils/dialog-account.c index 5eb2cf71f7..d6bbf9a127 100644 --- a/gnucash/gnome-utils/dialog-account.c +++ b/gnucash/gnome-utils/dialog-account.c @@ -1866,7 +1866,7 @@ gnc_ui_edit_account_window(GtkWindow *parent, Account *account) parent_acct = gnc_account_get_parent (account); if (parent_acct == NULL) - parent_acct = account; /* must be at the root */ + parent_acct = account; /* must be at the root */ gtk_tree_view_collapse_all (aw->parent_tree); gnc_tree_view_account_set_selected_account ( @@ -1951,23 +1951,23 @@ gnc_account_renumber_update_examples (RenumberDialog *data) prefix = gtk_editable_get_chars(GTK_EDITABLE(data->prefix), 0, -1); interval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(data->interval)); if (interval <= 0) - interval = 10; + interval = 10; num_digits = (unsigned int)log10((double)(data->num_children * interval)) + 1; if (strlen (prefix)) - str = g_strdup_printf("%s-%0*d", prefix, num_digits, interval); + str = g_strdup_printf("%s-%0*d", prefix, num_digits, interval); else - str = g_strdup_printf("%0*d", num_digits, interval); + str = g_strdup_printf("%0*d", num_digits, interval); gtk_label_set_text(GTK_LABEL(data->example1), str); g_free(str); if (strlen (prefix)) - str = g_strdup_printf("%s-%0*d", prefix, num_digits, - interval * data->num_children); + str = g_strdup_printf("%s-%0*d", prefix, num_digits, + interval * data->num_children); else - str = g_strdup_printf("%0*d", num_digits, - interval * data->num_children); + str = g_strdup_printf("%0*d", num_digits, + interval * data->num_children); gtk_label_set_text(GTK_LABEL(data->example2), str); g_free(str); @@ -2004,27 +2004,28 @@ gnc_account_renumber_response_cb (GtkDialog *dialog, { gtk_widget_hide(data->dialog); children = gnc_account_get_children_sorted(data->parent); - if (children == NULL) - { - PWARN ("Can't renumber children of an account with no children!"); - g_free (data); - return; - } + if (children == NULL) + { + PWARN ("Can't renumber children of an account with no children!"); + g_free (data); + return; + } prefix = gtk_editable_get_chars(GTK_EDITABLE(data->prefix), 0, -1); - interval = - gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(data->interval)); - if (interval <= 0) - interval = 10; + interval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(data->interval)); + + if (interval <= 0) + interval = 10; + num_digits = (unsigned int)log10 ((double)(data->num_children * interval) + 1); gnc_set_busy_cursor (NULL, TRUE); for (tmp = children, i = 1; tmp; tmp = g_list_next(tmp), i += 1) { - if (strlen (prefix)) - str = g_strdup_printf("%s-%0*d", prefix, - num_digits, interval * i); - else - str = g_strdup_printf("%0*d", num_digits, interval * i); + if (strlen (prefix)) + str = g_strdup_printf("%s-%0*d", prefix, + num_digits, interval * i); + else + str = g_strdup_printf("%0*d", num_digits, interval * i); xaccAccountSetCode(tmp->data, str); g_free(str); } @@ -2058,7 +2059,7 @@ gnc_account_renumber_create_dialog (GtkWidget *window, Account *account) data->dialog = GTK_WIDGET(gtk_builder_get_object (builder, "account_renumber_dialog")); gtk_window_set_transient_for(GTK_WINDOW(data->dialog), GTK_WINDOW(window)); g_object_set_data_full(G_OBJECT(data->dialog), "builder", builder, - g_object_unref); + g_object_unref); widget = GTK_WIDGET(gtk_builder_get_object (builder, "header_label")); string = g_strdup_printf(_( "Renumber the immediate sub-accounts of %s? " diff --git a/gnucash/ui/gnc-plugin-page-account-tree-ui.xml b/gnucash/ui/gnc-plugin-page-account-tree-ui.xml index 19708176f8..c07f440531 100644 --- a/gnucash/ui/gnc-plugin-page-account-tree-ui.xml +++ b/gnucash/ui/gnc-plugin-page-account-tree-ui.xml @@ -3,10 +3,10 @@ - + - + @@ -14,29 +14,29 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - - + + + +