forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupexportform.cpp
124 lines (94 loc) · 3.96 KB
/
groupexportform.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
/*
** 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 <QFileDialog>
#include "ui_groupexportform.h"
#include "groupexportform.h"
#include "zgrouptreemodel.h"
#include "words.h"
#include "groups.h"
#include "globalui.h"
#include "formstates.h"
//-------------------------------------------------------------
GroupExportForm::GroupExportForm(QWidget *parent) : base(parent), ui(new Ui::GroupExportForm), kanjimodel(nullptr), wordsmodel(nullptr)
{
ui->setupUi(this);
gUI->scaleWidget(this);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &GroupExportForm::exportClicked);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &GroupExportForm::closeCancel);
translateTexts();
FormStates::restoreDialogSize("GroupExport", this, true);
}
GroupExportForm::~GroupExportForm()
{
delete ui;
}
bool GroupExportForm::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;
}
void GroupExportForm::exportClicked()
{
QString fname = QFileDialog::getSaveFileName(nullptr, tr("Save export file"), QString(), QString("%1 (*.zkanji.export)").arg(tr("Export file")));
if (fname.isEmpty())
closeCancel();
std::vector<KanjiGroup*> kanjilist;
std::vector<WordGroup*> wordslist;
QSet<GroupBase*> sel;
kanjimodel->checkedGroups(sel);
kanjilist.reserve(sel.size());
for (auto it = sel.begin(); it != sel.end(); ++it)
{
if ((*it)->isCategory())
continue;
kanjilist.push_back((KanjiGroup*)*it);
}
wordsmodel->checkedGroups(sel);
wordslist.reserve(sel.size());
for (auto it = sel.begin(); it != sel.end(); ++it)
{
if ((*it)->isCategory())
continue;
wordslist.push_back((WordGroup*)*it);
}
Dictionary *dict = ZKanji::dictionary(ZKanji::dictionaryPosition(ui->dictCBox->currentIndex()));
dict->exportUserData(fname, kanjilist, ui->kanjiBox->isChecked(), wordslist, ui->wordsBox->isChecked());
closeOk();
}
void GroupExportForm::modelDataChanged()
{
if (kanjimodel == nullptr)
return;
bool sel = kanjimodel->hasChecked() || wordsmodel->hasChecked();
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(sel);
}
void GroupExportForm::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, &GroupExportForm::modelDataChanged);
ui->wordsTree->setModel(wordsmodel);
connect(wordsmodel, (void(CheckedGroupTreeModel::*)(const TreeItem *, const TreeItem *, const QVector<int> &))&CheckedGroupTreeModel::dataChanged, this, &GroupExportForm::modelDataChanged);
ui->kanjiTree->expandAll();
ui->wordsTree->expandAll();
modelDataChanged();
}
void GroupExportForm::translateTexts()
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(qApp->translate("ButtonBox", "Export"));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(qApp->translate("ButtonBox", "Cancel"));
}
//-------------------------------------------------------------