Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
272 lines (226 loc) · 8.43 KB
/
Copy pathmainwindow.cpp
File metadata and controls
272 lines (226 loc) · 8.43 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
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QString>
#include "journallist.h"
#include <QHeaderView>
#include <QModelIndex>
#include <QSortFilterProxyModel>
#include <QMessageBox>
void MainWindow::yearValue(QString yearValue)
{
QDate dateToday = QDate::currentDate();
int year = dateToday.year();
year -= 10;
int l = year;
l += 5;
if(yearValue.toInt()<year)
{
for(int k = 0;k < m_model->columnCount();k++)
{
m_model->item(m_model->rowCount()-1,k)->setData(QBrush(Qt::red),Qt::BackgroundRole);
}
}
else if(yearValue.toInt()<l)
{
for(int k = 0;k < m_model->columnCount();k++)
{
m_model->item(m_model->rowCount()-1,k)->setData(QBrush(Qt::green),Qt::BackgroundRole);
}
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//No date later than the currrent date can be entered QRegExp rx1("[A-Za-z\\s\\-]+");
QDate dateToday = QDate::currentDate();
ui->spinBox_Year->setMaximum(dateToday.year());
//Checking if the user entered valid input
QRegExp rx1("30\\-(04|06|09|11)\\-20[0-1][0-9]");
QValidator *validator1 = new QRegExpValidator(rx1, this);
ui->lineEdit_Author->setValidator(validator1);
QRegExp rx2("[A-Za-z\\s]+[\\s]+[\\d]+[\\s]+[A-Za-z\\s]+");
QValidator *validator2 = new QRegExpValidator(rx2, this);
ui->lineEdit_Title->setValidator(validator2);
QRegExp rx3("[A-Za-z\\s]+[\\s]+[\\d]+[\\s]+[A-Za-z\\s]+");
QValidator *validator3 = new QRegExpValidator(rx3, this);
ui->lineEdit_Journal->setValidator(validator3);
QRegExp rx4("[0-9]+[\\-]+[0-9]+");
QValidator *validator4 = new QRegExpValidator(rx4, this);
ui->lineEdit_Pages->setValidator(validator4);
m_model = new QStandardItemModel(this);
m_model->setHeaderData(0,Qt::Horizontal,QString("Author"),Qt::DisplayRole);
m_model->setHeaderData(1,Qt::Horizontal,QString("Year"),Qt::DisplayRole);
m_model->setHeaderData(2,Qt::Horizontal,QString("Title"),Qt::DisplayRole);
m_model->setHeaderData(3,Qt::Horizontal,QString("Journal"),Qt::DisplayRole);
m_model->setHeaderData(4,Qt::Horizontal,QString("Volume"),Qt::DisplayRole);
m_model->setHeaderData(5,Qt::Horizontal,QString("Issue"),Qt::DisplayRole);
m_model->setHeaderData(6,Qt::Horizontal,QString("Pages"),Qt::DisplayRole);
QStringList list;
list.append("Author");
list.append("Year");
list.append("Title");
list.append("Journal");
list.append("Volume");
list.append("Issue");
list.append("Pages");
m_model->setHorizontalHeaderLabels(list);
list.pop_back();
ui->comboBox_Filter->addItems(list);
//Able to sort the table according to the headers of the table
ui->tableView->setModel(m_model);
ui->tableView->setSortingEnabled(true);
//Load the tables with some preloaded data
QString preload1,preload2,preload3,preload4,preload5,preload6,preload7;
preload1 = "RJ Nkomo-Smythe";
preload2 = "1983";
preload3 = "Journal of the Ages";
preload4 = "National Geographic";
preload5 = "4";
preload6 = "6";
preload7 = "12-23";
items.append(new QStandardItem(preload1));
items.append(new QStandardItem(preload2));
items.append(new QStandardItem(preload3));
items.append(new QStandardItem(preload4));
items.append(new QStandardItem(preload5));
items.append(new QStandardItem(preload6));
items.append(new QStandardItem(preload7));
m_model->appendRow(items);
yearValue(preload2);
items.clear();
preload1 = "JJ Jacobs";
preload2 = "2013";
preload3 = "South African";
preload4 = "Journal of science";
preload5 = "48";
preload6 = "112";
preload7 = "56-72";
items.append(new QStandardItem(preload1));
items.append(new QStandardItem(preload2));
items.append(new QStandardItem(preload3));
items.append(new QStandardItem(preload4));
items.append(new QStandardItem(preload5));
items.append(new QStandardItem(preload6));
items.append(new QStandardItem(preload7));
m_model->appendRow(items);
yearValue(preload2);
items.clear();
preload1 = "JJ Bravo";
preload2 = "2019";
preload3 = "South African";
preload4 = "Fire and Flame";
preload5 = "23";
preload6 = "152";
preload7 = "60-70";
items.append(new QStandardItem(preload1));
items.append(new QStandardItem(preload2));
items.append(new QStandardItem(preload3));
items.append(new QStandardItem(preload4));
items.append(new QStandardItem(preload5));
items.append(new QStandardItem(preload6));
items.append(new QStandardItem(preload7));
m_model->appendRow(items);
yearValue(preload2);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_buttonRest_clicked()
{
}
void MainWindow::on_pushButton_Add_clicked()
{
items.clear();
QString temp1,temp2,temp3;
temp1 = QString::number(ui->spinBox_Year->value());
temp2 = QString::number(ui->spinBox_Volume->value());
temp3 = QString::number(ui->spinBox_Issue->value());
items.append(new QStandardItem(ui->lineEdit_Author->text()));
items.append(new QStandardItem(temp1));
items.append(new QStandardItem(ui->lineEdit_Title->text()));
items.append(new QStandardItem(ui->lineEdit_Journal->text()));
items.append(new QStandardItem(temp2));
items.append(new QStandardItem(temp3));
items.append(new QStandardItem(ui->lineEdit_Pages->text()));
m_model->appendRow(items);
ui->lineEdit_Author->clear();
ui->spinBox_Year->clear();
ui->spinBox_Issue->clear();
ui->spinBox_Volume->clear();
ui->lineEdit_Pages->clear();
ui->lineEdit_Title->clear();
ui->lineEdit_Journal->clear();
//Check to see the reference
QDate dateToday = QDate::currentDate();
int year = dateToday.year();
year -= 10;
int l = year;
l += 5;
if(ui->spinBox_Year->value()<year)
{
for(int k = 0;k < m_model->columnCount();k++)
{
m_model->item(m_model->rowCount()-1,k)->setData(QBrush(Qt::red),Qt::BackgroundRole);
}
}
else if(ui->spinBox_Year->value()<l)
{
for(int k = 0;k < m_model->columnCount();k++)
{
m_model->item(m_model->rowCount()-1,k)->setData(QBrush(Qt::green),Qt::BackgroundRole);
}
}
}
void MainWindow::on_pushButton_Remove_clicked()
{
QModelIndex index = ui->tableView->currentIndex();
m_model->removeRow(index.row());
}
void MainWindow::on_pushButton_Find_clicked()
{
QString findName = ui->lineEdit_Filter->text().trimmed();
QAbstractItemModel *modl = m_model;
QSortFilterProxyModel proxy;
proxy.setSourceModel(modl);
int k = ui->comboBox_Filter->currentIndex();
proxy.setFilterKeyColumn(k);
proxy.setFilterFixedString(findName);
// now the proxy only contains rows that match the name
// let's take the first one and map it to the original model
QModelIndex matchingIndex = proxy.mapToSource(proxy.index(0,0));
if(matchingIndex.isValid()){
ui->tableView->scrollTo(matchingIndex,QAbstractItemView::EnsureVisible);
//Testing code:
for(int i = 0; i < items.count(); i++)
{
if(matchingIndex.row()!=i)
ui->tableView->hideRow(i);
}
QString columnAndRow = "Match Found! Row: ";
int r = matchingIndex.row();
columnAndRow.append(QString::number(r));
columnAndRow.append(" || Column: ");
int c = matchingIndex.column();
columnAndRow.append(QString::number(c));
QMessageBox::information(this,"Name Search:",columnAndRow);
} else {
QString columnAndRow = "Match not found!Row: ";
int r = matchingIndex.row();
columnAndRow.append(QString::number(r));
columnAndRow.append(" || Column: ");
int c = matchingIndex.column();
columnAndRow.append(QString::number(c));
QMessageBox::information(this,"Name Search:", columnAndRow);
}
}
void MainWindow::on_pushButton_Reset_clicked()
{
for(int i = 0; i < m_model->rowCount(); i++)
{
ui->tableView->showRow(i);
}
}