-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
197 lines (171 loc) · 7.35 KB
/
mainwindow.cpp
File metadata and controls
197 lines (171 loc) · 7.35 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(sqlite3*& DB, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, DB(DB)
{
ui->setupUi(this);
centralWidget = new QSplitter(this);
browsingWidget = new QSplitter(Qt::Vertical);
tableWidget = new TableViewWidget(DB, this);
controlsLayout = new QVBoxLayout();
controlsLayoutWidget = new QWidget(this);
stackedWidget = new QStackedWidget(this);
bookView = new BookViewWidget(this);
authorView = new AuthorViewWidget(this);
userView = new UserViewWidget(this);
noObjectChosen = new QLabel(this);
noObjectChosen->setAlignment(Qt::AlignCenter);
this->setCentralWidget(centralWidget);
centralWidget->addWidget(browsingWidget);
centralWidget->addWidget(stackedWidget);
centralWidget->setSizes({1480, 440});
// Controls
controlsLayoutWidget->setLayout(controlsLayout);
browsingWidget->addWidget(controlsLayoutWidget);
controlsLayoutWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setupNewBtn();
browsingWidget->setCollapsible(0, false);
// Table
browsingWidget->addWidget(tableWidget);
connect(tableWidget->gridWidget, &QTableWidget::cellDoubleClicked, this, &MainWindow::onObjectSelected);
browsingWidget->setSizes({20, 1000});
browsingWidget->setCollapsible(1, false);
// Selection Overview
centralWidget->setCollapsible(0, false);
centralWidget->setCollapsible(1, false);
QWidget* container = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(container);
container->setLayout(layout);
layout->addWidget(noObjectChosen, Qt::AlignCenter);
stackedWidget->addWidget(container);
stackedWidget->addWidget(bookView);
stackedWidget->addWidget(authorView);
stackedWidget->addWidget(userView);
noObjectChosen->setText("Nothing is selected! Select an object to inspect it.");
noObjectChosen->setWordWrap(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onObjectSelected(int row, int column) {
Q_UNUSED(column);
switch (tableWidget->curObjectType) {
case TableViewWidget::BOOK: {
int id = tableWidget->gridWidget->item(row, 0)->data(Qt::UserRole).toInt();
QString title = tableWidget->gridWidget->item(row, 1)->data(Qt::UserRole).toString();
int year = tableWidget->gridWidget->item(row, 2)->data(Qt::UserRole).toInt();
Book book(id, title, year);
bookView->openBook(DB, book);
stackedWidget->setCurrentWidget(bookView);
break;
}
case TableViewWidget::AUTHOR: {
int id = tableWidget->gridWidget->item(row, 0)->data(Qt::UserRole).toInt();
QString forename = tableWidget->gridWidget->item(row, 1)->data(Qt::UserRole).toString();
QString surname = tableWidget->gridWidget->item(row, 2)->data(Qt::UserRole).toString();
QString bio = tableWidget->gridWidget->item(row, 3)->data(Qt::UserRole).toString();
QDate birth = tableWidget->gridWidget->item(row, 4)->data(Qt::UserRole).toDate();
QDate death = tableWidget->gridWidget->item(row, 5)->data(Qt::UserRole).toDate();
Author author(id, forename, surname, bio, birth, death);
authorView->openAuthor(author);
stackedWidget->setCurrentWidget(authorView);
break;
}
case TableViewWidget::USER: {
int id = tableWidget->gridWidget->item(row, 0)->data(Qt::UserRole).toInt();
QString forename = tableWidget->gridWidget->item(row, 1)->data(Qt::UserRole).toString();
QString surname = tableWidget->gridWidget->item(row, 2)->data(Qt::UserRole).toString();
QDate birth = tableWidget->gridWidget->item(row, 3)->data(Qt::UserRole).toDate();
QString email = tableWidget->gridWidget->item(row, 4)->data(Qt::UserRole).toString();
QString phone = tableWidget->gridWidget->item(row, 5)->data(Qt::UserRole).toString();
User user(id, forename, surname, birth, email, phone);
userView->openUser(user);
stackedWidget->setCurrentWidget(userView);
break;
}
default:
qDebug() << "Idk. Look into MainWindow::onObjectSelected";
}
}
void MainWindow::setupNewBtn() {
QHBoxLayout* btnRowLayout = new QHBoxLayout();
QPushButton* dropdownButton = new QPushButton("+", this);
dropdownButton->setFixedSize(35, 35);
dropdownButton->setStyleSheet(
"QPushButton::menu-indicator { width: 0px; height: 0px; }"
"QPushButton { text-align: center; padding: 0px; }"
);
QMenu* menu = new QMenu(dropdownButton);
menu->setStyleSheet(
"QMenu {"
" padding: 0px;"
" margin: 0px;"
" border: 1px solid #dcdcdc;"
" background-color: #212121;"
" border-radius: 0px;"
"}"
"QMenu::item {"
" padding: 4px 8px;"
" margin: 0px;"
"}"
"QMenu::item:selected {"
" background-color: #303030;" // Optional: hover effect
"}"
);
QAction* option1 = menu->addAction("New Book");
QAction* option2 = menu->addAction("New Author");
QAction* option3 = menu->addAction("New User");
connect(option1, &QAction::triggered, this, [this]() { openDialog(0); });
connect(option2, &QAction::triggered, this, [this]() { openDialog(1); });
connect(option3, &QAction::triggered, this, [this]() { openDialog(2); });
dropdownButton->setMenu(menu);
btnRowLayout->addWidget(dropdownButton);
QPushButton* openBooks = new QPushButton("Books", this);
QPushButton* openAuthors = new QPushButton("Authors", this);
QPushButton* openUsers = new QPushButton("Users", this);
connect(openBooks, &QPushButton::clicked, this, [this]() {
std::vector<Book> books = getBooks(DB);
tableWidget->populateTable(books);
});
connect(openAuthors, &QPushButton::clicked, this, [this]() {
std::vector<Author> authors = getAuthors(DB);
tableWidget->populateTable(authors);
});
connect(openUsers, &QPushButton::clicked, this, [this]() {
std::vector<User> users = getUsers(DB);
tableWidget->populateTable(users);
});
// Optional: Add a stretch at the end to push all buttons to the left
btnRowLayout->addStretch();
btnRowLayout->addWidget(openBooks);
btnRowLayout->addWidget(openAuthors);
btnRowLayout->addWidget(openUsers);
controlsLayout->addLayout(btnRowLayout);
}
void MainWindow::openDialog(int objectType) { // 0 -> Book; 1 -> Author; 2 -> User
switch (objectType) {
case 0: {
AddBookDialog* dialog = new AddBookDialog(DB, this);
connect(dialog, &AddBookDialog::bookSubmitted, this, [this](const QString& title, int year, QList<int> authorIds){
int bookID = addBook(DB, title, year);
for (int authorID : authorIds)
linkAuthorToBook(DB, authorID, bookID);
});
dialog->exec();
std::vector<Book> books = getBooks(DB);
break;
}
case 1: {
AddAuthorDialog* dialog = new AddAuthorDialog(this);
connect(dialog, &AddAuthorDialog::authorSubmitted, this, [this](const QString& forename, const QString& surname, const QString& bio, const QDate& birth, const QDate& death){
addAuthor(DB, forename, surname, bio, birth, death);
});
dialog->exec();
std::vector<Author> authors = getAuthors(DB);
break;
}
}
}