~ubuntu-branches/ubuntu/lucid/kkbswitch/lucid

« back to all changes in this revision

Viewing changes to kkbswitch/kbconfigdlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lior Kaplan
  • Date: 2005-09-07 02:01:14 UTC
  • Revision ID: james.westby@ubuntu.com-20050907020114-2wyo9eu21uihq86n
Tags: upstream-1.4.3
ImportĀ upstreamĀ versionĀ 1.4.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          kbconfigdlg.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sun Jul 8 2001
 
5
    copyright            : (C) 2001 by Leonid Zeitlin
 
6
    email                : lz@europe.com
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
#include "kbconfigdlg.h"
 
19
#include "kbpickicondlg.h"
 
20
#include "boldlistboxitem.h"
 
21
 
 
22
#include <qlayout.h>
 
23
#include <qlabel.h>
 
24
#include <qcheckbox.h>
 
25
#include <qwhatsthis.h>
 
26
#include <qvbox.h>
 
27
#include <qstyle.h>
 
28
#include <qgroupbox.h>
 
29
#include <qheader.h>
 
30
#include <qobjectlist.h>
 
31
#include <qpushbutton.h>
 
32
#include <qcombobox.h>
 
33
 
 
34
#include <kdeversion.h>
 
35
#include <klistbox.h>
 
36
#include <klocale.h>
 
37
#include <kdebug.h>
 
38
#if KDE_VERSION_MAJOR >= 3
 
39
  #include <kapplication.h>
 
40
#else
 
41
  #include <kapp.h>
 
42
#endif
 
43
#include <klistview.h>
 
44
#include <kkeydialog.h>
 
45
#include <kconfig.h>
 
46
 
 
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
 
50
 */
 
51
class SmallerKeyChooser : public KKeyChooser {
 
52
private:
 
53
  QSize m_size_hint;
 
54
  void calcSizeHint();
 
55
public:
 
56
  SmallerKeyChooser(KGlobalAccel *accel, QWidget *parent) :
 
57
    KKeyChooser(accel, parent) { calcSizeHint(); }
 
58
  virtual QSize sizeHint() const { return m_size_hint; }
 
59
};
 
60
 
 
61
void SmallerKeyChooser::calcSizeHint()
 
62
{
 
63
        m_size_hint = KKeyChooser::sizeHint();
 
64
 
 
65
  KListView *lv = NULL;
 
66
  QGroupBox *gb = NULL;
 
67
  const QObjectList *objects = children();
 
68
  QObjectListIt iter(*objects);
 
69
  QObject *obj;
 
70
 
 
71
  while ( (obj = iter.current()) ) {
 
72
                ++iter;
 
73
                if (obj->inherits("KListView"))
 
74
                  lv = dynamic_cast<KListView*>(obj);
 
75
                else if (obj->inherits("QGroupBox"))
 
76
                  gb = dynamic_cast<QGroupBox*>(obj);  
 
77
        }
 
78
        if (!lv || !gb) return; // oops, should not happen
 
79
        
 
80
        QListViewItem *item = lv->firstChild();
 
81
        if (!item) return; 
 
82
 
 
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);
 
88
}       
 
89
 
 
90
static inline bool iconTypeShowsFlag(KBConfig::IconStyle icon_style)
 
91
{
 
92
        return icon_style == KBConfig::ICON_FLAG || icon_style == KBConfig::ICON_CODE_AND_FLAG;
 
93
}
 
94
 
 
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*/)
 
98
{
 
99
  m_kbconf = kbconf;
 
100
  m_default_groupno = m_kbconf->default_groupno();
 
101
 
 
102
  setHelp(QString("configure-kkbswitch"));
 
103
  setButtonBoxOrientation(Horizontal);
 
104
  setupGeneralPage();
 
105
  setupShortcutsPage();
 
106
 
 
107
  showConfig();
 
108
  slotIconTypeSelected(m_kbconf->icon_style());
 
109
}
 
110
 
 
111
KBConfigDlg::~KBConfigDlg(){
 
112
}
 
113
 
 
114
void KBConfigDlg::setupGeneralPage()
 
115
{
 
116
  QFrame *page = addPage(i18n("&General")); //makeMainWidget();
 
117
  QVBoxLayout *vlayout = new QVBoxLayout(page);
 
118
  vlayout->setSpacing(2);
 
119
 
 
120
  QLabel *lbl = new QLabel(i18n("Available &keyboard layouts:"), page);
 
121
  vlayout->addWidget(lbl);
 
122
 
 
123
  QHBoxLayout *groupsLayout = new QHBoxLayout(vlayout);
 
124
  groupsLayout->setSpacing(2);
 
125
 
 
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."));
 
137
 
 
138
  QVBoxLayout *btnLayout = new QVBoxLayout(groupsLayout);
 
139
  btnLayout->setSpacing(2);
 
140
 
 
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."));
 
146
 
 
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."));
 
152
    
 
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"));
 
158
 
 
159
  btnLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Minimum,
 
160
    QSizePolicy::MinimumExpanding));
 
161
 
 
162
  lbl = new QLabel(i18n("Layout &icon style:"), page);
 
163
  vlayout->addWidget(lbl);
 
164
 
 
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."
 
179
    "</ul></p>"));
 
180
 
 
181
  lbl = new QLabel(i18n("&Layout applies to:"), page);
 
182
  vlayout->addWidget(lbl);
 
183
  
 
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>"));
 
198
  
 
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"));
 
204
 
 
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"));*/
 
210
 
 
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"));
 
215
}
 
216
        
 
217
void KBConfigDlg::setupShortcutsPage()
 
218
{
 
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"));
 
226
    
 
227
  keyChooser = new SmallerKeyChooser(m_kbconf->keys(), box);
 
228
}
 
229
 
 
230
/** Display the current KBSwitch configuration in the dialog */
 
231
void KBConfigDlg::showConfig(){
 
232
  int i;
 
233
  KBGroup *group;
 
234
  KConfig *conf = kapp->config();
 
235
 
 
236
  m_iconpaths.clear();
 
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());
 
244
  }
 
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());
 
254
}
 
255
 
 
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();
 
262
  if (dlg.exec()) {
 
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);
 
269
    }  
 
270
  }
 
271
}
 
272
 
 
273
void KBConfigDlg::slotSetDefaultGroup()
 
274
{
 
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));
 
285
      item->setBold(true);
 
286
      lbGroups->triggerUpdate(false);
 
287
    }  
 
288
  }
 
289
}
 
290
 
 
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);  
 
298
}
 
299
 
 
300
/** No descriptions */
 
301
void KBConfigDlg::slotListBoxExecuted(QListBoxItem *){
 
302
        if (iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())))
 
303
    slotPickIcon();
 
304
}
 
305
 
 
306
/** No descriptions */
 
307
void KBConfigDlg::saveConfig(){
 
308
  QString path;
 
309
  const QPixmap *pix;
 
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);
 
319
  }
 
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);
 
330
 
 
331
  //m_kbconf->notifyChanged();
 
332
  conf->sync();
 
333
}
 
334
 
 
335
/** No descriptions */
 
336
void KBConfigDlg::slotOk(){
 
337
  saveConfig();
 
338
  KDialogBase::slotOk();
 
339
}
 
340
 
 
341
/** No descriptions */
 
342
void KBConfigDlg::slotApply(){
 
343
  saveConfig();
 
344
  KDialogBase::slotApply();
 
345
}
 
346
 
 
347
void KBConfigDlg::slotIconTypeSelected(int index)
 
348
{
 
349
  KBConfig::IconStyle icon_style = KBConfig::IconStyle(index);
 
350
  redrawIcons(icon_style);
 
351
  slotLayoutSelected();
 
352
}
 
353
 
 
354
void KBConfigDlg::redrawIcons(KBConfig::IconStyle icon_style)
 
355
{
 
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);
 
365
  }
 
366
  lbGroups->setCurrentItem(curIndex);
 
367
}
 
368
 
 
369
void KBConfigDlg::slotCancel()
 
370
{
 
371
        KConfig *config = kapp->config();
 
372
        config->rollback();
 
373
        config->reparseConfiguration();
 
374
        KDialogBase::slotCancel();
 
375
}
 
376
 
 
377
void KBConfigDlg::slotUseShortcutsToggled(bool on)
 
378
{
 
379
        keyChooser->setEnabled(on);
 
380
}
 
381
 
 
382
 
 
383
/*!
 
384
    \fn KBConfigDlg::slotSetDefaultIcon()
 
385
 */
 
386
void KBConfigDlg::slotSetDefaultIcon()
 
387
{
 
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()));
 
392
}
 
393
 
 
394
 
 
395
/*!
 
396
    \fn KBConfigDlg::checkIconDefault()
 
397
 */
 
398
void KBConfigDlg::checkIconDefault(int index)
 
399
{
 
400
  KConfig *conf = kapp->config();
 
401
  if (index == -1 || ! iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem()))) {
 
402
    btnSetDefaultIcon->setEnabled(false);
 
403
  }  
 
404
  else {
 
405
    conf->setGroup(ICONS_SECTION);
 
406
    btnSetDefaultIcon->setEnabled(!conf->readEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/).isNull());
 
407
  }  
 
408
}