-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cpp
146 lines (115 loc) · 3.7 KB
/
index.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
#include "index.h"
#include "ui_index.h"
#include "product.h"
#include "shop.h"
#include "credit_manager.h"
#include "user.h"
#include <QtSql>
#include <QSqlTableModel>
#include <QTableView>
#include <QGridLayout>
index::index(QWidget *parent, QSqlDatabase *db, class user *user):
QDialog(parent),
ui(new Ui::index)
{
this->db = *db;
ui->setupUi(this);
this->user = user;
ui->logged_in_user->setText(user->get_username());
QSqlQuery query;
QString qry_cmd = "SELECT credit FROM users WHERE username='%1';";
qry_cmd = qry_cmd.arg(user->get_username());
query.prepare(qry_cmd);
query.exec();
query.next();
ui->user_credit->setText(query.value(0).toString());
if ( !db->open() )
{
ui->db_status->setText("Can't Connect To Database!");
}
else
{
ui->db_status->setText("Connected");
}
QLineEdit *search = ui->search_input;
connect(search, SIGNAL(textChanged(const QString &)), this, SLOT(on_search_clicked()));
if ( user->get_is_admin() == 0 )
{
QCommandLinkButton *credit_manager = new QCommandLinkButton;
credit_manager->setObjectName("credit_manager");
credit_manager->setText("Charge Account / Redeem Coupon");
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(credit_manager->sizePolicy().hasHeightForWidth());
credit_manager->setSizePolicy(sizePolicy);
ui->buttons_box->addWidget(credit_manager);
connect(credit_manager, SIGNAL(clicked()), this, SLOT(on_credit_manager_clicked()));
}
else
{
QCommandLinkButton *new_product = new QCommandLinkButton;
new_product->setObjectName("new_product");
new_product->setText("New Product");
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(new_product->sizePolicy().hasHeightForWidth());
new_product->setSizePolicy(sizePolicy);
ui->buttons_box->addWidget(new_product);
connect(new_product, SIGNAL(clicked()), this, SLOT(on_new_product_clicked()));
}
}
index::~index()
{
delete ui;
}
void index::on_new_product_clicked()
{
Product new_product;
new_product.show();
new_product.exec();
}
void index::on_search_clicked()
{
QString search_input = ui->search_input->text();
QString search_by = ui->search_by->currentText();
if ( search_by == "-----" )
{
QMessageBox::information(this, "Field Required", "Please Select What You Want To Search By");
return;
}
else if ( search_input == "" )
{
QMessageBox::information(this, "Field Required", "Please Type Search Text");
return;
}
QSqlTableModel *model = new QSqlTableModel(this, db);
model->setTable("products");
QString filter = "\"%1\" LIKE '%%2%'";
filter = filter.arg(search_by, search_input);
model->setFilter(filter);
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->show();
}
void index::on_all_products_clicked()
{
shop all_products(this, user);
all_products.show();
all_products.exec();
}
void index::on_credit_manager_clicked()
{
credit_manager crdt(this, user->get_username());
crdt.show();
crdt.exec();
QSqlQuery query;
QString qry_cmd = "SELECT credit FROM users WHERE username='%1';";
qry_cmd = qry_cmd.arg(user->get_username());
query.prepare(qry_cmd);
query.exec();
query.next();
this->ui->user_credit->setText(query.value(0).toString());
}