summaryrefslogtreecommitdiffstats
path: root/kkbswitch/kbconfigdlg.cpp
blob: e299aea67e29aebcc862788f02c9c928c904e96d (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/***************************************************************************
                          kbconfigdlg.cpp  -  description
                             -------------------
    begin                : Sun Jul 8 2001
    copyright            : (C) 2001 by Leonid Zeitlin
    email                : lz@europe.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kbconfigdlg.h"
#include "kbpickicondlg.h"
#include "boldlistboxitem.h"

#include <ntqlayout.h>
#include <ntqlabel.h>
#include <ntqcheckbox.h>
#include <ntqwhatsthis.h>
#include <ntqvbox.h>
#include <ntqstyle.h>
#include <ntqgroupbox.h>
#include <ntqheader.h>
#include <ntqobjectlist.h>
#include <ntqpushbutton.h>
#include <ntqcombobox.h>

#include <tdeversion.h>
#include <tdelistbox.h>
#include <tdelocale.h>
#include <kdebug.h>
#if TDE_VERSION_MAJOR >= 3
  #include <tdeapplication.h>
#else
  #include <kapp.h>
#endif
#include <tdelistview.h>
#include <kkeydialog.h>
#include <tdeconfig.h>

/* This little subclass of KKeyChooser reimplements sizeHint() to
   look a little smaller in our config dialog. The default size
   is way too large IMHO and makes General page look oversized
 */
class SmallerKeyChooser : public KKeyChooser {
private:
  TQSize m_size_hint;
  void calcSizeHint();
public:
  SmallerKeyChooser(TDEGlobalAccel *accel, TQWidget *parent) :
    KKeyChooser(accel, parent) { calcSizeHint(); }
  virtual TQSize sizeHint() const { return m_size_hint; }
};

void SmallerKeyChooser::calcSizeHint()
{
	m_size_hint = KKeyChooser::sizeHint();

  TDEListView *lv = NULL;
  TQGroupBox *gb = NULL;
  const TQObjectList *objects = children();
  TQObjectListIt iter(*objects);
  TQObject *obj;

  while ( (obj = iter.current()) ) {
		++iter;
		if (obj->inherits("TDEListView"))
		  lv = dynamic_cast<TDEListView*>(obj);
		else if (obj->inherits("TQGroupBox"))
		  gb = dynamic_cast<TQGroupBox*>(obj);  
	}
	if (!lv || !gb) return; // oops, should not happen
	
	TQListViewItem *item = lv->firstChild();
	if (!item) return; 

	int height = item->height() * (1 + item->childCount()) + lv->header()->height() +
	  style().pixelMetric(TQStyle::PM_ScrollBarExtent) + gb->sizeHint().height() +
	  layout()->spacing() * 2;
	int width = lv->columnWidth(0) + lv->columnWidth(1);
	m_size_hint = TQSize(width, height);
}	

static inline bool iconTypeShowsFlag(KBConfig::IconStyle icon_style)
{
	return icon_style == KBConfig::ICON_FLAG || icon_style == KBConfig::ICON_CODE_AND_FLAG;
}

KBConfigDlg::KBConfigDlg(KBConfig *kbconf, TQWidget *parent, const char *name )
  : KDialogBase(Tabbed, i18n("Configure")/*caption*/, Ok | Apply | Cancel | Help,
      Ok, parent, name, true /*modal*/)
{
  m_kbconf = kbconf;
  m_default_groupno = m_kbconf->default_groupno();

  setHelp(TQString("configure-kkbswitch"));
  setButtonBoxOrientation(Horizontal);
  setupGeneralPage();
  setupShortcutsPage();

  showConfig();
  slotIconTypeSelected(m_kbconf->icon_style());
}

KBConfigDlg::~KBConfigDlg(){
}

void KBConfigDlg::setupGeneralPage()
{
  TQFrame *page = addPage(i18n("&General")); //makeMainWidget();
  TQVBoxLayout *vlayout = new TQVBoxLayout(page);
  vlayout->setSpacing(2);

  TQLabel *lbl = new TQLabel(i18n("Available &keyboard layouts:"), page);
  vlayout->addWidget(lbl);

  TQHBoxLayout *groupsLayout = new TQHBoxLayout(vlayout);
  groupsLayout->setSpacing(2);

  lbGroups = new TDEListBox(page);
  TQObject::connect(lbGroups, SIGNAL(selectionChanged()), this, SLOT(slotLayoutSelected()));
  TQObject::connect(lbGroups, SIGNAL(doubleClicked(TQListBoxItem *)), this,
    SLOT(slotListBoxExecuted(TQListBoxItem *)));
  groupsLayout->addWidget(lbGroups);
  lbl->setBuddy(lbGroups);
  TQWhatsThis::add(lbGroups, i18n("This list box shows keyboard layouts available in your system.\n"
    "Select a layout and click \"Change Icon...\" button to change the icon for a layout.\n"
    "If you have configured a non-default icon, you can reset the icon to default with \"Use Default Icon\" button.\n"
    "The layout shown is bold is the default layout. Use \"Set as default\" button "
    "to set the default layout."));

  TQVBoxLayout *btnLayout = new TQVBoxLayout(groupsLayout);
  btnLayout->setSpacing(2);

  btnChangeIcon = new TQPushButton(i18n("Cha&nge Icon..."), page);
  TQObject::connect(btnChangeIcon, SIGNAL(clicked()), this, SLOT(slotPickIcon()));
  btnLayout->addWidget(btnChangeIcon, 0, TQt::AlignTop);
  TQWhatsThis::add(btnChangeIcon, i18n("Click this button to change the icon for the "
    "layout selected in the list box to the left."));

  btnSetDefaultIcon = new TQPushButton(i18n("Use &Default Icon"), page);
  TQObject::connect(btnSetDefaultIcon, SIGNAL(clicked()), this, SLOT(slotSetDefaultIcon()));
  btnLayout->addWidget(btnSetDefaultIcon, 0, TQt::AlignTop); 
  TQWhatsThis::add(btnSetDefaultIcon, i18n("Click this button to use default icon for the " 
    "layout selected in the list box to the left."));
    
  btnSetDefaultGroup = new TQPushButton(i18n("&Set as Default"), page);
  TQObject::connect(btnSetDefaultGroup, SIGNAL(clicked()), this, SLOT(slotSetDefaultGroup()));
  btnLayout->addWidget(btnSetDefaultGroup, 0, TQt::AlignTop);
  TQWhatsThis::add(btnSetDefaultGroup, i18n("Click this button to set the layout selected "
    "in the list box to the left as the default"));

  btnLayout->addItem(new TQSpacerItem(1, 1, TQSizePolicy::Minimum,
    TQSizePolicy::MinimumExpanding));

  lbl = new TQLabel(i18n("Layout &icon style:"), page);
  vlayout->addWidget(lbl);

  cbxIconType = new TQComboBox(page);
  cbxIconType->setEditable(false);
  cbxIconType->insertItem(i18n("Country flag"));
  cbxIconType->insertItem(i18n("Language code"));
  cbxIconType->insertItem(i18n("Flag and code"));
  TQObject::connect(cbxIconType, SIGNAL(activated(int)), this, SLOT(slotIconTypeSelected(int)));
  vlayout->addWidget(cbxIconType);
  lbl->setBuddy(cbxIconType);
  TQWhatsThis::add(cbxIconType, i18n("<p>Select the style of icons representing the "
    "current keyboard layout.\n"
    "You can choose from the following styles:"
    "<ul><li><b>Country flag</b> - displays the corresponding country flag"
    "<li><b>Language code</b> - displays the language ISO 2-letter code"
    "<li><b>Flag and code</b> - displays the language code superimposed over the country flag."
    "</ul></p>"));

  lbl = new TQLabel(i18n("&Layout applies to:"), page);
  vlayout->addWidget(lbl);
  
  cbxGroupScope = new TQComboBox(page);
  cbxGroupScope->setEditable(false);
  cbxGroupScope->insertItem(i18n("All windows"));
  cbxGroupScope->insertItem(i18n("Windows of one application"));
  cbxGroupScope->insertItem(i18n("One window"));
  vlayout->addWidget(cbxGroupScope);
  lbl->setBuddy(cbxGroupScope);
   TQWhatsThis::add(cbxGroupScope, i18n("<p>Select what windows the currently selected "
     "keyboard layout applies to:\n"
     "<ul><li><b>All windows</b> - one layout applies to all windows on your desktop\n"
     "<li><b>Windows of one application</b> - layout applies to one application; each " 
       "application can have its own layout. When you switch between applications, layout follows the active application\n"
     "<li><b>One window</b> - layout applies to one window only; each window can have its own layout. "
     "When you switch between windows, layout follows the active window.</ul></p>"));
  
  chkToggleMode = new TQCheckBox(i18n("Use \"&Toggle Mode\""), page);
  vlayout->addWidget(chkToggleMode);
  TQWhatsThis::add(chkToggleMode, i18n("Toggle mode is useful when you have more than two keyboard "
    "layouts defined. When toggle mode is on your normal layout switch key toggles between two most frequently used layouts. "
    "To activate other layouts use KKBSwitch's tray popup menu"));

  /*chkPerwindowGroup = new TQCheckBox(i18n("Use &per-window layout"), page);
  vlayout->addWidget(chkPerwindowGroup);
  TQWhatsThis::add(chkPerwindowGroup, i18n("When this checkbox is checked, "
    "each window has its own current keyboard layout. When it's unchecked, "
    "the current layout affects all windows"));*/

  chkAutostart = new TQCheckBox(i18n("A&utostart"), page);
  vlayout->addWidget(chkAutostart);
  TQWhatsThis::add(chkAutostart, i18n("When this checkbox is checked, KKBSwitch "
    "will start automatically when you log in"));
}
	
void KBConfigDlg::setupShortcutsPage()
{
  TQVBox *box = addVBoxPage(i18n("Sho&rtcuts"));
  chkUseShortcuts = new TQCheckBox(i18n("Use shortcuts to &activate keyboard layouts"), box);
  connect(chkUseShortcuts, SIGNAL(toggled(bool)), this, SLOT(slotUseShortcutsToggled(bool)));
  TQWhatsThis::add(chkUseShortcuts, i18n("Check this checkbox to be able to quickly "
    "activate any keyboard layout with keyboard shorcuts. Once this checkbox "
    "is checked, you can adjust the shortcuts at the key chooser pane below. "
    "Especially useful if you have three or four keyboard layouts configured"));
    
  keyChooser = new SmallerKeyChooser(m_kbconf->keys(), box);
}

/** Display the current KBSwitch configuration in the dialog */
void KBConfigDlg::showConfig(){
  int i;
  KBGroup *group;
  TDEConfig *conf = kapp->config();

  m_iconpaths.clear();
  conf->setGroup(ICONS_SECTION);
  for (i = 0; i < m_kbconf->groupCount(); i++) {
    group = m_kbconf->getGroup(i);
    (void) new BoldListBoxItem(lbGroups, group->getPixmap(), group->getName(),
      i == m_default_groupno);
    //m_iconpaths.append(conf->readEntry(KBConfig::entryForGroup(i)));
    m_iconpaths.append(group->getIconPath());
  }
  if (m_kbconf->groupCount() > 0) lbGroups->setCurrentItem(0);
  lbGroups->setMinimumHeight(lbGroups->itemHeight() * (m_kbconf->groupCount() + 1));
  cbxIconType->setCurrentItem(m_kbconf->icon_style());
  cbxGroupScope->setCurrentItem(m_kbconf->group_scope());
  chkToggleMode->setChecked(m_kbconf->toggle_mode());
  //chkPerwindowGroup->setChecked(m_kbconf->perwindow_group());
  chkAutostart->setChecked(m_kbconf->autostart());
  chkUseShortcuts->setChecked(m_kbconf->use_shortcuts());
  slotUseShortcutsToggled(m_kbconf->use_shortcuts());
}

/** Fire up "Pick Icon" dialog */
void KBConfigDlg::slotPickIcon(){
	int index = lbGroups->currentItem();
  TQApplication::setOverrideCursor(TQCursor(TQt::WaitCursor));
  KBPickIconDlg dlg(m_iconpaths[index], *(lbGroups->pixmap(index)), this);
  TQApplication::restoreOverrideCursor();
  if (dlg.exec()) {
		TQString path = dlg.getIconPath();
    KBGroup *group = m_kbconf->getGroup(index);
		if (!path.isEmpty() && path != group->getIconPath()) {
  		kapp->config()->writeEntry(group->getName()/*KBConfig::entryForGroup(index)*/, path);
  		redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
      checkIconDefault(index);
    }  
  }
}

void KBConfigDlg::slotSetDefaultGroup()
{
  int new_default_group = lbGroups->currentItem();
  if (m_default_groupno != new_default_group) {
		BoldListBoxItem *item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(m_default_groupno));
		item->setBold(false);
    m_default_groupno = new_default_group;
    // NB: don't use selectedItem(); it's not present in TQt 3.0, and
    // we want to maintain compatibility with it
    int curitem = lbGroups->currentItem();
    if (curitem > -1) { // should not be -1, but just in case
      item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(curitem));
      item->setBold(true);
      lbGroups->triggerUpdate(false);
    }  
  }
}

/** No descriptions */
void KBConfigDlg::slotLayoutSelected(){
  btnChangeIcon->setEnabled(lbGroups->currentItem() != -1 &&
    iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())));
  checkIconDefault(lbGroups->currentItem());  
  btnSetDefaultGroup->setEnabled(lbGroups->currentItem() != -1 &&
    lbGroups->currentItem() != m_default_groupno);  
}

/** No descriptions */
void KBConfigDlg::slotListBoxExecuted(TQListBoxItem *){
	if (iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())))
    slotPickIcon();
}

/** No descriptions */
void KBConfigDlg::saveConfig(){
  TQString path;
  const TQPixmap *pix;
  TDEConfig *conf = kapp->config();
  conf->setGroup(ICONS_SECTION);
  for (int i = 0; i < m_kbconf->groupCount(); i++) {
    path = m_iconpaths[i];
    if (!path.isEmpty()) m_kbconf->getGroup(i)->setIconPath(path);
    //if (!path.isEmpty() && path != m_kbconf->getGroup(i)->getIconPath())
    //  conf->writeEntry(KBConfig::entryForGroup(i), path);
    pix = lbGroups->pixmap(i);
    if (pix) m_kbconf->getGroup(i)->setPixmap(*pix);
  }
  m_kbconf->set_toggle_mode(chkToggleMode->isChecked());
  m_kbconf->set_group_scope(KBConfig::GroupScope(cbxGroupScope->currentItem()));
  m_kbconf->set_default_groupno(m_default_groupno);
  //m_kbconf->set_perwindow_group(chkPerwindowGroup->isChecked());
  m_kbconf->set_icon_style(KBConfig::IconStyle(cbxIconType->currentItem()));
  m_kbconf->set_autostart(chkAutostart->isChecked());
  m_kbconf->set_use_shortcuts(chkUseShortcuts->isChecked());
  keyChooser->commitChanges();
  m_kbconf->checkKeysEnabled();
  m_kbconf->save(conf);

  //m_kbconf->notifyChanged();
  conf->sync();
}

/** No descriptions */
void KBConfigDlg::slotOk(){
  saveConfig();
  KDialogBase::slotOk();
}

/** No descriptions */
void KBConfigDlg::slotApply(){
  saveConfig();
  KDialogBase::slotApply();
}

void KBConfigDlg::slotIconTypeSelected(int index)
{
  KBConfig::IconStyle icon_style = KBConfig::IconStyle(index);
  redrawIcons(icon_style);
  slotLayoutSelected();
}

void KBConfigDlg::redrawIcons(KBConfig::IconStyle icon_style)
{
  TQValueVector<TQPixmap> pixlist;
  m_kbconf->drawIcons(icon_style, &pixlist, &m_iconpaths);
	int curIndex = lbGroups->currentItem();
  // NB: don't use count(), use size(); count() is not present in TQt 3.0, and
  // we want to maintain compatibility with it
  for (unsigned int i = 0; i < pixlist.size(); i++) {
    BoldListBoxItem *curItem = dynamic_cast<BoldListBoxItem*>(lbGroups->item(i));
    lbGroups->changeItem(new BoldListBoxItem(NULL, pixlist[i],
      curItem->text(), curItem->bold()), i);
  }
  lbGroups->setCurrentItem(curIndex);
}

void KBConfigDlg::slotCancel()
{
	TDEConfig *config = kapp->config();
	config->rollback();
	config->reparseConfiguration();
	KDialogBase::slotCancel();
}

void KBConfigDlg::slotUseShortcutsToggled(bool on)
{
	keyChooser->setEnabled(on);
}


/*!
    \fn KBConfigDlg::slotSetDefaultIcon()
 */
void KBConfigDlg::slotSetDefaultIcon()
{
	int index = lbGroups->currentItem();
  kapp->config()->setGroup(ICONS_SECTION);
	kapp->config()->deleteEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/);
	redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
}


/*!
    \fn KBConfigDlg::checkIconDefault()
 */
void KBConfigDlg::checkIconDefault(int index)
{
  TDEConfig *conf = kapp->config();
  if (index == -1 || ! iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem()))) {
    btnSetDefaultIcon->setEnabled(false);
  }  
  else {
    conf->setGroup(ICONS_SECTION);
    btnSetDefaultIcon->setEnabled(!conf->readEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/).isNull());
  }  
}