-
Notifications
You must be signed in to change notification settings - Fork 10
/
dictionaryeditorform.cpp
302 lines (239 loc) · 9.6 KB
/
dictionaryeditorform.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
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
/*
** 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 <QPushButton>
#include <QInputDialog>
#include <QMessageBox>
#include "dictionaryeditorform.h"
#include "ui_dictionaryeditorform.h"
#include "globalui.h"
#include "words.h"
#include "dialogs.h"
#include "zkanjimain.h"
#include "zui.h"
#include "settings.h"
#include "formstates.h"
//-------------------------------------------------------------
DictionaryEditorModel::DictionaryEditorModel(ZListView *view, QWidget *parent) : base(parent), view(view)
{
connect(gUI, &GlobalUI::dictionaryAdded, this, &DictionaryEditorModel::handleAdded);
connect(gUI, &GlobalUI::dictionaryRemoved, this, &DictionaryEditorModel::handleRemoved);
connect(gUI, &GlobalUI::dictionaryMoved, this, &DictionaryEditorModel::handleMoved);
connect(gUI, &GlobalUI::dictionaryRenamed, this, &DictionaryEditorModel::handleRenamed);
connect(gUI, &GlobalUI::dictionaryFlagChanged, this, &DictionaryEditorModel::handleFlagChanged);
}
DictionaryEditorModel::~DictionaryEditorModel()
{
}
bool DictionaryEditorModel::orderChanged() const
{
return orderchanged;
}
int DictionaryEditorModel::rowCount(const QModelIndex &/*index*/) const
{
return ZKanji::dictionaryCount();
}
QVariant DictionaryEditorModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DecorationRole)
{
int h = view->rowHeight(0) * 0.8;
return ZKanji::dictionaryFlag(QSize(h, h), ZKanji::dictionary(ZKanji::dictionaryPosition(index.row()))->name(), Flags::Flag);
}
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
return ZKanji::dictionary(ZKanji::dictionaryPosition(index.row()))->name();
}
bool DictionaryEditorModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || role != Qt::EditRole)
return false;
int row = index.row();
QString str = value.toString().trimmed();
if (str.isEmpty())
return false;
if (ZKanji::dictionaryOrder(0) == row || ZKanji::dictionary(ZKanji::dictionaryPosition(row))->name().toLower() == str.toLower())
return false;
if (ZKanji::dictionaryNameTaken(str))
{
QMessageBox::warning(qApp->activeWindow(), "zkanji", tr("The name entered for the dictionary is already in use. Please try again with a different name."));
return false;
}
if (!ZKanji::dictionaryNameValid(str))
{
QMessageBox::warning(qApp->activeWindow(), "zkanji", tr("The name entered for the dictionary is invalid. Make sure the name is a valid file name under your system, and such file does not exist in your data folder already."));
return false;
}
ZKanji::renameDictionary(ZKanji::dictionaryPosition(row), str);
return true;
}
Qt::ItemFlags DictionaryEditorModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
if (index.row() == ZKanji::dictionaryOrder(0))
return base::flags(index);
return base::flags(index) | Qt::ItemIsEditable;
}
Qt::DropActions DictionaryEditorModel::supportedDragActions() const
{
return Qt::MoveAction;
}
Qt::DropActions DictionaryEditorModel::supportedDropActions(bool samesource, const QMimeData * /*mime*/) const
{
if (samesource)
return Qt::MoveAction;
return 0;
}
bool DictionaryEditorModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /*column*/, const QModelIndex &parent) const
{
return !parent.isValid() && row != -1 && action == Qt::MoveAction && data->hasFormat(QStringLiteral("zkanji/dictionaryordering")) && data->data(QStringLiteral("zkanji/dictionaryordering")).size() == sizeof(int);
}
bool DictionaryEditorModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
if (!canDropMimeData(data, action, row, column, parent))
return false;
int orig = *((int*)data->data(QStringLiteral("zkanji/dictionaryordering")).constData());
if (orig == row || orig + 1 == row)
return false;
ZKanji::moveDictionaryOrder(orig, row);
return true;
}
QMimeData* DictionaryEditorModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mime = new QMimeData();
QByteArray arr;
arr.resize(sizeof(int));
int *dat = (int*)arr.data();
*dat = indexes.front().row();
mime->setData(QStringLiteral("zkanji/dictionaryordering"), arr);
return mime;
}
void DictionaryEditorModel::handleAdded()
{
signalRowsInserted({ { (int)ZKanji::dictionaryCount() - 1, 1 } });
}
void DictionaryEditorModel::handleRemoved(int /*index*/, int order)
{
signalRowsRemoved({ { order, order } });
}
void DictionaryEditorModel::handleMoved(int from, int to)
{
orderchanged = true;
signalRowsMoved({ { from, from } }, to);
}
void DictionaryEditorModel::handleRenamed(const QString &/*oldname*/, int /*index*/, int order)
{
emit dataChanged(base::index(order, 0), base::index(order, columnCount() - 1));
}
void DictionaryEditorModel::handleFlagChanged(int /*index*/, int order)
{
emit dataChanged(base::index(order, 0), base::index(order, columnCount() - 1));
}
//-------------------------------------------------------------
DictionaryEditorForm* DictionaryEditorForm::instance = nullptr;
void DictionaryEditorForm::execute()
{
if (instance == nullptr)
instance = new DictionaryEditorForm(gUI->activeMainForm());
instance->show();
instance->raise();
instance->activateWindow();
}
DictionaryEditorForm::DictionaryEditorForm(QWidget *parent) : base(parent), ui(new Ui::DictionaryEditorForm)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
gUI->scaleWidget(this);
model = new DictionaryEditorModel(ui->dictView, this);
ui->dictView->setModel(model);
ui->dictView->setAcceptDrops(true);
ui->dictView->setDragEnabled(true);
ui->dictView->setCurrentRow(0);
connect(ui->buttonBox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &DictionaryEditorForm::close);
//connect(gUI, &GlobalUI::dictionaryReset, this, &DictionaryEditorForm::);
translateTexts();
FormStates::restoreDialogSize("DictionaryEditor", this, true);
}
DictionaryEditorForm::~DictionaryEditorForm()
{
instance = nullptr;
delete ui;
}
void DictionaryEditorForm::on_upButton_clicked(bool /*checked*/)
{
ZKanji::moveDictionaryOrder(ui->dictView->currentRow(), ui->dictView->currentRow() - 1);
}
void DictionaryEditorForm::on_downButton_clicked(bool /*checked*/)
{
ZKanji::moveDictionaryOrder(ui->dictView->currentRow(), ui->dictView->currentRow() + 2);
}
void DictionaryEditorForm::on_createButton_clicked(bool /*checked*/)
{
bool ok;
QString str = QInputDialog::getText(this, "zkanji", tr("Enter name for the new dictionary:"), QLineEdit::Normal, QString(), &ok);
if (ok == false)
return;
if (ZKanji::dictionaryNameTaken(str))
{
QMessageBox::warning(this, "zkanji", tr("The name entered for the new dictionary is already in use. Please try again with a different name."));
return;
}
if (!ZKanji::dictionaryNameValid(str))
{
QMessageBox::warning(this, "zkanji", tr("The name entered for the new dictionary is invalid. Make sure the name is a valid file name under your system, and such file does not exist in your data folder already."));
return;
}
Dictionary *dict = new Dictionary();
dict->setName(str);
dict->setToModified();
ZKanji::addDictionary(dict);
}
void DictionaryEditorForm::on_editButton_clicked(bool /*checked*/)
{
editDictionaryText(ZKanji::dictionary(ZKanji::dictionaryPosition(ui->dictView->currentRow())));
}
void DictionaryEditorForm::on_delButton_clicked(bool /*checked*/)
{
if (QMessageBox::warning(this, "zkanji", tr("The data stored for the selected dictionary, including words, study statistics and groups will be deleted. The file holding the dictionary's data will be deleted as well. This operation cannot be undone.\n\nDo you want to delete the dictionary?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
return;
QString name = ZKanji::dictionary(ZKanji::dictionaryPosition(ui->dictView->currentRow()))->name();
QFile f(ZKanji::userFolder() + "/data/" + name + ".zkdict");
f.remove();
f.setFileName(ZKanji::userFolder() + "/data/" + name + ".zkuser");
f.remove();
ZKanji::deleteDictionary(ZKanji::dictionaryPosition(ui->dictView->currentRow()));
}
void DictionaryEditorForm::on_dictView_currentRowChanged(int curr, int /*prev*/)
{
ui->upButton->setEnabled(curr > 0);
ui->downButton->setEnabled(curr != -1 && curr != ZKanji::dictionaryCount() - 1);
ui->editButton->setEnabled(curr != -1 && ZKanji::dictionaryPosition(curr) != 0);
ui->delButton->setEnabled(curr != -1 && ZKanji::dictionaryPosition(curr) != 0);
}
bool DictionaryEditorForm::event(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
translateTexts();
return base::event(e);
}
void DictionaryEditorForm::closeEvent(QCloseEvent *e)
{
if (model->orderChanged())
Settings::saveIniFile();
base::closeEvent(e);
}
void DictionaryEditorForm::translateTexts()
{
ui->retranslateUi(this);
ui->buttonBox->button(QDialogButtonBox::Close)->setText(qApp->translate("ButtonBox", "Close"));
}
//-------------------------------------------------------------
void editDictionaries()
{
DictionaryEditorForm::execute();
//(new DictionaryEditorForm(gUI->activeMainForm()))->show();
}
//-------------------------------------------------------------