Skip to content

Commit

Permalink
Support negative opening balance via "-" button
Browse files Browse the repository at this point in the history
Add a "-" toggle button to the opening amount text field in the account
settings. When engaged, the text turns gNegativeColor and the initial
transaction has a negative amount.

A bit convoluted maybe, but a CurrencyBox doesn't know negative numbers…
  • Loading branch information
humdingerb committed Nov 20, 2024
1 parent 70739d3 commit 9ae438f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/AccountSettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Help.h"
#include "MsgDefs.h"
#include "PrefWindow.h"
#include "Preferences.h"

#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Account"
Expand All @@ -31,6 +32,7 @@
enum {
M_EDIT_ACCOUNT_SETTINGS = 'east',
M_DATA_CHANGED = 'dtch',
M_NEGATIVE_AMOUNT = 'nega',
M_TOGGLE_USE_DEFAULT = 'tgud'
};
// clang-format on
Expand Down Expand Up @@ -59,20 +61,32 @@ AccountSettingsWindow::AccountSettingsWindow(Account* account)
BSize(be_plain_font->StringWidth("QuiteALongAccountName"),
B_SIZE_UNSET));

fOpeningDate = new DateBox("opendate", "", NULL, new BMessage(M_DATA_CHANGED));
fOpeningDate = new DateBox("opendate", NULL, NULL, new BMessage(M_DATA_CHANGED));
CalendarButton* calendarButton = new CalendarButton(fOpeningDate);

fOpeningAmount = new CurrencyBox("openamount", B_TRANSLATE("Opening balance:"), "",
new BMessage(M_DATA_CHANGED));
fOpeningAmount = new CurrencyBox("openamount", NULL, NULL, new BMessage(M_DATA_CHANGED));

fNegativeButton = new BButton("\xcc\xb5", new BMessage(M_NEGATIVE_AMOUNT));
fNegativeButton->SetBehavior(BButton::B_TOGGLE_BEHAVIOR);
fNegativeButton->SetToolTip(B_TRANSLATE("Use a negative opening balance."));
float height;
fOpeningAmount->GetPreferredSize(NULL, &height);
BSize size(height - 2, height);
fNegativeButton->SetExplicitSize(size);

if (foundOpeningTransaction) {
fOpeningDate->SetDate(fOpeningTransaction.Date());
fOpeningDate->Validate();
BString tempstr;
gCurrentLocale.CurrencyToString((fOpeningTransaction.Amount() < 0)
bool negative = fOpeningTransaction.Amount() < 0;
gCurrentLocale.CurrencyToString(negative
? fOpeningTransaction.Amount().InvertAsCopy()
: fOpeningTransaction.Amount(), tempstr);
fOpeningAmount->SetText(tempstr.String());
if (negative) {
fNegativeButton->SetValue(B_CONTROL_ON);
PostMessage(M_NEGATIVE_AMOUNT);
}
}

fUseDefault = new BCheckBox("usedefault", B_TRANSLATE("Use system currency format"),
Expand Down Expand Up @@ -107,17 +121,22 @@ AccountSettingsWindow::AccountSettingsWindow(Account* account)
.Add(calendarButton)
.End();

BView* amountWidget = new BView("amountwidget", B_WILL_DRAW);
BLayoutBuilder::Group<>(amountWidget, B_HORIZONTAL, -2)
.Add(fOpeningAmount)
.Add(fNegativeButton)
.End();

BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_DEFAULT_SPACING)
.AddGrid(B_USE_HALF_ITEM_SPACING)
.AddGrid(B_USE_SMALL_SPACING)
.Add(fAccountName->CreateLabelLayoutItem(), 0, 0)
.Add(fAccountName->CreateTextViewLayoutItem(), 1, 0)
.Add(new BStringView(NULL, B_TRANSLATE("Opening date:")), 0, 1)
.Add(calendarWidget, 1, 1)
.Add(fOpeningAmount->CreateLabelLayoutItem(), 0, 2)
.Add(fOpeningAmount->CreateTextViewLayoutItem(), 1, 2)
.Add(new BStringView(NULL, B_TRANSLATE("Opening balance:")), 0, 2)
.Add(amountWidget, 1, 2)
.End()
.AddStrut(B_USE_HALF_ITEM_SPACING)
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(fUseDefault)
.Add(fPrefView)
Expand Down Expand Up @@ -183,7 +202,8 @@ AccountSettingsWindow::MessageReceived(BMessage* msg)

// Opening balance date and amount not empty, create opening trnsaction.
if (strlen(fOpeningAmount->Text()) > 0 && strlen(fOpeningDate->Text()) > 0) {
fOpeningTransaction.Set(fAccount, fOpeningDate->Text(), "DEP", NULL,
const char* type = fNegativeButton->Value() == B_CONTROL_OFF ? "DEP" : "ATM";
fOpeningTransaction.Set(fAccount, fOpeningDate->Text(), type, NULL,
fOpeningAmount->Text(), B_TRANSLATE_CONTEXT("Opening balance", "CommonTerms"), NULL,
fOpeningTransaction.Status());
try {
Expand Down Expand Up @@ -215,6 +235,13 @@ AccountSettingsWindow::MessageReceived(BMessage* msg)
_UpdateStates();
break;
}
case M_NEGATIVE_AMOUNT:
{
rgb_color color = fNegativeButton->Value() == B_CONTROL_OFF
? ui_color(B_CONTROL_TEXT_COLOR) : gNegativeColor;
fOpeningAmount->TextView()->SetFontAndColor(be_plain_font, B_FONT_ALL, &color);
break;
}
default:
{
BWindow::MessageReceived(msg);
Expand Down
1 change: 1 addition & 0 deletions src/AccountSettingsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AccountSettingsWindow : public BWindow {
AutoTextControl* fAccountName;
DateBox* fOpeningDate;
CurrencyBox* fOpeningAmount;
BButton* fNegativeButton;
BButton* fOK;
CurrencyPrefView* fPrefView;
Account* fAccount;
Expand Down

0 comments on commit 9ae438f

Please sign in to comment.