-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
198 lines (179 loc) · 6.82 KB
/
mainwindow.cpp
File metadata and controls
198 lines (179 loc) · 6.82 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
//mainwindow.cpp відповідає за функціонування вікна застосунку
#include "mainwindow.h"
#include <QGridLayout>
#include <string>
#include <QDir>
#include <QInputDialog>
#include <QMessageBox>
mainWindow::mainWindow() // Main window
{
//Setting main window parameters
setWindowTitle("Word list");
setFixedSize(450, 260);
//Layout definition
QGridLayout *layout = new QGridLayout(this);
//Main window buttons definition
addButton = new QPushButton(tr("Add word to list"), this);
deleteButton = new QPushButton(tr("Remove word from list"), this);
findShortButton = new QPushButton(tr("Find shortest word"), this);
findUnicButton = new QPushButton(tr("Find unic words"), this);
invertListButton = new QPushButton(tr("Invert list"), this);
deleteListButton = new QPushButton(tr("Delete list"), this);
//List design
table = new QTableWidget(this);
table->setColumnCount(1);
table->setColumnWidth(0, 271);
table->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Word")));
table->setEditTriggers(QTableWidget::NoEditTriggers);
//Label definition
findUnicLabel = new QLabel(this);
findUnicLabel->setAlignment(Qt::AlignCenter);
findUnicLabel->QLabel::setFixedHeight(20);
findShortLabel = new QLabel(this);
findShortLabel->setAlignment(Qt::AlignCenter);
findShortLabel->QLabel::setFixedHeight(20);
QLabel *functionsLabel = new QLabel(tr("Functions"));
functionsLabel->setAlignment(Qt::AlignCenter);
functionsLabel->setFont(QFont("Arial", 12, QFont::Bold));
functionsLabel->QLabel::setFixedHeight(20);
QLabel *listLabel = new QLabel(tr("List"));
listLabel->setAlignment(Qt::AlignCenter);
listLabel->setFont(QFont("Arial", 12, QFont::Bold));
listLabel->QLabel::setFixedHeight(20);
//Connecting objects to corresponding functions
connect(addButton, &QAbstractButton::clicked, this, &mainWindow::addNewElement);
connect(this, &mainWindow::pushFrontSignal, this, &mainWindow::pushFront);
connect(this, &mainWindow::pushBackSignal, this, &mainWindow::pushBack);
connect(findShortButton, &QAbstractButton::clicked, this, &mainWindow::findShortElement);
connect(findUnicButton, &QAbstractButton::clicked, this, &mainWindow::findUnicElement);
connect(invertListButton, &QAbstractButton::clicked, this, &mainWindow::invertList);
connect(deleteButton, &QAbstractButton::clicked, this, &mainWindow::deleteElement);
connect(deleteListButton, &QAbstractButton::clicked, this, &mainWindow::deleteList);
//Adding objects to layout
layout->addWidget(functionsLabel, 0, 0);
layout->addWidget(listLabel, 0, 1);
layout->addWidget(addButton, 1, 0);
layout->addWidget(deleteButton, 2, 0);
layout->addWidget(findShortButton, 3, 0);
layout->addWidget(findShortLabel, 4, 0);
layout->addWidget(findUnicButton, 5, 0);
layout->addWidget(findUnicLabel, 6, 0);
layout->addWidget(invertListButton, 7, 0);
layout->addWidget(deleteListButton, 8, 0);
layout->addWidget(table, 1, 1, 8, 1);
setLayout(layout); //Setting layout
}
void mainWindow::addNewElement() //Add element window
{
bool ok;
QStringList items;
items << tr("To top") << tr("To bottom") << tr("By number");
QString item = QInputDialog::getItem(this, tr("Add word"), tr("Enter element number"), items, 0, false, &ok);
if(ok && !item.isEmpty())
{
if(item == items[0]) //Add element to the top
{
QString text = QInputDialog::getText(this, tr("Add word"), tr("Enter element name:"),
QLineEdit::Normal, QDir::home().dirName(), &ok);
if(ok && !text.isEmpty())
{
emit pushFrontSignal(text);
}
}
else if(item == items[1]) //Add element to the bottom
{
QString text = QInputDialog::getText(this, tr("Add word"), tr("Enter element name:"),
QLineEdit::Normal, QDir::home().dirName(), &ok);
if(ok && !text.isEmpty())
{
emit pushBackSignal(text);
}
}
else if(item == items[2]) //Add element by user choice
{
insertDialog = new AddElementDialog(this);
insertDialog->QWidget::setWindowTitle("Add element");
if(insertDialog->exec())
{
connect(insertDialog, &AddElementDialog::sendElementSignal, this, &mainWindow::insert);
}
}
}
}
void mainWindow::findShortElement() //Finding shortest element
{
findShortLabel->setText(list.findShort());
}
void mainWindow::findUnicElement() //Finding unic elements
{
findUnicLabel->setText(list.findUnic());
}
void mainWindow::invertList() //Display inverted list
{
table->clear();
list.invertList();
QTableWidgetItem *item;
table->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Word")));
for(int i = 0; i < table->rowCount(); i++)
{
item = new QTableWidgetItem(list.get(i));
table->setItem(i, 0, item);
}
}
void mainWindow::pushFront(QString data) //Displaying "adding to the top" changes
{
list.pushFront(data);
table->insertRow(0);
QTableWidgetItem *item = new QTableWidgetItem(data);
table->setItem(0, 0, item);
table->setRowHeight(0, 20);
}
void mainWindow::pushBack(QString data) //Displaying "adding to the bottom" changes
{
list.pushBack(data);
table->insertRow(table->rowCount());
QTableWidgetItem *item = new QTableWidgetItem(data);
table->setItem(table->rowCount()-1, 0, item);
table->setRowHeight(table->rowCount()-1, 20);
}
void mainWindow::insert(QString data, int number) //Displaying "adding by user choice" changes
{
if(number > list.getSize())
{
pushBack(data);
}
else if(number<list.getSize())
{
pushFront(data);
}
else
{
list.insert(data, number);
table->insertRow(number);
table->setRowHeight(number, 20);
QTableWidgetItem *item = new QTableWidgetItem(data);
table->setItem(number, 0, item);
}
}
void mainWindow::deleteElement() //Displaying "remove element" changes
{
bool ok;
int inputInt = QInputDialog::getInt(this, tr("Delete element"),
tr("Choose element to delete:"), 1, 1, list.getSize(), 1, &ok);
if (ok)
{
list.deleteElement(inputInt-1);
table->removeRow(inputInt-1);
}
}
void mainWindow::deleteList() //Displaying list deleting
{
QMessageBox::StandardButton warningMessage;
warningMessage = QMessageBox::question(this, tr("Delete list"),
tr("Are you sure?"));
if(warningMessage == QMessageBox::Yes)
{
table->setRowCount(0);
list.deleteList();
}
}