1
/***************************************************************************
2
kbconfigdlg.cpp - description
5
copyright : (C) 2001 by Leonid Zeitlin
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
18
#include "kbconfigdlg.h"
19
#include "kbpickicondlg.h"
20
#include "boldlistboxitem.h"
24
#include <qcheckbox.h>
25
#include <qwhatsthis.h>
28
#include <qgroupbox.h>
30
#include <qobjectlist.h>
31
#include <qpushbutton.h>
32
#include <qcombobox.h>
34
#include <kdeversion.h>
38
#if KDE_VERSION_MAJOR >= 3
39
#include <kapplication.h>
43
#include <klistview.h>
44
#include <kkeydialog.h>
47
/* This little subclass of KKeyChooser reimplements sizeHint() to
48
look a little smaller in our config dialog. The default size
49
is way too large IMHO and makes General page look oversized
51
class SmallerKeyChooser : public KKeyChooser {
56
SmallerKeyChooser(KGlobalAccel *accel, QWidget *parent) :
57
KKeyChooser(accel, parent) { calcSizeHint(); }
58
virtual QSize sizeHint() const { return m_size_hint; }
61
void SmallerKeyChooser::calcSizeHint()
63
m_size_hint = KKeyChooser::sizeHint();
67
const QObjectList *objects = children();
68
QObjectListIt iter(*objects);
71
while ( (obj = iter.current()) ) {
73
if (obj->inherits("KListView"))
74
lv = dynamic_cast<KListView*>(obj);
75
else if (obj->inherits("QGroupBox"))
76
gb = dynamic_cast<QGroupBox*>(obj);
78
if (!lv || !gb) return; // oops, should not happen
80
QListViewItem *item = lv->firstChild();
83
int height = item->height() * (1 + item->childCount()) + lv->header()->height() +
84
style().pixelMetric(QStyle::PM_ScrollBarExtent) + gb->sizeHint().height() +
85
layout()->spacing() * 2;
86
int width = lv->columnWidth(0) + lv->columnWidth(1);
87
m_size_hint = QSize(width, height);
90
static inline bool iconTypeShowsFlag(KBConfig::IconStyle icon_style)
92
return icon_style == KBConfig::ICON_FLAG || icon_style == KBConfig::ICON_CODE_AND_FLAG;
95
KBConfigDlg::KBConfigDlg(KBConfig *kbconf, QWidget *parent, const char *name )
96
: KDialogBase(Tabbed, i18n("Configure")/*caption*/, Ok | Apply | Cancel | Help,
97
Ok, parent, name, true /*modal*/)
100
m_default_groupno = m_kbconf->default_groupno();
102
setHelp(QString("configure-kkbswitch"));
103
setButtonBoxOrientation(Horizontal);
105
setupShortcutsPage();
108
slotIconTypeSelected(m_kbconf->icon_style());
111
KBConfigDlg::~KBConfigDlg(){
114
void KBConfigDlg::setupGeneralPage()
116
QFrame *page = addPage(i18n("&General")); //makeMainWidget();
117
QVBoxLayout *vlayout = new QVBoxLayout(page);
118
vlayout->setSpacing(2);
120
QLabel *lbl = new QLabel(i18n("Available &keyboard layouts:"), page);
121
vlayout->addWidget(lbl);
123
QHBoxLayout *groupsLayout = new QHBoxLayout(vlayout);
124
groupsLayout->setSpacing(2);
126
lbGroups = new KListBox(page);
127
QObject::connect(lbGroups, SIGNAL(selectionChanged()), this, SLOT(slotLayoutSelected()));
128
QObject::connect(lbGroups, SIGNAL(doubleClicked(QListBoxItem *)), this,
129
SLOT(slotListBoxExecuted(QListBoxItem *)));
130
groupsLayout->addWidget(lbGroups);
131
lbl->setBuddy(lbGroups);
132
QWhatsThis::add(lbGroups, i18n("This list box shows keyboard layouts available in your system.\n"
133
"Select a layout and click \"Change Icon...\" button to change the icon for a layout.\n"
134
"If you have configured a non-default icon, you can reset the icon to default with \"Use Default Icon\" button.\n"
135
"The layout shown is bold is the default layout. Use \"Set as default\" button "
136
"to set the default layout."));
138
QVBoxLayout *btnLayout = new QVBoxLayout(groupsLayout);
139
btnLayout->setSpacing(2);
141
btnChangeIcon = new QPushButton(i18n("Cha&nge Icon..."), page);
142
QObject::connect(btnChangeIcon, SIGNAL(clicked()), this, SLOT(slotPickIcon()));
143
btnLayout->addWidget(btnChangeIcon, 0, Qt::AlignTop);
144
QWhatsThis::add(btnChangeIcon, i18n("Click this button to change the icon for the "
145
"layout selected in the list box to the left."));
147
btnSetDefaultIcon = new QPushButton(i18n("Use &Default Icon"), page);
148
QObject::connect(btnSetDefaultIcon, SIGNAL(clicked()), this, SLOT(slotSetDefaultIcon()));
149
btnLayout->addWidget(btnSetDefaultIcon, 0, Qt::AlignTop);
150
QWhatsThis::add(btnSetDefaultIcon, i18n("Click this button to use default icon for the "
151
"layout selected in the list box to the left."));
153
btnSetDefaultGroup = new QPushButton(i18n("&Set as Default"), page);
154
QObject::connect(btnSetDefaultGroup, SIGNAL(clicked()), this, SLOT(slotSetDefaultGroup()));
155
btnLayout->addWidget(btnSetDefaultGroup, 0, Qt::AlignTop);
156
QWhatsThis::add(btnSetDefaultGroup, i18n("Cick this button to set the layout selected "
157
"in the list box to the left as the default"));
159
btnLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum,
160
QSizePolicy::MinimumExpanding));
162
lbl = new QLabel(i18n("Layout &icon style:"), page);
163
vlayout->addWidget(lbl);
165
cbxIconType = new QComboBox(page);
166
cbxIconType->setEditable(false);
167
cbxIconType->insertItem(i18n("Country flag"));
168
cbxIconType->insertItem(i18n("Language code"));
169
cbxIconType->insertItem(i18n("Flag and code"));
170
QObject::connect(cbxIconType, SIGNAL(activated(int)), this, SLOT(slotIconTypeSelected(int)));
171
vlayout->addWidget(cbxIconType);
172
lbl->setBuddy(cbxIconType);
173
QWhatsThis::add(cbxIconType, i18n("<p>Select the style of icons representing the "
174
"current keyboard layout.\n"
175
"You can choose from the following styles:"
176
"<ul><li><b>Country flag</b> - displays the corresponding contry flag"
177
"<li><b>Language code</b> - displays the language ISO 2-letter code"
178
"<li><b>Flag and code</b> - displays the language code superimposed over the country flag."
181
lbl = new QLabel(i18n("&Layout applies to:"), page);
182
vlayout->addWidget(lbl);
184
cbxGroupScope = new QComboBox(page);
185
cbxGroupScope->setEditable(false);
186
cbxGroupScope->insertItem(i18n("All windows"));
187
cbxGroupScope->insertItem(i18n("Windows of one application"));
188
cbxGroupScope->insertItem(i18n("One window"));
189
vlayout->addWidget(cbxGroupScope);
190
lbl->setBuddy(cbxGroupScope);
191
QWhatsThis::add(cbxGroupScope, i18n("<p>Select what windows the currently selected "
192
"keyboard layout applies to:\n"
193
"<ul><li><b>All windows</b> - one layout applies to all windows on your desktop\n"
194
"<li><b>Windows of one application</b> - layout applies to one application; each "
195
"application can have its own layout. When you switch between applications, layout follows the active application\n"
196
"<li><b>One window</b> - layout applies to one window only; each window can have its own layout. "
197
"When you switch between windows, layout follows the active window.</ul></p>"));
199
chkToggleMode = new QCheckBox(i18n("Use \"&Toggle Mode\""), page);
200
vlayout->addWidget(chkToggleMode);
201
QWhatsThis::add(chkToggleMode, i18n("Toggle mode is useful when you have more than two keyboard "
202
"layouts defined. When toggle mode is on your normal layout switch key toggles between two most frequently used layouts. "
203
"To activate other layouts use KKBSwitch's tray popup menu"));
205
/*chkPerwindowGroup = new QCheckBox(i18n("Use &per-window layout"), page);
206
vlayout->addWidget(chkPerwindowGroup);
207
QWhatsThis::add(chkPerwindowGroup, i18n("When this checkbox is checked, "
208
"each window has its own current keyboard layout. When it's unchecked, "
209
"the current layout affects all windows"));*/
211
chkAutostart = new QCheckBox(i18n("A&utostart"), page);
212
vlayout->addWidget(chkAutostart);
213
QWhatsThis::add(chkAutostart, i18n("When this checkbox is checked, KKBSwitch "
214
"will start automatically when you log in"));
217
void KBConfigDlg::setupShortcutsPage()
219
QVBox *box = addVBoxPage(i18n("Sho&rtcuts"));
220
chkUseShortcuts = new QCheckBox(i18n("Use shortcuts to &activate keyboard layouts"), box);
221
connect(chkUseShortcuts, SIGNAL(toggled(bool)), this, SLOT(slotUseShortcutsToggled(bool)));
222
QWhatsThis::add(chkUseShortcuts, i18n("Check this checkbox to be able to quickly "
223
"activate any keyboard layout with keyboard shorcuts. Once this checkbox "
224
"is checked, you can adjust the shortcuts at the key chooser pane below. "
225
"Especially useful if you have three or four keyboard layouts configured"));
227
keyChooser = new SmallerKeyChooser(m_kbconf->keys(), box);
230
/** Display the current KBSwitch configuration in the dialog */
231
void KBConfigDlg::showConfig(){
234
KConfig *conf = kapp->config();
237
conf->setGroup(ICONS_SECTION);
238
for (i = 0; i < m_kbconf->groupCount(); i++) {
239
group = m_kbconf->getGroup(i);
240
(void) new BoldListBoxItem(lbGroups, group->getPixmap(), group->getName(),
241
i == m_default_groupno);
242
//m_iconpaths.append(conf->readEntry(KBConfig::entryForGroup(i)));
243
m_iconpaths.append(group->getIconPath());
245
if (m_kbconf->groupCount() > 0) lbGroups->setCurrentItem(0);
246
lbGroups->setMinimumHeight(lbGroups->itemHeight() * (m_kbconf->groupCount() + 1));
247
cbxIconType->setCurrentItem(m_kbconf->icon_style());
248
cbxGroupScope->setCurrentItem(m_kbconf->group_scope());
249
chkToggleMode->setChecked(m_kbconf->toggle_mode());
250
//chkPerwindowGroup->setChecked(m_kbconf->perwindow_group());
251
chkAutostart->setChecked(m_kbconf->autostart());
252
chkUseShortcuts->setChecked(m_kbconf->use_shortcuts());
253
slotUseShortcutsToggled(m_kbconf->use_shortcuts());
256
/** Fire up "Pick Icon" dialog */
257
void KBConfigDlg::slotPickIcon(){
258
int index = lbGroups->currentItem();
259
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
260
KBPickIconDlg dlg(m_iconpaths[index], *(lbGroups->pixmap(index)), this);
261
QApplication::restoreOverrideCursor();
263
QString path = dlg.getIconPath();
264
KBGroup *group = m_kbconf->getGroup(index);
265
if (!path.isEmpty() && path != group->getIconPath()) {
266
kapp->config()->writeEntry(group->getName()/*KBConfig::entryForGroup(index)*/, path);
267
redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
268
checkIconDefault(index);
273
void KBConfigDlg::slotSetDefaultGroup()
275
int new_default_group = lbGroups->currentItem();
276
if (m_default_groupno != new_default_group) {
277
BoldListBoxItem *item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(m_default_groupno));
278
item->setBold(false);
279
m_default_groupno = new_default_group;
280
// NB: don't use selectedItem(); it's not present in Qt 3.0, and
281
// we want to maintain compatibility with it
282
int curitem = lbGroups->currentItem();
283
if (curitem > -1) { // should not be -1, but just in case
284
item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(curitem));
286
lbGroups->triggerUpdate(false);
291
/** No descriptions */
292
void KBConfigDlg::slotLayoutSelected(){
293
btnChangeIcon->setEnabled(lbGroups->currentItem() != -1 &&
294
iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())));
295
checkIconDefault(lbGroups->currentItem());
296
btnSetDefaultGroup->setEnabled(lbGroups->currentItem() != -1 &&
297
lbGroups->currentItem() != m_default_groupno);
300
/** No descriptions */
301
void KBConfigDlg::slotListBoxExecuted(QListBoxItem *){
302
if (iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())))
306
/** No descriptions */
307
void KBConfigDlg::saveConfig(){
310
KConfig *conf = kapp->config();
311
conf->setGroup(ICONS_SECTION);
312
for (int i = 0; i < m_kbconf->groupCount(); i++) {
313
path = m_iconpaths[i];
314
if (!path.isEmpty()) m_kbconf->getGroup(i)->setIconPath(path);
315
//if (!path.isEmpty() && path != m_kbconf->getGroup(i)->getIconPath())
316
// conf->writeEntry(KBConfig::entryForGroup(i), path);
317
pix = lbGroups->pixmap(i);
318
if (pix) m_kbconf->getGroup(i)->setPixmap(*pix);
320
m_kbconf->set_toggle_mode(chkToggleMode->isChecked());
321
m_kbconf->set_group_scope(KBConfig::GroupScope(cbxGroupScope->currentItem()));
322
m_kbconf->set_default_groupno(m_default_groupno);
323
//m_kbconf->set_perwindow_group(chkPerwindowGroup->isChecked());
324
m_kbconf->set_icon_style(KBConfig::IconStyle(cbxIconType->currentItem()));
325
m_kbconf->set_autostart(chkAutostart->isChecked());
326
m_kbconf->set_use_shortcuts(chkUseShortcuts->isChecked());
327
keyChooser->commitChanges();
328
m_kbconf->checkKeysEnabled();
329
m_kbconf->save(conf);
331
//m_kbconf->notifyChanged();
335
/** No descriptions */
336
void KBConfigDlg::slotOk(){
338
KDialogBase::slotOk();
341
/** No descriptions */
342
void KBConfigDlg::slotApply(){
344
KDialogBase::slotApply();
347
void KBConfigDlg::slotIconTypeSelected(int index)
349
KBConfig::IconStyle icon_style = KBConfig::IconStyle(index);
350
redrawIcons(icon_style);
351
slotLayoutSelected();
354
void KBConfigDlg::redrawIcons(KBConfig::IconStyle icon_style)
356
QValueVector<QPixmap> pixlist;
357
m_kbconf->drawIcons(icon_style, &pixlist, &m_iconpaths);
358
int curIndex = lbGroups->currentItem();
359
// NB: don't use count(), use size(); count() is not present in Qt 3.0, and
360
// we want to maintain compatibility with it
361
for (unsigned int i = 0; i < pixlist.size(); i++) {
362
BoldListBoxItem *curItem = dynamic_cast<BoldListBoxItem*>(lbGroups->item(i));
363
lbGroups->changeItem(new BoldListBoxItem(NULL, pixlist[i],
364
curItem->text(), curItem->bold()), i);
366
lbGroups->setCurrentItem(curIndex);
369
void KBConfigDlg::slotCancel()
371
KConfig *config = kapp->config();
373
config->reparseConfiguration();
374
KDialogBase::slotCancel();
377
void KBConfigDlg::slotUseShortcutsToggled(bool on)
379
keyChooser->setEnabled(on);
384
\fn KBConfigDlg::slotSetDefaultIcon()
386
void KBConfigDlg::slotSetDefaultIcon()
388
int index = lbGroups->currentItem();
389
kapp->config()->setGroup(ICONS_SECTION);
390
kapp->config()->deleteEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/);
391
redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
396
\fn KBConfigDlg::checkIconDefault()
398
void KBConfigDlg::checkIconDefault(int index)
400
KConfig *conf = kapp->config();
401
if (index == -1 || ! iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem()))) {
402
btnSetDefaultIcon->setEnabled(false);
405
conf->setGroup(ICONS_SECTION);
406
btnSetDefaultIcon->setEnabled(!conf->readEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/).isNull());