forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitionwidget.cpp
211 lines (170 loc) · 6.01 KB
/
definitionwidget.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
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
/*
** Copyright 2007-2013, 2017-2018 Sólyom Zoltán
** This file is part of zkanji, a free software released under the terms of the
** GNU General Public License version 3. See the file LICENSE for details.
**/
#include "definitionwidget.h"
#include "ui_definitionwidget.h"
#include "words.h"
#include "checked_cast.h"
//-------------------------------------------------------------
DefinitionWidget::DefinitionWidget(QWidget *parent) : base(parent), ui(new Ui::DefinitionWidget), dict(nullptr), ignorechange(false)
{
ui->setupUi(this);
}
DefinitionWidget::~DefinitionWidget()
{
delete ui;
}
void DefinitionWidget::setWords(Dictionary *d, const std::vector<int> &words)
{
if (dict != nullptr)
{
disconnect(dict, &Dictionary::entryRemoved, this, &DefinitionWidget::dictEntryRemoved);
disconnect(dict, &Dictionary::entryChanged, this, &DefinitionWidget::dictEntryChanged);
}
dict = d;
if (dict != nullptr)
{
connect(dict, &Dictionary::entryRemoved, this, &DefinitionWidget::dictEntryRemoved);
connect(dict, &Dictionary::entryChanged, this, &DefinitionWidget::dictEntryChanged);
}
list = words;
std::sort(list.begin(), list.end());
// Find out if any word on the list has custom meaning set.
data.resize(list.size());
for (int ix = 0, siz = tosigned(list.size()); ix != siz; ++ix)
data[ix] = dict->studyDefinition(list[ix]) != nullptr ? 1 : 0;
//if (list.size() != 1)
//{
// ui->defEdit->setText(QString());
// ui->defEdit->setPlaceholderText(QString());
//}
//else
//{
// auto def = dict->studyDefinition(list.front());
// if (def != nullptr)
// ui->defEdit->setText(def->toQString());
// else
// ui->defEdit->setText(QString());
//}
update();
}
bool DefinitionWidget::event(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
ui->retranslateUi(this);
return base::event(e);
}
void DefinitionWidget::on_defResetButton_clicked(bool)
{
ignorechange = true;
for (int ix = 0, siz = tosigned(list.size()); ix != siz; ++ix)
{
data[ix] = 0;
dict->setWordStudyDefinition(list[ix], QString());
}
ui->defResetButton->setEnabled(false);
if (list.size() == 1)
{
if (ui->defEdit->hasFocus())
ui->defEdit->setText(original);
else
ui->defEdit->setText(QString());
}
// ui->defSaveButton->setEnabled(!ui->defEdit->text().isEmpty() && ui->defEdit->text() != original);
ignorechange = false;
}
void DefinitionWidget::dictEntryRemoved(int windex)
{
int pos = indexpos(windex);
if (pos == tosigned(list.size()))
return;
if (list.size() == 1 && list[pos] != windex)
{
--list.front();
return;
}
if (list[pos] == windex)
{
list.erase(list.begin() + pos);
data.erase(data.begin() + pos);
}
while (pos != tosigned(list.size()))
{
--list[pos];
++pos;
}
update();
}
void DefinitionWidget::dictEntryChanged(int windex, bool /*studydef*/)
{
int pos = indexpos(windex);
if (pos != tosigned(list.size()) && list[pos] == windex)
data[pos] = dict->studyDefinition(list[pos]) != nullptr ? 1 : 0;
if (list.size() != 1)
update();
else if (list.front() == windex)
{
original = dict->wordDefinitionString(windex, false);
if (!ui->defEdit->hasFocus())
ui->defEdit->setPlaceholderText(original);
const QCharString *def = dict->studyDefinition(windex);
if (!ui->defEdit->hasFocus() || (def == nullptr && !ui->defEdit->text().isEmpty() && ui->defEdit->text() != original) || (def != nullptr && ui->defEdit->text() != def->toQString()))
ui->defEdit->setText(def == nullptr ? (!ui->defEdit->hasFocus() ? QString() : original) : def->toQString());
ui->defResetButton->setEnabled(def != nullptr);
}
}
void DefinitionWidget::on_defEdit_textEdited(const QString &str)
{
if (list.empty() || list.size() != 1)
return;
ui->defSaveButton->setEnabled(!str.isEmpty() && str != original);
}
void DefinitionWidget::on_defEdit_focusChanged(bool activated)
{
ui->defEdit->setPlaceholderText(activated ? QString() : original);
if (activated && ui->defEdit->text().isEmpty())
ui->defEdit->setText(original);
if (!activated && ui->defEdit->text() == original)
ui->defEdit->setText(QString());
ui->defSaveButton->setEnabled(!ui->defEdit->text().isEmpty() && ui->defEdit->text() != original);
}
void DefinitionWidget::on_defSaveButton_clicked(bool /*checked*/)
{
if (list.empty() || list.size() != 1)
return;
dict->setWordStudyDefinition(list.front(), ui->defEdit->text());
}
int DefinitionWidget::indexpos(int windex)
{
return std::lower_bound(list.begin(), list.end(), windex) - list.begin();
}
void DefinitionWidget::update()
{
if (ignorechange)
return;
ui->defEdit->setEnabled(list.size() == 1);
ui->defSaveButton->setEnabled(false);
if (!ui->defEdit->isEnabled())
{
ui->defEdit->setText(QString());
ui->defEdit->setPlaceholderText(QString());
//ui->defSaveButton->setEnabled(false);
bool found = false;
for (int ix = 0, siz = tosigned(data.size()); !found && ix != siz; ++ix)
found = data[ix] == 1;
ui->defResetButton->setEnabled(found);
}
else
{
original = dict->wordDefinitionString(list.front(), false);
const QCharString *def = dict->studyDefinition(list.front());
if (!ui->defEdit->hasFocus())
ui->defEdit->setPlaceholderText(original);
ui->defEdit->setText(def == nullptr ? (!ui->defEdit->hasFocus() ? QString() : original) : def->toQString());
ui->defResetButton->setEnabled(def != nullptr);
//ui->defSaveButton->setEnabled(!ui->defEdit->text().isEmpty() && ui->defEdit->text() != original && (def == nullptr || ui->defEdit->text() != def->toQString()));
}
}
//-------------------------------------------------------------