KKBSwitch – keyboard layout indicator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kbconfigdlg.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "kbconfigdlg.h"
  17. #include "kbpickicondlg.h"
  18. #include "boldlistboxitem.h"
  19. #include <ntqlayout.h>
  20. #include <ntqlabel.h>
  21. #include <ntqcheckbox.h>
  22. #include <ntqwhatsthis.h>
  23. #include <ntqvbox.h>
  24. #include <ntqstyle.h>
  25. #include <ntqgroupbox.h>
  26. #include <ntqheader.h>
  27. #include <ntqobjectlist.h>
  28. #include <ntqpushbutton.h>
  29. #include <ntqcombobox.h>
  30. #include <tdeversion.h>
  31. #include <tdelistbox.h>
  32. #include <tdelocale.h>
  33. #include <kdebug.h>
  34. #if TDE_VERSION_MAJOR >= 3
  35. #include <tdeapplication.h>
  36. #else
  37. #include <kapp.h>
  38. #endif
  39. #include <tdelistview.h>
  40. #include <kkeydialog.h>
  41. #include <tdeconfig.h>
  42. /* This little subclass of KKeyChooser reimplements sizeHint() to
  43. look a little smaller in our config dialog. The default size
  44. is way too large IMHO and makes General page look oversized
  45. */
  46. class SmallerKeyChooser : public KKeyChooser {
  47. private:
  48. TQSize m_size_hint;
  49. void calcSizeHint();
  50. public:
  51. SmallerKeyChooser(TDEGlobalAccel *accel, TQWidget *parent) :
  52. KKeyChooser(accel, parent) { calcSizeHint(); }
  53. virtual TQSize sizeHint() const { return m_size_hint; }
  54. };
  55. void SmallerKeyChooser::calcSizeHint()
  56. {
  57. m_size_hint = KKeyChooser::sizeHint();
  58. TDEListView *lv = NULL;
  59. TQGroupBox *gb = NULL;
  60. const TQObjectList *objects = children();
  61. TQObjectListIt iter(*objects);
  62. TQObject *obj;
  63. while ( (obj = iter.current()) ) {
  64. ++iter;
  65. if (obj->inherits("TDEListView"))
  66. lv = dynamic_cast<TDEListView*>(obj);
  67. else if (obj->inherits("TQGroupBox"))
  68. gb = dynamic_cast<TQGroupBox*>(obj);
  69. }
  70. if (!lv || !gb) return; // oops, should not happen
  71. TQListViewItem *item = lv->firstChild();
  72. if (!item) return;
  73. int height = item->height() * (1 + item->childCount()) + lv->header()->height() +
  74. style().pixelMetric(TQStyle::PM_ScrollBarExtent) + gb->sizeHint().height() +
  75. layout()->spacing() * 2;
  76. int width = lv->columnWidth(0) + lv->columnWidth(1);
  77. m_size_hint = TQSize(width, height);
  78. }
  79. static inline bool iconTypeShowsFlag(KBConfig::IconStyle icon_style)
  80. {
  81. return icon_style == KBConfig::ICON_FLAG || icon_style == KBConfig::ICON_CODE_AND_FLAG;
  82. }
  83. KBConfigDlg::KBConfigDlg(KBConfig *kbconf, TQWidget *parent, const char *name )
  84. : KDialogBase(Tabbed, i18n("Configure")/*caption*/, Ok | Apply | Cancel | Help,
  85. Ok, parent, name, true /*modal*/)
  86. {
  87. m_kbconf = kbconf;
  88. m_default_groupno = m_kbconf->default_groupno();
  89. setHelp(TQString("configure-kkbswitch"));
  90. setButtonBoxOrientation(Horizontal);
  91. setupGeneralPage();
  92. setupShortcutsPage();
  93. showConfig();
  94. slotIconTypeSelected(m_kbconf->icon_style());
  95. }
  96. KBConfigDlg::~KBConfigDlg(){
  97. }
  98. void KBConfigDlg::setupGeneralPage()
  99. {
  100. TQFrame *page = addPage(i18n("&General")); //makeMainWidget();
  101. TQVBoxLayout *vlayout = new TQVBoxLayout(page);
  102. vlayout->setSpacing(2);
  103. TQLabel *lbl = new TQLabel(i18n("Available &keyboard layouts:"), page);
  104. vlayout->addWidget(lbl);
  105. TQHBoxLayout *groupsLayout = new TQHBoxLayout(vlayout);
  106. groupsLayout->setSpacing(2);
  107. lbGroups = new TDEListBox(page);
  108. TQObject::connect(lbGroups, SIGNAL(selectionChanged()), this, SLOT(slotLayoutSelected()));
  109. TQObject::connect(lbGroups, SIGNAL(doubleClicked(TQListBoxItem *)), this,
  110. SLOT(slotListBoxExecuted(TQListBoxItem *)));
  111. groupsLayout->addWidget(lbGroups);
  112. lbl->setBuddy(lbGroups);
  113. TQWhatsThis::add(lbGroups, i18n("This list box shows keyboard layouts available in your system.\n"
  114. "Select a layout and click \"Change Icon...\" button to change the icon for a layout.\n"
  115. "If you have configured a non-default icon, you can reset the icon to default with \"Use Default Icon\" button.\n"
  116. "The layout shown is bold is the default layout. Use \"Set as default\" button "
  117. "to set the default layout."));
  118. TQVBoxLayout *btnLayout = new TQVBoxLayout(groupsLayout);
  119. btnLayout->setSpacing(2);
  120. btnChangeIcon = new TQPushButton(i18n("Cha&nge Icon..."), page);
  121. TQObject::connect(btnChangeIcon, SIGNAL(clicked()), this, SLOT(slotPickIcon()));
  122. btnLayout->addWidget(btnChangeIcon, 0, TQt::AlignTop);
  123. TQWhatsThis::add(btnChangeIcon, i18n("Click this button to change the icon for the "
  124. "layout selected in the list box to the left."));
  125. btnSetDefaultIcon = new TQPushButton(i18n("Use &Default Icon"), page);
  126. TQObject::connect(btnSetDefaultIcon, SIGNAL(clicked()), this, SLOT(slotSetDefaultIcon()));
  127. btnLayout->addWidget(btnSetDefaultIcon, 0, TQt::AlignTop);
  128. TQWhatsThis::add(btnSetDefaultIcon, i18n("Click this button to use default icon for the "
  129. "layout selected in the list box to the left."));
  130. btnSetDefaultGroup = new TQPushButton(i18n("&Set as Default"), page);
  131. TQObject::connect(btnSetDefaultGroup, SIGNAL(clicked()), this, SLOT(slotSetDefaultGroup()));
  132. btnLayout->addWidget(btnSetDefaultGroup, 0, TQt::AlignTop);
  133. TQWhatsThis::add(btnSetDefaultGroup, i18n("Click this button to set the layout selected "
  134. "in the list box to the left as the default"));
  135. btnLayout->addItem(new TQSpacerItem(1, 1, TQSizePolicy::Minimum,
  136. TQSizePolicy::MinimumExpanding));
  137. lbl = new TQLabel(i18n("Layout &icon style:"), page);
  138. vlayout->addWidget(lbl);
  139. cbxIconType = new TQComboBox(page);
  140. cbxIconType->setEditable(false);
  141. cbxIconType->insertItem(i18n("Country flag"));
  142. cbxIconType->insertItem(i18n("Language code"));
  143. cbxIconType->insertItem(i18n("Flag and code"));
  144. TQObject::connect(cbxIconType, SIGNAL(activated(int)), this, SLOT(slotIconTypeSelected(int)));
  145. vlayout->addWidget(cbxIconType);
  146. lbl->setBuddy(cbxIconType);
  147. TQWhatsThis::add(cbxIconType, i18n("<p>Select the style of icons representing the "
  148. "current keyboard layout.\n"
  149. "You can choose from the following styles:"
  150. "<ul><li><b>Country flag</b> - displays the corresponding country flag"
  151. "<li><b>Language code</b> - displays the language ISO 2-letter code"
  152. "<li><b>Flag and code</b> - displays the language code superimposed over the country flag."
  153. "</ul></p>"));
  154. lbl = new TQLabel(i18n("&Layout applies to:"), page);
  155. vlayout->addWidget(lbl);
  156. cbxGroupScope = new TQComboBox(page);
  157. cbxGroupScope->setEditable(false);
  158. cbxGroupScope->insertItem(i18n("All windows"));
  159. cbxGroupScope->insertItem(i18n("Windows of one application"));
  160. cbxGroupScope->insertItem(i18n("One window"));
  161. vlayout->addWidget(cbxGroupScope);
  162. lbl->setBuddy(cbxGroupScope);
  163. TQWhatsThis::add(cbxGroupScope, i18n("<p>Select what windows the currently selected "
  164. "keyboard layout applies to:\n"
  165. "<ul><li><b>All windows</b> - one layout applies to all windows on your desktop\n"
  166. "<li><b>Windows of one application</b> - layout applies to one application; each "
  167. "application can have its own layout. When you switch between applications, layout follows the active application\n"
  168. "<li><b>One window</b> - layout applies to one window only; each window can have its own layout. "
  169. "When you switch between windows, layout follows the active window.</ul></p>"));
  170. chkToggleMode = new TQCheckBox(i18n("Use \"&Toggle Mode\""), page);
  171. vlayout->addWidget(chkToggleMode);
  172. TQWhatsThis::add(chkToggleMode, i18n("Toggle mode is useful when you have more than two keyboard "
  173. "layouts defined. When toggle mode is on your normal layout switch key toggles between two most frequently used layouts. "
  174. "To activate other layouts use KKBSwitch's tray popup menu"));
  175. /*chkPerwindowGroup = new TQCheckBox(i18n("Use &per-window layout"), page);
  176. vlayout->addWidget(chkPerwindowGroup);
  177. TQWhatsThis::add(chkPerwindowGroup, i18n("When this checkbox is checked, "
  178. "each window has its own current keyboard layout. When it's unchecked, "
  179. "the current layout affects all windows"));*/
  180. chkAutostart = new TQCheckBox(i18n("A&utostart"), page);
  181. vlayout->addWidget(chkAutostart);
  182. TQWhatsThis::add(chkAutostart, i18n("When this checkbox is checked, KKBSwitch "
  183. "will start automatically when you log in"));
  184. }
  185. void KBConfigDlg::setupShortcutsPage()
  186. {
  187. TQVBox *box = addVBoxPage(i18n("Sho&rtcuts"));
  188. chkUseShortcuts = new TQCheckBox(i18n("Use shortcuts to &activate keyboard layouts"), box);
  189. connect(chkUseShortcuts, SIGNAL(toggled(bool)), this, SLOT(slotUseShortcutsToggled(bool)));
  190. TQWhatsThis::add(chkUseShortcuts, i18n("Check this checkbox to be able to quickly "
  191. "activate any keyboard layout with keyboard shorcuts. Once this checkbox "
  192. "is checked, you can adjust the shortcuts at the key chooser pane below. "
  193. "Especially useful if you have three or four keyboard layouts configured"));
  194. keyChooser = new SmallerKeyChooser(m_kbconf->keys(), box);
  195. }
  196. /** Display the current KBSwitch configuration in the dialog */
  197. void KBConfigDlg::showConfig(){
  198. int i;
  199. KBGroup *group;
  200. TDEConfig *conf = kapp->config();
  201. m_iconpaths.clear();
  202. conf->setGroup(ICONS_SECTION);
  203. for (i = 0; i < m_kbconf->groupCount(); i++) {
  204. group = m_kbconf->getGroup(i);
  205. (void) new BoldListBoxItem(lbGroups, group->getPixmap(), group->getName(),
  206. i == m_default_groupno);
  207. //m_iconpaths.append(conf->readEntry(KBConfig::entryForGroup(i)));
  208. m_iconpaths.append(group->getIconPath());
  209. }
  210. if (m_kbconf->groupCount() > 0) lbGroups->setCurrentItem(0);
  211. lbGroups->setMinimumHeight(lbGroups->itemHeight() * (m_kbconf->groupCount() + 1));
  212. cbxIconType->setCurrentItem(m_kbconf->icon_style());
  213. cbxGroupScope->setCurrentItem(m_kbconf->group_scope());
  214. chkToggleMode->setChecked(m_kbconf->toggle_mode());
  215. //chkPerwindowGroup->setChecked(m_kbconf->perwindow_group());
  216. chkAutostart->setChecked(m_kbconf->autostart());
  217. chkUseShortcuts->setChecked(m_kbconf->use_shortcuts());
  218. slotUseShortcutsToggled(m_kbconf->use_shortcuts());
  219. }
  220. /** Fire up "Pick Icon" dialog */
  221. void KBConfigDlg::slotPickIcon(){
  222. int index = lbGroups->currentItem();
  223. TQApplication::setOverrideCursor(TQCursor(TQt::WaitCursor));
  224. KBPickIconDlg dlg(m_iconpaths[index], *(lbGroups->pixmap(index)), this);
  225. TQApplication::restoreOverrideCursor();
  226. if (dlg.exec()) {
  227. TQString path = dlg.getIconPath();
  228. KBGroup *group = m_kbconf->getGroup(index);
  229. if (!path.isEmpty() && path != group->getIconPath()) {
  230. kapp->config()->writeEntry(group->getName()/*KBConfig::entryForGroup(index)*/, path);
  231. redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
  232. checkIconDefault(index);
  233. }
  234. }
  235. }
  236. void KBConfigDlg::slotSetDefaultGroup()
  237. {
  238. int new_default_group = lbGroups->currentItem();
  239. if (m_default_groupno != new_default_group) {
  240. BoldListBoxItem *item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(m_default_groupno));
  241. item->setBold(false);
  242. m_default_groupno = new_default_group;
  243. // NB: don't use selectedItem(); it's not present in TQt 3.0, and
  244. // we want to maintain compatibility with it
  245. int curitem = lbGroups->currentItem();
  246. if (curitem > -1) { // should not be -1, but just in case
  247. item = dynamic_cast<BoldListBoxItem*>(lbGroups->item(curitem));
  248. item->setBold(true);
  249. lbGroups->triggerUpdate(false);
  250. }
  251. }
  252. }
  253. /** No descriptions */
  254. void KBConfigDlg::slotLayoutSelected(){
  255. btnChangeIcon->setEnabled(lbGroups->currentItem() != -1 &&
  256. iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())));
  257. checkIconDefault(lbGroups->currentItem());
  258. btnSetDefaultGroup->setEnabled(lbGroups->currentItem() != -1 &&
  259. lbGroups->currentItem() != m_default_groupno);
  260. }
  261. /** No descriptions */
  262. void KBConfigDlg::slotListBoxExecuted(TQListBoxItem *){
  263. if (iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem())))
  264. slotPickIcon();
  265. }
  266. /** No descriptions */
  267. void KBConfigDlg::saveConfig(){
  268. TQString path;
  269. const TQPixmap *pix;
  270. TDEConfig *conf = kapp->config();
  271. conf->setGroup(ICONS_SECTION);
  272. for (int i = 0; i < m_kbconf->groupCount(); i++) {
  273. path = m_iconpaths[i];
  274. if (!path.isEmpty()) m_kbconf->getGroup(i)->setIconPath(path);
  275. //if (!path.isEmpty() && path != m_kbconf->getGroup(i)->getIconPath())
  276. // conf->writeEntry(KBConfig::entryForGroup(i), path);
  277. pix = lbGroups->pixmap(i);
  278. if (pix) m_kbconf->getGroup(i)->setPixmap(*pix);
  279. }
  280. m_kbconf->set_toggle_mode(chkToggleMode->isChecked());
  281. m_kbconf->set_group_scope(KBConfig::GroupScope(cbxGroupScope->currentItem()));
  282. m_kbconf->set_default_groupno(m_default_groupno);
  283. //m_kbconf->set_perwindow_group(chkPerwindowGroup->isChecked());
  284. m_kbconf->set_icon_style(KBConfig::IconStyle(cbxIconType->currentItem()));
  285. m_kbconf->set_autostart(chkAutostart->isChecked());
  286. m_kbconf->set_use_shortcuts(chkUseShortcuts->isChecked());
  287. keyChooser->commitChanges();
  288. m_kbconf->checkKeysEnabled();
  289. m_kbconf->save(conf);
  290. //m_kbconf->notifyChanged();
  291. conf->sync();
  292. }
  293. /** No descriptions */
  294. void KBConfigDlg::slotOk(){
  295. saveConfig();
  296. KDialogBase::slotOk();
  297. }
  298. /** No descriptions */
  299. void KBConfigDlg::slotApply(){
  300. saveConfig();
  301. KDialogBase::slotApply();
  302. }
  303. void KBConfigDlg::slotIconTypeSelected(int index)
  304. {
  305. KBConfig::IconStyle icon_style = KBConfig::IconStyle(index);
  306. redrawIcons(icon_style);
  307. slotLayoutSelected();
  308. }
  309. void KBConfigDlg::redrawIcons(KBConfig::IconStyle icon_style)
  310. {
  311. TQValueVector<TQPixmap> pixlist;
  312. m_kbconf->drawIcons(icon_style, &pixlist, &m_iconpaths);
  313. int curIndex = lbGroups->currentItem();
  314. // NB: don't use count(), use size(); count() is not present in TQt 3.0, and
  315. // we want to maintain compatibility with it
  316. for (unsigned int i = 0; i < pixlist.size(); i++) {
  317. BoldListBoxItem *curItem = dynamic_cast<BoldListBoxItem*>(lbGroups->item(i));
  318. lbGroups->changeItem(new BoldListBoxItem(NULL, pixlist[i],
  319. curItem->text(), curItem->bold()), i);
  320. }
  321. lbGroups->setCurrentItem(curIndex);
  322. }
  323. void KBConfigDlg::slotCancel()
  324. {
  325. TDEConfig *config = kapp->config();
  326. config->rollback();
  327. config->reparseConfiguration();
  328. KDialogBase::slotCancel();
  329. }
  330. void KBConfigDlg::slotUseShortcutsToggled(bool on)
  331. {
  332. keyChooser->setEnabled(on);
  333. }
  334. /*!
  335. \fn KBConfigDlg::slotSetDefaultIcon()
  336. */
  337. void KBConfigDlg::slotSetDefaultIcon()
  338. {
  339. int index = lbGroups->currentItem();
  340. kapp->config()->setGroup(ICONS_SECTION);
  341. kapp->config()->deleteEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/);
  342. redrawIcons(KBConfig::IconStyle(cbxIconType->currentItem()));
  343. }
  344. /*!
  345. \fn KBConfigDlg::checkIconDefault()
  346. */
  347. void KBConfigDlg::checkIconDefault(int index)
  348. {
  349. TDEConfig *conf = kapp->config();
  350. if (index == -1 || ! iconTypeShowsFlag(KBConfig::IconStyle(cbxIconType->currentItem()))) {
  351. btnSetDefaultIcon->setEnabled(false);
  352. }
  353. else {
  354. conf->setGroup(ICONS_SECTION);
  355. btnSetDefaultIcon->setEnabled(!conf->readEntry(m_kbconf->getGroup(index)->getName()/*KBConfig::entryForGroup(index)*/).isNull());
  356. }
  357. }
  358. #include "kbconfigdlg.moc"