forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionarytextform.cpp
192 lines (148 loc) · 5.2 KB
/
dictionarytextform.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
/*
** 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 <QFileDialog>
#include <QSvgRenderer>
#include <QMessageBox>
#include <QPainter>
#include "dictionarytextform.h"
#include "ui_dictionarytextform.h"
#include "words.h"
#include "globalui.h"
#include "zui.h"
#include "generalsettings.h"
#include "formstates.h"
//-------------------------------------------------------------
static std::map<Dictionary*, DictionaryTextForm*> _dicttextforms;
DictionaryTextForm::DictionaryTextForm(QWidget *parent) : base(parent), ui(new Ui::DictionaryTextForm), dict(nullptr), flagerased(false)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
gUI->scaleWidget(this);
connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &DictionaryTextForm::accept);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &DictionaryTextForm::close);
connect(gUI, &GlobalUI::dictionaryRemoved, this, &DictionaryTextForm::dictionaryRemoved);
connect(gUI, &GlobalUI::dictionaryRenamed, this, &DictionaryTextForm::dictionaryRenamed);
FormStates::restoreDialogSize("DictionaryText", this, true);
}
DictionaryTextForm::~DictionaryTextForm()
{
if (dict != nullptr)
_dicttextforms.erase(dict);
delete ui;
}
void DictionaryTextForm::exec(Dictionary *d)
{
auto it = _dicttextforms.find(d);
if (it != _dicttextforms.end())
{
it->second->raise();
it->second->activateWindow();
deleteLater();
return;
}
_dicttextforms[d] = this;
dict = d;
ui->dictImgLabel->setPixmap(ZKanji::dictionaryFlag(QSize(Settings::scaled(18), Settings::scaled(18)), dict->name(), Flags::Flag));
translateTexts();
ui->infoText->setPlainText(dict->infoText());
connect(d, &Dictionary::dictionaryReset, this, &DictionaryTextForm::dictionaryReset);
connect(gUI, &GlobalUI::dictionaryReplaced, this, &DictionaryTextForm::dictionaryReplaced);
ui->removeButton->setEnabled(ZKanji::dictionaryHasCustomFlag(dict->name()));
show();
}
void DictionaryTextForm::accept()
{
dict->setInfoText(ui->infoText->toPlainText());
if (!flagdata.isEmpty())
{
ZKanji::assignDictionaryFlag(flagdata, dict->name());
dict->setToModified();
}
else if (flagerased)
{
ZKanji::unassignDictionaryFlag(dict->name());
dict->setToModified();
}
close();
}
void DictionaryTextForm::dictionaryRemoved(int /*index*/, int /*order*/, void *oldaddress)
{
if (oldaddress == dict)
close();
}
void DictionaryTextForm::dictionaryRenamed(const QString &/*oldname*/, int index, int /*order*/)
{
if (ZKanji::dictionary(index) == dict)
setWindowTitle(tr("zkanji - Edit information: %1").arg(dict->name()));
}
void DictionaryTextForm::dictionaryReset()
{
ui->infoText->setPlainText(dict->infoText());
}
void DictionaryTextForm::dictionaryReplaced(Dictionary *olddict, Dictionary * /*newdict*/, int /*index*/)
{
if (dict == olddict)
close();
}
void DictionaryTextForm::on_browseButton_clicked()
{
QString fname;
fname = QFileDialog::getOpenFileName(this, tr("Select dictionary flag image"), QString(), QString("%1 (*.svg)").arg(tr("SVG file")));
if (fname.isEmpty())
return;
QFile f(fname);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this, "zkanji", tr("Couldn't open the selected file."));
return;
}
QByteArray arr = f.readAll();
QSvgRenderer sr(arr);
if (!sr.isValid())
{
QMessageBox::warning(this, "zkanji", tr("Couldn't read the image file."));
return;
}
flagdata = arr;
flagerased = false;
ui->removeButton->setEnabled(true);
int s = Settings::scaled(18);
QPixmap img(QSize(s, s));
img.fill(QColor(0, 0, 0, 0));
QPainter p(&img);
sr.render(&p, QRectF(0, s * 0.0625, s, s));
p.end();
ui->dictImgLabel->setPixmap(img /*ZKanji::dictionaryFlag(, dict->name(), Flags::Flag)*/);
}
void DictionaryTextForm::on_removeButton_clicked()
{
//ZKanji::unassignDictionaryFlag(dict->name());
ui->removeButton->setEnabled(false);
flagdata.clear();
flagerased = true;
ui->dictImgLabel->setPixmap(ZKanji::dictionaryFlag(QSize(Settings::scaled(18), Settings::scaled(18)), "", Flags::Flag));
}
bool DictionaryTextForm::event(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
translateTexts();
return base::event(e);
}
void DictionaryTextForm::translateTexts()
{
ui->retranslateUi(this);
setWindowTitle(tr("zkanji - Edit information: %1").arg(dict->name()));
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(qApp->translate("ButtonBox", "OK"));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(qApp->translate("ButtonBox", "Cancel"));
}
//-------------------------------------------------------------
void editDictionaryText(Dictionary *d)
{
DictionaryTextForm *f = new DictionaryTextForm(gUI->activeMainForm());
f->exec(d);
}
//-------------------------------------------------------------