-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultDialog.cpp
354 lines (327 loc) · 10.4 KB
/
ResultDialog.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <wx/sizer.h>
#include <wx/grid.h>
#include <wx/dynarray.h>
#include <wx/msgdlg.h>
#include <wx/filedlg.h>
#include "Main.hpp"
#include "work.hpp"
#include "ResultDialog.hpp"
#include "ConfigDialog.hpp"
#include "CandidateDialog.hpp"
wxBEGIN_EVENT_TABLE(MyResultDialog, wxDialog)
EVT_GRID_CELL_RIGHT_CLICK(MyResultDialog::OnCellRightClick)
EVT_GRID_LABEL_RIGHT_CLICK(MyResultDialog::OnLabelRightClick)
EVT_GRID_CELL_CHANGED(MyResultDialog::OnCellChanged)
EVT_GRID_SELECT_CELL(MyResultDialog::OnSelectCell)
EVT_MENU(MENU_RESULT_DIALOG_DELETE, MyResultDialog::OnDelete)
EVT_MENU(MENU_RESULT_DIALOG_DELETE_GROUP, MyResultDialog::OnDeleteGroup)
EVT_MENU(MENU_RESULT_DIALOG_SHOW_GROUP, MyResultDialog::OnShowGroup)
EVT_MENU(MENU_RESULT_DIALOG_DELETE_ALL, MyResultDialog::OnDeleteAll)
EVT_MENU(MENU_RESULT_DIALOG_EXPORT, MyResultDialog::OnExport)
EVT_RIGHT_UP(MyResultDialog::OnRightUp)
wxEND_EVENT_TABLE()
#define ID_COL (0)
#define NAME_COL (1)
#define DEPART_COL (2)
#define ACTIVITY_COL (3)
#define ACTIVITY_DETAIL_COL (4)
#define STATUS_COL (5)
#define COL_COUNT (6)
MyResultDialog::MyResultDialog(MyFrame *pFrame, MyConfigDialog *pConfigDialog)
:wxDialog(pFrame, wxID_ANY, "抽奖结果", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
{
mpFrame = pFrame;
mpConfigDialog = pConfigDialog;
mpGrid = new wxGrid(this, wxID_ANY);
// mpGrid->SetCellHighlightPenWidth(0);
// mpGrid->EnableEditing(false);
mpGrid->CreateGrid(0, COL_COUNT, wxGrid::wxGridSelectRows);
mpGrid->SetColLabelValue(ID_COL, "工号");
mpGrid->SetColFormatNumber(ID_COL);
mpGrid->SetColLabelValue(NAME_COL, "姓名");
mpGrid->SetColLabelValue(DEPART_COL, "部门");
mpGrid->SetColLabelValue(ACTIVITY_COL, "奖项");
mpGrid->SetColLabelValue(ACTIVITY_DETAIL_COL, "奖品");
mpGrid->SetColLabelValue(STATUS_COL, "是否领取");
mpGrid->SetColFormatBool(STATUS_COL);
SetSize(600,300);
mCurrentLine = -1;
LoadFromFile("./media/result.txt");
}
MyResultDialog::~MyResultDialog(){
SaveToFile("./media/result.txt");
}
void MyResultDialog::UpdateRow(int iIndex){
if (iIndex >= mpGrid->GetNumberRows()){
return;
}
wxGridCellAttr *pAttr = new wxGridCellAttr;
pAttr->SetReadOnly();
pAttr->SetTextColour(*wxBLACK);
if (mpGrid->GetCellValue(iIndex, STATUS_COL) == "1"){
pAttr->SetTextColour(*wxLIGHT_GREY);
}
mpGrid->SetRowAttr(iIndex, pAttr);
for (int i = 0; i < COL_COUNT; i++){
mpGrid->SetReadOnly(iIndex, i);
}
if (mpGrid->GetCellValue(iIndex, NAME_COL).IsEmpty()){
mpGrid->SetCellSize(iIndex, 0, 1, COL_COUNT);
mpGrid->SetCellAlignment(iIndex, 0, wxALIGN_LEFT, wxALIGN_CENTER);
}
mpGrid->SetReadOnly(iIndex, STATUS_COL, false);
}
void MyResultDialog::OnCellRightClick(wxGridEvent &evt){
wxMenu menu;
if (mpGrid->GetCellValue(evt.GetRow(), NAME_COL).IsEmpty()){
bool bTake = false;
int i = 0;
for (i = evt.GetRow()+1; i < mpGrid->GetNumberRows(); i++){
if (mpGrid->GetCellValue(i, NAME_COL).IsEmpty()){
break;
}
if (mpGrid->GetCellValue(i, STATUS_COL) == "1"){
bTake = true;
break;
}
}
menu.Append(MENU_RESULT_DIALOG_SHOW_GROUP, "显示结果");
if (!bTake){
menu.Append(MENU_RESULT_DIALOG_DELETE_GROUP, "本组弃奖");
}
}
else{
if (mpGrid->GetCellValue(evt.GetRow(), STATUS_COL) != "1"){
menu.Append(MENU_RESULT_DIALOG_DELETE, "弃奖");
}
}
menu.Append(MENU_RESULT_DIALOG_EXPORT, "导出");
menu.Append(MENU_RESULT_DIALOG_DELETE_ALL, "清空抽奖结果");
PopupMenu(&menu);
}
void MyResultDialog::OnRightUp(wxMouseEvent &evt){
}
void MyResultDialog::OnLabelRightClick(wxGridEvent &evt){
wxMenu menu;
menu.Append(MENU_RESULT_DIALOG_EXPORT, "导出");
menu.Append(MENU_RESULT_DIALOG_DELETE_ALL, "清空抽奖结果");
PopupMenu(&menu);
}
void MyResultDialog::OnSelectCell(wxGridEvent &evt)
{
mCurrentLine = evt.GetRow();
UpdateRow(evt.GetRow());
}
void MyResultDialog::OnCellChanged(wxGridEvent &evt)
{
UpdateRow(evt.GetRow());
mpGrid->Refresh();
}
int MyResultDialog::GetCurrentLine()
{
return mCurrentLine;
}
void MyResultDialog::SaveToFile(const wxString &strFileName){
wxString ret;
for (int i = 0; i < mpGrid->GetNumberRows(); i++){
for (int j = 0; j < mpGrid->GetNumberCols(); j++){
ret += mpGrid->GetCellValue(i, j);
ret += "\t";
}
ret += "\r\n";
}
FILE *fp = fopen(strFileName.c_str(), "w");
if (NULL == fp){
return;
}
fwrite(ret.c_str(), 1, strlen(ret.c_str()), fp);
fclose(fp);
}
void MyResultDialog::LoadFromFile(const wxString &strFileName)
{
FILE *fp = fopen(strFileName.c_str(), "r");
if (NULL == fp){
return;
}
char line[1024 * 10];
int iRow = 0;
while(fgets(line, 1024 * 10, fp)){
int iLen = strlen(line);
iRow = mpGrid->GetNumberRows();
mpGrid->AppendRows();
std::string text;
int iCol = 0;
for (int i = 0; i < iLen; i++){
if (line[i] == '\t' || line[i] == '\r' || line[i] == '\n'){
if (iCol >= COL_COUNT){
break;
}
mpGrid->SetCellValue(iRow, iCol, text);
text = "";
iCol++;
}
else{
text += line[i];
}
}
UpdateRow(iRow);
iRow++;
}
if (mpGrid->GetNumberRows() != 0){
mpGrid->SetGridCursor(mpGrid->GetNumberRows() - 1, 0);
mpGrid->SelectRow(mpGrid->GetNumberRows() - 1);
}
fclose(fp);
}
void MyResultDialog::OnExport(wxCommandEvent &evt)
{
wxFileDialog dlg(this, _("导出到文件"), "", "result.txt",
"txt files (*.txt)|*.txt",
wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
if (dlg.ShowModal() == wxID_CANCEL){
return;
}
SaveToFile(dlg.GetPath());
}
void MyResultDialog::OnDelete(wxCommandEvent &evt)
{
int iCur = GetCurrentLine();
if (iCur < 1){
return;
}
mpConfigDialog->UpdateActivity(mpGrid->GetCellValue(iCur, ACTIVITY_COL), mpGrid->GetCellValue(iCur, ACTIVITY_DETAIL_COL), -1);
mpGrid->DeleteRows(iCur, 1);
for (int i = iCur-1; i >= 0; i--){
if (mpGrid->GetCellValue(i, NAME_COL).IsEmpty()){
// this is the title line.
wxString strCount = mpGrid->GetCellValue(i, DEPART_COL);
long iCount = 0;
strCount.ToLong(&iCount);
iCount--;
if (iCount <= 0){
mpGrid->DeleteRows(i, 1);
return;
}
wxString group = wxString::Format("%s (%s)共%d人", mpGrid->GetCellValue(i, ACTIVITY_COL), mpGrid->GetCellValue(i, ACTIVITY_DETAIL_COL), iCount);
mpGrid->SetCellValue(i, ID_COL, group);
mpGrid->SetCellValue(i, DEPART_COL, wxString::Format("%d", iCount));
break;
}
}
}
void MyResultDialog::OnDeleteGroup(wxCommandEvent &evt)
{
int iCur = GetCurrentLine();
if (iCur < 0){
return;
}
int answer = wxMessageBox("确认本组弃奖?","确认",wxYES_NO | wxCANCEL, this);
if (answer != wxYES){
return;
}
if (mpGrid->GetCellValue(iCur, NAME_COL).IsEmpty()){
wxString strCount = mpGrid->GetCellValue(iCur, DEPART_COL);
long iCount = 0;
strCount.ToLong(&iCount);
mpConfigDialog->UpdateActivity(mpGrid->GetCellValue(iCur, ACTIVITY_COL), mpGrid->GetCellValue(iCur, ACTIVITY_DETAIL_COL), -iCount);
mpGrid->DeleteRows(iCur, iCount+1);
}
}
void MyResultDialog::OnDeleteAll(wxCommandEvent &evt)
{
int answer = wxMessageBox("确认清空抽奖结果?","确认",wxYES_NO | wxCANCEL, this);
if (answer != wxYES){
return;
}
mpConfigDialog->UpdateAllActivity();
mpGrid->DeleteRows(0, mpGrid->GetNumberRows());
}
void MyResultDialog::OnShowGroup(wxCommandEvent &evt)
{
int iCur = GetCurrentLine();
if (iCur < 0){
return;
}
if (!mpGrid->GetCellValue(iCur, NAME_COL).IsEmpty()){
return;
}
// call ShowResult
wxString strCount = mpGrid->GetCellValue(iCur, DEPART_COL);
long iCount = 0;
strCount.ToLong(&iCount);
wxString strTitle = wxString::Format("%s %s 共%d人",
mpGrid->GetCellValue(iCur, ACTIVITY_COL),
mpGrid->GetCellValue(iCur, ACTIVITY_DETAIL_COL), iCount);
std::vector<MyCandidate *> candidateList;
for (int i = 0; i < iCount; i++){
MyCandidate *pRet = new MyCandidate;
pRet->mId = mpGrid->GetCellValue(iCur + 1 + i, ID_COL);
pRet->mName = mpGrid->GetCellValue(iCur+1 +i, NAME_COL);
pRet->mDepart = mpGrid->GetCellValue(iCur + 1 + i, DEPART_COL);
candidateList.push_back(pRet);
}
mpFrame->ShowResult(strTitle, candidateList);
}
void MyResultDialog::AddResults(MyActivity *pAct, std::vector<MyCandidate *> &resultList)
{
if (resultList.empty() || NULL == pAct){
return;
}
// todo:[email protected]
// 如果有多个子奖项,依次添加多个。
if (pAct->mSubAct.size() > 1){
int iOffset = 0;
for (int i = 0; i < pAct->mSubAct.size(); i++){
std::vector<MyCandidate *> result;
for (int j = 0; j < pAct->mSubAct[i]->mCount; j++){
result.push_back(resultList[iOffset++]);
}
AddResults(pAct->mSubAct[i],result);
}
return;
}
// todo:[email protected]
// find the right position to add the new result
int iIndex = mpGrid->GetNumberRows();
mpGrid->AppendRows(1);
wxString group = wxString::Format("%s (%s)共%d人", pAct->mName, pAct->mDesc, resultList.size());
mpGrid->SetCellValue(iIndex, ID_COL, group);
mpGrid->SetCellValue(iIndex, DEPART_COL, wxString::Format("%d", resultList.size()));
mpGrid->SetCellValue(iIndex, ACTIVITY_COL, pAct->mName);
mpGrid->SetCellValue(iIndex, ACTIVITY_DETAIL_COL, pAct->mDesc);
UpdateRow(iIndex);
for (int i = 0; i < resultList.size(); i++){
MyCandidate *pCan = resultList[i];
if (NULL == pCan){
continue;
}
iIndex = mpGrid->GetNumberRows();
mpGrid->AppendRows();
mpGrid->SetCellValue(iIndex, ID_COL,pCan->mId);
mpGrid->SetCellValue(iIndex, NAME_COL, pCan->mName);
mpGrid->SetCellValue(iIndex, DEPART_COL, pCan->mDepart);
mpGrid->SetCellValue(iIndex, ACTIVITY_COL, pAct->mName);
mpGrid->SetCellValue(iIndex, ACTIVITY_DETAIL_COL, pAct->mDesc);
mpGrid->SetCellValue(iIndex, STATUS_COL, "0");
UpdateRow(iIndex+i);
}
if (IsShown()){
Refresh();
}
SaveToFile("./media/result.txt");
}
void MyResultDialog::GetResultSet(const wxString &strPool, std::set<wxString> &rets)
{
rets.empty();
for (int i = 0; i < mpGrid->GetNumberRows(); i++){
// this is a group title and not in current strPool
if (mpGrid->GetCellValue(i, NAME_COL).IsEmpty() && (!mpConfigDialog->IsPool(mpGrid->GetCellValue(i, ACTIVITY_COL), mpGrid->GetCellValue(i, ACTIVITY_DETAIL_COL), strPool))){
wxString strCount = mpGrid->GetCellValue(i, DEPART_COL);
long iCount = 0;
strCount.ToLong(&iCount);
i += iCount;
continue;
}
rets.insert(mpGrid->GetCellValue(i, ID_COL));
}
}