-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinMonthReport.cpp
More file actions
279 lines (226 loc) · 8.16 KB
/
Copy pathFinMonthReport.cpp
File metadata and controls
279 lines (226 loc) · 8.16 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <QtGui>
#include <QtSvg>
#include "FinMonthReport.h"
#include "FinDatabase.h"
FinMonthReportPreferencesDlg::FinMonthReportPreferencesDlg(QWidget* parent)
: QDialog(parent)
{
createWidgets();
}
FinMonthReportPreferencesDlg::~FinMonthReportPreferencesDlg()
{
}
int FinMonthReportPreferencesDlg::getMonth() const
{
return QVariant(m_leMonth->text()).toInt();
}
int FinMonthReportPreferencesDlg::getYear() const
{
return QVariant(m_leYear->text()).toInt();
}
void FinMonthReportPreferencesDlg::createWidgets()
{
QVBoxLayout* vbox = new QVBoxLayout;
m_leMonth = new QLineEdit;
m_leMonth->setValidator(new QIntValidator(1, 12, m_leMonth));
m_leYear = new QLineEdit;
m_leYear->setValidator(new QIntValidator(1000, 9999, m_leYear));
QHBoxLayout *hbox= new QHBoxLayout;
hbox->addWidget(new QLabel(tr("Month:")), 0);
hbox->addWidget(m_leMonth, 1);
vbox->addLayout(hbox, 0);
hbox = new QHBoxLayout;
hbox->addWidget(new QLabel(tr("Year:")), 0);
hbox->addWidget(m_leYear, 1);
vbox->addLayout(hbox, 0);
QPushButton *okButton = new QPushButton(tr("OK"));
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
connect(okButton, SIGNAL(clicked()), this, SLOT(onOk()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
hbox = new QHBoxLayout;
hbox->addStretch(1);
hbox->addWidget(okButton);
hbox->addWidget(cancelButton);
vbox->addStretch(1);
vbox->addLayout(hbox, 0);
setLayout(vbox);
setWindowTitle(tr("Month report preferences"));
m_leMonth->setText(QVariant(QDate::currentDate().month()).toString());
m_leYear->setText(QVariant(QDate::currentDate().year()).toString());
}
void FinMonthReportPreferencesDlg::onOk()
{
if (m_leMonth->text().isEmpty()
|| m_leYear->text().isEmpty())
{
QMessageBox::information(this, tr("Error"), tr("Nonvalid data"), QMessageBox::Ok);
return;
}
accept();
}
namespace {
QByteArray createSvg(double income, double charge, double selInc = 0, double selCh = 0)
{
qDebug() << "Income: " << income << "(" << selInc << ")\nCharge: " << charge << "(" << selCh << ")";
QString svg(
"<?xml version='1.0' standalone='no'?>"
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' "
"'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>"
"<svg width='200' height='200' version='1.1' xmlns='http://www.w3.org/2000/svg'>");
int incH, chH;
if (income >= charge)
{
incH = 200;
chH = int(200 * (charge / income));
qDebug() << (charge / income);
}
else
{
incH = int(200 * (income / charge));
chH = 200;
qDebug() << (income / charge);
}
int selIncH = int(incH * (selInc / income));
int selChH = int(chH * (selCh / charge));
svg +=
"<rect x='0' y='" + QVariant(200 - incH).toString()
+ "' width='100' height='" + QVariant(incH).toString() + "' "
"style='fill:green;stroke:green;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9'/>"
"<rect x='100' y='" + QVariant(200 - chH).toString()
+ "' width='100' height='" + QVariant(chH).toString() + "' "
"style='fill:red;stroke:green;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9'/>"
"<rect x='0' y='" + QVariant(200 - selIncH).toString()
+ "' width='100' height='" + QVariant(selIncH).toString() + "' "
"style='fill:green;stroke:green;stroke-width:5;fill-opacity:0.3;stroke-opacity:0.9'/>"
"<rect x='100' y='" + QVariant(200 - selChH).toString()
+ "' width='100' height='" + QVariant(selChH).toString() + "' "
"style='fill:red;stroke:green;stroke-width:5;fill-opacity:0.3;stroke-opacity:0.9'/>"
"<g transform='translate(10,190)'><text id='TextElement' x='0' y='0' style='font-size:14;'>"
+ QVariant(income).toString() + "</text></g>"
"<g transform='translate(110,190)'><text id='TextElement' x='0' y='0' style='font-size:14;'>"
+ QVariant(-charge).toString() + "</text></g>"
"</svg>";
qDebug() << svg;
return svg.toAscii();
}
}
FinMonthGraph::FinMonthGraph(double income, double charge)
: m_income(income), m_charge(charge), m_selInc(0), m_selCh(0)
{
m_swHist = new QSvgWidget;
QVBoxLayout* vbox = new QVBoxLayout;
vbox->addWidget(m_swHist, 1);
m_swHist->load(createSvg(income, -charge));
setLayout(vbox);
}
FinMonthGraph::~FinMonthGraph()
{
}
void FinMonthGraph::selectIncome(double val)
{
m_selInc = val;
m_swHist->load(createSvg(m_income, -m_charge, m_selInc, -m_selCh));
}
void FinMonthGraph::selectCharge(double val)
{
m_selCh = val;
m_swHist->load(createSvg(m_income, -m_charge, m_selInc, -m_selCh));
}
void FinMonthReport::incomeSelectionChanged()
{
QList<QTreeWidgetItem*> items = m_twIncome->selectedItems();
double selVal = 0;
for (int i=0; i<items.size(); ++i)
selVal += items.at(i)->data(0, Qt::DisplayRole).toDouble();
m_graph->selectIncome(selVal);
}
void FinMonthReport::chargeSelectionChanged()
{
QList<QTreeWidgetItem*> items = m_twCharge->selectedItems();
double selVal = 0;
for (int i=0; i<items.size(); ++i)
selVal += items.at(i)->data(0, Qt::DisplayRole).toDouble();
m_graph->selectCharge(selVal);
}
QTreeWidget* FinMonthReport::createOperationList(bool oprType)
{
QTreeWidget* tw = new QTreeWidget;
tw->setColumnCount(2);
QStringList header;
header << tr("Sum");
if (oprType)
header << tr("Income");
else
header << tr("Charge");
tw->setHeaderLabels(header);
tw->setSelectionMode(QAbstractItemView::ExtendedSelection);
if (oprType)
connect(tw, SIGNAL(itemSelectionChanged()), this, SLOT(incomeSelectionChanged()));
else
connect(tw, SIGNAL(itemSelectionChanged()), this, SLOT(chargeSelectionChanged()));
Records recs;
FinDatabase::Instance()->loadRangeOperations(
m_date,
QDate(m_date.year(), m_date.month(), m_date.daysInMonth()),
oprType,
recs);
qDebug() << recs.size();
QList<QTreeWidgetItem*> items;
for (int i=0; i< recs.size(); ++i)
{
QStringList vals;
vals << QVariant(recs[i].num).toString() << recs[i].name;
QTreeWidgetItem* item = new QTreeWidgetItem(vals);
items.append(item);
}
tw->addTopLevelItems(items);
return tw;
}
FinMonthReport::FinMonthReport(int month, int year)
: QDialog(NULL, Qt::Dialog), m_date(year, month, 1)
{
double charge = FinDatabase::Instance()->calcMonthCharge(month, year);
double income = FinDatabase::Instance()->calcMonthIncome(month, year);
Records charges;
FinDatabase::Instance()->loadMaxCharges(month, year, 3, charges);
Records incomes;
FinDatabase::Instance()->loadMaxIncomes(month, year, 3, incomes);
QString report;
QDate fromDate(year, month, 1);
QDate toDate(year, month, fromDate.daysInMonth());
report += tr("from ") + fromDate.toString() + tr(" to ") + toDate.toString() + "\n";
report += tr("Income: ") + QVariant(income).toString() + "\n";
for (int i=0; i<incomes.size(); ++i)
report += " " + incomes[i].name + " (" + QVariant(incomes[i].num).toString() + ")\n";
report += "\n";
report += tr("Charge: ") + QVariant(-charge).toString() + "\n";
for (int i=0; i<charges.size(); ++i)
report += " " + charges[i].name + " (" + QVariant(charges[i].num).toString() + ")\n";
report += "\n";
QString title = tr("Finances: ") + QVariant(month).toString() + "." + QVariant(year).toString();
setWindowTitle(title);
m_twIncome = createOperationList(true);
m_twCharge = createOperationList(false);
QSplitter* spl = new QSplitter;
spl->addWidget(m_twIncome);
spl->addWidget(m_twCharge);
spl->setOrientation(Qt::Vertical);
QVBoxLayout *vbox = new QVBoxLayout;
setLayout(vbox);
m_teReport = new QTextEdit;
m_teReport->setReadOnly(true);
m_teReport->setPlainText(report);
m_graph = new FinMonthGraph(income, charge);
QTabWidget* tw = new QTabWidget;
tw->addTab(m_graph, tr("Graph"));
tw->addTab(m_teReport, tr("Text"));
QSplitter* splAll = new QSplitter;
splAll->addWidget(spl);
splAll->addWidget(tw);
splAll->setStretchFactor(0, 1);
splAll->setStretchFactor(1, 5);
vbox->addWidget(splAll, 1);
}
FinMonthReport::~FinMonthReport()
{
}