-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
304 lines (263 loc) · 9.9 KB
/
mainwindow.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "chartbuilder.h"
#include "exporter.h"
#include <QDebug>
#include <QPieSeries>
#include <QFileDialog>
//CONSTRUCTOR & DESTRUCTOR
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
//UI
ui->setupUi(this);
//TABS
ui->tabWidget->setTabEnabled(1, false);
ui->tabWidget->setTabEnabled(2, false);
//BUTTONS
ui->exportSummaryPushButton->setEnabled(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
//------DATA LOADING-------
void MainWindow::loadData()
{
//chart
ui->chartViewOverall->setChart(cBuilder.buildEIChart(vManager, database));
ui->chartViewOverall->setRenderHint(QPainter::Antialiasing);
//summary
loadSummaryData();
//incomes tab
loadIncomesTabData();
//expenses tab
loadExpensesTabData();
}
void MainWindow::loadSummaryData()
{
//total expense
float totalExpense = vManager.getTotalExpense(database);
ui->totalExpenseValue->setText(QString::number(totalExpense));
//total income
float totalIncome = vManager.getTotalIncome(database);
ui->totalIncomeValue->setText(QString::number(totalIncome));
//status
float diff = totalIncome - totalExpense;
if ((diff > -1000) and (diff < 0))
{
ui->statusValue->setText("Warning: " + QString::number(totalIncome - totalExpense));
ui->statusValue->setStyleSheet("QLabel { background-color: orange; color: black; border-radius: 9px}");
} else if (diff < -1000)
{
ui->statusValue->setText("ALERT: " + QString::number(totalIncome - totalExpense));
ui->statusValue->setStyleSheet("QLabel { background-color: red; color: black; border-radius: 9px}");
} else {
ui->statusValue->setText("Good: " + QString::number(totalIncome - totalExpense));
ui->statusValue->setStyleSheet("QLabel { background-color: green; color: black; border-radius: 9px}");
}
loadPersonData();
}
void MainWindow::loadPersonData()
{
ui->personListSelectorWidget->setLayoutMode(QListView::SinglePass);
ui->personListSelectorWidget->clear();
ui->personListSelectorWidget->insertItems(0, vManager.getPersonsArray(database));
//ui->personListSelectorWidget->sortItems(Qt::SortOrder::AscendingOrder);
//ui->personListSelectorWidget->setSortingEnabled(true);
}
void MainWindow::loadIncomesTabData()
{
database.incomesTableModel->setTable("incomes");
database.incomesTableModel->sort(0, Qt::SortOrder::AscendingOrder);
ui->incomesTableView->setModel(database.incomesTableModel.get());
ui->incomesTotalValueLabel->setText(QString::number(vManager.getTotalIncome(database)));
}
void MainWindow::loadExpensesTabData()
{
database.expensesTableModel->setTable("expenses");
database.expensesTableModel->sort(0, Qt::SortOrder::AscendingOrder);
ui->expensesTableView->setModel(database.expensesTableModel.get());
ui->expensesTotalValueLabel->setText(QString::number(vManager.getTotalExpense(database)));
}
//------RESPONDING TO EVENTS------
//--SUMMARY BUTTONS
void MainWindow::on_newPushButton_clicked() // new database creation
{
QString dbFilename = QFileDialog::getSaveFileName(this, tr("Create Budget Database"), tr("newBudget.sqlite"), tr("*.sqlite"));
if (!dbFilename.isEmpty())
{
database.createNewDatabase(dbFilename);
loadData();
ui->budgetNameLabel->setText(dbFilename);
switchEnabledElements(true);
}
}
void MainWindow::on_loadPushButton_clicked() // loading existing database
{
QString dbFilename = QFileDialog::getOpenFileName(this, tr("Open Budget Database"), tr(".../"), tr("*.sqlite"));
if (!dbFilename.isEmpty())
{
database.openExistingDatabase(dbFilename);
loadData();
ui->budgetNameLabel->setText(dbFilename);
switchEnabledElements(true);
}
}
void MainWindow::on_actionOpen_triggered() // loading existing database by top menu
{
on_loadPushButton_clicked();
}
/*
void MainWindow::on_personListSelectorWidget_currentRowChanged(int currentRow) // response to change in person selector
{
QString person = ui->personListSelectorWidget->item(currentRow)->text();
ui->personExpensePercent->setText(QString::number(vManager.getPersonExpense(database, person) * 100 / vManager.getTotalExpense(database), 'f', 2) + "%");
ui->personIncomePercent->setText(QString::number(vManager.getPersonIncome(database, person) * 100 / vManager.getTotalIncome(database), 'f', 2) + "%");
}
*/
void MainWindow::on_personListSelectorWidget_itemClicked(QListWidgetItem *item)
{
QString person = item->text();
ui->personExpensePercent->setText(QString::number(vManager.getPersonExpense(database, person) * 100 / vManager.getTotalExpense(database), 'f', 2) + "%");
ui->personIncomePercent->setText(QString::number(vManager.getPersonIncome(database, person) * 100 / vManager.getTotalIncome(database), 'f', 2) + "%");
}
void MainWindow::on_exportSummaryPushButton_clicked()
{
QString filename = QFileDialog::getSaveFileName(this, tr("Export to .txt file"), tr("summaryExport"), tr("Text file (*.txt);;Excel datasheet (*.xlsx)"));
if (!filename.isEmpty() and filename.endsWith(".txt")) {
SummaryExporter s1(database, vManager);
s1.exportToTxt(filename);
} else if (!filename.isEmpty() and filename.endsWith(".xlsx")) {
SummaryExporter s2(database, vManager);
s2.exportToExcel(filename);
} else if (!filename.isEmpty()) {
QMessageBox fileError;
fileError.setText("Failed to export!");
fileError.setDetailedText("Wrong file format selected");
fileError.setDefaultButton(QMessageBox::Ok);
fileError.exec();
}
}
//--INCOMES BUTTONS
void MainWindow::on_incomesSubmitButton_clicked() // saving changes made earlier
{
database.incomesTableModel->database().transaction();
if (database.incomesTableModel->submitAll())
{
database.incomesTableModel->database().commit();
} else
{
database.incomesTableModel->database().rollback();
}
loadData();
}
void MainWindow::on_incomesDeleteIncomeButton_clicked() // deleting selected incomes
{
QItemSelectionModel *selectedRows = ui->incomesTableView->selectionModel();
if (selectedRows->hasSelection())
{
QModelIndexList rows = selectedRows->selectedIndexes();
database.incomesTableModel->removeRows(rows.first().row(), rows.count());
}
}
void MainWindow::on_incomesNewIncomeButton_clicked() // adding new income
{
database.incomesTableModel->insertRows(0, 1);
}
void MainWindow::on_exportIncomesPushButton_clicked() //exorting incomes data to file
{
QString filename = QFileDialog::getSaveFileName(this, tr("Export to .txt file"), tr("incomesExport"), tr("Text file (*.txt);;Excel datasheet (*.xlsx)"));
if (!filename.isEmpty() and filename.endsWith(".txt")) {
TableExporter e1("incomes", database);
try {
e1.exportToTxt(filename);
} catch (BadTableNameException &ex) {
}
} else if (!filename.isEmpty() and filename.endsWith(".xlsx")) {
TableExporter e2("incomes", database);
try {
e2.exportToExcel(filename);
} catch (BadTableNameException &ex) {
}
} else {
QMessageBox errorBox;
errorBox.setText("Couldn't export file:");
errorBox.setDetailedText("Wrong file extension selected");
errorBox.setDefaultButton(QMessageBox::Ok);
errorBox.exec();
}
}
//--EXPENSES BUTTONS
void MainWindow::on_expensesNewIncomeButton_clicked() //adding new expense
{
database.expensesTableModel->insertRows(0, 1);
}
void MainWindow::on_expensesDeleteIncomeButton_clicked() //deleting selected expenses
{
QItemSelectionModel *selectedRows = ui->expensesTableView->selectionModel();
if (selectedRows->hasSelection())
{
QModelIndexList rows = selectedRows->selectedIndexes();
database.expensesTableModel->removeRows(rows.first().row(), rows.count());
}
}
void MainWindow::on_expensesSubmitButton_clicked() //saving changes made earlier
{
database.expensesTableModel->database().transaction();
if (database.expensesTableModel->submitAll())
{
database.expensesTableModel->database().commit();
} else
{
database.expensesTableModel->database().rollback();
}
loadData();
}
void MainWindow::on_exportExpensesPushButton_clicked() //exporting expenses data to file
{
QString filename = QFileDialog::getSaveFileName(this, tr("Export to .txt file"), tr("expensesExport"), tr("Text file (*.txt);;Excel datasheet (*.xlsx)"));
if (!filename.isEmpty() and filename.endsWith(".txt")) {
TableExporter e1("expenses", database);
try {
e1.exportToTxt(filename);
} catch (BadTableNameException &ex) {
e1.printFailure("Zła nazwa tabeli!!");
}
} else if (!filename.isEmpty() and filename.endsWith(".xlsx")) {
TableExporter e2("expenses", database);
try {
e2.exportToExcel(filename);
} catch (BadTableNameException &ex) {
e2.printFailure("Zła nazwa tabeli!!");
}
} else {
QMessageBox errorBox;
errorBox.setText("Couldn't export file:");
errorBox.setDetailedText("Wrong file extension selected");
errorBox.setDefaultButton(QMessageBox::Ok);
errorBox.exec();
}
}
//--------OTHERS--------
void MainWindow::switchEnabledElements(bool state)
{
ui->personListSelectorWidget->setEnabled(state);
ui->totalIncome->setEnabled(state);
ui->totalIncomeValue->setEnabled(state);
ui->totalExpense->setEnabled(state);
ui->totalExpenseValue->setEnabled(state);
ui->tabWidget->setTabEnabled(1, state);
ui->tabWidget->setTabEnabled(2, state);
ui->status->setEnabled(state);
ui->exportSummaryPushButton->setEnabled(state);
}
void MainWindow::on_MainWindow_destroyed()
{
database.closeDatabase();
}
void MainWindow::on_actionExit_triggered()
{
QApplication::quit();
}