aboutsummaryrefslogtreecommitdiff
path: root/settingsdialog.cpp
blob: 52cf791e9568d675e0636f13c667231ab68c3a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QFontDialog>

SettingsDialog::SettingsDialog(Database *db, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SettingsDialog)
    , database(db)
    , currentAmountFont("Courier New", 10)
    , weekStartDay(1)
{
    ui->setupUi(this);
    
    connect(ui->amountFontBtn, &QPushButton::clicked, this, &SettingsDialog::onChooseAmountFont);
    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &SettingsDialog::saveSettings);
    
    loadSettings();
}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::loadSettings()
{
    // Load settings from database
    QString currency = database->getSetting("currency_symbol", "$");
    QString fontFamily = database->getSetting("amount_font", "Courier New");
    int fontSize = database->getSetting("amount_font_size", "10").toInt();
    int defaultPeriod = database->getSetting("default_period", "2").toInt();
    bool showAccountBalances = database->getSetting("show_account_balances", "0").toInt();
    weekStartDay = database->getSetting("week_start_day", "1").toInt();
    int projectionMonths = database->getSetting("projection_months", "3").toInt();
    
    // Set UI
    ui->currencyEdit->setText(currency);
    currentAmountFont = QFont(fontFamily, fontSize);
    ui->amountFontBtn->setText(QString("%1, %2pt").arg(fontFamily).arg(fontSize));
    ui->defaultPeriodCombo->setCurrentIndex(defaultPeriod);
    ui->defaultShowAccountBalancesCheck->setChecked(showAccountBalances);
    ui->weekStartDayCombo->setCurrentIndex(weekStartDay - 1);
    ui->projectionMonthsSpin->setValue(projectionMonths);
}

void SettingsDialog::saveSettings()
{
    // Save to database
    database->setSetting("currency_symbol", ui->currencyEdit->text());
    database->setSetting("amount_font", currentAmountFont.family());
    database->setSetting("amount_font_size", QString::number(currentAmountFont.pointSize()));
    database->setSetting("default_period", QString::number(ui->defaultPeriodCombo->currentIndex()));
    database->setSetting("show_account_balances", QString::number(ui->defaultShowAccountBalancesCheck->isChecked() ? 1 : 0));
    database->setSetting("week_start_day", QString::number(ui->weekStartDayCombo->currentIndex() + 1));
    database->setSetting("projection_months", QString::number(ui->projectionMonthsSpin->value()));
    
    weekStartDay = ui->weekStartDayCombo->currentIndex() + 1;
}

void SettingsDialog::onChooseAmountFont()
{
    bool ok;
    QFont selectedFont = QFontDialog::getFont(&ok, currentAmountFont, this, "Choose Amount Font", QFontDialog::MonospacedFonts);
    
    if (ok) {
        currentAmountFont = selectedFont;
        ui->amountFontBtn->setText(QString("%1, %2pt").arg(selectedFont.family()).arg(selectedFont.pointSize()));
    }
}