-
Notifications
You must be signed in to change notification settings - Fork 0
/
transakceform.cpp
176 lines (153 loc) · 5.13 KB
/
transakceform.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
#include "transakceform.h"
#include "ui_transakceform.h"
#include "transakce.h"
#include "sqlI.h"
#include <QComboBox>
#include <QMessageBox>
void TransakceForm::fillForm()
{
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setRowCount(0);
ui->tableWidget->setColumnWidth(0,150);
ui->tableWidget->setColumnWidth(1,100);
ui->tableWidget->setColumnWidth(2,250);
QStringList labels; labels << tr("Konto") << tr("Částka") << tr("Poznámka");
ui->tableWidget->setHorizontalHeaderLabels(labels);
connect(ui->spnAmount,SIGNAL(valueChanged(int)),this,SLOT(checkAmount()));
if(addingNew){
ui->dateEdit->setDate(QDate::currentDate());
} else {
ui->dateEdit->setDate(transakce->getDatum());
ui->spnAmount->setValue(transakce->getSum());
QList<Transakce::Rozpis> r=transakce->getRozpis();
QList<Transakce::Rozpis>::const_iterator i;
for(i=r.constBegin(); i != r.constEnd(); ++i){
addRow((*i).konto,(*i).amount,(*i).notice);
}
}
checkAmount();
}
void TransakceForm::addRow()
{
addRow(QString(),0,QString(""));
}
void TransakceForm::addRow(QString _konto,int _amount, QString _notice)
{
QComboBox *konto=new QComboBox(this);
QStringList lstKonta=db->getTransakceAccounts();
//lstKonta << "Jedno konto" << "druhe konto" << "dalsi konto";
konto->addItems(lstKonta);
konto->setEditable(true);
if(!_konto.isNull()) //konto->addItem(_konto);
konto->setCurrentText(_konto);
QSpinBox *amount=new QSpinBox(this);
amount->setSuffix(tr(" Kč"));
amount->setMinimum(-999999);
amount->setMaximum(999999);
amount->setValue(_amount);
connect(amount,SIGNAL(valueChanged(int)),this,SLOT(checkAmount()));
QComboBox *notice=new QComboBox(this);
QStringList lstNotice=db->getTransakceNotices();
//lstNotice << "Nájem" << "Zálohy" << "Jistota";
notice->addItems(lstNotice);
notice->setEditable(true);
notice->setCurrentText(_notice);
int newLine=ui->tableWidget->rowCount();
ui->tableWidget->insertRow(newLine);
ui->tableWidget->setCellWidget(newLine,0,konto);
ui->tableWidget->setCellWidget(newLine,1,amount);
ui->tableWidget->setCellWidget(newLine,2,notice);
}
void TransakceForm::removeRow()
{
int toRemove = ui->tableWidget->currentRow();
ui->tableWidget->removeRow(toRemove);
}
void TransakceForm::save()
{
QList<Transakce::Rozpis> rozpis;
int rows = ui->tableWidget->rowCount();
for(int i=0; i<rows;i++){
QComboBox *konto = (QComboBox *) ui->tableWidget->cellWidget(i,0);
QSpinBox *amount = (QSpinBox *) ui->tableWidget->cellWidget(i,1);
QComboBox *notice = (QComboBox *) ui->tableWidget->cellWidget(i,2);
rozpis << Transakce::Rozpis(konto->currentText(),
amount->value(),
notice->currentText());
}
if(addingNew){
transakce = Transakce::createTransakce(db,
ui->dateEdit->date(),
ui->spnAmount->value(),
rozpis);
ownTransakce = true;
}else{
transakce->updDatum(ui->dateEdit->date());
transakce->updSum(ui->spnAmount->value());
transakce->updRozpis(rozpis);
transakce->update();
}
}
TransakceForm::TransakceForm(sqlI *_db, QWidget *parent) :
db(_db),
QDialog(parent),
ui(new Ui::TransakceForm),addingNew(true),ownTransakce(false)
{
ui->setupUi(this);
setWindowTitle(tr("Add transaction"));
ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
ui->buttonBox->addButton(tr("Insert"),QDialogButtonBox::AcceptRole);
fillForm();
}
TransakceForm::TransakceForm(sqlI *_db, Transakce *t, QWidget *parent) :
db(_db),transakce(t),
QDialog(parent),
ui(new Ui::TransakceForm),addingNew(false),ownTransakce(true)
{
ui->setupUi(this);
setWindowTitle(tr("Edit transaction"));
ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Save);
fillForm();
}
TransakceForm::~TransakceForm()
{
delete ui;
if(ownTransakce) delete transakce;
}
void TransakceForm::on_btnAdd_clicked()
{
addRow();
checkAmount();
}
bool TransakceForm::checkAmount()
{
int rows = ui->tableWidget->rowCount();
int sum = 0;
for(int i=0; i<rows;i++){
QSpinBox *spin = (QSpinBox *)ui->tableWidget->cellWidget(i,1);
sum += spin->value();
}
if(sum == ui->spnAmount->value()){
ui->spnAmount->setStyleSheet("");
return true;
}else{
ui->spnAmount->setStyleSheet("background: #f88;");
return false;
}
}
void TransakceForm::on_btnRemove_clicked()
{
removeRow();
checkAmount();
}
void TransakceForm::on_buttonBox_clicked(QAbstractButton *button)
{
if(ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole){
if(!checkAmount()){
QMessageBox::critical(this,tr("Špatně vyplněný formulář!"),tr("Neodpovídá součet transakce. Prosím, opravte hodnoty. "));
return;
}
save();
accept();
}
}