-
Notifications
You must be signed in to change notification settings - Fork 10
/
grouppickerform.cpp
155 lines (124 loc) · 4.56 KB
/
grouppickerform.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
/*
** 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 "zui.h"
#include "ui_grouppickerform.h"
#include "grouppickerform.h"
#include "words.h"
#include "groups.h"
#include "zabstracttreemodel.h"
#include "globalui.h"
#include "formstates.h"
//-------------------------------------------------------------
/* static */
GroupBase* GroupPickerForm::select(GroupWidget::Modes mode, const QString &instructions, Dictionary *dict, bool onlycategories, bool showdicts, QWidget *parent)
{
GroupPickerForm *f = new GroupPickerForm(mode, onlycategories, parent);
if (!showdicts)
f->hideDictionarySelect();
f->setDictionary(dict);
f->setInstructions(instructions);
FormStates::restoreDialogSize("GroupPicker", f, true);
GroupBase *result = nullptr;
connect(f, &GroupPickerForm::groupSelected, [&result](Dictionary * /*dict*/, GroupBase *group) {
result = group;
});
f->showModal();
return result;
}
GroupPickerForm::GroupPickerForm(bool onlycategories, QWidget *parent) : base(parent), ui(new Ui::GroupPickerForm), selectButton(nullptr)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
gUI->scaleWidget(this);
ui->instructionLabel->hide();
ui->groupWidget->setDictionary(ZKanji::dictionary(0));
ui->dictCBox->setCurrentIndex(ZKanji::dictionaryOrder(0));
ui->groupWidget->setMode(GroupWidget::Words);
if (onlycategories)
ui->groupWidget->showOnlyCategories();
selectButton = ui->buttonBox->addButton(QString(), QDialogButtonBox::AcceptRole);
translateTexts();
connect(selectButton, &QPushButton::clicked, this, &GroupPickerForm::selectClicked);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &GroupPickerForm::close);
}
GroupPickerForm::GroupPickerForm(GroupWidget::Modes mode, bool onlycategories, QWidget *parent) : base(parent), ui(new Ui::GroupPickerForm), selectButton(nullptr)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setAttribute(Qt::WA_QuitOnClose, false);
gUI->scaleWidget(this);
ui->instructionLabel->hide();
ui->groupWidget->setDictionary(ZKanji::dictionary(0));
ui->dictCBox->setCurrentIndex(ZKanji::dictionaryOrder(0));
ui->groupWidget->setMode(mode);
if (onlycategories)
ui->groupWidget->showOnlyCategories();
selectButton = ui->buttonBox->addButton(QString(), QDialogButtonBox::AcceptRole);
translateTexts();
connect(selectButton, &QPushButton::clicked, this, &GroupPickerForm::selectClicked);
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &GroupPickerForm::close);
}
GroupPickerForm::~GroupPickerForm()
{
delete ui;
}
void GroupPickerForm::hideDictionarySelect()
{
ui->dictWidget->hide();
}
void GroupPickerForm::setDictionary(Dictionary *dict)
{
if (ui->dictWidget->isVisibleTo(this))
ui->dictCBox->setCurrentIndex(ZKanji::dictionaryOrder(ZKanji::dictionaryIndex(dict)));
ui->groupWidget->setDictionary(dict);
}
void GroupPickerForm::setMode(GroupWidget::Modes mode)
{
ui->groupWidget->setMode(mode);
}
void GroupPickerForm::setInstructions(const QString &instructions)
{
if (instructions.isEmpty())
ui->instructionLabel->hide();
else
{
ui->instructionLabel->show();
ui->instructionLabel->setText(instructions);
}
}
bool GroupPickerForm::event(QEvent *e)
{
if (e->type() == QEvent::LanguageChange)
{
ui->retranslateUi(this);
translateTexts();
}
return base::event(e);
}
void GroupPickerForm::translateTexts()
{
if (selectButton != nullptr)
selectButton->setText(tr("Select group"));
ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(qApp->translate("ButtonBox", "Cancel"));
}
void GroupPickerForm::selectClicked()
{
emit groupSelected(ui->groupWidget->dictionary(), ui->groupWidget->current());
modalClose(ModalResult::Ok);
}
void GroupPickerForm::on_groupWidget_currentItemChanged(GroupBase *current)
{
if (selectButton != nullptr)
selectButton->setEnabled(current != nullptr && (ui->groupWidget->onlyCategories() || !current->isCategory()));
}
void GroupPickerForm::on_dictCBox_currentIndexChanged(int index)
{
if (!ui->dictCBox->isVisibleTo(this))
return;
ui->groupWidget->setDictionary(ZKanji::dictionary(ZKanji::dictionaryPosition(index)));
}
//-------------------------------------------------------------