-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
149 lines (121 loc) · 4.22 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QStringBuilder>
#include <QMessageBox>
#include <QDebug>
#include <QSqlTableModel>
#include <QFileDialog>
#include <QSqlRecord>
#include <QSqlField>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase( "QODBC" );
//Dialog to open the .accdb
QString fileName = QFileDialog::getOpenFileName(
this,
"Open the WindSELECT Access database",
"",
tr("WindSELECT database (*.accdb)")
);
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=" + fileName);
if(db.open()==false){
QMessageBox::critical(0,"Nope","Your file isn't where I thought it would be.");
}
//Populate the left hand list box with all of the files in the Access database
QSqlQuery qryTableNames;
qryTableNames.exec("select * from parentTable"); //Parent table contains all unique IDs
while ( qryTableNames.next() ){
QString id(qryTableNames.value(0).toString());
QString name(qryTableNames.value(1).toString());
QString proj_name(qryTableNames.value(2).toString());
QString rev_no(qryTableNames.value(3).toString());
QString rev_date(qryTableNames.value(4).toString());
ui->listWidget->addItem(id % ". " % proj_name % " - Rev. " % rev_no % " - " % name % " - " % rev_date.left(10));
}
}
MainWindow::~MainWindow()
{
delete ui;
}
//Compare button
void MainWindow::on_pushButton_clicked()
{
//Create a Table Model
QString q_filter = QString("");
QList<QString> rows;
QSqlTableModel *model = new QSqlTableModel;
model->setTable("important");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
for(int i = 0; i < ui->listWidget_2->count(); i++)
{
//Use a regex to extract the ID from the row
QString row_id = ui->listWidget_2->item(i)->text();
QRegExp rx("(^\\d+)");
rx.indexIn(row_id);
row_id = rx.cap(1);
//Build the filter for the Table Model
if(i > 0)
q_filter = q_filter + " or ";
q_filter = QString(q_filter + "parentTableFK = " + row_id);
}
//Run the query, populate the table
model->setFilter(q_filter);
model -> select();
this->ui->tableView->setModel(model);
this->ui->tableView->show();
}
//Search button
void MainWindow::on_pushButton_2_clicked()
{
QString search = ui->lineEdit->text();
ui->listWidget->clear();
qDebug() << search;
//Query for the search term
QSqlQuery qry;
qry.prepare("SELECT * FROM parentTable WHERE ID LIKE ? OR uploaderName LIKE ? OR projectName LIKE ? OR revisionDate LIKE ? ");
qry.addBindValue("%" + search + "%");
qry.addBindValue("%" + search + "%");
qry.addBindValue("%" + search + "%");
qry.addBindValue("%" + search + "%");
qry.exec();
while ( qry.next() ){
QString id(qry.value(0).toString());
QString name(qry.value(1).toString());
QString proj_name(qry.value(2).toString());
QString rev_no(qry.value(3).toString());
QString rev_date(qry.value(4).toString());
ui->listWidget->addItem(id % ". " % proj_name % " - Rev. " % rev_no % " - " % name % " - " % rev_date.left(10));
}
}
//Remove item button
void MainWindow::on_pushButton_3_clicked()
{
qDeleteAll(ui->listWidget_2->selectedItems());
}
//Add item button
void MainWindow::on_pushButton_4_clicked()
{
QList<QListWidgetItem *> items_to_add = ui->listWidget->selectedItems();
for (int i = 0; i < items_to_add.size(); ++i) {
ui->listWidget_2->addItem(items_to_add.at(i)->text());
}
}
//Double click item on left box (add)
void MainWindow::on_listWidget_doubleClicked(const QModelIndex &index)
{
QList<QListWidgetItem *> items_to_add = ui->listWidget->selectedItems();
for (int i = 0; i < items_to_add.size(); ++i) {
ui->listWidget_2->addItem(items_to_add.at(i)->text());
}
}
//Double click item on right box (remove)
void MainWindow::on_listWidget_2_doubleClicked(const QModelIndex &index)
{
qDeleteAll(ui->listWidget_2->selectedItems());
}