-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainform.cpp
184 lines (161 loc) · 6.87 KB
/
mainform.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
#include "mainform.h"
#include "employeeform.h"
#include <QSqlRelationalDelegate>
#include <QModelIndex>
#include <QSqlRecord>
#include <QSqlQuery>
#include <QMessageBox>
#include <QHeaderView>
#include <QGridLayout>
MainForm::MainForm()
{
createDepartmentPanel();
createEmployeePanel();
addButton = new QPushButton(tr("Add Dept."));
deleteButton = new QPushButton(tr("Delete Dept."));
editButton = new QPushButton(tr("Edit Employees..."));
quitButton = new QPushButton(tr("Quit"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::ActionRole);
connect(addButton, SIGNAL(clicked()),this,SLOT(addDepartment()));
connect(deleteButton,SIGNAL(clicked()), this, SLOT(deleteDepartment()));
connect(editButton, SIGNAL(clicked()), this, SLOT(editEmployees()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
departmentView->setCurrentIndex(departmentModel->index(0,0));
QVBoxLayout *departmentLayout = new QVBoxLayout;
departmentLayout->addWidget(departmentLabel);
departmentLayout->addWidget(departmentView);
QVBoxLayout *employeeLayout = new QVBoxLayout;
employeeLayout->addWidget(employeeLabel);
employeeLayout->addWidget(employeeView);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(departmentLayout);
mainLayout->addLayout(employeeLayout);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
}
void MainForm::createDepartmentPanel()
{
departmentPanel = new QWidget;
departmentModel = new QSqlRelationalTableModel(this);
departmentModel->setTable("department");
departmentModel->setRelation(Department_LocationId,
QSqlRelation("location", "id", "name"));
departmentModel->setSort(Department_Name, Qt::AscendingOrder);
departmentModel->setHeaderData(Department_Name, Qt::Horizontal,
tr("Dept."));
departmentModel->setHeaderData(Department_LocationId,
Qt::Horizontal, tr("Location"));
departmentModel->select();
departmentView = new QTableView;
departmentView->setModel(departmentModel);
departmentView->setItemDelegate(new QSqlRelationalDelegate(this));
departmentView->setSelectionMode(
QAbstractItemView::SingleSelection);
departmentView->setSelectionBehavior(QAbstractItemView::SelectRows);
departmentView->setColumnHidden(Department_Id, true);
departmentView->resizeColumnsToContents();
//departmentView->horizontalHeader()->setStretchLastSection(true);
departmentView->horizontalHeader()->setStretchLastSection(true);
departmentLabel = new QLabel(tr("Depar&tments"));
departmentLabel->setBuddy(departmentView);
connect(departmentView->selectionModel(),
SIGNAL(currentRowChanged(const QModelIndex &,const QModelIndex&)),
this, SLOT(updateEmployeeView()));
}
void MainForm::createEmployeePanel()
{
employeePanel = new QWidget;
employeeModel = new QSqlRelationalTableModel(this);
employeeModel->setTable("employee");
employeeModel->setRelation(Employee_DepartmentId,
QSqlRelation("department","id","name"));
employeeModel->setSort(Employee_Name, Qt::AscendingOrder);
employeeModel->setHeaderData(Employee_Name, Qt::Horizontal,
tr("Name"));
employeeModel->setHeaderData(Employee_Extension, Qt::Horizontal,
tr("Ext."));
employeeModel->setHeaderData(Employee_Email, Qt::Horizontal,
tr("Email"));
employeeView = new QTableView;
employeeView->setModel(employeeModel);
employeeView->setSelectionMode(QAbstractItemView::SingleSelection);
employeeView->setSelectionBehavior(QAbstractItemView::SelectRows);
employeeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
employeeView->horizontalHeader()->setStretchLastSection(true);
employeeView->setColumnHidden(Employee_Id, true);
employeeView->setColumnHidden(Employee_DepartmentId, true);
employeeView->setColumnHidden(Employee_StartDate, true);
employeeLabel = new QLabel(tr("E&mployees"));
employeeLabel->setBuddy(employeeView);
}
void MainForm::updateEmployeeView()
{
QModelIndex index = departmentView->currentIndex();
if (index.isValid()){
QSqlRecord record = departmentModel->record(index.row());
int id = record.value("id").toInt();
employeeModel->setFilter(QString("departmentid = %1").arg(id));
employeeLabel->setText(tr("E&mployees in the %1 Department").arg(record.value("name").toString()));
}else {
employeeModel->setFilter("departmentid = -1");
employeeLabel->setText(tr("E&mployees"));
}
employeeModel->select();
employeeView->horizontalHeader()->setVisible(
employeeModel->rowCount() > 0);
}
void MainForm::addDepartment()
{
int row = departmentModel->rowCount();
departmentModel->insertRow(row);
QModelIndex indexT = departmentModel->index(row, Department_Name);
departmentView->setCurrentIndex(indexT);
departmentView->edit(indexT);
}
void MainForm::deleteDepartment()
{
QModelIndex index = departmentView->currentIndex();
if (!index.isValid())
return;
QSqlDatabase::database().transaction();
QSqlRecord record = departmentModel->record(index.row());
int id = record.value(Department_Id).toInt();
int numEmployees = 0;
QSqlQuery query(QString("SELECT COUNT(*) FROM employee"
"WHERE departmentid = %1").arg(id));
if (query.next())
numEmployees = query.value(0).toInt();
if (numEmployees > 0){
int r = QMessageBox::warning(this,tr("Delete Department"),
tr("Delete %1 and all its employees?")
.arg(record.value(Department_Name).toString()),
QMessageBox::Yes | QMessageBox::No);
if (r == QMessageBox::No){
QSqlDatabase::database().rollback();
return;
}
query.exec(QString("DELETE FROM employee"
"WHERE departmentid = %1").arg(id));
}
departmentModel->removeRow(index.row());
departmentModel->submitAll();
QSqlDatabase::database().commit();
updateEmployeeView();
departmentView->setFocus();
}
void MainForm::editEmployees()
{
int employeeId = -1;
QModelIndex index = employeeView->currentIndex();
if (index.isValid()){
QSqlRecord record = employeeModel->record(index.row());
employeeId = record.value(Employee_Id).toInt();
}
EmployeeForm form(employeeId, this);
form.exec();
updateEmployeeView();
}