-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheLastStand.cpp
More file actions
368 lines (314 loc) · 11.9 KB
/
TheLastStand.cpp
File metadata and controls
368 lines (314 loc) · 11.9 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "TheLastStand.h"
#include <QListWidget>
#include <QFormLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include "RepositoryExceptions.h"
#include "DomainValidator.h"
#include <QLabel>
#include <qwidget.h>
#include <QMessageBox>
#include <sstream>
#include <iostream>
#include <fstream>
#include <string.h>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QTableView>
TheLastStand::TheLastStand( Service _service, bool check, QWidget* parent)
: QMainWindow(parent), service{_service}, memOrfile{check}
{
ui.setupUi(this);
this->init();
this->connectSignalsAndSlots();
this->populateItemsList();
}
void TheLastStand::addItemButtonHandler()
{
try {
QString name = ui.Name->text();
QString form = ui.Form->text();
QString material = ui.Material->text();
QString age = ui.Age->text();
ui.Name->clear();
ui.Form->clear();
ui.Age->clear();
ui.Material->clear();
if (fileRepoName != "" && this->memOrfile == false)
this->service.writeToFile(this->fileRepoName);
emit addItemSignal(age.toInt(), name.toStdString(), form.toStdString(), material.toStdString());
}
catch (RepositoryExceptionsInherited exception) {
QMessageBox msgBox;
msgBox.setText(exception.what());
msgBox.exec();
}
catch (ValidationExceptionInherited exception) {
QMessageBox msgBox;
msgBox.setText(exception.what());
msgBox.exec();
}
}
void TheLastStand::deleteItemButtonHandler()
{
int index = this->getSelectedIndex();
if (index < 0 || index >= this->service.getRepoService().size())
return;
Domain item = this->service.getRepoService()[index];
this->service.deleteService(item.getName());
ui.Name->clear();
ui.Form->clear();
ui.Age->clear();
ui.Material->clear();
if (fileRepoName != "" && this->memOrfile == false)
this->service.writeToFile(this->fileRepoName);
emit itemsUpdatedSignal();
}
void TheLastStand::updateItemButtonHandler()
{
QString name = ui.Name->text();
QString form = ui.Form->text();
QString material = ui.Material->text();
QString age = ui.Age->text();
this->service.updateService( name.toStdString(), form.toStdString(), material.toStdString(), age.toInt());
ui.Name->clear();
ui.Form->clear();
ui.Age->clear();
ui.Material->clear();
if (fileRepoName != "" && this->memOrfile == false)
this->service.writeToFile(this->fileRepoName);
emit itemsUpdatedSignal();
}
void TheLastStand::fileRepoSelectorButtonHandler()
{
/* QMessageBox box;
box.setText(ui.fileRepoEdit->text());*/
//box.exec();
try {
this->service.readFromFile(ui.fileRepoEdit->text().toStdString());
fileRepoName = ui.fileRepoEdit->text().toStdString();
emit itemsUpdatedSignal();
}
catch (RepositoryExceptionsInherited exception) {
QMessageBox box;
box.setText(QString::fromStdString(exception.what()));
box.exec();
}
}
void TheLastStand::htmlCsvRepoButtonHandler()
{
std::string file = ui.htmlOrCSVEdit->text().toStdString();
this->file = file;
char buffer[6];
std::size_t length = file.copy(buffer, -1, file.size() - 4);
buffer[length] = '\0';
if (strcmp(buffer, "html") == 0)
this->fileType = "html";
else
if (strcmp(buffer, ".csv") == 0)
this->fileType = "csv";
QMessageBox box;
box.setText(QString::fromStdString(this->file));
box.exec();
}
void TheLastStand::nextButtonHandler()
{
QMessageBox box;
//Domain item;
box.setText(QString::fromStdString(this->service.nextService().toString().str()));
box.exec();
}
void TheLastStand::saveButtonHandler()
{
if(ui.modeBItem->count() != 0)
ui.modeBItem->clear();
std::vector<Domain> vector = this->service.getMyVectorService();
this->service.saveService(ui.Name->text().toStdString());
for (auto& item : vector) {
QString itemInList = QString::fromStdString(item.toString().str());
QFont font{ "Verdana", 15 };
QListWidgetItem* item = new QListWidgetItem{ itemInList };
item->setFont(font);
ui.modeBItem->addItem(item);
}
}
void TheLastStand::filterButtonHandler()
{
if (ui.modeBItem->count() != 0)
ui.modeBItem->clear();
int age = ui.Age->text().toInt();
std::string material = ui.Material->text().toStdString();
QMessageBox box;
box.setText(QString::fromStdString(material));
box.exec();
std::vector<Domain> vector = this->service.getAllByMaterialAndLesserValueService(material, age);
for (auto& item : vector) {
QString itemInList = QString::fromStdString(item.toString().str());
QFont font{ "Verdana", 15 };
QListWidgetItem* item = new QListWidgetItem{ itemInList };
item->setFont(font);
ui.modeBItem->addItem(item);
}
}
void TheLastStand::undoButtonHandler()
{
this->service.undo();
emit itemsUpdatedSignal();
}
void TheLastStand::redoButtonHandler()
{
this->service.redo();
emit itemsUpdatedSignal();
}
void TheLastStand::modeAButtonHandler()
{
ui.listButton->hide();
ui.filterButton->hide();
ui.nextButton->hide();
ui.modeBItem->hide();
ui.saveButton->show();
ui.deleteButton->show();
ui.updateButton->show();
ui.addButton->show();
ui.itemsList->show();
ui.Name->show();
ui.Form->show();
ui.Age->show();
ui.Material->show();
}
void TheLastStand::modeBButtonHandler()
{
ui.saveButton->hide();
ui.deleteButton->hide();
ui.updateButton->hide();
ui.addButton->hide();
ui.itemsList->hide();
ui.Name->hide();
ui.Form->hide();
ui.Age->hide();
ui.Material->hide();
ui.listButton->show();
ui.filterButton->show();
ui.nextButton->show();
ui.modeBItem->show();
}
int TheLastStand::getSelectedIndex()
{
if (ui.itemsList->count() == 0)
return -1;
QModelIndexList index = ui.itemsList->selectionModel()->selectedIndexes();
if (index.size() == 0) {
ui.Age->clear();
ui.Form->clear();
ui.Material->clear();
ui.Name->clear();
return -1;
}
int indexAtRow = index.at(0).row();
return indexAtRow;
}
void TheLastStand::populateItemsList()
{
if (ui.itemsList->count() > 0)
ui.itemsList->clear();
//this->service.addService("f", "f", "f", 234);
for (auto& item : this->service.getRepoService())
{
QString itemInList = QString::fromStdString(item.toString().str());
QFont font{ "Verdana", 15 };
QListWidgetItem* item = new QListWidgetItem{ itemInList };
item->setFont(font);
ui.itemsList->addItem(item);
}
if (this->service.getRepoService().size() > 0)
ui.itemsList->setCurrentRow(0);
}
void TheLastStand::connectSignalsAndSlots()
{
QObject::connect(this, &TheLastStand::itemsUpdatedSignal, this, &TheLastStand::populateItemsList);
QObject::connect(ui.itemsList, &QListWidget::itemSelectionChanged, this, [this]() {this->listItemChanged(); });
//buttons
QObject::connect(ui.modeBButton, &QPushButton::clicked, this, &TheLastStand::modeBButtonHandler);
QObject::connect(ui.modeAButton, &QPushButton::clicked, this, &TheLastStand::modeAButtonHandler);
QObject::connect(ui.fileRepo, &QPushButton::clicked, this, &TheLastStand::fileRepoSelectorButtonHandler);
QObject::connect(ui.addButton, &QPushButton::clicked, this, &TheLastStand::addItemButtonHandler);
QObject::connect(ui.updateButton, &QPushButton::clicked, this, &TheLastStand::updateItemButtonHandler);
QObject::connect(ui.deleteButton, &QPushButton::clicked, this, &TheLastStand::deleteItemButtonHandler);
QObject::connect(ui.saveButton, &QPushButton::clicked, this, &TheLastStand::saveButtonHandler);
QObject::connect(ui.filterButton, &QPushButton::clicked, this, &TheLastStand::filterButtonHandler);
QObject::connect(ui.listButton, &QPushButton::clicked, this, &TheLastStand::listMyList);
QObject::connect(ui.HTML_CSVREPO, &QPushButton::clicked, this, &TheLastStand::htmlCsvRepoButtonHandler);
QObject::connect(ui.nextButton, &QPushButton::clicked, this, &TheLastStand::nextButtonHandler);
QObject::connect(ui.UndoButton, &QPushButton::clicked, this, &TheLastStand::undoButtonHandler);
QObject::connect(ui.RedoButton, &QPushButton::clicked, this, &TheLastStand::redoButtonHandler);
QObject::connect(this->undoShortcut, &QShortcut::activated, this, &TheLastStand::undoButtonHandler);
QObject::connect(this->redoShortcut, &QShortcut::activated, this, &TheLastStand::redoButtonHandler);
//connect addItem signal to addItem slot, which adds an item to the vector
QObject::connect(this, SIGNAL(addItemSignal(const int&, const string&, const string&, const string&)),
this, SLOT(addItem(const int&, const string&, const string&, const string&)));
QObject::connect(this, SIGNAL(myListUpdatedSignal(std::string& _name)), this, SLOT(saveItem(const string& _name)));
//QObject::connect(this, SIGNAL(listMyList()), this, SLOT)
QObject::connect(this, SIGNAL(itemsUpdatedSignal(const int&, const string&, const string&, const string&)),
this, SLOT(updateItem(const int& _age, const string & _name, const string & _form, const string & _material)));
}
void TheLastStand::listItemChanged()
{
int index = this->getSelectedIndex();
if (index == -1)
return;
if (index >= this->service.getRepoService().size())
return;
Domain item = this->service.getRepoService()[index];
ui.Name->setText(QString::fromStdString(item.getName()));
ui.Form->setText(QString::fromStdString(item.getForm()));
ui.Material->setText(QString::fromStdString(item.getMaterial()));
ui.Age->setText(QString::fromStdString(std::to_string(item.getAge())));
}
void TheLastStand::addItem(const int& _age, const string& _name, const string& _form, const string& _material)
{
this->service.addService(_name, _material, _form, _age);
emit itemsUpdatedSignal();
}
void TheLastStand::saveItem(const string& _name)
{
this->service.saveService(_name);
}
void TheLastStand::listMyList()
{
//PaginatedGenesTableModel* tableModel = new PaginatedGenesTableModel;
// PaginatedTableModel* tableModel = new PaginatedTableModel{this->service};
this->listDialog->show();
if (this->fileType == "csv")
this->service.writeToCSV(this->file);
else if (this->fileType == "html")
this->service.writeToHTML(file);
system(file.c_str());
}
void TheLastStand::updateItem(const int& _age, const string& _name, const string& _form, const string& _material)
{
this->service.updateService(_name, _material, _form, _age);
emit itemsUpdatedSignal();
}
void TheLastStand::init()
{
if (this->memOrfile) {
ui.fileRepoEdit->setVisible(false);
ui.fileRepo->setVisible(false);
}
this->listDialog = new QDialog;
/*QHBoxLayout* dialogLayout = new QHBoxLayout{ listDialog };
QTableView* tableView = new QTableView{ listDialog };
PaginatedTableModel* model = new PaginatedTableModel{ this->service };
tableView->resizeColumnsToContents();
tableView->setModel(model);
tableView->verticalHeader()->hide();
dialogLayout->addWidget(tableView);*/
QHBoxLayout* layout = new QHBoxLayout{ listDialog };
this->modelView = new QTableView{ listDialog };
PaginatedTableModel* tableModel = new PaginatedTableModel{ this->service };
// this->modelView.setModel(tableModel);
// this->modelView = new QTableVIew{}
//this->listDialog->setModal(true);
this->modelView->setModel(tableModel);
layout->addWidget(modelView);
}