-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
265 lines (218 loc) · 8.81 KB
/
mainwindow.cpp
File metadata and controls
265 lines (218 loc) · 8.81 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
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent){
mainView = new QTreeView(this);
store = new Inventory();
broadcast = new Broadcast(this);
//Define actions and add to toolbar
addItem = new QAction("Add", this);
transact = new QAction("Transact", this);
restore = new QAction("Restore", this);
about = new QAction("About", this);
help = new QAction("Help", this);
bCast = new QAction("Broadcast", this);
toolbar = addToolBar("Main Toolbar");
toolbar->addAction(transact);
toolbar->addAction(addItem);
toolbar->addAction(restore);
toolbar->addAction(about);
toolbar->addAction(help);
toolbar->addAction(bCast);
//Provides a model for the treeview and separates the model into three columns.
customerModel = new QStandardItemModel(this);
customerModel->setHorizontalHeaderLabels(QStringList() << "Transaction" << "Type" << "Quantity");
//Provides functionality to action button presses
connect(about, &QAction::triggered, this, &MainWindow::showAbout);
connect(help, &QAction::triggered, this, &MainWindow::showHelp);
connect(restore, &QAction::triggered, this, &MainWindow::showRestore);
connect(addItem, &QAction::triggered, this, &MainWindow::showAddItem);
connect(transact, &QAction::triggered, this, &MainWindow::showTransact);
connect(bCast, &QAction::triggered, this, &MainWindow::showBroadcast);
//Displays GUI
mainView->setModel(customerModel);
setCentralWidget(mainView);
}
MainWindow::~MainWindow() {}
void MainWindow::showAbout(){
QFile file(":/about.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString text = file.readAll();
QMessageBox::about(this, "About", text);
} else {
QMessageBox::warning(this, "Error", "Could not load");
}
}
void MainWindow::showHelp(){
QFile file(":/help.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString text = file.readAll();
QMessageBox::about(this, "Help", text);
} else {
QMessageBox::warning(this, "Error", "Could not load");
}
}
void MainWindow::showRestore(){
store->restoreItems();
QMessageBox::about(this, "Restore", "Inventory Successfully Restored");
}
void MainWindow::showAddItem(){
//Creates the window, dropdown box and input field.
QDialog dialog(this);
dialog.setWindowTitle("Add Item");
QVBoxLayout* layout = new QVBoxLayout(&dialog);
QLabel* prompt = new QLabel("Enter item details below:");
layout->addWidget(prompt);
QComboBox* typeCombo = new QComboBox();
typeCombo->addItem("Book");
typeCombo->addItem("Magazine");
layout->addWidget(typeCombo);
QLineEdit* input = new QLineEdit();
input->setPlaceholderText("Item name");
layout->addWidget(input);
QPushButton* addButton = new QPushButton("Add Item");
layout->addWidget(addButton);
//Checks to see if the input ais invalid (empty).
connect(addButton, &QPushButton::clicked, this, [&]() {
if (input->text().isEmpty()) {
QMessageBox::warning(&dialog, "Invalid Input", "Please enter a valid name");
return;
}
else{
dialog.accept();
}
});
//Confirms input and adds to the backup list.
if (dialog.exec() == QDialog::Accepted) {
Item* item = new Item(input->text(), typeCombo->currentText());
store->addItem(item);
store->getBackup()=store->getItems();
}
}
void MainWindow::showTransact(){
//Generates window and input field.
QDialog display(this);
display.setWindowTitle("Transaction");
QGridLayout* layout = new QGridLayout(&display);
QLabel* prompt = new QLabel("Enter transaction details below:");
layout->addWidget(prompt, 0,1);
QComboBox* customer = new QComboBox(&display);
for (Customer* c : store->getCustomers()){
customer->addItem(c->getName(), QVariant::fromValue(c));
}
layout->addWidget(new QLabel("Customer:"), 1,0);
layout->addWidget(customer, 1,1);
QComboBox* itemName = new QComboBox(&display);
for (Item* item : store->getItems()){
itemName->addItem(item->getName(), QVariant::fromValue(item));
}
layout->addWidget(new QLabel("Item Name:"), 2,0);
layout->addWidget(itemName, 2,1);
QSpinBox* quantity = new QSpinBox(&display);
quantity->setMinimum(1);
quantity->setMaximum(100);
quantity->setValue(1);
quantity->setSingleStep(1);
layout->addWidget(new QLabel("Quantity:"), 3,0);
layout->addWidget(quantity, 3,1);
QPushButton* addButton = new QPushButton("Add");
layout->addWidget(addButton, 4,1);
QListWidget* list = new QListWidget(&display);
layout->addWidget(list, 5,1);
QPushButton* transactButton = new QPushButton("Transact");
layout->addWidget(transactButton, 6,1);
//Validates input when trying to add items to cart. Displays valid transactions
//on the bottom menu.
connect(addButton, &QPushButton::clicked, this, [&](){
Customer* cP = customer->currentData().value<Customer*>();
Item* iP = itemName->currentData().value<Item*>();
if (!iP) {
QMessageBox::warning(&display, "Invalid Item", "The selected item is no longer available.");
return;
}
Transaction* transaction = new Transaction();
transaction->setCustomer(cP);
transaction->setItem(iP);
transaction->setQuantity(quantity->value());
transaction->setDateTime(QDateTime::currentDateTime());
QString c = transaction->getCustomer()->getName();
QString i = transaction->getItem()->getName();
int q = transaction->getQuantity();
QDateTime dt = transaction->getdateTime();
if( store->processTransaction(&display, *transaction)){
QString statement = c + " " + i + " " + QString::number(q) + " " + dt.toString("yyyy-MM-dd hh:mm:ss");
list->addItem(statement);
store->addTransaction(transaction);
}
else{
delete transaction;
return;
}
});
//Confirms transaction and updates transaction history treeview.
connect(transactButton, &QPushButton::clicked, this, [&](){
display.accept();
updateMainView();
});
display.exec();
}
void MainWindow::updateMainView(){
customerModel->clear();
customerModel->setHorizontalHeaderLabels(QStringList() << "Transaction" << "Type" << "Quantity");
for (Customer* c : store->getCustomers()){
QStandardItem* cItem = new QStandardItem(c->getName());
for(Transaction* t : store->getTransactions()){
if (t->getCustomer() == c) {
QString dateTime = t->getdateTime().toString("yyyy-MM-dd hh:mm:ss");
QStandardItem* dateItem = nullptr;
for (int i = 0; i < cItem->rowCount(); ++i) {
QStandardItem* child = cItem->child(i);
if (child->text() == dateTime) {
dateItem = child;
break;
}
}
if (!dateItem) {
dateItem = new QStandardItem(dateTime);
cItem->appendRow(dateItem);
}
QList<QStandardItem*> transactionRow;
QStandardItem* itemName = new QStandardItem(t->getItem()->getName());
QStandardItem* itemType = new QStandardItem(t->getItem()->getType());
QStandardItem* quantity = new QStandardItem(QString::number(t->getQuantity()));
transactionRow << itemName << itemType << quantity;
dateItem->appendRow(transactionRow);
}
}
customerModel->appendRow(cItem);
}
}
void MainWindow::showBroadcast(){
QString xml = toXML();
broadcast->broadcast(xml);
}
QString MainWindow::toXML(){
QString xmlString;
QXmlStreamWriter xml(&xmlString);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeStartElement("ModelData");
for (Customer* customer : store->getCustomers()) {
xml.writeStartElement("customer");
xml.writeAttribute("name", customer->getName());
for (Transaction* transaction : store->getTransactions()) {
xml.writeStartElement("transaction");
xml.writeAttribute("date", transaction->getdateTime().toString());
for (Item* item : store->getItems()) {
xml.writeEmptyElement("lineitem");
xml.writeAttribute("name", item->getName());
xml.writeAttribute("type", item->getType());
xml.writeAttribute("quantity", QString::number(transaction->getQuantity()));
}
xml.writeEndElement();
}
xml.writeEndElement();
}
xml.writeEndElement();
xml.writeEndDocument();
return xmlString;
}