-
Notifications
You must be signed in to change notification settings - Fork 0
/
pages.cpp
207 lines (170 loc) · 5.94 KB
/
pages.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
#include "pages.h"
#include "func.h"
#define SET_GREEN_BG setStyleSheet("background-color: rgb(127, 255, 127);");
#define SET_RED_BG setStyleSheet("background-color: rgb(255, 127, 127);");
#define SET_BLUE_BG setStyleSheet("background-color: rgb(127, 127, 255);");
#define SET_FLOAT_QREGEXP(labelName) \
QRegExp floatRx("^([0-9]+)[.]([0-9]+)&"); \
(labelName)->setValidator(new QRegExpValidator(floatRx));
ConsumePage::ConsumePage(QWidget *parent) : QWidget(parent)
{
// TODO:
moneyLabel=new QLabel(tr("Consume Balance: "));
moneyEdit=new QLineEdit;
tipLabel=new QLabel;
confirmButton=new QPushButton(tr("Consume Now"));
confirmButton->setEnabled(false);
SET_FLOAT_QREGEXP(moneyEdit);
QHBoxLayout *enterLayout=new QHBoxLayout;
enterLayout->addWidget(moneyLabel);
enterLayout->addWidget(moneyEdit);
QVBoxLayout *mainLayout=new QVBoxLayout;
mainLayout->addLayout(enterLayout);
mainLayout->addWidget(tipLabel);
mainLayout->addWidget(confirmButton);
setLayout(mainLayout);
connect( confirmButton, SIGNAL(clicked()), this, SLOT(consume()) );
}
// TODO:
int ConsumePage::consume()
{
/**
* return 1 means no enough money
*/
User* toConsume=userPtrVec.at(contentUserIndex);
int result=toConsume->consume( moneyEdit->text().toDouble() );
if (result!=0) {
tipLabel->setText(tr("Consume failed!<br>Check if has enough money."));
tipLabel->SET_RED_BG;
return 1;
}
emit consumeFinished();
tipLabel->setText(tr("Consume successfully."));
tipLabel->SET_GREEN_BG;
return 0;
}
ChargePage::ChargePage(QWidget *parent) : QWidget(parent)
{
moneyLabel=new QLabel(tr("Charge Balance: "));
moneyEdit=new QLineEdit;
tipLabel=new QLabel;
confirmButton=new QPushButton(tr("Charge Now"));
confirmButton->setEnabled(false);
SET_FLOAT_QREGEXP(moneyEdit);
QHBoxLayout *enterLayout=new QHBoxLayout;
enterLayout->addWidget(moneyLabel);
enterLayout->addWidget(moneyEdit);
QVBoxLayout *mainLayout=new QVBoxLayout;
mainLayout->addLayout(enterLayout);
mainLayout->addWidget(tipLabel);
mainLayout->addWidget(confirmButton);
setLayout(mainLayout);
connect( confirmButton, SIGNAL(clicked()), this, SLOT(charge()) );
}
// TODO:
int ChargePage::charge()
{
User* toCharge=userPtrVec.at(contentUserIndex);
int result=toCharge->charge( moneyEdit->text().toDouble() );
if (result!=0) {
tipLabel->setText(tr("Charge failed!<br>Check if it has enough free disk space."));
tipLabel->SET_RED_BG;
return -1;
}
emit chargeFinished();
tipLabel->setText(tr("Charge successfully."));
tipLabel->SET_GREEN_BG;
return 0;
}
AddUserPage::AddUserPage(QWidget *parent) : QWidget(parent)
{
idTitleLabel=new QLabel(tr("ID Number:"));
idEdit=new QLineEdit;
nameTitleLabel=new QLabel(tr("User Name:"));
nameEdit=new QLineEdit;
moneyTitleLabel=new QLabel(tr("Primary Balance:"));
moneyEdit=new QLineEdit;
tipLabel=new QLabel;
confirmButton=new QPushButton(tr("Add This User"));
QRegExp intRx("^[0-9]{1,5}&");
idEdit->setValidator(new QRegExpValidator(intRx, this));
SET_FLOAT_QREGEXP(moneyEdit);
QGridLayout *gridLayout=new QGridLayout;
gridLayout->addWidget(idTitleLabel, 0, 0);
gridLayout->addWidget(idEdit, 0, 1);
gridLayout->addWidget(nameTitleLabel, 1, 0);
gridLayout->addWidget(nameEdit, 1, 1);
gridLayout->addWidget(moneyTitleLabel, 2, 0);
gridLayout->addWidget(moneyEdit, 2, 1);
QVBoxLayout *mainLayout=new QVBoxLayout;
mainLayout->addLayout(gridLayout);
mainLayout->addWidget(tipLabel);
mainLayout->addWidget(confirmButton);
setLayout(mainLayout);
connect( confirmButton, SIGNAL(clicked()), this, SLOT(addIt()) );
}
int AddUserPage::addIt()
{
// FIXME:
unsigned int userNum=(unsigned int)(idEdit->text().toInt());
int result=addUser( userNum, nameEdit->text().toStdString(), moneyEdit->text().toDouble() );
if (result==1) {
tipLabel->setText(tr("The user number is already exists!"));
tipLabel->SET_BLUE_BG
return -1;
}
if (result<0) {
tipLabel->setText(tr("Add user failed!"));
return -1;
}
// Return 0 means successfully.
if (result==0) {
emit addFinished( userNum, nameEdit->text(), moneyEdit->text().toDouble() );
tipLabel->setText(tr("Add user successfully!"));
tipLabel->SET_GREEN_BG;
}
return result;
}
DeleteUserPage::DeleteUserPage(QWidget *parent) : QWidget(parent)
{
alertLabel=new QLabel(tr("<font color=red>Are you sure to delete this user's informations?<br>User informations can not be recovered if you deleted them.<font>"));
tipLabel=new QLabel();
confirmCheckBox=new QCheckBox(tr("Confirm to Delete the User."));
confirmButton=new QPushButton(tr("Delete This User"));
confirmButton->setEnabled(false);
QVBoxLayout *mainLayout=new QVBoxLayout;
mainLayout->addWidget(alertLabel);
mainLayout->addWidget(confirmCheckBox);
mainLayout->addWidget(confirmButton);
mainLayout->addWidget(tipLabel);
connect( confirmCheckBox, SIGNAL(stateChanged(int)), this, SLOT(confirmed(int)) );
connect( confirmButton, SIGNAL(clicked()), this, SLOT(deleteIt()) );
setLayout(mainLayout);
}
void DeleteUserPage::confirmed(int state)
{
if (state!=0) {
qDebug() << "Cheched.";
if (ifSelectedContentUser) {
confirmButton->setEnabled(true);
}
} else {
qDebug() << "Not Checked.";
confirmButton->setEnabled(false);
}
}
// TODO:
void DeleteUserPage::deleteIt()
{
int result=1;
if (ifSelectedContentUser) {
result=deleteUser(contentUserIndex);
}
if (result!=0) {
tipLabel->setText(tr("Delete failed. Please check your disk space."));
tipLabel->SET_RED_BG;
}
emit deleteFinished();
tipLabel->setText(tr("Delete successfully!"));
tipLabel->SET_GREEN_BG;
}