From d3140af6a091a6c5dac61b966386f1028c1d6d78 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Wed, 7 Sep 2022 23:42:04 +0800 Subject: [PATCH 1/7] [assistant-stock-transaction] use SPLIT_COL_TOOLIP enum rename SPLIT_COL_MEMO_ESCAPED to SPLIT_COL_TOOLIP --- gnucash/gnome/assistant-stock-transaction.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 705146dd0a..2aeef93b81 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -75,7 +75,7 @@ enum split_cols { SPLIT_COL_ACCOUNT = 0, SPLIT_COL_MEMO, - SPLIT_COL_MEMO_ESCAPED, + SPLIT_COL_TOOLTIP, SPLIT_COL_DEBIT, SPLIT_COL_CREDIT, NUM_SPLIT_COLS @@ -695,7 +695,7 @@ check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, gtk_list_store_set (list, &iter, SPLIT_COL_ACCOUNT, acctstr, SPLIT_COL_MEMO, memostr, - SPLIT_COL_MEMO_ESCAPED, memostr_escaped, + SPLIT_COL_TOOLTIP, memostr_escaped, SPLIT_COL_DEBIT, debit_side ? amtstr : "", SPLIT_COL_CREDIT, !debit_side ? amtstr : "", -1); @@ -1253,7 +1253,7 @@ stock_assistant_create (StockTransactionInfo *info) g_signal_connect (G_OBJECT(info->window), "destroy", G_CALLBACK (stock_assistant_window_destroy_cb), info); gtk_tree_view_set_tooltip_column (GTK_TREE_VIEW (info->finish_split_view), - SPLIT_COL_MEMO_ESCAPED); + SPLIT_COL_TOOLTIP); gtk_assistant_set_forward_page_func (GTK_ASSISTANT(info->window), (GtkAssistantPageFunc)forward_page_func, From 79af372194b0d70506b057507f26e812d68dddff Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Wed, 7 Sep 2022 23:46:58 +0800 Subject: [PATCH 2/7] [assistant-stock-transaction] test splitinfo before check_page call ... This will be useful for adding Units column --- gnucash/gnome/assistant-stock-transaction.cpp | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 2aeef93b81..3b5c29ab7d 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -638,8 +638,6 @@ check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, FieldMask splitfield, Account *acct, GtkWidget *memo, GtkWidget *gae, gnc_commodity *comm, const char* page, StringVec& errors) { - if (splitfield == FieldMask::DISABLED) - return; const char* missing_str = N_("(missing)"); const gchar* amtstr; gnc_numeric amount; @@ -766,26 +764,32 @@ to ensure proper recording."), new_date_str, last_split_date_str); add_error_str (errors, N_("Cannot cover buy more units than owed")); } - check_page (list, debit, credit, info->txn_type->stock_value, info->acct, - info->stock_memo_edit, info->stock_value_edit, info->currency, - NC_ ("Stock Assistant: Page name", "stock value"), errors); + if (info->txn_type->stock_value != FieldMask::DISABLED) + check_page (list, debit, credit, info->txn_type->stock_value, info->acct, + info->stock_memo_edit, info->stock_value_edit, info->currency, + NC_ ("Stock Assistant: Page name", "stock value"), errors); - check_page (list, debit, credit, info->txn_type->cash_value, - gas_account (info->cash_account), info->cash_memo_edit, - info->cash_value, info->currency, - NC_ ("Stock Assistant: Page name", "cash"), errors); + if (info->txn_type->cash_value != FieldMask::DISABLED) + check_page (list, debit, credit, info->txn_type->cash_value, + gas_account (info->cash_account), info->cash_memo_edit, + info->cash_value, info->currency, + NC_ ("Stock Assistant: Page name", "cash"), errors); - auto capitalize_fees = gtk_toggle_button_get_active - (GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox)); - check_page (list, debit, credit, info->txn_type->fees_value, - capitalize_fees ? info->acct : gas_account (info->fees_account), - info->fees_memo_edit, info->fees_value, info->currency, - NC_ ("Stock Assistant: Page name", "fees"), errors); - - check_page (list, debit, credit, info->txn_type->dividend_value, - gas_account (info->dividend_account), - info->dividend_memo_edit, info->dividend_value, info->currency, - NC_ ("Stock Assistant: Page name", "dividend"), errors); + if (info->txn_type->fees_value != FieldMask::DISABLED) + { + auto capitalize_fees = gtk_toggle_button_get_active + (GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox)); + check_page (list, debit, credit, info->txn_type->fees_value, + capitalize_fees ? info->acct : gas_account (info->fees_account), + info->fees_memo_edit, info->fees_value, info->currency, + NC_ ("Stock Assistant: Page name", "fees"), errors); + } + + if (info->txn_type->dividend_value != FieldMask::DISABLED) + check_page (list, debit, credit, info->txn_type->dividend_value, + gas_account (info->dividend_account), + info->dividend_memo_edit, info->dividend_value, info->currency, + NC_ ("Stock Assistant: Page name", "dividend"), errors); // the next two checks will involve the two capgains splits: // income side and stock side. The capgains_value ^ From 391c7b2e757c6a2b5a16c09c36b9868c3498b798 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Wed, 7 Sep 2022 23:48:41 +0800 Subject: [PATCH 3/7] [assistant-stock-transaction] modify GtkListStore outside check_page Pull out add_to_summary_table function to populate the summary_info. Preparation for next commit --- gnucash/gnome/assistant-stock-transaction.cpp | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 3b5c29ab7d..4825c472cf 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -633,6 +633,31 @@ add_error_str (StringVec& errors, const char* str) errors.emplace_back (_(str)); } +struct SummaryLineInfo +{ + bool debit_side; + std::string account; + std::string memo; + std::string value; +}; + +static void +add_to_summary_table (GtkListStore *list, SummaryLineInfo line) +{ + GtkTreeIter iter; + auto tooltip = g_markup_escape_text (line.memo.c_str(), -1); + gtk_list_store_append (list, &iter); + gtk_list_store_set (list, &iter, + SPLIT_COL_ACCOUNT, line.account.c_str(), + SPLIT_COL_MEMO, line.memo.c_str(), + SPLIT_COL_TOOLTIP, tooltip, + SPLIT_COL_DEBIT, line.debit_side ? line.value.c_str() : "", + SPLIT_COL_CREDIT, !line.debit_side ? line.value.c_str() : "", + -1); + g_free (tooltip); +} + + static void check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, FieldMask splitfield, Account *acct, GtkWidget *memo, GtkWidget *gae, @@ -675,7 +700,6 @@ check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, } auto memostr = gtk_entry_get_text (GTK_ENTRY (memo)); - auto memostr_escaped = g_markup_escape_text (memostr, -1); const gchar *acctstr; if (acct) @@ -688,16 +712,8 @@ check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, acctstr = _(missing_str); } - GtkTreeIter iter; - gtk_list_store_append (list, &iter); - gtk_list_store_set (list, &iter, - SPLIT_COL_ACCOUNT, acctstr, - SPLIT_COL_MEMO, memostr, - SPLIT_COL_TOOLTIP, memostr_escaped, - SPLIT_COL_DEBIT, debit_side ? amtstr : "", - SPLIT_COL_CREDIT, !debit_side ? amtstr : "", - -1); - g_free (memostr_escaped); + SummaryLineInfo line = { debit_side, acctstr, memostr, amtstr }; + add_to_summary_table (list, line); } static inline Account* From bd0e9cdae1a5f4b1ebd4f7be50ec4022a63f84fc Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Thu, 8 Sep 2022 09:11:04 +0800 Subject: [PATCH 4/7] [assistant-stock-transaction] add_to_summary_table outside check_page refactor; refresh_page_finish calls add_to_summary_table instead of check_page. --- gnucash/gnome/assistant-stock-transaction.cpp | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 4825c472cf..2ceb5d4e2c 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -659,23 +659,24 @@ add_to_summary_table (GtkListStore *list, SummaryLineInfo line) static void -check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, +check_page (SummaryLineInfo& line, gnc_numeric& debit, gnc_numeric& credit, FieldMask splitfield, Account *acct, GtkWidget *memo, GtkWidget *gae, gnc_commodity *comm, const char* page, StringVec& errors) { const char* missing_str = N_("(missing)"); - const gchar* amtstr; gnc_numeric amount; - bool debit_side = (splitfield & FieldMask::ENABLED_DEBIT); + + line.memo = gtk_entry_get_text (GTK_ENTRY (memo)); + line.debit_side = (splitfield & FieldMask::ENABLED_DEBIT); if (gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (gae), &amount, true, nullptr)) { if (splitfield & FieldMask::ALLOW_ZERO) - amtstr = ""; + line.value = ""; else { add_error (errors, N_("Amount for %s is missing."), page); - amtstr = _(missing_str); + line.value = _(missing_str); } } else @@ -690,30 +691,24 @@ check_page (GtkListStore *list, gnc_numeric& debit, gnc_numeric& credit, if (gnc_numeric_negative_p (amount)) { amount = gnc_numeric_neg (amount); - debit_side = !debit_side; + line.debit_side = !line.debit_side; } - if (debit_side) + if (line.debit_side) debit = gnc_numeric_add_fixed (debit, amount); else credit = gnc_numeric_add_fixed (credit, amount); - amtstr = xaccPrintAmount (amount, gnc_commodity_print_info (comm, true)); + line.value = xaccPrintAmount (amount, gnc_commodity_print_info (comm, true)); } - auto memostr = gtk_entry_get_text (GTK_ENTRY (memo)); - const gchar *acctstr; - if (acct) - acctstr = xaccAccountGetName (acct); + line.account = xaccAccountGetName (acct); else if ((splitfield & FieldMask::ALLOW_ZERO) && gnc_numeric_zero_p (amount)) - acctstr = ""; + line.account = ""; else { add_error (errors, N_("Account for %s is missing"), page); - acctstr = _(missing_str); + line.account = _(missing_str); } - - SummaryLineInfo line = { debit_side, acctstr, memostr, amtstr }; - add_to_summary_table (list, line); } static inline Account* @@ -733,6 +728,7 @@ refresh_page_finish (StockTransactionInfo *info) gnc_numeric debit = gnc_numeric_zero (); gnc_numeric credit = gnc_numeric_zero (); StringVec errors, warnings; + SummaryLineInfo line; // check the stock transaction date. If there are existing stock // transactions dated after the date specified, it is very likely @@ -781,31 +777,41 @@ to ensure proper recording."), new_date_str, last_split_date_str); } if (info->txn_type->stock_value != FieldMask::DISABLED) - check_page (list, debit, credit, info->txn_type->stock_value, info->acct, + { + check_page (line, debit, credit, info->txn_type->stock_value, info->acct, info->stock_memo_edit, info->stock_value_edit, info->currency, NC_ ("Stock Assistant: Page name", "stock value"), errors); + add_to_summary_table (list, line); + } if (info->txn_type->cash_value != FieldMask::DISABLED) - check_page (list, debit, credit, info->txn_type->cash_value, + { + check_page (line, debit, credit, info->txn_type->cash_value, gas_account (info->cash_account), info->cash_memo_edit, info->cash_value, info->currency, NC_ ("Stock Assistant: Page name", "cash"), errors); + add_to_summary_table (list, line); + } if (info->txn_type->fees_value != FieldMask::DISABLED) { auto capitalize_fees = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (info->capitalize_fees_checkbox)); - check_page (list, debit, credit, info->txn_type->fees_value, + check_page (line, debit, credit, info->txn_type->fees_value, capitalize_fees ? info->acct : gas_account (info->fees_account), info->fees_memo_edit, info->fees_value, info->currency, NC_ ("Stock Assistant: Page name", "fees"), errors); + add_to_summary_table (list, line); } if (info->txn_type->dividend_value != FieldMask::DISABLED) - check_page (list, debit, credit, info->txn_type->dividend_value, + { + check_page (line, debit, credit, info->txn_type->dividend_value, gas_account (info->dividend_account), info->dividend_memo_edit, info->dividend_value, info->currency, NC_ ("Stock Assistant: Page name", "dividend"), errors); + add_to_summary_table (list, line); + } // the next two checks will involve the two capgains splits: // income side and stock side. The capgains_value ^ @@ -813,16 +819,18 @@ to ensure proper recording."), new_date_str, last_split_date_str); // flags. if (info->txn_type->capgains_value != FieldMask::DISABLED) { - check_page (list, debit, credit, info->txn_type->capgains_value, + check_page (line, debit, credit, info->txn_type->capgains_value, gas_account (info->capgains_account), info->capgains_memo_edit, info->capgains_value, info->currency, NC_ ("Stock Assistant: Page name", "capital gains"), errors); + add_to_summary_table (list, line); - check_page (list, debit, credit, + check_page (line, debit, credit, info->txn_type->capgains_value ^ (FieldMask::ENABLED_CREDIT | FieldMask::ENABLED_DEBIT), info->acct, info->capgains_memo_edit, info->capgains_value, info->currency, NC_ ("Stock Assistant: Page name", "capital gains"), errors); + add_to_summary_table (list, line); } if (!gnc_numeric_equal (debit, credit)) From c1c82b96a95bf0142ad4976607d909d3d29394d4 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Thu, 8 Sep 2022 09:16:21 +0800 Subject: [PATCH 5/7] [assistant-stock-transaction] add Units column --- gnucash/gnome/assistant-stock-transaction.cpp | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 2ceb5d4e2c..c06f8553bc 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -78,6 +78,7 @@ enum split_cols SPLIT_COL_TOOLTIP, SPLIT_COL_DEBIT, SPLIT_COL_CREDIT, + SPLIT_COL_UNITS, NUM_SPLIT_COLS }; @@ -639,6 +640,7 @@ struct SummaryLineInfo std::string account; std::string memo; std::string value; + std::string units; }; static void @@ -653,6 +655,7 @@ add_to_summary_table (GtkListStore *list, SummaryLineInfo line) SPLIT_COL_TOOLTIP, tooltip, SPLIT_COL_DEBIT, line.debit_side ? line.value.c_str() : "", SPLIT_COL_CREDIT, !line.debit_side ? line.value.c_str() : "", + SPLIT_COL_UNITS, line.units.c_str(), -1); g_free (tooltip); } @@ -667,6 +670,7 @@ check_page (SummaryLineInfo& line, gnc_numeric& debit, gnc_numeric& credit, gnc_numeric amount; line.memo = gtk_entry_get_text (GTK_ENTRY (memo)); + line.units = ""; line.debit_side = (splitfield & FieldMask::ENABLED_DEBIT); if (gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (gae), &amount, true, nullptr)) @@ -759,14 +763,25 @@ to ensure proper recording."), new_date_str, last_split_date_str); } } + if (info->txn_type->stock_value == FieldMask::DISABLED) + line = { false, xaccAccountGetName (info->acct), "", "", "" }; + else + check_page (line, debit, credit, info->txn_type->stock_value, info->acct, + info->stock_memo_edit, info->stock_value_edit, info->currency, + NC_ ("Stock Assistant: Page name", "stock value"), errors); + + if (info->txn_type->stock_amount != FieldMask::DISABLED) { auto stock_amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT(info->stock_amount_edit)); + auto stock_pinfo = gnc_commodity_print_info + (xaccAccountGetCommodity (info->acct), true); if (!gnc_numeric_positive_p (stock_amount)) add_error_str (errors, N_("Stock amount must be positive")); if (info->txn_type->stock_amount & FieldMask::ENABLED_CREDIT) stock_amount = gnc_numeric_neg (stock_amount); + line.units = xaccPrintAmount (stock_amount, stock_pinfo); auto new_bal = gnc_numeric_add_fixed (info->balance_at_date, stock_amount); if (gnc_numeric_positive_p (info->balance_at_date) && gnc_numeric_negative_p (new_bal)) @@ -776,13 +791,7 @@ to ensure proper recording."), new_date_str, last_split_date_str); add_error_str (errors, N_("Cannot cover buy more units than owed")); } - if (info->txn_type->stock_value != FieldMask::DISABLED) - { - check_page (line, debit, credit, info->txn_type->stock_value, info->acct, - info->stock_memo_edit, info->stock_value_edit, info->currency, - NC_ ("Stock Assistant: Page name", "stock value"), errors); - add_to_summary_table (list, line); - } + add_to_summary_table (list, line); if (info->txn_type->cash_value != FieldMask::DISABLED) { @@ -1174,7 +1183,8 @@ get_treeview (GtkBuilder *builder, const gchar *treeview_label) gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(view), gnc_tree_view_get_grid_lines_pref ()); auto store = gtk_list_store_new (NUM_SPLIT_COLS, G_TYPE_STRING, G_TYPE_STRING, - G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, + G_TYPE_STRING); gtk_tree_view_set_model(view, GTK_TREE_MODEL(store)); g_object_unref(store); @@ -1204,6 +1214,13 @@ get_treeview (GtkBuilder *builder, const gchar *treeview_label) (_("Credit"), renderer, "text", SPLIT_COL_CREDIT, nullptr); gtk_tree_view_append_column(view, column); + renderer = gtk_cell_renderer_text_new(); + gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5); + gtk_cell_renderer_set_padding (renderer, 5, 0); + column = gtk_tree_view_column_new_with_attributes + (_("Units"), renderer, "text", SPLIT_COL_UNITS, nullptr); + gtk_tree_view_append_column(view, column); + return GTK_WIDGET (view); } From 6da1ddaec6fd58e4364d357947c64d13505ddf2d Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Thu, 8 Sep 2022 09:20:10 +0800 Subject: [PATCH 6/7] [assistant-stock-transaction] hide zero-fee line in summary table --- gnucash/gnome/assistant-stock-transaction.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index c06f8553bc..6fef3e0b73 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -637,6 +637,7 @@ add_error_str (StringVec& errors, const char* str) struct SummaryLineInfo { bool debit_side; + bool value_is_zero; std::string account; std::string memo; std::string value; @@ -675,6 +676,7 @@ check_page (SummaryLineInfo& line, gnc_numeric& debit, gnc_numeric& credit, if (gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (gae), &amount, true, nullptr)) { + line.value_is_zero = false; if (splitfield & FieldMask::ALLOW_ZERO) line.value = ""; else @@ -702,6 +704,7 @@ check_page (SummaryLineInfo& line, gnc_numeric& debit, gnc_numeric& credit, else credit = gnc_numeric_add_fixed (credit, amount); line.value = xaccPrintAmount (amount, gnc_commodity_print_info (comm, true)); + line.value_is_zero = gnc_numeric_zero_p (amount); } if (acct) @@ -764,7 +767,7 @@ to ensure proper recording."), new_date_str, last_split_date_str); } if (info->txn_type->stock_value == FieldMask::DISABLED) - line = { false, xaccAccountGetName (info->acct), "", "", "" }; + line = { false, false, xaccAccountGetName (info->acct), "", "", "" }; else check_page (line, debit, credit, info->txn_type->stock_value, info->acct, info->stock_memo_edit, info->stock_value_edit, info->currency, @@ -810,7 +813,8 @@ to ensure proper recording."), new_date_str, last_split_date_str); capitalize_fees ? info->acct : gas_account (info->fees_account), info->fees_memo_edit, info->fees_value, info->currency, NC_ ("Stock Assistant: Page name", "fees"), errors); - add_to_summary_table (list, line); + if (!line.value_is_zero) + add_to_summary_table (list, line); } if (info->txn_type->dividend_value != FieldMask::DISABLED) From eab8a218b1b7379c59b91c0f94fc3bb82561cd44 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Fri, 9 Sep 2022 18:01:28 +0800 Subject: [PATCH 7/7] [assistant-stock-transaction] add translator hints --- gnucash/gnome/assistant-stock-transaction.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gnucash/gnome/assistant-stock-transaction.cpp b/gnucash/gnome/assistant-stock-transaction.cpp index 6fef3e0b73..91156a57d8 100644 --- a/gnucash/gnome/assistant-stock-transaction.cpp +++ b/gnucash/gnome/assistant-stock-transaction.cpp @@ -667,6 +667,8 @@ check_page (SummaryLineInfo& line, gnc_numeric& debit, gnc_numeric& credit, FieldMask splitfield, Account *acct, GtkWidget *memo, GtkWidget *gae, gnc_commodity *comm, const char* page, StringVec& errors) { + // Translators: (missing) denotes that the amount or account is + // not provided, or incorrect, in the Stock Transaction Assistant. const char* missing_str = N_("(missing)"); gnc_numeric amount;