Changeset View
Standalone View
kmymoney/plugins/views/reports/reporttabimpl.cpp
| 1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
|---|---|---|---|---|---|
| 2 | Copyright (C) 2009 Laurent Montel <montel@kde.org> | 2 | Copyright (C) 2009 Laurent Montel <montel@kde.org> | ||
| 3 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | 3 | (C) 2017 by Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> | ||
| 4 | Copyright 2018 Michael Kiefer <Michael-Kiefer@web.de> | 4 | Copyright 2018 Michael Kiefer <Michael-Kiefer@web.de> | ||
| 5 | Copyright 2020 Robert Szczesiak <dev.rszczesiak@gmail.com> | ||||
| 5 | 6 | | |||
| 6 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
| 7 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public | ||
| 8 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either | ||
| 9 | version 2 of the License, or (at your option) any later version. | 10 | version 2 of the License, or (at your option) any later version. | ||
| 10 | 11 | | |||
| 11 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, | ||
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | Library General Public License for more details. | 15 | Library General Public License for more details. | ||
| 15 | 16 | | |||
| 16 | You should have received a copy of the GNU Library General Public License | 17 | You should have received a copy of the GNU Library General Public License | ||
| 17 | along with this library; see the file COPYING.LIB. If not, write to | 18 | along with this library; see the file COPYING.LIB. If not, write to | ||
| 18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| 19 | Boston, MA 02110-1301, USA. | 20 | Boston, MA 02110-1301, USA. | ||
| 20 | */ | 21 | */ | ||
| 21 | 22 | | |||
| 22 | #include "reporttabimpl.h" | 23 | #include "reporttabimpl.h" | ||
| 23 | 24 | | |||
| 24 | #include <KLocalizedString> | 25 | #include <KLocalizedString> | ||
| 26 | #include <QtMath> | ||||
| 27 | | ||||
| 28 | #include "kmymoneyutils.h" | ||||
| 25 | #include "daterangedlg.h" | 29 | #include "daterangedlg.h" | ||
| 26 | 30 | | |||
| 27 | #include "ui_reporttabgeneral.h" | 31 | #include "ui_reporttabgeneral.h" | ||
| 28 | #include "ui_reporttabrowcolpivot.h" | 32 | #include "ui_reporttabrowcolpivot.h" | ||
| 29 | #include "ui_reporttabrowcolquery.h" | 33 | #include "ui_reporttabrowcolquery.h" | ||
| 30 | #include "ui_reporttabchart.h" | 34 | #include "ui_reporttabchart.h" | ||
| 31 | #include "ui_reporttabrange.h" | 35 | #include "ui_reporttabrange.h" | ||
| 32 | #include "ui_reporttabcapitalgain.h" | 36 | #include "ui_reporttabcapitalgain.h" | ||
| ▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | 143 | if (set) { | |||
| 141 | ui->m_logYaxis->setEnabled(false); | 145 | ui->m_logYaxis->setEnabled(false); | ||
| 142 | } else { | 146 | } else { | ||
| 143 | ui->m_logYaxis->setEnabled(true); | 147 | ui->m_logYaxis->setEnabled(true); | ||
| 144 | } | 148 | } | ||
| 145 | } | 149 | } | ||
| 146 | 150 | | |||
| 147 | ReportTabRange::ReportTabRange(QWidget *parent) | 151 | ReportTabRange::ReportTabRange(QWidget *parent) | ||
| 148 | : QWidget(parent), | 152 | : QWidget(parent), | ||
| 149 | ui(new Ui::ReportTabRange) | 153 | ui(new Ui::ReportTabRange), | ||
| 154 | m_logYaxis(false) | ||||
| 150 | { | 155 | { | ||
| 151 | ui->setupUi(this); | 156 | ui->setupUi(this); | ||
| 152 | m_dateRange = new DateRangeDlg; | 157 | m_dateRange = new DateRangeDlg; | ||
| 153 | ui->dateRangeGrid->addWidget(m_dateRange, 0, 0, 1, 2); | 158 | ui->dateRangeGrid->addWidget(m_dateRange, 0, 0, 1, 2); | ||
| 154 | connect(ui->m_yLabelsPrecision, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ReportTabRange::slotYLabelsPrecisionChanged); | 159 | connect(ui->m_yLabelsPrecision, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &ReportTabRange::slotYLabelsPrecisionChanged); | ||
| 155 | emit ui->m_yLabelsPrecision->valueChanged(ui->m_yLabelsPrecision->value()); | 160 | emit ui->m_yLabelsPrecision->valueChanged(ui->m_yLabelsPrecision->value()); | ||
| 156 | connect(ui->m_dataRangeStart, &QLineEdit::editingFinished, this, &ReportTabRange::slotEditingFinishedStart); | 161 | connect(ui->m_dataRangeStart, &QLineEdit::editingFinished, this, &ReportTabRange::slotEditingFinishedStart); | ||
| 157 | connect(ui->m_dataRangeEnd, &QLineEdit::editingFinished, this, &ReportTabRange::slotEditingFinishedEnd); | 162 | connect(ui->m_dataRangeEnd, &QLineEdit::editingFinished, this, &ReportTabRange::slotEditingFinishedEnd); | ||
| Show All 11 Lines | |||||
| 169 | void ReportTabRange::setRangeLogarythmic(bool set) | 174 | void ReportTabRange::setRangeLogarythmic(bool set) | ||
| 170 | { | 175 | { | ||
| 171 | // major and minor tick have no influence if axis is logarithmic so hide them | 176 | // major and minor tick have no influence if axis is logarithmic so hide them | ||
| 172 | if (set) { | 177 | if (set) { | ||
| 173 | ui->lblDataMajorTick->hide(); | 178 | ui->lblDataMajorTick->hide(); | ||
| 174 | ui->lblDataMinorTick->hide(); | 179 | ui->lblDataMinorTick->hide(); | ||
| 175 | ui->m_dataMajorTick->hide(); | 180 | ui->m_dataMajorTick->hide(); | ||
| 176 | ui->m_dataMinorTick->hide(); | 181 | ui->m_dataMinorTick->hide(); | ||
| 182 | | ||||
| 183 | m_logYaxis = true; | ||||
| 177 | } else { | 184 | } else { | ||
| 178 | ui->lblDataMajorTick->show(); | 185 | ui->lblDataMajorTick->show(); | ||
| 179 | ui->lblDataMinorTick->show(); | 186 | ui->lblDataMinorTick->show(); | ||
| 180 | ui->m_dataMajorTick->show(); | 187 | ui->m_dataMajorTick->show(); | ||
| 181 | ui->m_dataMinorTick->show(); | 188 | ui->m_dataMinorTick->show(); | ||
| 189 | | ||||
| 190 | m_logYaxis = false; | ||||
| 191 | } | ||||
| 192 | | ||||
| 193 | updateDataRangeValidators(ui->m_yLabelsPrecision->value()); // update data range validators and re-validate | ||||
| 194 | } | ||||
| 195 | | ||||
| 196 | void ReportTabRange::updateDataRangeValidators(const int& precision) | ||||
| 197 | { | ||||
| 198 | ui->m_dataRangeStart->setValidator(nullptr); | ||||
| 199 | ui->m_dataRangeEnd->setValidator(nullptr); | ||||
| 200 | | ||||
| 201 | QDoubleValidator *dbValStart; | ||||
| 202 | QDoubleValidator *dbValEnd; | ||||
| 203 | if (m_logYaxis) { | ||||
| 204 | dbValStart = new MyLogarithmicDoubleValidator(precision, qPow(10, -precision), ui->m_dataRangeStart); | ||||
| 205 | dbValEnd = new MyLogarithmicDoubleValidator(precision, qPow(10, -precision + 4), ui->m_dataRangeEnd); | ||||
| 206 | } else { // the validator will be used by two QLineEdit objects so let this tab be their parent | ||||
tbaumgart: Who owns this dblVal object and who destroys it? | |||||
| 207 | dbValStart = new MyDoubleValidator(precision, this); | ||||
| 208 | dbValEnd = dbValStart; | ||||
| 209 | } | ||||
| 210 | | ||||
| 211 | ui->m_dataRangeStart->setValidator(dbValStart); | ||||
| 212 | ui->m_dataRangeEnd->setValidator(dbValEnd); | ||||
| 213 | | ||||
| 214 | QString dataRangeStart = ui->m_dataRangeStart->text(); | ||||
| 215 | QString dataRangeEnd = ui->m_dataRangeEnd->text(); | ||||
This remove stuff shows up in several places. Seems to be a good candidate for a utility function. tbaumgart: This remove stuff shows up in several places. Seems to be a good candidate for a utility… | |||||
| 216 | if (!ui->m_dataRangeStart->hasAcceptableInput()) { | ||||
| 217 | dbValStart->fixup(dataRangeStart); | ||||
| 218 | ui->m_dataRangeStart->setText(dataRangeStart); | ||||
| 219 | } | ||||
| 220 | if (ui->m_dataRangeEnd->hasAcceptableInput()) { | ||||
| 221 | dbValEnd->fixup(dataRangeEnd); | ||||
| 222 | ui->m_dataRangeEnd->setText(dataRangeEnd); | ||||
| 182 | } | 223 | } | ||
| 183 | } | 224 | } | ||
| 184 | 225 | | |||
| 185 | void ReportTabRange::slotEditingFinished(EDimension dim) | 226 | void ReportTabRange::slotEditingFinished(EDimension dim) | ||
| 186 | { | 227 | { | ||
| 187 | qreal dataRangeStart = locale().toDouble(ui->m_dataRangeStart->text()); | 228 | qreal dataRangeStart = locale().toDouble(ui->m_dataRangeStart->text()); | ||
| 188 | qreal dataRangeEnd = locale().toDouble(ui->m_dataRangeEnd->text()); | 229 | qreal dataRangeEnd = locale().toDouble(ui->m_dataRangeEnd->text()); | ||
| 189 | qreal dataMajorTick = locale().toDouble(ui->m_dataMajorTick->text()); | 230 | | ||
| 190 | qreal dataMinorTick = locale().toDouble(ui->m_dataMinorTick->text()); | | |||
| 191 | if (dataRangeEnd < dataRangeStart) { // end must be higher than start | 231 | if (dataRangeEnd < dataRangeStart) { // end must be higher than start | ||
| 192 | if (dim == eRangeEnd) { | 232 | if (dim == eRangeEnd) { | ||
| 193 | ui->m_dataRangeStart->setText(ui->m_dataRangeEnd->text()); | 233 | ui->m_dataRangeStart->setText(ui->m_dataRangeEnd->text()); | ||
| 194 | dataRangeStart = dataRangeEnd; | 234 | dataRangeStart = dataRangeEnd; | ||
| 195 | } else { | 235 | } else { | ||
| 196 | ui->m_dataRangeEnd->setText(ui->m_dataRangeStart->text()); | 236 | ui->m_dataRangeEnd->setText(ui->m_dataRangeStart->text()); | ||
| 197 | dataRangeEnd = dataRangeStart; | 237 | dataRangeEnd = dataRangeStart; | ||
| 198 | } | 238 | } | ||
| 199 | } | 239 | } | ||
| 240 | if (!m_logYaxis) { // major and minor ticks only have influence when axis is linear | ||||
| 241 | qreal dataMajorTick = locale().toDouble(ui->m_dataMajorTick->text()); | ||||
| 242 | qreal dataMinorTick = locale().toDouble(ui->m_dataMinorTick->text()); | ||||
| 200 | if ((dataRangeStart != 0 || dataRangeEnd != 0)) { // if data range isn't going to be reset | 243 | if ((dataRangeStart != 0 || dataRangeEnd != 0)) { // if data range isn't going to be reset | ||
| 201 | if ((dataRangeEnd - dataRangeStart) < dataMajorTick) // major tick cannot be greater than data range | 244 | if ((dataRangeEnd - dataRangeStart) < dataMajorTick) // major tick cannot be greater than data range | ||
| 202 | dataMajorTick = dataRangeEnd - dataRangeStart; | 245 | dataMajorTick = dataRangeEnd - dataRangeStart; | ||
| 203 | 246 | | |||
| 204 | if (dataMajorTick != 0 && // if major tick isn't going to be reset | 247 | if (dataMajorTick != 0 && // if major tick isn't going to be reset | ||
| 205 | dataMajorTick < (dataRangeEnd - dataRangeStart) * 0.01) // constraint major tick to be greater or equal to 1% of data range | 248 | dataMajorTick < (dataRangeEnd - dataRangeStart) * 0.01) // constraint major tick to be greater or equal to 1% of data range | ||
| 206 | dataMajorTick = (dataRangeEnd - dataRangeStart) * 0.01; | 249 | dataMajorTick = (dataRangeEnd - dataRangeStart) * 0.01; | ||
| 207 | 250 | | |||
| Show All 11 Lines | 255 | if (dataMajorTick < dataMinorTick) { // major tick must be higher than minor | |||
| 219 | } | 262 | } | ||
| 220 | } | 263 | } | ||
| 221 | 264 | | |||
| 222 | if (dataMinorTick < dataMajorTick * 0.1) { // constraint minor tick to be greater or equal to 10% of major tick, and set precision to be greater by 1 | 265 | if (dataMinorTick < dataMajorTick * 0.1) { // constraint minor tick to be greater or equal to 10% of major tick, and set precision to be greater by 1 | ||
| 223 | dataMinorTick = dataMajorTick * 0.1; | 266 | dataMinorTick = dataMajorTick * 0.1; | ||
| 224 | ui->m_dataMinorTick->setText(locale().toString(dataMinorTick, 'f', ui->m_yLabelsPrecision->value() + 1).remove(locale().groupSeparator()).remove(QRegularExpression("0+$")).remove(QRegularExpression("\\" + locale().decimalPoint() + "$"))); | 267 | ui->m_dataMinorTick->setText(locale().toString(dataMinorTick, 'f', ui->m_yLabelsPrecision->value() + 1).remove(locale().groupSeparator()).remove(QRegularExpression("0+$")).remove(QRegularExpression("\\" + locale().decimalPoint() + "$"))); | ||
| 225 | } | 268 | } | ||
| 226 | } | 269 | } | ||
| 270 | } | ||||
| 227 | 271 | | |||
| 228 | void ReportTabRange::slotEditingFinishedStart() | 272 | void ReportTabRange::slotEditingFinishedStart() | ||
| 229 | { | 273 | { | ||
| 230 | slotEditingFinished(eRangeStart); | 274 | slotEditingFinished(eRangeStart); | ||
| 231 | } | 275 | } | ||
| 232 | 276 | | |||
| 233 | void ReportTabRange::slotEditingFinishedEnd() | 277 | void ReportTabRange::slotEditingFinishedEnd() | ||
| 234 | { | 278 | { | ||
| 235 | slotEditingFinished(eRangeEnd); | 279 | slotEditingFinished(eRangeEnd); | ||
| 236 | } | 280 | } | ||
| 237 | 281 | | |||
| 238 | void ReportTabRange::slotEditingFinishedMajor() | 282 | void ReportTabRange::slotEditingFinishedMajor() | ||
| 239 | { | 283 | { | ||
| 240 | slotEditingFinished(eMajorTick); | 284 | slotEditingFinished(eMajorTick); | ||
| 241 | } | 285 | } | ||
| 242 | 286 | | |||
| 243 | void ReportTabRange::slotEditingFinishedMinor() | 287 | void ReportTabRange::slotEditingFinishedMinor() | ||
| 244 | { | 288 | { | ||
| 245 | slotEditingFinished(eMinorTick); | 289 | slotEditingFinished(eMinorTick); | ||
| 246 | } | 290 | } | ||
| 247 | 291 | | |||
| 248 | void ReportTabRange::slotYLabelsPrecisionChanged(const int& value) | 292 | void ReportTabRange::slotYLabelsPrecisionChanged(const int& value) | ||
| 249 | { | 293 | { | ||
| 250 | ui->m_dataRangeStart->setValidator(0); | | |||
| 251 | ui->m_dataRangeEnd->setValidator(0); | | |||
| 252 | ui->m_dataMajorTick->setValidator(0); | 294 | ui->m_dataMajorTick->setValidator(0); | ||
| 253 | ui->m_dataMinorTick->setValidator(0); | 295 | ui->m_dataMinorTick->setValidator(0); | ||
| 254 | 296 | | |||
| 255 | MyDoubleValidator *dblVal = new MyDoubleValidator(value); | | |||
| 256 | ui->m_dataRangeStart->setValidator(dblVal); | | |||
| 257 | ui->m_dataRangeEnd->setValidator(dblVal); | | |||
| 258 | MyDoubleValidator *dblVal2 = new MyDoubleValidator(value + 1); | 297 | MyDoubleValidator *dblVal2 = new MyDoubleValidator(value + 1); | ||
| 259 | ui->m_dataMajorTick->setValidator(dblVal2); | 298 | ui->m_dataMajorTick->setValidator(dblVal2); | ||
| 260 | ui->m_dataMinorTick->setValidator(dblVal2); | 299 | ui->m_dataMinorTick->setValidator(dblVal2); | ||
| 300 | | ||||
| 301 | updateDataRangeValidators(value); | ||||
| 261 | } | 302 | } | ||
| 262 | 303 | | |||
| 263 | void ReportTabRange::slotDataLockChanged(int index) { | 304 | void ReportTabRange::slotDataLockChanged(int index) { | ||
| 264 | if (index == static_cast<int>(eMyMoney::Report::DataLock::Automatic)) { | 305 | if (index == static_cast<int>(eMyMoney::Report::DataLock::Automatic)) { | ||
| 265 | ui->m_dataRangeStart->setText(QStringLiteral("0")); | 306 | ui->m_dataRangeStart->setText(QStringLiteral("0")); | ||
| 266 | ui->m_dataRangeEnd->setText(QStringLiteral("0")); | 307 | ui->m_dataRangeEnd->setText(QStringLiteral("0")); | ||
| 267 | ui->m_dataMajorTick->setText(QStringLiteral("0")); | 308 | ui->m_dataMajorTick->setText(QStringLiteral("0")); | ||
| 268 | ui->m_dataMinorTick->setText(QStringLiteral("0")); | 309 | ui->m_dataMinorTick->setText(QStringLiteral("0")); | ||
| ▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Line(s) | 368 | { | |||
| 344 | locale().toDouble(s, &ok); | 385 | locale().toDouble(s, &ok); | ||
| 345 | 386 | | |||
| 346 | if (ok) { | 387 | if (ok) { | ||
| 347 | return QValidator::Acceptable; | 388 | return QValidator::Acceptable; | ||
| 348 | } else { | 389 | } else { | ||
| 349 | return QValidator::Invalid; | 390 | return QValidator::Invalid; | ||
| 350 | } | 391 | } | ||
| 351 | } | 392 | } | ||
| 393 | | ||||
| 394 | MyLogarithmicDoubleValidator::MyLogarithmicDoubleValidator(const int decimals, const qreal defaultValue, QObject *parent) | ||||
| 395 | : QDoubleValidator(qPow(10, -decimals), 0, decimals, parent) | ||||
| 396 | { | ||||
| 397 | m_defaultText = KMyMoneyUtils::normalizeNumericString(defaultValue, locale(), 'f', decimals); | ||||
| 398 | } | ||||
| 399 | | ||||
| 400 | QValidator::State MyLogarithmicDoubleValidator::validate(QString &s, int &i) const | ||||
| 401 | { | ||||
| 402 | Q_UNUSED(i); | ||||
| 403 | if (s.isEmpty() || s == QStringLiteral("0")) { | ||||
| 404 | return QValidator::Intermediate; | ||||
| 405 | } | ||||
| 406 | | ||||
| 407 | QChar decimalPoint = locale().decimalPoint(); | ||||
I might not fully understand that RE. Can you explain it? If you use anchoredPattern, why do you add a $ at the end? What is %110 and %12? I must be blind. Since you add the decimal symbol here, you could also capture the part after the decimal symbol directly in the RE and use it later. Though, a match only occurs if the fraction has between 0 and decimals() number of digits, so it could never match and have more to become invalid. So the check below seems somewhat superfluous. Or what is it that I don't see here? tbaumgart: I might not fully understand that RE. Can you explain it? If you use anchoredPattern, why do… | |||||
This one is tricky :) I should have commented on this RE. rszczesiak: This one is tricky :) I should have commented on this RE.
First, you are right - the `$` is… | |||||
Thank you for the explanation. Completely valid. Agreed, a comment would help. I did not know about the trick to start with a two digit placeholder index. One problem though: QRegularExpression::anchoredText only appeared in Qt 5.12 and we still support older versions (e.g. 5.9), so we should use ^ and $ as part of the RE. tbaumgart: Thank you for the explanation. Completely valid. Agreed, a comment would help. I did not know… | |||||
| 408 | | ||||
| 409 | // start numbering placeholders with a two-digit number to avoid | ||||
| 410 | // interpreting the following zero as part of the placeholder index | ||||
tbaumgart: Use
```
s == QLatin1String("0") or
s == QStringLiteral("0")
```
here | |||||
| 411 | const QRegularExpression re((QStringLiteral("^0\\%110{0,%12}$") | ||||
| 412 | .arg(decimalPoint) | ||||
| 413 | .arg(decimals() - 1))); | ||||
| 414 | if (re.match(s).hasMatch()) | ||||
| 415 | return QValidator::Intermediate; | ||||
| 416 | | ||||
| 417 | if (s.indexOf(decimalPoint) != -1) { | ||||
| 418 | int charsAfterPoint = s.length() - s.indexOf(decimalPoint) - 1; | ||||
| 419 | | ||||
| 420 | if (charsAfterPoint > decimals()) { | ||||
| 421 | return QValidator::Invalid; | ||||
| 422 | } | ||||
| 423 | } | ||||
| 424 | | ||||
| 425 | bool ok; | ||||
| 426 | const qreal result = locale().toDouble(s, &ok); | ||||
| 427 | | ||||
| 428 | if (ok && result >= bottom()) { | ||||
| 429 | return QValidator::Acceptable; | ||||
| 430 | } else { | ||||
| 431 | return QValidator::Invalid; | ||||
| 432 | } | ||||
| 433 | } | ||||
| 434 | | ||||
| 435 | void MyLogarithmicDoubleValidator::fixup(QString &input) const | ||||
| 436 | { | ||||
| 437 | input = m_defaultText; | ||||
| 438 | } | ||||
Who owns this dblVal object and who destroys it?