-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
399 lines (345 loc) · 13.1 KB
/
mainwindow.cpp
File metadata and controls
399 lines (345 loc) · 13.1 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":images//img//in.png"));
setInitialization();
setBlock();
// Валидаторы
intValid = new QIntValidator();
doubleValid = new QDoubleValidator();
QRegularExpression expr("1|0|true|false");
boolValid = new QRegularExpressionValidator(expr);
}
// Начальная иницилизация
void MainWindow::setInitialization(){
// treeWidget
ui->treeWidget->setColumnCount(2);
QStringList labels;
labels << "Sections/Keys" << "Values";
ui->treeWidget->setHeaderLabels(labels);
// horizontalLayout_1
ui->label_sections->setText("Sections");
// horizontalLayout_2
// groupeBox_adding
ui->groupBox_add->setTitle("Adding");
ui->label_key_add->setText("Key");
ui->label_type->setText("Type");
ui->label_value->setText("Value");
ui->button_add->setText("Add");
// groupBox_deleting
ui->groupBox_del->setTitle("Deleting");
ui->label_key_del->setText("Key");
ui->button_delete->setText("Delete");
// Иконка на кнопку "плюс"
QPixmap add_1(":/images/img/plus.png");
QIcon ButtonPlus(add_1);
ui->button_Plus->setIcon(ButtonPlus);
// Иконка на кнопку "минус"
QPixmap add_2(":/images/img/minus.png");
QIcon ButtonMinus(add_2);
ui->button_Minus->setIcon(ButtonMinus);
}
// Заблокируем кнопочки
void MainWindow:: setBlock(){
ui->button_add->setEnabled(false);
ui->button_delete->setEnabled(false);
ui->label_key_add->setEnabled(false);
ui->button_delete->setEnabled(false);
ui->comboBox_sections->setEnabled(false);
ui->comboBox_key->setEnabled(false);
ui->comboBox_type->setEnabled(false);
ui->lineEdit_key->setEnabled(false);
ui->lineEdit_value->setEnabled(false);
ui->button_Plus->setEnabled(false);
ui->button_Minus->setEnabled(false);
}
// Разблокируем кнопочки
void MainWindow:: setUnblock(){
ui->button_add->setEnabled(true);
ui->button_delete->setEnabled(true);
ui->label_key_add->setEnabled(true);
ui->button_delete->setEnabled(true);
ui->comboBox_sections->setEnabled(true);
ui->comboBox_key->setEnabled(true);
ui->comboBox_type->setEnabled(true);
ui->lineEdit_key->setEnabled(true);
ui->lineEdit_value->setEnabled(true);
ui->button_Plus->setEnabled(true);
ui->button_Minus->setEnabled(true);
}
// Деструктор
MainWindow::~MainWindow()
{
delete ui;
delete intValid;
delete doubleValid;
delete boolValid;
delete myIniFile;
}
// Очистим дерево
void MainWindow::setClear()
{
ui->treeWidget->clear();
ui->comboBox_sections->clear();
ui->comboBox_type->clear();
ui->comboBox_key->clear();
ui->lineEdit_key->clear();
ui->lineEdit_value->clear();
}
// Открываем файл
void MainWindow::on_actionOpen_file_triggered()
{
QString filePath = QFileDialog::getOpenFileName(this, "Open File",
QDir::currentPath(), "Ini files (*.ini)");
//Собираем данные из ListWidget
std::set <QString> texts;
int n = ui->listWidget->count();
for (int i = 0; i < n ; ++i){
QModelIndex *model_index = new QModelIndex(ui->listWidget->model()->index(i,0));
QString q = model_index->data(Qt::DisplayRole).toString();
texts.insert(q);
}
// проверка, что этот файл уже был выбран
if (texts.count(filePath) == 0){
ui->listWidget->addItem(filePath);
}
else{
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Этот файл уже выбран!");
msgBox.exec();
}
texts.clear();
}
// Открываем директорию
void MainWindow::on_actionOpen_directory_triggered()
{
QString directoryPath = QFileDialog::getExistingDirectory(this,
tr("Choose Directory"),QDir::currentPath(),
QFileDialog::DontResolveSymlinks);
QDir dir(directoryPath);
if (dir.isEmpty()){
// Проверка, что директория может не содержать файлы
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("В этой директории нет файлов!");
msgBox.exec();
}else{
QFileInfoList list = dir.entryInfoList();
ui->listWidget->clear();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if (!fileInfo.fileName().isEmpty() && (fileInfo.fileName().indexOf(".ini") != -1)) {
ui->listWidget->addItem(fileInfo.path() + "/" + fileInfo.fileName());
}
}
}
}
// Заполняем окно информацией файла
void MainWindow::setInfoOfFile()
{
// Очищаем дерево
setClear();
// Заполняем comboBox_type
std::set<std::string> AddingType {"int", "double", "bool", "string"};
for (const auto& n : AddingType){
ui->comboBox_type->addItem(QString::fromStdString(n));
}
// Заполняем treeWidget и comboBox_sections
for (const auto &it : myIniFile->getSections()) {
// Создаём item-секцию - потомка от treeWidget
QTreeWidgetItem *root = new QTreeWidgetItem(ui->treeWidget);
// Помещаем в неё название секции
root->setText(0, QString::fromStdString(it.first));
// Помещаем секцию в tree
ui->treeWidget->addTopLevelItem(root);
// Помещаем название секции в comboBox_sections
ui->comboBox_sections->addItem(QString::fromStdString(it.first));
// Идём по всем ключам секции
for (const auto &it_2 : it.second) {
// Создаём item-ключ-значение - потомок от секции
QTreeWidgetItem *child = new QTreeWidgetItem();
// Помещаем в него ключ и значение
child->setText(0, QString::fromStdString(it_2.first));//ключ
child->setText(1, QString::fromStdString(it_2.second)); //значение
// Добавляем в секцию нашу пару ключ-значение
root->addChild(child);
}
}
}
// Кликаем на файл в списке
void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
// Берём адрес файла, открываем его
QString fileName = item->text();
myIniFile = new IniFile(fileName.toStdString());
// Разблокируем кнопки, заполним информацию о файле
setUnblock();
setInfoOfFile();
}
// Добавляем секцию
void MainWindow::on_button_Plus_clicked()
{
bool ok;
QString name = QInputDialog::getText(this, tr("Adding section"),
tr("Enter the name of the section:"), QLineEdit::Normal, QDir::home().dirName(), &ok);
// Попытка создать секцию без имени
if (ok && name.isEmpty()){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Название секции не может быть пусто!");
msgBox.exec();
}
// Такая секция уже есть
else if (ok && myIniFile->isSectionExist(name.toStdString())){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Такая секция уже существует!");
msgBox.exec();
}
// Создаём секцию
else if (ok){
// Добавляем секцию в файл
myIniFile->addNewSection(name.toStdString());
// Добавляем секцию в comboBox_sections и обновляем его
ui->comboBox_sections->addItem(name);
// Обновляем дерево файла
setInfoOfFile();
}
}
// Удаляем секцию, которая была выбрана в comboBox_sections
void MainWindow::on_button_Minus_clicked()
{
// Получаем имя секции из comboBox
QString name = ui->comboBox_sections->currentText();
// Удаляем секцию из файла
myIniFile->deleteSection(name.toStdString());
// Обновляем дерево
setInfoOfFile();
}
// Кликаем на какую-то секцию/ключ/значение
void MainWindow::on_treeWidget_itemSelectionChanged()
{
// Меняем секцию в box
QTreeWidgetItem* temp = ui->treeWidget->currentItem();
// Если колонка 1 пустая, то мы кликнули на секцию
// Поменяем секцию в comboBox_sections
if (temp->text(1).isEmpty()) {
ui->comboBox_sections->setCurrentText(temp->text(0));
}
// Если колонка 1 не пустая, то мы кликнули на пару ключ-значение
// Поменяем секцию и ключ в двух box
else {
ui->comboBox_sections->setCurrentText(temp->parent()->text(0));
ui->comboBox_key->setCurrentText(temp->text(0));
}
}
// Замечено изменение в comboBox_sections
void MainWindow::on_comboBox_sections_currentTextChanged(const QString &arg1)
{
// Очищаем box с ключами
ui->comboBox_key->clear();
// Берём мапу со всеми секциями и ключами
SectionsMap sectionInfo = myIniFile->getSections();
// Проходим по всем ключам выбранной секции и добавляем их в box с ключами
for (const auto &element : sectionInfo[arg1.toStdString()]){
ui->comboBox_key->addItem(QString::fromStdString(element.first));
}
}
// Замечено изменение в comboBox_type - меняем валидаторы
void MainWindow::on_comboBox_type_currentTextChanged(const QString &arg1)
{
ui->lineEdit_key->clear();
ui->lineEdit_value->clear();
if (arg1 == "int") {
ui->lineEdit_value->setValidator(intValid);
}
else if (arg1 == "double") {
ui->lineEdit_value->setValidator(doubleValid);
}
else if (arg1 == "bool") {
ui->lineEdit_value->setValidator(boolValid);
}
else {
ui->lineEdit_value->setValidator(0);
}
}
// Добавляем пару ключ-значение
void MainWindow::on_button_add_clicked()
{
// Считываем всё, что нужно
QString section = ui->comboBox_sections->currentText();
QString type_value = ui->comboBox_type->currentText();
QString key = ui->lineEdit_key->text();
QString value = ui->lineEdit_value->text();
// Ключ пустой
if (key.isEmpty()){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Вы не ввели название ключа!");
msgBox.exec();
}
// Значение пустое
else if (value.isEmpty()){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Вы не ввели значение!");
msgBox.exec();
}
else {
if (type_value == "int"){
myIniFile->writeInt(section.toStdString(), key.toStdString(), value.toInt());
}else if (type_value == "double"){
std::string str = value.toStdString();
// Меняем все запятые на точки
std::replace(str.begin(), str.end(), ',', '.');
myIniFile->writeDouble(section.toStdString(), key.toStdString(), stod(str));
}
else if(type_value == "string"){
myIniFile->writeString(section.toStdString(), key.toStdString(), value.toStdString());
}
else{
if ((type_value == "true") || (type_value == "1")){
myIniFile->writeString(section.toStdString(), key.toStdString(), value.toStdString());
}else{
myIniFile->writeString(section.toStdString(), key.toStdString(), value.toStdString());
}
}
// Обновляем дерево
setInfoOfFile();
}
}
// Удаляем ключ
void MainWindow::on_button_delete_clicked()
{
QString section = ui->comboBox_sections->currentText();
QString key = ui->comboBox_key->currentText();
if (key.isEmpty()){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Удалять нечего");
msgBox.exec();
}
else{
myIniFile->deleteKey(section.toStdString(), key.toStdString());
setInfoOfFile();
}
}
// Сохраняем файл
void MainWindow::on_actionSave_triggered()
{
// Нечего сохранять
if (ui->listWidget->count() == 0){
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Нет открытых файлов!");
msgBox.exec();
}
else
{
myIniFile->save();
}
}