-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistoryviewer.cpp
More file actions
173 lines (163 loc) · 5.11 KB
/
historyviewer.cpp
File metadata and controls
173 lines (163 loc) · 5.11 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
#include "historyviewer.h"
#include <QSqlRelationalDelegate>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QSqlQuery>
#include <QMessageBox>
const QString and_word = " AND ";
const QString last_month_only = "date(TIMESTAMP) > date('now','-1 month')";
const QString strPos = "CHANGE > 0";
const QString strNeg = "CHANGE < 0";
const int ADD_ONLY = 0;
const int CONSUME_ONLY = 1;
const int ADD_AND_CONSUME = 2;
HistoryViewer::HistoryViewer(QWidget *parent, QString item_name): QDialog(parent)
{
specific_item_name = item_name;
add_consume_status = ADD_AND_CONSUME;
show_this_month_only = false;
setWindowTitle(tr("History"));
initialzeLayout();
initialzeModel();
}
void HistoryViewer::initialzeModel()
{
model = new QSqlRelationalTableModel(this);
model->setTable("RECORDS");
model->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit);
model->setRelation(1, QSqlRelation("ITEMS", "ID", "NAME"));
model->setHeaderData(0, Qt::Horizontal, tr("Time"));
model->setHeaderData(1, Qt::Horizontal, tr("Item"));
model->setHeaderData(2, Qt::Horizontal, tr("Changed number"));
model->setHeaderData(3, Qt::Horizontal, tr("Remaining number"));
updateFilter();
model->select();
view->setModel(model);
}
void HistoryViewer::initialzeLayout()
{
cbox = new QComboBox(this);
cbox->insertItem(ADD_ONLY, tr("Add only"));
cbox->insertItem(CONSUME_ONLY, tr("Consume only"));
cbox->insertItem(ADD_AND_CONSUME, tr("All"));
cbox->setCurrentIndex(ADD_AND_CONSUME);
connect(cbox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &HistoryViewer::commitTypeToShow);
QPushButton *btn = new QPushButton(tr("Commit"), this);
connect(btn, &QPushButton::pressed, this, &HistoryViewer::commitName);
QPushButton *btn2 = new QPushButton(tr("Summary"), this);
connect(btn2, &QPushButton::pressed, this, &HistoryViewer::showSummary);
name_input = new QLineEdit(specific_item_name, this);
QCheckBox *btn_month_only = new QCheckBox(tr("Within a month only"), this);
connect(btn_month_only, &QCheckBox::stateChanged, this, &HistoryViewer::showThisMonthOnly);
view = new QTableView(this);
view->setItemDelegate(new QSqlRelationalDelegate(this));
QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *l1 = new QHBoxLayout(this);
QHBoxLayout *l2 = new QHBoxLayout(this);
QLabel *label1 = new QLabel(tr("Name:"), this);
l1->addWidget(label1);
l1->addWidget(name_input);
l1->addWidget(btn);
layout->addLayout(l1);
l2->addWidget(btn_month_only);
l2->addWidget(cbox);
l2->addWidget(btn2);
layout->addLayout(l2);
layout->addWidget(view);
setLayout(layout);
}
QString &appendFilter(QString &in, const QString &filter)
{
if(in == "")
in += filter;
else
in = in+and_word+filter;
return in;
}
void HistoryViewer::updateFilter()
{
QString filter = "";
if(specific_item_name != "") {
appendFilter(filter, QString("NAME = '%1'").arg(specific_item_name));
}
if(add_consume_status == ADD_ONLY) {
appendFilter(filter, strPos);
} else if(add_consume_status == CONSUME_ONLY) {
appendFilter(filter, strNeg);
}
if(show_this_month_only)
appendFilter(filter, last_month_only);
qDebug() << filter;
model->setFilter(filter);
}
void HistoryViewer::showThisMonthOnly(int state)
{
if(state == Qt::Checked)
show_this_month_only = true;
else
show_this_month_only = false;
updateFilter();
}
void HistoryViewer::showSummary()
{
int add = 0, consume = 0;
QString selector = "SELECT SUM(CHANGE) FROM RECORDS WHERE ";
if(specific_item_name != "") {
QDialog *dialog = new QDialog(this);
QString name = QString("ID = (SELECT ID FROM ITEMS WHERE NAME = '%1')").arg(specific_item_name);
QString cond = name;
dialog->setWindowTitle(tr("Summary of %1").arg(specific_item_name));
QSqlQuery query;
appendFilter(cond, strPos);
if(show_this_month_only)
appendFilter(cond, last_month_only);
QString tmp = selector+cond;
qDebug() << tmp;
query.prepare(tmp);
query.exec();
if(query.next()) {
add = query.value(0).toInt();
}
query.finish();
cond = name;
appendFilter(cond, strNeg);
if(show_this_month_only)
appendFilter(cond, last_month_only);
query.prepare(selector+cond);
query.exec();
if(query.next()) {
consume = -query.value(0).toInt();
}
QLabel *timeRange = new QLabel(this);
if(show_this_month_only) {
timeRange->setText(tr("Summary Within a month"));
} else {
timeRange->setText(tr("Summary of all times"));
}
QLabel *sumAdd = new QLabel(tr("Summary of addition: %1").arg(add), this);
QLabel *sumCon = new QLabel(tr("Summary of Consumption: %1").arg(consume), this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(timeRange);
layout->addWidget(sumAdd);
layout->addWidget(sumCon);
dialog->setLayout(layout);
dialog->exec();
} else {
QMessageBox::information(NULL, tr("Invalid Request"), tr("You haven't selected an item yet!"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}
}
void HistoryViewer::commitTypeToShow(int index)
{
add_consume_status = index;
qDebug() << index;
updateFilter();
}
void HistoryViewer::commitName()
{
specific_item_name = name_input->text();
updateFilter();
}