Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/dialogs/kcurrencycalculator.cpp
Context not available. | |||||
9 | John C <thetacoturtle@users.sourceforge.net> | 9 | John C <thetacoturtle@users.sourceforge.net> | ||
---|---|---|---|---|---|
10 | Thomas Baumgart <ipwizard@users.sourceforge.net> | 10 | Thomas Baumgart <ipwizard@users.sourceforge.net> | ||
11 | Kevin Tambascio <ktambascio@users.sourceforge.net> | 11 | Kevin Tambascio <ktambascio@users.sourceforge.net> | ||
12 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | ||||
12 | ***************************************************************************/ | 13 | ***************************************************************************/ | ||
13 | 14 | | |||
14 | /*************************************************************************** | 15 | /*************************************************************************** | ||
Context not available. | |||||
31 | #include <QCheckBox> | 32 | #include <QCheckBox> | ||
32 | #include <QPushButton> | 33 | #include <QPushButton> | ||
33 | #include <QDialogButtonBox> | 34 | #include <QDialogButtonBox> | ||
34 | #include <QVBoxLayout> | | |||
35 | 35 | | |||
36 | // ---------------------------------------------------------------------------- | 36 | // ---------------------------------------------------------------------------- | ||
37 | // KDE Includes | 37 | // KDE Includes | ||
Context not available. | |||||
41 | // ---------------------------------------------------------------------------- | 41 | // ---------------------------------------------------------------------------- | ||
42 | // Project Includes | 42 | // Project Includes | ||
43 | 43 | | |||
44 | #include "ui_kcurrencycalculator.h" | ||||
45 | | ||||
44 | #include "mymoneyfile.h" | 46 | #include "mymoneyfile.h" | ||
45 | #include "mymoneyaccount.h" | 47 | #include "mymoneyaccount.h" | ||
46 | #include "mymoneysecurity.h" | 48 | #include "mymoneysecurity.h" | ||
Context not available. | |||||
52 | #include "mymoneytransaction.h" | 54 | #include "mymoneytransaction.h" | ||
53 | #include "kmymoneyglobalsettings.h" | 55 | #include "kmymoneyglobalsettings.h" | ||
54 | 56 | | |||
55 | #include "kmymoneyutils.h" | 57 | class KCurrencyCalculatorPrivate | ||
58 | { | ||||
59 | Q_DISABLE_COPY(KCurrencyCalculatorPrivate) | ||||
60 | Q_DECLARE_PUBLIC(KCurrencyCalculator) | ||||
61 | | ||||
62 | public: | ||||
63 | KCurrencyCalculatorPrivate(KCurrencyCalculator *qq, | ||||
64 | const MyMoneySecurity& from, | ||||
65 | const MyMoneySecurity& to, | ||||
66 | const MyMoneyMoney& value, | ||||
67 | const MyMoneyMoney& shares, | ||||
68 | const QDate& date, | ||||
69 | const signed64 resultFraction) : | ||||
70 | q_ptr(qq), | ||||
71 | ui(new Ui::KCurrencyCalculator), | ||||
72 | m_fromCurrency(from), | ||||
73 | m_toCurrency(to), | ||||
74 | m_result(shares.abs()), | ||||
75 | m_value(value.abs()), | ||||
76 | m_date(date), | ||||
77 | m_resultFraction(resultFraction) | ||||
78 | { | ||||
79 | } | ||||
80 | | ||||
81 | ~KCurrencyCalculatorPrivate() | ||||
82 | { | ||||
83 | delete ui; | ||||
84 | } | ||||
85 | | ||||
86 | void init() | ||||
87 | { | ||||
88 | Q_Q(KCurrencyCalculator); | ||||
89 | ui->setupUi(q); | ||||
90 | auto file = MyMoneyFile::instance(); | ||||
91 | | ||||
92 | //set main widget of QDialog | ||||
93 | ui->buttonGroup1->setId(ui->m_amountButton, 0); | ||||
94 | ui->buttonGroup1->setId(ui->m_rateButton, 1); | ||||
95 | | ||||
96 | ui->m_dateFrame->hide(); | ||||
97 | if (m_date.isValid()) | ||||
98 | ui->m_dateEdit->setDate(m_date); | ||||
99 | else | ||||
100 | ui->m_dateEdit->setDate(QDate::currentDate()); | ||||
101 | | ||||
102 | ui->m_fromCurrencyText->setText(QString(MyMoneySecurity::securityTypeToString(m_fromCurrency.securityType()) + ' ' + (m_fromCurrency.isCurrency() ? m_fromCurrency.id() : m_fromCurrency.tradingSymbol()))); | ||||
103 | ui->m_toCurrencyText->setText(QString(MyMoneySecurity::securityTypeToString(m_toCurrency.securityType()) + ' ' + (m_toCurrency.isCurrency() ? m_toCurrency.id() : m_toCurrency.tradingSymbol()))); | ||||
104 | | ||||
105 | //set bold font | ||||
106 | auto boldFont = ui->m_fromCurrencyText->font(); | ||||
107 | boldFont.setBold(true); | ||||
108 | ui->m_fromCurrencyText->setFont(boldFont); | ||||
109 | boldFont = ui->m_toCurrencyText->font(); | ||||
110 | boldFont.setBold(true); | ||||
111 | ui->m_toCurrencyText->setFont(boldFont); | ||||
112 | | ||||
113 | ui->m_fromAmount->setText(m_value.formatMoney(QString(), MyMoneyMoney::denomToPrec(m_fromCurrency.smallestAccountFraction()))); | ||||
114 | | ||||
115 | ui->m_dateText->setText(QLocale().toString(m_date)); | ||||
116 | | ||||
117 | ui->m_updateButton->setChecked(KMyMoneyGlobalSettings::priceHistoryUpdate()); | ||||
118 | | ||||
119 | // setup initial result | ||||
120 | if (m_result == MyMoneyMoney() && !m_value.isZero()) { | ||||
121 | const MyMoneyPrice &pr = file->price(m_fromCurrency.id(), m_toCurrency.id(), m_date); | ||||
122 | if (pr.isValid()) { | ||||
123 | m_result = m_value * pr.rate(m_toCurrency.id()); | ||||
124 | } | ||||
125 | } | ||||
126 | | ||||
127 | // fill in initial values | ||||
128 | ui->m_toAmount->loadText(m_result.formatMoney(QString(), MyMoneyMoney::denomToPrec(m_resultFraction))); | ||||
129 | ui->m_toAmount->setPrecision(MyMoneyMoney::denomToPrec(m_resultFraction)); | ||||
130 | | ||||
131 | ui->m_conversionRate->setPrecision(m_fromCurrency.pricePrecision()); | ||||
132 | | ||||
133 | q->connect(ui->m_amountButton, &QAbstractButton::clicked, q, &KCurrencyCalculator::slotSetToAmount); | ||||
134 | q->connect(ui->m_rateButton, &QAbstractButton::clicked, q, &KCurrencyCalculator::slotSetExchangeRate); | ||||
135 | | ||||
136 | q->connect(ui->m_toAmount, &kMyMoneyEdit::valueChanged, q, &KCurrencyCalculator::slotUpdateResult); | ||||
137 | q->connect(ui->m_conversionRate, &kMyMoneyEdit::valueChanged, q, &KCurrencyCalculator::slotUpdateRate); | ||||
56 | 138 | | |||
57 | bool KCurrencyCalculator::setupSplitPrice(MyMoneyMoney& shares, const MyMoneyTransaction& t, const MyMoneySplit& s, const QMap<QString, MyMoneyMoney>& priceInfo, QWidget* parentWidget) | 139 | // use this as the default | ||
140 | ui->m_amountButton->animateClick(); | ||||
141 | q->slotUpdateResult(ui->m_toAmount->text()); | ||||
142 | | ||||
143 | // If the from security is not a currency, we only allow entering a price | ||||
144 | if (!m_fromCurrency.isCurrency()) { | ||||
145 | ui->m_rateButton->animateClick(); | ||||
146 | ui->m_amountButton->hide(); | ||||
147 | ui->m_toAmount->hide(); | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | void updateExample(const MyMoneyMoney& price) | ||||
152 | { | ||||
153 | QString msg; | ||||
154 | if (price.isZero()) { | ||||
155 | msg = QString("1 %1 = ? %2").arg(m_fromCurrency.tradingSymbol()) | ||||
156 | .arg(m_toCurrency.tradingSymbol()); | ||||
157 | if (m_fromCurrency.isCurrency()) { | ||||
158 | msg += QString("\n"); | ||||
159 | msg += QString("1 %1 = ? %2").arg(m_toCurrency.tradingSymbol()) | ||||
160 | .arg(m_fromCurrency.tradingSymbol()); | ||||
161 | } | ||||
162 | } else { | ||||
163 | msg = QString("1 %1 = %2 %3").arg(m_fromCurrency.tradingSymbol()) | ||||
164 | .arg(price.formatMoney(QString(), m_fromCurrency.pricePrecision())) | ||||
165 | .arg(m_toCurrency.tradingSymbol()); | ||||
166 | if (m_fromCurrency.isCurrency()) { | ||||
167 | msg += QString("\n"); | ||||
168 | msg += QString("1 %1 = %2 %3").arg(m_toCurrency.tradingSymbol()) | ||||
169 | .arg((MyMoneyMoney::ONE / price).formatMoney(QString(), m_toCurrency.pricePrecision())) | ||||
170 | .arg(m_fromCurrency.tradingSymbol()); | ||||
171 | } | ||||
172 | } | ||||
173 | ui->m_conversionExample->setText(msg); | ||||
174 | ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!price.isZero()); | ||||
175 | } | ||||
176 | | ||||
177 | KCurrencyCalculator *q_ptr; | ||||
178 | Ui::KCurrencyCalculator *ui; | ||||
179 | MyMoneySecurity m_fromCurrency; | ||||
180 | MyMoneySecurity m_toCurrency; | ||||
181 | MyMoneyMoney m_result; | ||||
182 | MyMoneyMoney m_value; | ||||
183 | QDate m_date; | ||||
184 | signed64 m_resultFraction; | ||||
185 | }; | ||||
186 | | ||||
187 | KCurrencyCalculator::KCurrencyCalculator(const MyMoneySecurity& from, | ||||
188 | const MyMoneySecurity& to, | ||||
189 | const MyMoneyMoney& value, | ||||
190 | const MyMoneyMoney& shares, | ||||
191 | const QDate& date, | ||||
192 | const signed64 resultFraction, | ||||
193 | QWidget *parent) : | ||||
194 | QDialog(parent), | ||||
195 | d_ptr(new KCurrencyCalculatorPrivate(this, | ||||
196 | from, | ||||
197 | to, | ||||
198 | value, | ||||
199 | shares, | ||||
200 | date, | ||||
201 | resultFraction)) | ||||
58 | { | 202 | { | ||
59 | bool rc = true; | 203 | Q_D(KCurrencyCalculator); | ||
60 | MyMoneyFile* file = MyMoneyFile::instance(); | 204 | d->init(); | ||
205 | } | ||||
206 | | ||||
207 | KCurrencyCalculator::~KCurrencyCalculator() | ||||
208 | { | ||||
209 | Q_D(KCurrencyCalculator); | ||||
210 | delete d; | ||||
211 | } | ||||
212 | | ||||
213 | bool KCurrencyCalculator::setupSplitPrice(MyMoneyMoney& shares, | ||||
214 | const MyMoneyTransaction& t, | ||||
215 | const MyMoneySplit& s, | ||||
216 | const QMap<QString, | ||||
217 | MyMoneyMoney>& priceInfo, | ||||
218 | QWidget* parentWidget) | ||||
219 | { | ||||
220 | auto rc = true; | ||||
221 | auto file = MyMoneyFile::instance(); | ||||
61 | 222 | | |||
62 | if (!s.value().isZero()) { | 223 | if (!s.value().isZero()) { | ||
63 | MyMoneyAccount cat = file->account(s.accountId()); | 224 | auto cat = file->account(s.accountId()); | ||
64 | MyMoneySecurity toCurrency; | 225 | MyMoneySecurity toCurrency; | ||
65 | toCurrency = file->security(cat.currencyId()); | 226 | toCurrency = file->security(cat.currencyId()); | ||
66 | // determine the fraction required for this category/account | 227 | // determine the fraction required for this category/account | ||
Context not available. | |||||
68 | 229 | | |||
69 | if (cat.currencyId() != t.commodity()) { | 230 | if (cat.currencyId() != t.commodity()) { | ||
70 | 231 | | |||
71 | MyMoneySecurity fromCurrency; | 232 | MyMoneyMoney toValue; | ||
72 | MyMoneyMoney fromValue, toValue; | 233 | auto fromCurrency = file->security(t.commodity()); | ||
73 | fromCurrency = file->security(t.commodity()); | | |||
74 | // display only positive values to the user | 234 | // display only positive values to the user | ||
75 | fromValue = s.value().abs(); | 235 | auto fromValue = s.value().abs(); | ||
76 | 236 | | |||
77 | // if we had a price info in the beginning, we use it here | 237 | // if we had a price info in the beginning, we use it here | ||
78 | if (priceInfo.find(cat.currencyId()) != priceInfo.end()) { | 238 | if (priceInfo.find(cat.currencyId()) != priceInfo.end()) { | ||
Context not available. | |||||
116 | return rc; | 276 | return rc; | ||
117 | } | 277 | } | ||
118 | 278 | | |||
119 | KCurrencyCalculator::KCurrencyCalculator(const MyMoneySecurity& from, const MyMoneySecurity& to, const MyMoneyMoney& value, const MyMoneyMoney& shares, const QDate& date, const signed64 resultFraction, QWidget *parent) : | | |||
120 | KCurrencyCalculatorDecl(parent), | | |||
121 | m_fromCurrency(from), | | |||
122 | m_toCurrency(to), | | |||
123 | m_result(shares.abs()), | | |||
124 | m_value(value.abs()), | | |||
125 | m_resultFraction(resultFraction), | | |||
126 | m_buttonBox(0) | | |||
127 | { | | |||
128 | MyMoneyFile* file = MyMoneyFile::instance(); | | |||
129 | | ||||
130 | //set main widget of QDialog | | |||
131 | QVBoxLayout *mainLayout = new QVBoxLayout; | | |||
132 | setLayout(mainLayout); | | |||
133 | mainLayout->addWidget(m_layoutWidget); | | |||
134 | | ||||
135 | m_buttonBox = new QDialogButtonBox(this); | | |||
136 | m_buttonBox->setStandardButtons(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); | | |||
137 | QPushButton *okButton = m_buttonBox->button(QDialogButtonBox::Ok); | | |||
138 | okButton->setDefault(true); | | |||
139 | /// @todo remove Ctrl-Enter behavior in future release | | |||
140 | okButton->setShortcut(Qt::CTRL | Qt::Key_Return); | | |||
141 | connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept())); | | |||
142 | connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject())); | | |||
143 | mainLayout->addWidget(m_buttonBox); | | |||
144 | | ||||
145 | buttonGroup1->setId(m_amountButton, 0); | | |||
146 | buttonGroup1->setId(m_rateButton, 1); | | |||
147 | | ||||
148 | m_dateFrame->hide(); | | |||
149 | if (date.isValid()) | | |||
150 | m_dateEdit->setDate(date); | | |||
151 | else | | |||
152 | m_dateEdit->setDate(QDate::currentDate()); | | |||
153 | | ||||
154 | m_fromCurrencyText->setText(QString(MyMoneySecurity::securityTypeToString(m_fromCurrency.securityType()) + ' ' + (m_fromCurrency.isCurrency() ? m_fromCurrency.id() : m_fromCurrency.tradingSymbol()))); | | |||
155 | m_toCurrencyText->setText(QString(MyMoneySecurity::securityTypeToString(m_toCurrency.securityType()) + ' ' + (m_toCurrency.isCurrency() ? m_toCurrency.id() : m_toCurrency.tradingSymbol()))); | | |||
156 | | ||||
157 | //set bold font | | |||
158 | QFont boldFont = m_fromCurrencyText->font(); | | |||
159 | boldFont.setBold(true); | | |||
160 | m_fromCurrencyText->setFont(boldFont); | | |||
161 | boldFont = m_toCurrencyText->font(); | | |||
162 | boldFont.setBold(true); | | |||
163 | m_toCurrencyText->setFont(boldFont); | | |||
164 | | ||||
165 | m_fromAmount->setText(m_value.formatMoney(QString(), MyMoneyMoney::denomToPrec(m_fromCurrency.smallestAccountFraction()))); | | |||
166 | | ||||
167 | m_dateText->setText(QLocale().toString(date)); | | |||
168 | | ||||
169 | m_updateButton->setChecked(KMyMoneyGlobalSettings::priceHistoryUpdate()); | | |||
170 | | ||||
171 | // setup initial result | | |||
172 | if (m_result == MyMoneyMoney() && !m_value.isZero()) { | | |||
173 | const MyMoneyPrice &pr = file->price(m_fromCurrency.id(), m_toCurrency.id(), date); | | |||
174 | if (pr.isValid()) { | | |||
175 | m_result = m_value * pr.rate(m_toCurrency.id()); | | |||
176 | } | | |||
177 | } | | |||
178 | | ||||
179 | // fill in initial values | | |||
180 | m_toAmount->loadText(m_result.formatMoney(QString(), MyMoneyMoney::denomToPrec(m_resultFraction))); | | |||
181 | m_toAmount->setPrecision(MyMoneyMoney::denomToPrec(m_resultFraction)); | | |||
182 | | ||||
183 | m_conversionRate->setPrecision(m_fromCurrency.pricePrecision()); | | |||
184 | | ||||
185 | connect(m_amountButton, SIGNAL(clicked()), this, SLOT(slotSetToAmount())); | | |||
186 | connect(m_rateButton, SIGNAL(clicked()), this, SLOT(slotSetExchangeRate())); | | |||
187 | | ||||
188 | connect(m_toAmount, SIGNAL(valueChanged(QString)), this, SLOT(slotUpdateResult(QString))); | | |||
189 | connect(m_conversionRate, SIGNAL(valueChanged(QString)), this, SLOT(slotUpdateRate(QString))); | | |||
190 | | ||||
191 | // use this as the default | | |||
192 | m_amountButton->animateClick(); | | |||
193 | slotUpdateResult(m_toAmount->text()); | | |||
194 | | ||||
195 | // If the from security is not a currency, we only allow entering a price | | |||
196 | if (!m_fromCurrency.isCurrency()) { | | |||
197 | m_rateButton->animateClick(); | | |||
198 | m_amountButton->hide(); | | |||
199 | m_toAmount->hide(); | | |||
200 | } | | |||
201 | okButton->setFocus(); | | |||
202 | } | | |||
203 | | ||||
204 | KCurrencyCalculator::~KCurrencyCalculator() | | |||
205 | { | | |||
206 | } | | |||
207 | | ||||
208 | void KCurrencyCalculator::setupPriceEditor() | 279 | void KCurrencyCalculator::setupPriceEditor() | ||
209 | { | 280 | { | ||
210 | m_dateFrame->show(); | 281 | Q_D(KCurrencyCalculator); | ||
211 | m_amountDateFrame->hide(); | 282 | d->ui->m_dateFrame->show(); | ||
212 | m_updateButton->setChecked(true); | 283 | d->ui->m_amountDateFrame->hide(); | ||
213 | m_updateButton->hide(); | 284 | d->ui->m_updateButton->setChecked(true); | ||
285 | d->ui->m_updateButton->hide(); | ||||
214 | } | 286 | } | ||
215 | 287 | | |||
216 | void KCurrencyCalculator::slotSetToAmount() | 288 | void KCurrencyCalculator::slotSetToAmount() | ||
217 | { | 289 | { | ||
218 | m_rateButton->setChecked(false); | 290 | Q_D(KCurrencyCalculator); | ||
219 | m_toAmount->setEnabled(true); | 291 | d->ui->m_rateButton->setChecked(false); | ||
220 | m_conversionRate->setEnabled(false); | 292 | d->ui->m_toAmount->setEnabled(true); | ||
293 | d->ui->m_conversionRate->setEnabled(false); | ||||
221 | } | 294 | } | ||
222 | 295 | | |||
223 | void KCurrencyCalculator::slotSetExchangeRate() | 296 | void KCurrencyCalculator::slotSetExchangeRate() | ||
224 | { | 297 | { | ||
225 | m_amountButton->setChecked(false); | 298 | Q_D(KCurrencyCalculator); | ||
226 | m_toAmount->setEnabled(false); | 299 | d->ui->m_amountButton->setChecked(false); | ||
227 | m_conversionRate->setEnabled(true); | 300 | d->ui->m_toAmount->setEnabled(false); | ||
301 | d->ui->m_conversionRate->setEnabled(true); | ||||
228 | } | 302 | } | ||
229 | 303 | | |||
230 | void KCurrencyCalculator::slotUpdateResult(const QString& /*txt*/) | 304 | void KCurrencyCalculator::slotUpdateResult(const QString& /*txt*/) | ||
231 | { | 305 | { | ||
232 | MyMoneyMoney result = m_toAmount->value(); | 306 | Q_D(KCurrencyCalculator); | ||
307 | MyMoneyMoney result = d->ui->m_toAmount->value(); | ||||
233 | MyMoneyMoney price(0, 1); | 308 | MyMoneyMoney price(0, 1); | ||
234 | 309 | | |||
235 | if (result.isNegative()) { | 310 | if (result.isNegative()) { | ||
236 | m_toAmount->setValue(-result); | 311 | d->ui->m_toAmount->setValue(-result); | ||
237 | slotUpdateResult(QString()); | 312 | slotUpdateResult(QString()); | ||
238 | return; | 313 | return; | ||
239 | } | 314 | } | ||
240 | 315 | | |||
241 | if (!result.isZero()) { | 316 | if (!result.isZero()) { | ||
242 | price = result / m_value; | 317 | price = result / d->m_value; | ||
243 | 318 | | |||
244 | m_conversionRate->loadText(price.formatMoney(QString(), m_fromCurrency.pricePrecision())); | 319 | d->ui->m_conversionRate->loadText(price.formatMoney(QString(), d->m_fromCurrency.pricePrecision())); | ||
245 | m_result = (m_value * price).convert(m_resultFraction); | 320 | d->m_result = (d->m_value * price).convert(d->m_resultFraction); | ||
246 | m_toAmount->loadText(m_result.formatMoney(m_resultFraction)); | 321 | d->ui->m_toAmount->loadText(d->m_result.formatMoney(d->m_resultFraction)); | ||
247 | } | 322 | } | ||
248 | updateExample(price); | 323 | d->updateExample(price); | ||
249 | } | 324 | } | ||
250 | 325 | | |||
251 | void KCurrencyCalculator::slotUpdateRate(const QString& /*txt*/) | 326 | void KCurrencyCalculator::slotUpdateRate(const QString& /*txt*/) | ||
252 | { | 327 | { | ||
253 | MyMoneyMoney price = m_conversionRate->value(); | 328 | Q_D(KCurrencyCalculator); | ||
329 | auto price = d->ui->m_conversionRate->value(); | ||||
254 | 330 | | |||
255 | if (price.isNegative()) { | 331 | if (price.isNegative()) { | ||
256 | m_conversionRate->setValue(-price); | 332 | d->ui->m_conversionRate->setValue(-price); | ||
257 | slotUpdateRate(QString()); | 333 | slotUpdateRate(QString()); | ||
258 | return; | 334 | return; | ||
259 | } | 335 | } | ||
260 | 336 | | |||
261 | if (!price.isZero()) { | 337 | if (!price.isZero()) { | ||
262 | m_conversionRate->loadText(price.formatMoney(QString(), m_fromCurrency.pricePrecision())); | 338 | d->ui->m_conversionRate->loadText(price.formatMoney(QString(), d->m_fromCurrency.pricePrecision())); | ||
263 | m_result = (m_value * price).convert(m_resultFraction); | 339 | d->m_result = (d->m_value * price).convert(d->m_resultFraction); | ||
264 | m_toAmount->loadText(m_result.formatMoney(QString(), MyMoneyMoney::denomToPrec(m_resultFraction))); | 340 | d->ui->m_toAmount->loadText(d->m_result.formatMoney(QString(), MyMoneyMoney::denomToPrec(d->m_resultFraction))); | ||
265 | } | 341 | } | ||
266 | updateExample(price); | 342 | d->updateExample(price); | ||
267 | } | | |||
268 | | ||||
269 | void KCurrencyCalculator::updateExample(const MyMoneyMoney& price) | | |||
270 | { | | |||
271 | QString msg; | | |||
272 | if (price.isZero()) { | | |||
273 | msg = QString("1 %1 = ? %2").arg(m_fromCurrency.tradingSymbol()) | | |||
274 | .arg(m_toCurrency.tradingSymbol()); | | |||
275 | if (m_fromCurrency.isCurrency()) { | | |||
276 | msg += QString("\n"); | | |||
277 | msg += QString("1 %1 = ? %2").arg(m_toCurrency.tradingSymbol()) | | |||
278 | .arg(m_fromCurrency.tradingSymbol()); | | |||
279 | } | | |||
280 | } else { | | |||
281 | msg = QString("1 %1 = %2 %3").arg(m_fromCurrency.tradingSymbol()) | | |||
282 | .arg(price.formatMoney(QString(), m_fromCurrency.pricePrecision())) | | |||
283 | .arg(m_toCurrency.tradingSymbol()); | | |||
284 | if (m_fromCurrency.isCurrency()) { | | |||
285 | msg += QString("\n"); | | |||
286 | msg += QString("1 %1 = %2 %3").arg(m_toCurrency.tradingSymbol()) | | |||
287 | .arg((MyMoneyMoney::ONE / price).formatMoney(QString(), m_toCurrency.pricePrecision())) | | |||
288 | .arg(m_fromCurrency.tradingSymbol()); | | |||
289 | } | | |||
290 | } | | |||
291 | m_conversionExample->setText(msg); | | |||
292 | m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!price.isZero()); | | |||
293 | } | 343 | } | ||
294 | 344 | | |||
295 | void KCurrencyCalculator::accept() | 345 | void KCurrencyCalculator::accept() | ||
296 | { | 346 | { | ||
297 | if (m_conversionRate->isEnabled()) | 347 | Q_D(KCurrencyCalculator); | ||
348 | if (d->ui->m_conversionRate->isEnabled()) | ||||
298 | slotUpdateRate(QString()); | 349 | slotUpdateRate(QString()); | ||
299 | else | 350 | else | ||
300 | slotUpdateResult(QString()); | 351 | slotUpdateResult(QString()); | ||
301 | 352 | | |||
302 | if (m_updateButton->isChecked()) { | 353 | if (d->ui->m_updateButton->isChecked()) { | ||
303 | MyMoneyPrice pr = MyMoneyFile::instance()->price(m_fromCurrency.id(), m_toCurrency.id(), m_dateEdit->date()); | 354 | auto pr = MyMoneyFile::instance()->price(d->m_fromCurrency.id(), d->m_toCurrency.id(), d->ui->m_dateEdit->date()); | ||
304 | if (!pr.isValid() | 355 | if (!pr.isValid() | ||
305 | || pr.date() != m_dateEdit->date() | 356 | || pr.date() != d->ui->m_dateEdit->date() | ||
306 | || (pr.date() == m_dateEdit->date() && pr.rate(m_fromCurrency.id()) != price())) { | 357 | || (pr.date() == d->ui->m_dateEdit->date() && pr.rate(d->m_fromCurrency.id()) != price())) { | ||
307 | pr = MyMoneyPrice(m_fromCurrency.id(), m_toCurrency.id(), m_dateEdit->date(), price(), i18n("User")); | 358 | pr = MyMoneyPrice(d->m_fromCurrency.id(), d->m_toCurrency.id(), d->ui->m_dateEdit->date(), price(), i18n("User")); | ||
308 | MyMoneyFileTransaction ft; | 359 | MyMoneyFileTransaction ft; | ||
309 | try { | 360 | try { | ||
310 | MyMoneyFile::instance()->addPrice(pr); | 361 | MyMoneyFile::instance()->addPrice(pr); | ||
Context not available. | |||||
316 | } | 367 | } | ||
317 | 368 | | |||
318 | // remember setting for next round | 369 | // remember setting for next round | ||
319 | KMyMoneyGlobalSettings::setPriceHistoryUpdate(m_updateButton->isChecked()); | 370 | KMyMoneyGlobalSettings::setPriceHistoryUpdate(d->ui->m_updateButton->isChecked()); | ||
320 | 371 | QDialog::accept(); | |||
321 | KCurrencyCalculatorDecl::accept(); | | |||
322 | } | 372 | } | ||
323 | 373 | | |||
324 | MyMoneyMoney KCurrencyCalculator::price() const | 374 | MyMoneyMoney KCurrencyCalculator::price() const | ||
325 | { | 375 | { | ||
376 | Q_D(const KCurrencyCalculator); | ||||
326 | // This should fix https://bugs.kde.org/show_bug.cgi?id=205254 and | 377 | // This should fix https://bugs.kde.org/show_bug.cgi?id=205254 and | ||
327 | // https://bugs.kde.org/show_bug.cgi?id=325953 as well as | 378 | // https://bugs.kde.org/show_bug.cgi?id=325953 as well as | ||
328 | // https://bugs.kde.org/show_bug.cgi?id=300965 | 379 | // https://bugs.kde.org/show_bug.cgi?id=300965 | ||
329 | if (m_amountButton->isChecked()) | 380 | if (d->ui->m_amountButton->isChecked()) | ||
330 | return m_toAmount->value().abs() / m_value.abs(); | 381 | return d->ui->m_toAmount->value().abs() / d->m_value.abs(); | ||
331 | else | 382 | else | ||
332 | return m_conversionRate->value(); | 383 | return d->ui->m_conversionRate->value(); | ||
333 | } | 384 | } | ||
Context not available. |