-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainWin.cpp
238 lines (201 loc) · 5.82 KB
/
mainWin.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
#include "mainWin.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QListWidget>
#include <QLabel>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include "domain.h"
#include "controller.h"
#include "update.h"
TravelAgencyGUI::TravelAgencyGUI(shared_ptr<Controller> ctrl, QWidget *parent) :
QWidget(parent), ui(new Ui::TravelAgencyGUIClass)
{
this->ctrl = ctrl;
initGUI();
// ui->setupUi(this);
reload();
}
void TravelAgencyGUI::initGUI()
{
//
//General layout of the window
//
QHBoxLayout* layout = new QHBoxLayout(this);
//
// Prepare left side components
//
QWidget* leftWidget = new QWidget();
QVBoxLayout* lLeft = new QVBoxLayout(leftWidget);
QLabel* lbl = new QLabel("List of offers");
lLeft->addWidget(lbl);
offerList = new QListWidget();
QObject::connect(offerList, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
lLeft->addWidget(offerList);
QPushButton* btnSortPrice = new QPushButton("Sort by Price");
QObject::connect(btnSortPrice, SIGNAL(clicked()), this, SLOT(sortPrice()));
lLeft->addWidget(btnSortPrice);
QPushButton* btnSortDest = new QPushButton("Sort by Destination");
QObject::connect(btnSortDest, SIGNAL(clicked()), this, SLOT(sortDest()));
lLeft->addWidget(btnSortDest);
QPushButton* btnSortType = new QPushButton("Sort by Type");
QObject::connect(btnSortType, SIGNAL(clicked()), this, SLOT(sortType()));
lLeft->addWidget(btnSortType);
//
// Prepare right side components
//
QWidget* rightWidget = new QWidget();
QVBoxLayout* lRight = new QVBoxLayout(rightWidget);
lbl = new QLabel("Destination");
lRight->addWidget(lbl);
txtDestination = new QLineEdit();
lRight->addWidget(txtDestination);
lbl = new QLabel("Type");
lRight->addWidget(lbl);
txtType = new QLineEdit();
lRight->addWidget(txtType);
lbl = new QLabel("Price");
lRight->addWidget(lbl);
txtPrice = new QLineEdit();
lRight->addWidget(txtPrice);
QPushButton* btnAdd = new QPushButton("Add");
QObject::connect(btnAdd, SIGNAL(clicked()), this, SLOT(addOffer()));
QObject::connect(btnAdd, SIGNAL(clicked()), this, SLOT(reload()));
lRight->addWidget(btnAdd);
QPushButton* btnUpdate = new QPushButton("Update");
QObject::connect(btnUpdate, SIGNAL(clicked()), this, SLOT(updateOffer()));
lRight->addWidget(btnUpdate);
QPushButton* btnRemove = new QPushButton("Remove");
QObject::connect(btnRemove, SIGNAL(clicked()), this, SLOT(removeOffer()));
lRight->addWidget(btnRemove);
QPushButton* btnFilterType = new QPushButton("Filter");
QObject::connect(btnFilterType, SIGNAL(clicked()), this, SLOT(filter()));
lRight->addWidget(btnFilterType);
QPushButton* btnExit = new QPushButton("Exit");
QObject::connect(btnExit, SIGNAL(clicked()), this, SLOT(close()));
lRight->addWidget(btnExit);
layout->addWidget(leftWidget);
layout->addWidget(rightWidget);
}
void TravelAgencyGUI::addOffer()
{
//Offer* of = new Offer(txtDestination->text().toStdString(), txtType->text().toStdString(), txtPrice->text().toInt());
try
{
ctrl->addOffer(txtDestination->text().toStdString(), txtType->text().toStdString(), txtPrice->text().toFloat());
}
catch (MyException ex)
{
QMessageBox::information(this, "Can't add offer", QString::fromStdString(ex.what()));
}
}
int TravelAgencyGUI::getSelectedIndex()
{
QModelIndexList indexes = offerList->selectionModel()->selectedIndexes();
if (indexes.size() == 0)
{
return -1;
}
return indexes.at(0).row();
}
void TravelAgencyGUI::reload(vector<Offer> offers)
{
vector<Offer> vectorOffers;
offerList->clear();
if (offers.size() != 0){
vectorOffers = offers;
}
else
{
vectorOffers = ctrl->getOffers();
}
for (int i = 0; i < ctrl->getSizeof(); i++)
{
Offer of = vectorOffers.at(i);
offerList->addItem(QString::fromStdString((of.getDest() + " : " + of.getType() + " : " + to_string(of.getPrice()))));
}
}
void TravelAgencyGUI::selectionChanged()
{
int i = getSelectedIndex();
if (i < 0)
{
return;
}
Offer of = ctrl->getbyId(i);
txtDestination->setText(QString::fromStdString((of.getDest())));
txtType->setText(QString::fromStdString((of.getType())));
txtPrice->setText(QString::number((of.getPrice())));
}
void TravelAgencyGUI::removeOffer()
{
int index = getSelectedIndex();
if (index<0) {
return;
}
ctrl->removeOffer(index);
reload();
txtDestination->clear();
txtType->clear();
txtPrice->clear();
}
void TravelAgencyGUI::updateOffer()
{
int i = getSelectedIndex();
if (i < 0) {
if (QMessageBox::Yes == QMessageBox::question(this, "No offer selected.", "Want to add it?", QMessageBox::Yes, QMessageBox::No)) {
addOffer();
reload();
}
return;
}
string destination = txtDestination->text().toStdString();
string type = txtType->text().toStdString();
float price = txtPrice->text().toFloat();
ctrl->modifyOffer(i, destination, type, price);
reload();
}
void TravelAgencyGUI::filter() {
vector<Offer> offers; //= ctrl->getOffers();
if (txtType->text() != "") {
string type = txtType->text().toStdString();
offers = ctrl->filterbyType(type, offers);
ctrl->printAll(offers);
}
if (txtPrice->text() != "") {
float price = txtPrice->text().toFloat();
offers = ctrl->filterbyPrice(price, offers);
ctrl->printAll(offers);
}
Update* updateW = new Update(offers, 0);
updateW->show();
}
void TravelAgencyGUI::sortPrice()
{
vector<Offer> offers = ctrl->getOffers();
sort(offers.begin(), offers.end(), [](Offer a, Offer b)
{
return a.getPrice() < b.getPrice();});
reload(offers);
}
void TravelAgencyGUI::sortDest()
{
vector<Offer> offers = ctrl->getOffers();
sort(offers.begin(), offers.end(), [](Offer a, Offer b)
{
return a.getDest() < b.getDest(); });
reload(offers);
}
void TravelAgencyGUI::sortType()
{
vector<Offer> offers = ctrl->getOffers();
sort(offers.begin(), offers.end(), [](Offer a, Offer b)
{
return a.getType() < b.getType(); });
reload(offers);
}
TravelAgencyGUI::~TravelAgencyGUI()
{
}