-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstuTable.cpp
311 lines (224 loc) · 6.17 KB
/
stuTable.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
305
306
307
308
309
310
311
#include "StuTable.h"
vector<string> StuTable::split(string str, char seperator='\t')
{
//the data to split is like this
"1\tmale\t18\tguangzhou\tcomputer";
vector<string>res;
for (int pos = 0; pos != string::npos;) {
//if(find the seperator,the function will return the position,else return string::npos
pos = str.find(seperator);
//get the string between
auto s = str.substr(0, pos);
//remove to get new Str
str = string(str.c_str() + pos + 1);
//cout << s << endl;
res.push_back(s);
}
return res;
}
StuTable::StuTable(int rowNum, int colNum)
:BasicWidget(0,0,0,0),
rowNum(rowNum),
colNum(colNum),
currentPage(0),
maxPage(0),
remainNum(0)
{
prevButton = new PushButton("上一页");
nextButton = new PushButton("下一页");
firstButton = new PushButton("首页");
endButton = new PushButton("尾页");
}
StuTable::~StuTable()
{
delete prevButton;
delete nextButton;
delete firstButton;
delete endButton;
cout << "free is finish!" << endl;
}
int StuTable::getRowNum() const {
return rowNum;
}
void StuTable::setRowNum(int rowNum) {
StuTable::rowNum = rowNum;
}
int StuTable::getColNum() const {
return colNum;
}
void StuTable::setColNum(int colNum) {
StuTable::colNum = colNum;
}
void StuTable::setHeader(const string& h)
{
header = h;
//calculate the '\t' 's number
int cols = count(h.begin(), h.end(),'\t');
setColNum(cols+1);
//cout << cols << endl;
//get the height of font
int fontWidth = ::textwidth("Electrical");
//setGridWidth
setGridWidth(fontWidth);
//get the height of font
int fontHeight = ::textheight("Electrical Engineering");
//setGridHeight
setGridHeight(fontHeight);
}
int StuTable::getGridWidth() const {
return gridWidth;
}
void StuTable::setGridWidth(int gridWidth) {
StuTable::gridWidth = gridWidth;
}
int StuTable::getGridHeight() const {
return gridHeight;
}
void StuTable::setGridHeight(int gridHeight) {
StuTable::gridHeight = gridHeight;
}
void StuTable::insertData(const string& data)
{
datas.push_back(data);
//update the page
updatePage();
}
void StuTable::show()
{
drawTableGrid();
//drawData
drawTableData();
////draw button
//drawButton();
//draw header
drawHeader();
}
void StuTable::drawTableGrid()
{
//should confirm the number of rows or and cols
//fillcircle(50, 50, 100);
setlinecolor(BLACK);
//draw the row
for (size_t i = 0; i < rowNum + 1; i++) {
line(m_x, m_y + i * gridHeight, m_x + colNum * gridWidth, m_y + i * gridHeight);
}
//draw the col
for (int i = 0; i < colNum + 1; i++) {
line(m_x + i * gridWidth, m_y, m_x + i * gridWidth, m_y + rowNum * gridHeight);
}
//if maxPage==0,dont need to draw the button
if (maxPage > 0) {
drawButton();
//cout << "draw the Button" << endl;
}
}
void StuTable::drawTableData()
{
//the begin position of data
int begin = currentPage * rowNum;
//the end position of data
int end = begin + rowNum;
if (end > datas.size()) {
end = datas.size();
}
//test
//deal with every line
for (int i = 0; i < datas.size(); i++) {
vector<string>line = split(datas[i]);
//cout << "i am writing data!" << "line size():" << line[0]<< endl;
//to fill in the frid
for (int j = 0; j < line.size(); j++) {
//calculate the grid x,y
int xx = m_x + j * gridWidth;
int yy = m_y + i * gridHeight;
//cout <<"sdrfrwsdfsdfcswdefwsedfcwedfc" << line[j] << endl;
//system("pause");
//draw the data
outtextxy(xx, yy, line[j].c_str());
}
}
}
void StuTable::drawButton()
{
static bool isMove = false;
if (!isMove) {
//set the position
prevButton->move(m_x, m_h + 30);
nextButton->move(prevButton->getMX() + prevButton->getMW(), prevButton->getMY());
firstButton->move(nextButton->getMX() + nextButton->getMW(), nextButton->getMY());
endButton->move(firstButton->getMX() + firstButton->getMW(), firstButton->getMY());
//cout << nextButton->getMX() << " Y::::" << nextButton->getMY() << endl;
isMove = true;
}
//show
prevButton->show();
nextButton->show();
firstButton->show();
endButton->show();
char pageInfo[256];
sprintf(pageInfo, "第%d页/共%d页", currentPage+1, maxPage+1);
//set color
//settextcolor(BLUE);
//show how many pages
outtextxy(endButton->getMX() + endButton->getMW() + 150, endButton->getMY()+30, pageInfo);
}
void StuTable::drawHeader()
{
//setLine
setlinestyle(PS_SOLID, 3);
//set header
::rectangle(m_x, m_y - 30, m_x + m_w, m_y);
for (int i = 0; i < colNum; i++) {
line(m_x + i * gridWidth, m_y - 30, m_x + i * gridWidth, m_y);
}
setlinestyle(PS_SOLID, 2);
//split the header
auto he = split(header);
settextcolor(RED);
for (int j = 0; j < he.size();j++) {
//HDistence
int distanceH = (gridWidth - ::textwidth(he[j].c_str())) / 2;
//VDistence
int distanceV = (40 - ::textheight(he[j].c_str())) / 2;
outtextxy(m_x + j * gridWidth+distanceH, m_y - gridHeight+distanceV, he[j].c_str());
}
settextcolor(BLUE);
}
void StuTable::updatePage()
{
if (rowNum > datas.size()) {
maxPage = 0;
remainNum = rowNum;
}
else {
maxPage = datas.size() / rowNum;
remainNum = datas.size() % rowNum;
}
//cout << "rowNum:" << rowNum << endl;
}
void StuTable::event()
{
prevButton->event();
nextButton->event();
firstButton->event();
endButton->event();
if (prevButton->isClicked()) {
if (currentPage == 0) {
currentPage = 0;
}
else {
currentPage--;
}
}
if (nextButton->isClicked()) {
if (currentPage != maxPage) {
currentPage++;
}
}
if (firstButton->isClicked()) {
currentPage = 0;
}
if (endButton->isClicked()) {
currentPage = maxPage;
}
}