-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewschemedialog.cpp
214 lines (188 loc) · 7.34 KB
/
newschemedialog.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
#include "newschemedialog.h"
#include "BLL/SchemeExporter/schemeexporter.h"
#include "UI/Common/spinner.h"
#include "UI/ErrorUi/errorui.h"
#include "UI/SchemesPage/innerexamwidget.h"
#include "nsclasspickerui.h"
#include "nsexaminfoui.h"
#include "nshallpickerui.h"
#include "nspreviewui.h"
#include "ui_newschemedialog.h"
#include <QDesktopServices>
#include <QUrl>
#include <QtConcurrent/QtConcurrentRun>
namespace ikoOSKAR {
namespace UI {
NewSchemeDialog::NewSchemeDialog(QWidget* parent)
: QDialog(parent)
, ui(new Ui::NewSchemeDialog)
, nav(new Common::TwoButtonNav)
, bll(new BLL::SchemeGenerator)
, authenticator(BLL::Authenticator::getInstance())
{
ui->setupUi(this);
ui->verticalLayout->addWidget(nav);
connect(bll, &BLL::SchemeGenerator::error, this, &NewSchemeDialog::handleError);
connect(nav->btnPrev, &QPushButton::clicked, this, &NewSchemeDialog::prevPage);
connect(nav->btnNext, &QPushButton::clicked, this, &NewSchemeDialog::nextPage);
nav->btnPrev->hide();
page = EXAM_INFO;
auto examInfo = new NSExamInfoUi();
ui->root->addWidget(examInfo);
}
NewSchemeDialog::~NewSchemeDialog()
{
delete ui;
delete bll;
delete nav;
}
void NewSchemeDialog::prevPage()
{
switch (page) {
case EXAM_INFO: {
// Illegal state, should never happen
return;
}
case CLASS_PICKER: {
nav->btnPrev->hide();
break;
}
case HALL_PICKER: {
break;
}
case PREVIEW: {
nav->btnNext->setText(" İleri ");
nav->btnNext->setIcon(QIcon(":/arrow-right.png"));
break;
}
case SPINNER: {
// Illegal state, there's no going back from the spinner page
return;
}
case RESULTS: {
// btnBack on the results subpage has a new purpose:
// opening the folder containing the newly created exam schemes
auto path = Shared::Scheme::path(examName, examDate);
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
return; // Don't decrease the page index
}
}
auto currentWidget = ui->root->widget(ui->root->currentIndex());
ui->root->removeWidget(currentWidget);
currentWidget->deleteLater();
page = (PageState)((int)page - 1);
}
void NewSchemeDialog::nextPage()
{
switch (page) {
case EXAM_INFO: {
auto examInfo = (NSExamInfoUi*)ui->root->currentWidget();
examName = examInfo->getExamName().simplified();
examDate = examInfo->getExamDate();
bll->setDate(examDate);
bool success = bll->setName(examName);
if (!success) {
// Necessary error mesage(s) have already been shown
return;
}
nav->btnPrev->show();
auto classPicker = new NSClassPickerUi();
ui->root->addWidget(classPicker);
ui->root->setCurrentIndex(ui->root->currentIndex() + 1);
break;
}
case CLASS_PICKER: {
auto classPicker = (NSClassPickerUi*)ui->root->currentWidget();
auto attendingClassNames = classPicker->getAttendingClasses();
bool success = bll->setAttendingClasses(attendingClassNames);
if (!success) {
// Necessary error mesage(s) have already been shown
return;
}
auto hallPicker = new NSHallPickerUi();
ui->root->addWidget(hallPicker);
ui->root->setCurrentIndex(ui->root->currentIndex() + 1);
break;
}
case HALL_PICKER: {
auto hallPicker = (NSHallPickerUi*)ui->root->currentWidget();
auto selectedHallNames = hallPicker->getSelectedHalls();
bool success = bll->setExamHalls(selectedHallNames);
if (!success) {
// Necessary error mesage(s) have already been shown
return;
}
auto preview = new NSPreviewUi(bll->preview());
ui->root->addWidget(preview);
ui->root->setCurrentIndex(ui->root->currentIndex() + 1);
nav->btnNext->setText(" Oluştur ");
nav->btnNext->setIcon(QIcon(":/check.png"));
break;
}
case PREVIEW: {
auto spinner = new Common::Spinner();
spinner->setTitle("Sınav düzeni oluşturuluyor. Lütfen bekleyiniz.");
spinner->start();
ui->root->addWidget(spinner);
ui->root->setCurrentIndex(ui->root->currentIndex() + 1);
nav->hide();
// generate and export scheme in another thread
QtConcurrent::run([&]() {
return bll->generate();
}).then([&](Shared::Scheme scheme) {
BLL::SchemeExporter exporter(scheme);
connect(&exporter, &BLL::SchemeExporter::exportFinished, this, &UI::NewSchemeDialog::onExportFinished);
exporter.exportAll();
}).then([&]() {
if (authenticator->isDemo()) {
authenticator->decreaseDemoRemainingsByOne();
}
});
break;
}
case SPINNER: {
// the scheme has been generated and exported as two xlsx files
auto spinner = (Common::Spinner*)ui->root->widget(ui->root->currentIndex());
ui->root->removeWidget(spinner);
spinner->stop();
spinner->deleteLater();
auto results = new InnerExamWidget(examName, examDate.toString("dd/MM/yyyy"), pathClassLists, pathHallLayouts);
ui->root->addWidget(results);
ui->root->setCurrentIndex(ui->root->currentIndex() + 1);
// Assign the nav buttons new purposes, give them bigger new icons and make them slightly taller
// After this reassignment, there'll be no page changes.
// btnPrev becomes btnOpenExamFolder
nav->btnPrev->setIcon(QIcon(":/folder.png"));
nav->btnPrev->setText(" Bu sınava ait klasörü aç ");
nav->btnPrev->setIconSize({ 32, 32 });
nav->btnPrev->setMinimumHeight(50);
// btnNext becomes btnClose (refer to the 'case: RESULTS' part below)
nav->btnNext->setIcon(QIcon(":/home.png"));
nav->btnNext->setText(" Ana sayfaya dön ");
nav->btnNext->setIconSize({ 32, 32 });
nav->btnNext->setMinimumHeight(50);
nav->btnNext->setLayoutDirection(Qt::LeftToRight);
nav->setMaximumHeight(50);
nav->show();
break;
}
case RESULTS: {
// Close the NewSchemeDialog with the accepted signal
accept();
return;
}
}
page = (PageState)((int)page + 1);
}
void NewSchemeDialog::handleError(const QString& errorMessage)
{
ErrorUi("HATA").displayMessage(errorMessage);
}
void NewSchemeDialog::onExportFinished(const QString& pathClassLists, const QString& pathHallLayouts)
{
this->pathClassLists = pathClassLists;
this->pathHallLayouts = pathHallLayouts;
nextPage(); // spinner -> results
}
} // namespace UI
} // namespace ikoOSKAR