forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionaryexportform.cpp
155 lines (119 loc) · 4.73 KB
/
dictionaryexportform.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
/*
** 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 <QSet>
#include "dictionaryexportform.h"
#include "ui_dictionaryexportform.h"
#include "zgrouptreemodel.h"
#include "words.h"
#include "groups.h"
#include "globalui.h"
#include "zui.h"
#include "formstates.h"
//-------------------------------------------------------------
DictionaryExportForm::DictionaryExportForm(QWidget *parent) : base(parent), ui(new Ui::DictionaryExportForm), kanjimodel(nullptr), wordsmodel(nullptr)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
gUI->scaleWidget(this);
exportbutton = ui->buttonBox->button(QDialogButtonBox::Ok);
exportbutton->setText(tr("Export"));
exportbutton->setEnabled(false);
connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &DictionaryExportForm::exportClicked);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &DictionaryExportForm::closeCancel);
translateTexts();
FormStates::restoreDialogSize("DictionaryExport", this, true);
}
bool DictionaryExportForm::exec(Dictionary *dict)
{
ui->dictCBox->setCurrentIndex(ZKanji::dictionaryOrder(ZKanji::dictionaryIndex(dict)));
if (kanjimodel == nullptr)
on_dictCBox_currentIndexChanged(ui->dictCBox->currentIndex());
showModal();
return modalResult() == ModalResult::Ok;
}
DictionaryExportForm::~DictionaryExportForm()
{
delete ui;
}
void DictionaryExportForm::exportClicked()
{
QString fname = QFileDialog::getSaveFileName(gUI->activeMainForm(), tr("Save export file"), QString(), QString("%1 (*.zkanji.dictionary)").arg(tr("Dictionary export file")));
if (fname.isEmpty())
closeCancel();
std::vector<ushort> klist;
std::vector<int> wlist;
if (ui->groupBox->isChecked())
{
std::vector<ushort> kanji;
std::vector<int> words;
QSet<ushort> kfound;
QSet<GroupBase*> sel;
kanjimodel->checkedGroups(sel);
for (GroupBase *g : sel)
{
if (g->isCategory())
continue;
KanjiGroup *kg = (KanjiGroup*)g;
auto &list = kg->getIndexes();
for (ushort val : list)
kfound.insert(val);
}
klist.reserve(kfound.size());
for (ushort val : kfound)
klist.push_back(val);
QSet<int> wfound;
wordsmodel->checkedGroups(sel);
for (GroupBase *g : sel)
{
if (g->isCategory())
continue;
WordGroup *wg = (WordGroup*)g;
auto &list = wg->getIndexes();
for (int val : list)
wfound.insert(val);
}
wlist.reserve(wfound.size());
for (int val : wfound)
wlist.push_back(val);
}
Dictionary *dict = ZKanji::dictionary(ZKanji::dictionaryPosition(ui->dictCBox->currentIndex()));
dict->exportDictionary(fname, ui->groupBox->isChecked(), klist, wlist);
closeOk();
}
void DictionaryExportForm::modelDataChanged()
{
bool sel = !ui->groupBox->isChecked() || kanjimodel->hasChecked() || wordsmodel->hasChecked();
exportbutton->setEnabled(sel);
}
void DictionaryExportForm::on_dictCBox_currentIndexChanged(int ix)
{
if (kanjimodel != nullptr)
kanjimodel->deleteLater();
if (wordsmodel != nullptr)
wordsmodel->deleteLater();
Dictionary *dict = ZKanji::dictionary(ZKanji::dictionaryPosition(ix));
kanjimodel = new CheckedGroupTreeModel(&dict->kanjiGroups(), false, false, ui->kanjiTree);
wordsmodel = new CheckedGroupTreeModel(&dict->wordGroups(), false, false, ui->wordsTree);
ui->kanjiTree->setModel(kanjimodel);
connect(kanjimodel, (void(CheckedGroupTreeModel::*)(const TreeItem *, const TreeItem *, const QVector<int> &))&CheckedGroupTreeModel::dataChanged, this, &DictionaryExportForm::modelDataChanged);
ui->wordsTree->setModel(wordsmodel);
connect(wordsmodel, (void(CheckedGroupTreeModel::*)(const TreeItem *, const TreeItem *, const QVector<int> &))&CheckedGroupTreeModel::dataChanged, this, &DictionaryExportForm::modelDataChanged);
ui->kanjiTree->expandAll();
ui->wordsTree->expandAll();
modelDataChanged();
}
void DictionaryExportForm::on_groupBox_toggled(bool /*on*/)
{
modelDataChanged();
}
void DictionaryExportForm::translateTexts()
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(qApp->translate("ButtonBox", "OK"));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(qApp->translate("ButtonBox", "Cancel"));
}
//-------------------------------------------------------------