-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp~
370 lines (288 loc) · 10.3 KB
/
mainwindow.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
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
369
370
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editdialog.h"
#include "createdialog.h"
#include "extinfo.h"
#include "ext2.h"
#include "advancedselect.h"
#include <QGraphicsOpacityEffect>
#include <QTimer>
#include <QDebug>
#include <QGridLayout>
#include <QToolTip>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->qry1Btn->setToolTip("View For all borrowers the sum of loans and total amount");
ui->qry2Btn->setToolTip("Select an INTERMEDIARY to view info on loans which he is involved");
ui->qry3Btn->setToolTip("Show borrowers with FUNCTION amounts > average");
ui->errors->setAutoFillBackground(true);
ui->errors->setAlignment(Qt::AlignCenter);
ui->comboBox->addItem("Select...");
ui->comboBox->addItem("BORROWER");
ui->comboBox->addItem("COMMITMENT");
ui->comboBox->addItem("DEADLINE");
ui->comboBox->addItem("INTERMEDIARY");
ui->comboBox->addItem("LENDER");
ui->comboBox->addItem("LOAN");
ui->comboBox->addItem("LOAN_REQUEST");
ui->comboBox->addItem("REPAYMENT");
ui->comboBox->addItem("TRUST");
ui->comboBox->addItem("LOAN_DISP");
ui->comboBox->addItem("COMPO_VIEW");
ui->extInfo->hide();
//Establish connection and open DB
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("microloans");
db.setUserName("root");
db.setPassword("SaSSanidV@riabl3");
if(db.open())
ui->label->setText("Connected");
else
ui->label->setText("Not Connected");
//QSqlDatabase dbPointer = QSqlDatabase::database();
model= new QSqlQueryModel();
tmodel = new QSqlTableModel(this,db);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_comboBox_activated(const QString &arg1)
{
if(arg1 =="BORROWER" || arg1 =="LENDER") ui->extInfo->show();
else ui->extInfo->hide();
if(arg1=="Select...") {ui->dbLabel->setText("Current Table: None "); return ;}
else ui->dbLabel->setText("Current table:" + arg1);
tmodel->setEditStrategy(QSqlTableModel::OnFieldChange);
tmodel->setTable(arg1);
tmodel->select();
model->clear();
ui->tableView->setModel(tmodel);
queryOK(NULL);
}
void MainWindow::on_deleteButton_clicked()
{
//check if something selected
QItemSelectionModel *select =ui->tableView->selectionModel();
QModelIndexList indexes = select->selectedRows();
QModelIndex index;
if(select->hasSelection())
{
foreach(index, indexes){
//QString text=QString("(%1,%2)").arg(index.row()).arg(index.column());
QString str = ui->tableView->model()->index(index.row() , 0).data().toString(); // ui->tableView->model()->data(index).toString();
qDebug() << str;
//execute delete query
QSqlQuery query;
QString currentTable = ui->comboBox->currentText();
ui->tableView->setModel(model);
query.prepare("DELETE FROM "+currentTable+" WHERE ID = "+"'"+str+"'");
model->setQuery(query);
if(query.exec());
else queryNotOK(query.lastError().text());
//refresh table in UI
on_comboBox_activated(currentTable);
}
}
else queryNotOK("Please select some entries to delete");
}
void MainWindow::on_editButton_clicked()
{
QItemSelectionModel *select =ui->tableView->selectionModel();
QModelIndexList indexes = select->selectedRows();
QModelIndex index;
if(indexes.count() > 1)
queryNotOK("You can edit one entry at a time");
else if(indexes.count() == 0)
queryNotOK("Please select table entry to edit");
else
{
editDialog editdialog(ui->comboBox->currentText(),tmodel,select);
editdialog.setModal(true);
editdialog.exec();
//editdialog.open();
//editdialog.deleteLater();
//ui->tableView->setModel(model);
on_comboBox_activated(ui->comboBox->currentText());
}
}
void MainWindow::queryOK(const QString &arg)
{
QString msg;
if(arg==NULL) msg="Query successful";
else msg = arg;
//output sucess mesage
QPalette sample_palette;
sample_palette.setColor(QPalette::Window, Qt::green);
sample_palette.setColor(QPalette::WindowText, Qt::black);
ui->errors->setPalette(sample_palette);
ui->errors->setText(msg);
fadeOut();
}
void MainWindow::queryNotOK(const QString &arg)
{
QString msg;
if(arg==NULL) msg="An error has occured";
else msg = arg;
QPalette sample_palette;
sample_palette.setColor(QPalette::Window,Qt::red);
sample_palette.setColor(QPalette::WindowText, Qt::black);
ui->errors->setPalette(sample_palette);
ui->errors->setText(msg);
fadeOut();
}
void MainWindow::fadeOut()
{
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
ui->errors->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
//start fade
a->setDuration(7350);
a->setStartValue(5);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
//a->pause();
//QTimer::singleShot(700, a, SLOT(start()));
a->start(QPropertyAnimation::DeleteWhenStopped);
//connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
//connect(a,SIGNAL(finished()),this,SLOT());
}
void MainWindow::on_createButton_clicked()
{
QItemSelectionModel *select =ui->tableView->selectionModel();
createDialog createdialog(ui->comboBox->currentText(),tmodel,select);
createdialog.setModal(true);
createdialog.exec();
createdialog.deleteLater();
on_comboBox_activated(ui->comboBox->currentText());
}
void MainWindow::on_quickSearch_returnPressed()
{
//tmodel->setFilter("TOWN LIKE '%ATH%' ");
int i,cc=tmodel->columnCount();
QString search = ui->quickSearch->text();
QString qry = NULL;
for(i=0;i<cc;i++){
if(i==0)
qry.append(tmodel->record().fieldName(i)+" LIKE '%"+search+"%'");
else
qry.append(" OR "+tmodel->record().fieldName(i)+
" LIKE '%"+search+"%'");
tmodel->setTable(ui->comboBox->currentText());
tmodel->setFilter(qry);
tmodel->select();
ui->tableView->setModel(tmodel);
}
}
void MainWindow::on_extInfo_clicked()
{
QItemSelectionModel *select =ui->tableView->selectionModel();
if(ui->comboBox->currentText()=="BORROWER")
{
if(select->hasSelection())
{
extInfo extinfodialog(tmodel,select);
extinfodialog.setModal(true);
extinfodialog.exec();
extinfodialog.deleteLater();
}
}
if(ui->comboBox->currentText()=="LENDER")
{
ext2 extinfodialog(tmodel,select);
extinfodialog.setModal(true);
extinfodialog.exec();
extinfodialog.deleteLater();
}
}
void MainWindow::on_quickSearch_textEdited(const QString &arg1)
{
//tmodel->setFilter("TOWN LIKE '%ATH%' ");
int i,cc=tmodel->columnCount();
QString search = arg1;
QString qry = NULL;
for(i=0;i<cc;i++){
if(i==0)
qry.append(tmodel->record().fieldName(i)+" LIKE '%"+search+"%'");
else
qry.append(" OR "+tmodel->record().fieldName(i)+
" LIKE '%"+search+"%'");
tmodel->setTable(ui->comboBox->currentText());
tmodel->setFilter(qry);
tmodel->select();
ui->tableView->setModel(tmodel);
}
}
void MainWindow::on_qry1Btn_clicked()
{
ui->dbLabel->setText("Current table: Query 1");
QString qry = QString("SELECT B.NAME AS BORROWER, COUNT(LO.ID) AS NUM_OF_LOANS, SUM(L.AMOUNT) AS WHOLE_SUM \
FROM BORROWER AS B, LOAN_REQUEST AS L, LOAN AS LO \
WHERE L.BID=B.ID AND L.DATE_OF_REQUEST=LO.DATE_OF_REQUEST AND LO.BID = B.ID GROUP BY B.ID ORDER BY WHOLE_SUM DESC");
advancedQueries(qry);
}
void MainWindow::advancedQueries(QString qry)
{
QSqlQuery query;
query.prepare(qry);
if(query.exec())
queryOK(NULL);
else {queryNotOK(NULL);return;}
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery(query);
ui->tableView->setModel(model);
}
void MainWindow::on_qry2Btn_clicked()
{
ui->dbLabel->setText("Current table: Query 2");
QItemSelectionModel *select =ui->tableView->selectionModel();
if(ui->comboBox->currentText() == "INTERMEDIARY"){
if(select->hasSelection()){
if(select->selectedRows().count() == 1){
QString MID = select->currentIndex().data().toString();
QString qry = QString("SELECT M.NAME AS INTERMEDIARY, L.BID AS BORROWER_ID, \
L.DATE_OF_REQUEST FROM LOAN AS L INNER JOIN INTERMEDIARY AS M \
ON M.ID=L.MID \
WHERE M.ID='"+MID +"'");
QSqlQuery query ;
query.prepare(qry);
if(query.exec()) queryOK(NULL);
else queryNotOK(query.lastError().text());
model->setQuery(query);
ui->tableView->setModel(model);
}
else if (select->selectedRows().count() > 1)
queryNotOK("Please select one entry");
}
else queryNotOK("Please select an intermediary");
}
else queryNotOK("Please select an intermediary");
}
void MainWindow::on_qry3Btn_clicked()
{
ui->dbLabel->setText("Current table: Query 3");
QString function;
if(ui->radioButton->isChecked()) function = "SUM";
else if (ui->radioButton_2->isChecked()) function = "AVG";
else {queryNotOK("Please select a function");return;}
QString qry = QString("SELECT B.NAME AS BORROWER,SUM(L.AMOUNT) AS TOTAL_AMOUNT \
FROM BORROWER AS B, LOAN_REQUEST AS L, LOAN AS LO \
WHERE B.ID=L.BID AND L.BID=LO.BID AND LO.DATE_OF_REQUEST = L.DATE_OF_REQUEST GROUP BY B.ID \
HAVING "+function+"(L.AMOUNT) > ( \
SELECT AVG(L.AMOUNT) FROM LOAN_REQUEST AS L, LOAN AS LO \
WHERE LO.BID=L.BID ) ; ");
QSqlQuery query ;
query.prepare(qry);
if(query.exec()) queryOK(NULL);
else queryNotOK(query.lastError().text());
model->setQuery(query);
ui->tableView->setModel(model);
}
void MainWindow::on_exitBtn_clicked()
{
this->close();
}