-
Notifications
You must be signed in to change notification settings - Fork 0
/
keydialog.cpp
168 lines (153 loc) · 4.99 KB
/
keydialog.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
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2016 Arek <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "keydialog.h"
#include <QtGui/QKeyEvent>
#include <QtCore/QtGlobal>
#include <QAbstractButton>
#include <QMessageBox>
#include <QDebug>
#include "keyboardview.h"
KeyDialog::KeyDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f)
{
ui = new Ui_KeyDialog();
ui->setupUi(this);
ui->mainLabelLineEdit->installEventFilter(this);
ui->secondLabelLineEdit->installEventFilter(this);
setModal(true);
resetCurrentItems();
connect(ui->styleChooser, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
this, SLOT(styleChanged(QListWidgetItem*)));
foreach(QAbstractButton* button, ui->buttonBox->buttons())
button->setFocusPolicy(Qt::NoFocus);
styleDialog = new StyleDialog();
connect(ui->addStyleButton, SIGNAL(pressed()), SLOT(addStyle()));
connect(ui->editStyleButton, SIGNAL(pressed()), SLOT(editStyle()));
connect(ui->deleteStyleButton, SIGNAL(pressed()), SLOT(deleteStyle()));
connect((KeyboardView*) parent, SIGNAL(zoomChanged(double)),
styleDialog, SLOT(changeScale(double)));
}
void KeyDialog::styleChanged(QListWidgetItem* item)
{
if (focusChanging)
return;
if (ui->secondLabelLineEdit->hasFocus())
currentItems[1] = item;
else
currentItems[0] = item;
}
int KeyDialog::exec(KeyItem* key)
{
if (key == nullptr) {
qDebug() << "can't open dialog for unknown key!";
return Rejected;
}
setWindowTitle(tr("Set labels for ") + key->keyId);
if (key == nullptr || ui->styleChooser->count() == 0)
return QDialog::Rejected;
keyItem = key;
ui->mainLabelLineEdit->setFocus();
if (keyItem->labelTop != "") {
ui->styleChooser->setCurrentText(key->style[0]);
currentItems[0] = ui->styleChooser->findItem(key->style[0]);
}
if (keyItem->labelTop != "")
currentItems[1] = ui->styleChooser->findItem(key->style[1]);
ui->mainLabelLineEdit->setText(key->labelTop);
ui->secondLabelLineEdit->setText(key->labelBottom);
int result = QDialog::exec();
if (result == Accepted)
{
keyItem->labelTop = ui->mainLabelLineEdit->text();
keyItem->labelBottom = ui->secondLabelLineEdit->text();
keyItem->setStyle(currentItems[0]->text(), 0);
if (keyItem->labelBottom != "")
keyItem->setStyle(currentItems[1]->text(), 1);
keyItem->updateItems();
}
return result;
}
bool KeyDialog::eventFilter(QObject* obj, QEvent* event)
{
if (obj == ui->mainLabelLineEdit || obj == ui->secondLabelLineEdit)
{
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
{
QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Down || keyEvent->key() == Qt::Key_Up)
return QApplication::sendEvent(ui->styleChooser, event);
}
if (event->type() == QEvent::FocusOut)
{
focusChanging = true;
if (obj == ui->mainLabelLineEdit)
currentItems[0] = ui->styleChooser->currentItem();
else
currentItems[1] = ui->styleChooser->currentItem();
}
if (event->type() == QEvent::FocusIn)
{
checkCurrentItems();
if (obj == ui->mainLabelLineEdit)
ui->styleChooser->setCurrentItem(currentItems[0]);
else
ui->styleChooser->setCurrentItem(currentItems[1]);
focusChanging = false;
}
}
return QDialog::eventFilter(obj, event);
}
void KeyDialog::resetCurrentItems()
{
currentItems[0] = currentItems[1] = ui->styleChooser->getDefault();
}
void KeyDialog::addStyle()
{
int result = styleDialog->exec(StyleModel::model->styles[ui->styleChooser->currentItem()->text()], false);
if (result == Accepted)
{
Style *style = new Style(styleDialog->style);
StyleModel::model->addStyle(style);
}
}
void KeyDialog::deleteStyle()
{
QString name = ui->styleChooser->currentItem()->text();
if (QMessageBox::question(this, "Really Delete?", "Really delete style " + name)
== QMessageBox::Yes) {
// FIXME occasional crash when style is used on some key -> race conditions
StyleModel::model->deleteStyle(name);
}
}
void KeyDialog::editStyle()
{
Style *edStyle = StyleModel::model->styles[ui->styleChooser->currentItem()->text()];
int result = styleDialog->exec(edStyle, true);
if (result == Accepted)
{
(*edStyle) = styleDialog->style;
StyleModel::model->styleChangedOk(edStyle);
}
}
void KeyDialog::checkCurrentItems()
{
for (int i = 0; i < 2; i++) {
if (!ui->styleChooser->isValidItem(currentItems[i]))
currentItems[i] = ui->styleChooser->getDefault();
}
}