Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/wizards/newaccountwizard/kaccounttypepage.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | kaccounttypepage.cpp | ||||
3 | ------------------- | ||||
4 | begin : Tue Sep 25 2006 | ||||
5 | copyright : (C) 2007 Thomas Baumgart | ||||
6 | email : Thomas Baumgart <ipwizard@users.sourceforge.net> | ||||
7 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | /*************************************************************************** | ||||
11 | * * | ||||
12 | * This program is free software; you can redistribute it and/or modify * | ||||
13 | * it under the terms of the GNU General Public License as published by * | ||||
14 | * the Free Software Foundation; either version 2 of the License, or * | ||||
15 | * (at your option) any later version. * | ||||
16 | * * | ||||
17 | ***************************************************************************/ | ||||
18 | | ||||
19 | #include "kaccounttypepage.h" | ||||
20 | #include "kaccounttypepage_p.h" | ||||
21 | | ||||
22 | // ---------------------------------------------------------------------------- | ||||
23 | // QT Includes | ||||
24 | | ||||
25 | #include <QDate> | ||||
26 | #include <QDialog> | ||||
27 | #include <QLabel> | ||||
28 | #include <QPointer> | ||||
29 | #include <QPushButton> | ||||
30 | | ||||
31 | // ---------------------------------------------------------------------------- | ||||
32 | // KDE Includes | ||||
33 | | ||||
34 | #include <KLineEdit> | ||||
35 | #include <KLocalizedString> | ||||
36 | #include <KMessageBox> | ||||
37 | | ||||
38 | // ---------------------------------------------------------------------------- | ||||
39 | // Project Includes | ||||
40 | | ||||
41 | #include "ui_kaccounttypepage.h" | ||||
42 | | ||||
43 | #include "kequitypriceupdatedlg.h" | ||||
44 | #include "kmymoneycurrencyselector.h" | ||||
45 | #include "kmymoneydateinput.h" | ||||
46 | #include "kmymoneyedit.h" | ||||
47 | #include "kmymoneygeneralcombo.h" | ||||
48 | #include "kmymoneyglobalsettings.h" | ||||
49 | #include "kmymoneywizardpage.h" | ||||
50 | #include "knewaccountwizard.h" | ||||
51 | #include "knewaccountwizard_p.h" | ||||
52 | #include "kbrokeragepage.h" | ||||
53 | #include "kcreditcardschedulepage.h" | ||||
54 | #include "kgeneralloaninfopage.h" | ||||
55 | #include "khierarchypage.h" | ||||
56 | #include "mymoneyaccount.h" | ||||
57 | #include "mymoneyfile.h" | ||||
58 | #include "mymoneymoney.h" | ||||
59 | #include "mymoneyprice.h" | ||||
60 | #include "mymoneysecurity.h" | ||||
61 | #include "wizardpage.h" | ||||
62 | #include "mymoneyenums.h" | ||||
63 | | ||||
64 | using namespace eMyMoney; | ||||
65 | | ||||
66 | namespace NewAccountWizard | ||||
67 | { | ||||
68 | AccountTypePage::AccountTypePage(Wizard* wizard) : | ||||
69 | QWidget(wizard), | ||||
70 | WizardPage<Wizard>(*new AccountTypePagePrivate(wizard), StepAccount, this, wizard) | ||||
71 | { | ||||
72 | Q_D(AccountTypePage); | ||||
73 | d->ui->setupUi(this); | ||||
74 | d->m_showPriceWarning = true; | ||||
75 | d->ui->m_typeSelection->insertItem(i18n("Checking"), (int)Account::Checkings); | ||||
76 | d->ui->m_typeSelection->insertItem(i18n("Savings"), (int)Account::Savings); | ||||
77 | d->ui->m_typeSelection->insertItem(i18n("Credit Card"), (int)Account::CreditCard); | ||||
78 | d->ui->m_typeSelection->insertItem(i18n("Cash"), (int)Account::Cash); | ||||
79 | d->ui->m_typeSelection->insertItem(i18n("Loan"), (int)Account::Loan); | ||||
80 | d->ui->m_typeSelection->insertItem(i18n("Investment"), (int)Account::Investment); | ||||
81 | d->ui->m_typeSelection->insertItem(i18n("Asset"), (int)Account::Asset); | ||||
82 | d->ui->m_typeSelection->insertItem(i18n("Liability"), (int)Account::Liability); | ||||
83 | if (KMyMoneyGlobalSettings::expertMode()) { | ||||
84 | d->ui->m_typeSelection->insertItem(i18n("Equity"), (int)Account::Equity); | ||||
85 | } | ||||
86 | | ||||
87 | d->ui->m_typeSelection->setCurrentItem((int)Account::Checkings); | ||||
88 | | ||||
89 | d->ui->m_currencyComboBox->setSecurity(MyMoneyFile::instance()->baseCurrency()); | ||||
90 | | ||||
91 | d->m_mandatoryGroup->add(d->ui->m_accountName); | ||||
92 | d->m_mandatoryGroup->add(d->ui->m_conversionRate->lineedit()); | ||||
93 | | ||||
94 | d->ui->m_conversionRate->setValue(MyMoneyMoney::ONE); | ||||
95 | slotUpdateCurrency(); | ||||
96 | | ||||
97 | connect(d->ui->m_typeSelection, &KMyMoneyGeneralCombo::itemSelected, this, &AccountTypePage::slotUpdateType); | ||||
98 | connect(d->ui->m_currencyComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &AccountTypePage::slotUpdateCurrency); | ||||
99 | connect(d->ui->m_conversionRate, &KMyMoneyEdit::textChanged, this, &AccountTypePage::slotUpdateConversionRate); | ||||
100 | connect(d->ui->m_conversionRate, &KMyMoneyEdit::valueChanged, this, &AccountTypePage::slotPriceWarning); | ||||
101 | connect(d->ui->m_onlineQuote, &QAbstractButton::clicked, this, &AccountTypePage::slotGetOnlineQuote); | ||||
102 | } | ||||
103 | | ||||
104 | AccountTypePage::~AccountTypePage() | ||||
105 | { | ||||
106 | } | ||||
107 | | ||||
108 | void AccountTypePage::slotUpdateType(int i) | ||||
109 | { | ||||
110 | Q_D(AccountTypePage); | ||||
111 | hideShowPages(static_cast<Account>(i)); | ||||
112 | d->ui->m_openingBalance->setDisabled(static_cast<Account>(i) == Account::Equity); | ||||
113 | } | ||||
114 | | ||||
115 | void AccountTypePage::hideShowPages(Account accountType) const | ||||
116 | { | ||||
117 | Q_D(const AccountTypePage); | ||||
118 | bool hideSchedulePage = (accountType != Account::CreditCard) | ||||
119 | && (accountType != Account::Loan); | ||||
120 | bool hideLoanPage = (accountType != Account::Loan); | ||||
121 | d->m_wizard->d_func()->setStepHidden(StepDetails, hideLoanPage); | ||||
122 | d->m_wizard->d_func()->setStepHidden(StepPayments, hideLoanPage); | ||||
123 | d->m_wizard->d_func()->setStepHidden(StepFees, hideLoanPage); | ||||
124 | d->m_wizard->d_func()->setStepHidden(StepSchedule, hideSchedulePage); | ||||
125 | d->m_wizard->d_func()->setStepHidden(StepPayout, (accountType != Account::Loan)); | ||||
126 | d->m_wizard->d_func()->setStepHidden(StepBroker, accountType != Account::Investment); | ||||
127 | d->m_wizard->d_func()->setStepHidden(StepParentAccount, accountType == Account::Loan); | ||||
128 | // Force an update of the steps in case the list has changed | ||||
129 | d->m_wizard->reselectStep(); | ||||
130 | } | ||||
131 | | ||||
132 | KMyMoneyWizardPage* AccountTypePage::nextPage() const | ||||
133 | { | ||||
134 | Q_D(const AccountTypePage); | ||||
135 | if (accountType() == Account::Loan) | ||||
136 | return d->m_wizard->d_func()->m_generalLoanInfoPage; | ||||
137 | if (accountType() == Account::CreditCard) | ||||
138 | return d->m_wizard->d_func()->m_schedulePage; | ||||
139 | if (accountType() == Account::Investment) | ||||
140 | return d->m_wizard->d_func()->m_brokeragepage; | ||||
141 | return d->m_wizard->d_func()->m_hierarchyPage; | ||||
142 | } | ||||
143 | | ||||
144 | QWidget* AccountTypePage::initialFocusWidget(void) const | ||||
145 | { | ||||
146 | Q_D(const AccountTypePage); | ||||
147 | return d->ui->m_accountName; | ||||
148 | } | ||||
149 | | ||||
150 | void AccountTypePage::slotUpdateCurrency() | ||||
151 | { | ||||
152 | Q_D(AccountTypePage); | ||||
153 | MyMoneyAccount acc; | ||||
154 | acc.setAccountType(accountType()); | ||||
155 | | ||||
156 | d->ui->m_openingBalance->setPrecision(MyMoneyMoney::denomToPrec(acc.fraction(currency()))); | ||||
157 | | ||||
158 | bool show = d->ui->m_currencyComboBox->security().id() != MyMoneyFile::instance()->baseCurrency().id(); | ||||
159 | d->ui->m_conversionLabel->setVisible(show); | ||||
160 | d->ui->m_conversionRate->setVisible(show); | ||||
161 | d->ui->m_conversionExample->setVisible(show); | ||||
162 | d->ui->m_onlineQuote->setVisible(show); | ||||
163 | d->ui->m_conversionRate->setEnabled(show); // make sure to include/exclude in mandatoryGroup | ||||
164 | d->ui->m_conversionRate->setPrecision(d->ui->m_currencyComboBox->security().pricePrecision()); | ||||
165 | d->m_mandatoryGroup->changed(); | ||||
166 | slotUpdateConversionRate(d->ui->m_conversionRate->lineedit()->text()); | ||||
167 | } | ||||
168 | | ||||
169 | void AccountTypePage::slotGetOnlineQuote() | ||||
170 | { | ||||
171 | Q_D(AccountTypePage); | ||||
172 | QString id = MyMoneyFile::instance()->baseCurrency().id() + ' ' + d->ui->m_currencyComboBox->security().id(); | ||||
173 | QPointer<KEquityPriceUpdateDlg> dlg = new KEquityPriceUpdateDlg(this, id); | ||||
174 | if (dlg->exec() == QDialog::Accepted) { | ||||
175 | const MyMoneyPrice &price = dlg->price(id); | ||||
176 | if (price.isValid()) { | ||||
177 | d->ui->m_conversionRate->setValue(price.rate(d->ui->m_currencyComboBox->security().id())); | ||||
178 | if (price.date() != d->ui->m_openingDate->date()) { | ||||
179 | priceWarning(true); | ||||
180 | } | ||||
181 | } | ||||
182 | } | ||||
183 | delete dlg; | ||||
184 | } | ||||
185 | | ||||
186 | void AccountTypePage::slotPriceWarning() | ||||
187 | { | ||||
188 | priceWarning(false); | ||||
189 | } | ||||
190 | | ||||
191 | void AccountTypePage::priceWarning(bool always) | ||||
192 | { | ||||
193 | Q_D(AccountTypePage); | ||||
194 | if (d->m_showPriceWarning || always) { | ||||
195 | KMessageBox::information(this, i18n("Please make sure to enter the correct conversion for the selected opening date. If you requested an online quote it might be provided for a different date."), i18n("Check date")); | ||||
196 | } | ||||
197 | d->m_showPriceWarning = false; | ||||
198 | } | ||||
199 | | ||||
200 | void AccountTypePage::slotUpdateConversionRate(const QString& txt) | ||||
201 | { | ||||
202 | Q_D(AccountTypePage); | ||||
203 | d->ui->m_conversionExample->setText(i18n("1 %1 equals %2", MyMoneyFile::instance()->baseCurrency().tradingSymbol(), MyMoneyMoney(txt).formatMoney(d->ui->m_currencyComboBox->security().tradingSymbol(), d->ui->m_currencyComboBox->security().pricePrecision()))); | ||||
204 | } | ||||
205 | | ||||
206 | bool AccountTypePage::isComplete() const | ||||
207 | { | ||||
208 | Q_D(const AccountTypePage); | ||||
209 | // check that the conversion rate is positive if enabled | ||||
210 | bool rc = !d->ui->m_conversionRate->isVisible() || (!d->ui->m_conversionRate->value().isZero() && !d->ui->m_conversionRate->value().isNegative()); | ||||
211 | if (!rc) { | ||||
212 | d->m_wizard->d_func()->m_nextButton->setToolTip(i18n("Conversion rate is not positive")); | ||||
213 | | ||||
214 | } else { | ||||
215 | rc = KMyMoneyWizardPage::isComplete(); | ||||
216 | | ||||
217 | if (!rc) { | ||||
218 | d->m_wizard->d_func()->m_nextButton->setToolTip(i18n("No account name supplied")); | ||||
219 | } | ||||
220 | } | ||||
221 | hideShowPages(accountType()); | ||||
222 | return rc; | ||||
223 | } | ||||
224 | | ||||
225 | Account AccountTypePage::accountType() const | ||||
226 | { | ||||
227 | Q_D(const AccountTypePage); | ||||
228 | return static_cast<Account>(d->ui->m_typeSelection->currentItem()); | ||||
229 | } | ||||
230 | | ||||
231 | const MyMoneySecurity& AccountTypePage::currency() const | ||||
232 | { | ||||
233 | Q_D(const AccountTypePage); | ||||
234 | return d->ui->m_currencyComboBox->security(); | ||||
235 | } | ||||
236 | | ||||
237 | void AccountTypePage::setAccount(const MyMoneyAccount& acc) | ||||
238 | { | ||||
239 | Q_D(const AccountTypePage); | ||||
240 | if (acc.accountType() != Account::Unknown) { | ||||
241 | if (acc.accountType() == Account::AssetLoan) { | ||||
242 | d->ui->m_typeSelection->setCurrentItem((int)Account::Loan); | ||||
243 | } else { | ||||
244 | d->ui->m_typeSelection->setCurrentItem((int)acc.accountType()); | ||||
245 | } | ||||
246 | } | ||||
247 | d->ui->m_openingDate->setDate(acc.openingDate()); | ||||
248 | d->ui->m_accountName->setText(acc.name()); | ||||
249 | } | ||||
250 | | ||||
251 | const MyMoneyAccount& AccountTypePage::parentAccount() | ||||
252 | { | ||||
253 | switch (accountType()) { | ||||
254 | case Account::CreditCard: | ||||
255 | case Account::Liability: | ||||
256 | case Account::Loan: // Can be either but we return liability here | ||||
257 | return MyMoneyFile::instance()->liability(); | ||||
258 | break; | ||||
259 | case Account::Equity: | ||||
260 | return MyMoneyFile::instance()->equity(); | ||||
261 | default: | ||||
262 | break; | ||||
263 | } | ||||
264 | return MyMoneyFile::instance()->asset(); | ||||
265 | } | ||||
266 | | ||||
267 | bool AccountTypePage::allowsParentAccount() const | ||||
268 | { | ||||
269 | return accountType() != Account::Loan; | ||||
270 | } | ||||
271 | } |