1
/***************************************************************************
2
* Copyright (C) 2010 by Kai Dombrowe <just89@gmx.de> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
22
#include "keyboardconfig.h"
23
#include "keyboardwizard.h"
24
#include "devicesearchdialog.h"
28
#include <kmessagebox.h>
29
#include <kconfiggroup.h>
32
#include <QtGui/QListWidget>
35
KeyboardConfig::KeyboardConfig(KConfig *cfg, QWidget *parent)
36
: RecordItNow::ConfigPage(cfg, parent)
41
addButton->setIcon(KIcon("list-add"));
42
removeButton->setIcon(KIcon("list-remove"));
43
upButton->setIcon(KIcon("go-up"));
44
downButton->setIcon(KIcon("go-down"));
45
searchButton->setIcon(KIcon("system-search"));
47
connect(addButton, SIGNAL(clicked()), this, SLOT(add()));
48
connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
49
connect(upButton, SIGNAL(clicked()), this, SLOT(up()));
50
connect(downButton, SIGNAL(clicked()), this, SLOT(down()));
51
connect(listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
52
connect(searchButton, SIGNAL(clicked()), this, SLOT(showSearchDialog()));
53
connect(kcfg_keyboardDevice, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
55
itemSelectionChanged();
60
QList<KeyboardKey> KeyboardConfig::readConfig(KConfig *cfg)
63
KConfigGroup group(cfg, "Keyboard");
64
const int count = group.readEntry("Keys", 0);
65
QList<KeyboardKey> keyMap;
67
for (int i = 0; i < count; i++) {
68
const int key = group.readEntry(QString("Key %1 Code").arg(i), -1);
69
const QString icon = group.readEntry(QString("Key %1 Icon").arg(i), QString());
70
const QString text = group.readEntry(QString("Key %1 Text").arg(i), QString());
72
keyMap.append(KeyboardKey(key, icon, text));
80
void KeyboardConfig::loadConfig()
83
QList<KeyboardKey> list(readConfig(config()));
84
foreach (const KeyboardKey &key, list) {
85
QListWidgetItem *item = new QListWidgetItem();
86
item->setText(key.text());
87
item->setIcon(KIcon(key.icon()));
88
item->setData(Qt::UserRole+1, key.code());
89
item->setData(Qt::UserRole+2, key.icon());
91
listWidget->addItem(item);
97
void KeyboardConfig::saveConfig()
100
KConfigGroup group(config(), "Keyboard");
102
for (int i = 0; i < listWidget->count(); i++) {
103
QListWidgetItem *item = listWidget->item(i);
104
group.writeEntry(QString("Key %1 Code").arg(i), item->data(Qt::UserRole+1).toInt());
105
group.writeEntry(QString("Key %1 Icon").arg(i), item->data(Qt::UserRole+2).toString());
106
group.writeEntry(QString("Key %1 Text").arg(i), item->text());
109
group.writeEntry("Keys", count);
114
void KeyboardConfig::setDefaults()
122
void KeyboardConfig::add()
125
KeyboardWizard *wizard = new KeyboardWizard(kcfg_keyboardDevice->text(), this);
126
connect(wizard, SIGNAL(wizardFinished(int, QString, QString)), this,
127
SLOT(wizardFinished(int, QString, QString)));
134
void KeyboardConfig::remove()
137
QList<QListWidgetItem*> items = listWidget->selectedItems();
138
if (items.isEmpty()) {
142
QListWidgetItem *item = items.first();
144
listWidget->takeItem(listWidget->row(item));
147
emit configChanged();
152
void KeyboardConfig::up()
155
QList<QListWidgetItem*> items = listWidget->selectedItems();
156
if (items.isEmpty()) {
160
QListWidgetItem *item = items.first();
161
const int index = listWidget->row(item);
163
listWidget->takeItem(index);
164
listWidget->insertItem(index-1, item);
166
listWidget->setCurrentItem(item);
168
emit configChanged();
173
void KeyboardConfig::down()
176
QList<QListWidgetItem*> items = listWidget->selectedItems();
177
if (items.isEmpty()) {
181
QListWidgetItem *item = items.first();
182
const int index = listWidget->row(item);
184
listWidget->takeItem(index);
185
listWidget->insertItem(index+1, item);
187
listWidget->setCurrentItem(item);
189
emit configChanged();
194
void KeyboardConfig::itemSelectionChanged()
197
if(listWidget->selectedItems().isEmpty()) {
198
upButton->setEnabled(false);
199
downButton->setEnabled(false);
200
removeButton->setEnabled(false);
202
const int index = listWidget->row(listWidget->selectedItems().first());
203
upButton->setEnabled(index != 0);
204
downButton->setEnabled(index != listWidget->count()-1);
205
removeButton->setEnabled(true);
211
void KeyboardConfig::wizardFinished(const int &key, const QString &icon, const QString &text)
214
for (int i = 0; i < listWidget->count(); i++) {
215
if (listWidget->item(i)->data(Qt::UserRole+1).toInt() == key) {
216
KMessageBox::error(this, i18n("There is already a key with code '%1' in your list!", key));
222
QListWidgetItem *item = new QListWidgetItem();
224
item->setIcon(KIcon(icon));
225
item->setData(Qt::UserRole+1, key);
226
item->setData(Qt::UserRole+2, icon);
228
listWidget->addItem(item);
230
emit configChanged();
235
void KeyboardConfig::showSearchDialog()
238
DeviceSearchDialog *dialog = new DeviceSearchDialog(KeyMon::DeviceInfo::KeyboardType, this);
239
connect(dialog, SIGNAL(deviceSelected(QString)), this, SLOT(searchDialogFinished(QString)));
246
void KeyboardConfig::searchDialogFinished(const QString &uuid)
249
kcfg_keyboardDevice->setText(uuid);
254
void KeyboardConfig::textChanged(const QString &text)
257
addButton->setDisabled(text.isEmpty());
262
#include "keyboardconfig.moc"