-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaryeditor.cpp
More file actions
135 lines (123 loc) · 4.11 KB
/
dictionaryeditor.cpp
File metadata and controls
135 lines (123 loc) · 4.11 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
#include "dictionaryeditor.h"
#include "ui_dictionaryeditor.h"
DictionaryEditor::DictionaryEditor(QWidget *parent) :
QWidget(parent),
ui(new Ui::DictionaryEditor)
{
ui->setupUi(this);
rmEditBtnGroup = new QButtonGroup;
connect(ui->addBtn, &QPushButton::clicked, [=]() {
auto key = ui->keywordEdit->text();
ui->keywordEdit->setText("");
std::vector<std::pair<QString, QString> > defs;
for (auto def = defEditGroup.begin(), example = exampleEditGroup.begin();
def != defEditGroup.end() && example != exampleEditGroup.end();
def++, example++)
{
if (def->first->text().length() > 0) {
defs.push_back(std::pair(def->first->text(), example->first->text()));
}
}
for (auto edit = defEditGroup.begin(), example = exampleEditGroup.begin();
edit != defEditGroup.end() && example != exampleEditGroup.end();
edit++, example++)
{
edit->first->setText("");
example->first->setText("");
}
if (defs.size() > 0 && !key.isEmpty())
emit addNewWord(key, defs);
});
connect(ui->resetBtn,
&QPushButton::clicked,
this,
&DictionaryEditor::resetDictionary);
connect(ui->addDefBtn, &QPushButton::clicked,
this, &DictionaryEditor::addNewDefEdit);
connect(ui->addDefBtn, &QPushButton::clicked,
this, &DictionaryEditor::addNewExEdit);
addNewDefEdit();
addNewExEdit();
}
DictionaryEditor::~DictionaryEditor()
{
delete ui;
}
void DictionaryEditor::addNewDefEdit() {
QLineEdit *newEdit = new QLineEdit;
QPushButton *newRemoveBtn = new QPushButton("-");
newRemoveBtn->setFont(QFont("Sans", 16));
newRemoveBtn->setMaximumWidth(48);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(newEdit);
layout->addWidget(newRemoveBtn);
layout->setContentsMargins(0,0,0,0);
QWidget *container = new QWidget;
container->setLayout(layout);
defEditGroup.push_back(std::make_pair(newEdit, newRemoveBtn));
ui->defLayout->addWidget(container);
connect(newRemoveBtn, &QPushButton::clicked,
this,
[=]() {
for (int i = 0; i < defEditGroup.size(); i++) {
auto item = defEditGroup[i];
if (item.second == newRemoveBtn) {
removeEdit(i);
removeExample(i);
}
}
});
}
void DictionaryEditor::addNewExEdit() {
QLineEdit *newEdit = new QLineEdit;
QPushButton *newRemoveBtn = new QPushButton("-");
newRemoveBtn->setFont(QFont("Sans", 16));
newRemoveBtn->setMaximumWidth(48);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(newEdit);
layout->addWidget(newRemoveBtn);
layout->setContentsMargins(0,0,0,0);
QWidget *container = new QWidget;
container->setLayout(layout);
exampleEditGroup.push_back(std::make_pair(newEdit, newRemoveBtn));
ui->exampleLayout->addWidget(container);
connect(newRemoveBtn, &QPushButton::clicked,
this,
[=]() {
for (int i = 0; i < exampleEditGroup.size(); i++) {
auto item = exampleEditGroup[i];
if (item.second == newRemoveBtn) {
removeEdit(i);
removeExample(i);
}
}
});
}
void DictionaryEditor::resetDefEdit() {
ui->keywordEdit->clear();
QLayoutItem *def, *ex;
while ((def = ui->defLayout->takeAt(0)) != nullptr
&& (ex = ui->exampleLayout->takeAt(0)) != nullptr)
{
delete def->widget();
delete def;
delete ex->widget();
delete ex;
}
defEditGroup.clear();
exampleEditGroup.clear();
addNewDefEdit();
addNewExEdit();
}
void DictionaryEditor::removeEdit(int i) {
QLayoutItem *def = ui->defLayout->takeAt(i);
delete def->widget();
delete def;
defEditGroup.erase(defEditGroup.begin()+i);
}
void DictionaryEditor::removeExample(int i) {
QLayoutItem *ex = ui->exampleLayout->takeAt(i);
delete ex->widget();
delete ex;
exampleEditGroup.erase(exampleEditGroup.begin()+i);
}