File indexing completed on 2024-06-16 04:46:39
0001 /* 0002 SPDX-FileCopyrightText: 2007-2011 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kbalancechartdlg.h" 0008 0009 // ---------------------------------------------------------------------------- 0010 // QT Includes 0011 0012 #include <QVBoxLayout> 0013 #include <QDialogButtonBox> 0014 #include <QWindow> 0015 0016 // ---------------------------------------------------------------------------- 0017 // KDE Includes 0018 0019 #include <KSharedConfig> 0020 #include <KWindowConfig> 0021 #include <KConfigGroup> 0022 #include <KLocalizedString> 0023 0024 // ---------------------------------------------------------------------------- 0025 // Project Includes 0026 0027 #include "mymoneyreport.h" 0028 #include "pivottable.h" 0029 #include "kreportchartview.h" 0030 #include "mymoneyenums.h" 0031 0032 using namespace reports; 0033 0034 KBalanceChartDlg::KBalanceChartDlg(const MyMoneyAccount& account, QWidget* parent) : 0035 QDialog(parent) 0036 { 0037 setWindowTitle(i18n("Balance of %1", account.name())); 0038 setSizeGripEnabled(true); 0039 setModal(true); 0040 0041 // restore the last used dialog size 0042 winId(); // needs to be called to create the QWindow 0043 KConfigGroup grp = KSharedConfig::openConfig()->group("KBalanceChartDlg"); 0044 if (grp.isValid()) { 0045 KWindowConfig::restoreWindowSize(windowHandle(), grp); 0046 } 0047 // let the minimum size be 700x500 0048 resize(QSize(700, 500).expandedTo(windowHandle() ? windowHandle()->size() : QSize())); 0049 0050 QVBoxLayout *mainLayout = new QVBoxLayout; 0051 setLayout(mainLayout); 0052 //draw the chart and add it to the main layout 0053 KReportChartView* chartWidget = drawChart(account); 0054 mainLayout->addWidget(chartWidget); 0055 0056 // add the buttons 0057 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); 0058 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0059 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0060 mainLayout->addWidget(buttonBox); 0061 } 0062 0063 0064 KBalanceChartDlg::~KBalanceChartDlg() 0065 { 0066 // store the last used dialog size 0067 KConfigGroup grp = KSharedConfig::openConfig()->group("KBalanceChartDlg"); 0068 if (grp.isValid()) { 0069 KWindowConfig::saveWindowSize(windowHandle(), grp); 0070 } 0071 } 0072 0073 KReportChartView* KBalanceChartDlg::drawChart(const MyMoneyAccount& account) 0074 { 0075 MyMoneyReport reportCfg = MyMoneyReport( 0076 eMyMoney::Report::RowType::AssetLiability, 0077 static_cast<unsigned>(eMyMoney::Report::ColumnType::Months), 0078 eMyMoney::TransactionFilter::Date::Last3ToNext3Months, 0079 eMyMoney::Report::DetailLevel::Total, 0080 i18n("%1 Balance History", account.name()), 0081 i18n("Generated Report") 0082 ); 0083 reportCfg.setChartByDefault(true); 0084 reportCfg.setChartCHGridLines(false); 0085 reportCfg.setChartSVGridLines(false); 0086 reportCfg.setChartDataLabels(false); 0087 reportCfg.setChartType(eMyMoney::Report::ChartType::Line); 0088 reportCfg.setChartPalette(eMyMoney::Report::ChartPalette::Application); 0089 reportCfg.setIncludingForecast(true); 0090 reportCfg.setIncludingBudgetActuals(true); 0091 if (account.accountType() == eMyMoney::Account::Type::Investment) { 0092 const auto subAccountList = account.accountList(); 0093 for (const auto& accountID : qAsConst(subAccountList)) 0094 reportCfg.addAccount(accountID); 0095 } else 0096 reportCfg.addAccount(account.id()); 0097 reportCfg.setColumnsAreDays(true); 0098 reportCfg.setConvertCurrency(false); 0099 reportCfg.setMixedTime(true); 0100 reports::PivotTable table(reportCfg); 0101 0102 reports::KReportChartView* chartWidget = new reports::KReportChartView(this); 0103 0104 table.drawChart(*chartWidget); 0105 0106 // add another row for limit 0107 bool needRow = false; 0108 bool haveMinBalance = false; 0109 bool haveMaxCredit = false; 0110 MyMoneyMoney minBalance, maxCredit; 0111 MyMoneyMoney factor(1, 1); 0112 if (account.accountGroup() == eMyMoney::Account::Type::Asset) 0113 factor = -factor; 0114 0115 if (!account.value("maxCreditEarly").isEmpty()) { 0116 needRow = true; 0117 haveMaxCredit = true; 0118 maxCredit = MyMoneyMoney(account.value("maxCreditEarly")) * factor; 0119 } 0120 if (!account.value("maxCreditAbsolute").isEmpty()) { 0121 needRow = true; 0122 haveMaxCredit = true; 0123 maxCredit = MyMoneyMoney(account.value("maxCreditAbsolute")) * factor; 0124 } 0125 0126 if (!account.value("minBalanceEarly").isEmpty()) { 0127 needRow = true; 0128 haveMinBalance = true; 0129 minBalance = MyMoneyMoney(account.value("minBalanceEarly")); 0130 } 0131 if (!account.value("minBalanceAbsolute").isEmpty()) { 0132 needRow = true; 0133 haveMinBalance = true; 0134 minBalance = MyMoneyMoney(account.value("minBalanceAbsolute")); 0135 } 0136 0137 if (needRow) { 0138 if (haveMinBalance) { 0139 chartWidget->drawLimitLine(minBalance.toDouble()); 0140 } 0141 if (haveMaxCredit) { 0142 chartWidget->drawLimitLine(maxCredit.toDouble()); 0143 } 0144 } 0145 0146 // always draw the y axis zero value line 0147 // TODO: port to KF5 - this crashes KChart 0148 //chartWidget->drawLimitLine(0); 0149 0150 //remove the legend 0151 chartWidget->removeLegend(); 0152 0153 return chartWidget; 0154 }