Fairly large overhaul of the JuK codebase to beat out a lot of the Qt 3 stuff.
[kdemultimedia.git] / juk / keydialog.cpp
blob36cd491f420702a30621f9b1266d6cde3621f229
1 /***************************************************************************
2 begin : Tue Mar 11 19:00:00 CET 2003
3 copyright : (C) 2003 by Stefan Asserhall
4 email : stefan.asserhall@telia.com
5 copyright : (c) 2007 by Michael Pyne
6 email : michael.pyne@kdemail.net
7 ***************************************************************************/
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 ***************************************************************************/
18 #include "keydialog.h"
19 #include "actioncollection.h"
21 #include <kconfig.h>
22 #include <klocale.h>
23 #include <kshortcutseditor.h>
24 #include <kglobal.h>
25 #include <kaction.h>
26 #include <kvbox.h>
27 #include <kconfiggroup.h>
29 #include <QButtonGroup>
30 #include <QRadioButton>
31 #include <QGroupBox>
32 #include <Q3WidgetStack>
34 // Table of shortcut keys for each action, key group and three or four button modifier
36 const KeyDialog::KeyInfo KeyDialog::keyInfo[] = {
37 { "playPause",
38 { KShortcut(),
39 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_P),
40 KShortcut(Qt::Key_MediaPlay) } },
41 { "stop",
42 { KShortcut(),
43 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_S),
44 KShortcut(Qt::Key_MediaStop) } },
45 { "back",
46 { KShortcut(),
47 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Left),
48 KShortcut(Qt::Key_MediaPrevious) } },
49 { "forward",
50 { KShortcut(),
51 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Right),
52 KShortcut(Qt::Key_MediaNext) } },
53 { "forwardAlbum",
54 { KShortcut(),
55 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Up),
56 KShortcut(Qt::CTRL+Qt::Key_MediaNext) } },
57 { "seekBack",
58 { KShortcut(),
59 KShortcut(Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Left),
60 KShortcut(Qt::SHIFT+Qt::Key_MediaPrevious) } },
61 { "seekForward",
62 { KShortcut(),
63 KShortcut(Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Right),
64 KShortcut(Qt::SHIFT+Qt::Key_MediaNext) } },
65 { "volumeUp",
66 { KShortcut(),
67 KShortcut(Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Up),
68 KShortcut(Qt::Key_VolumeUp) } },
69 { "volumeDown",
70 { KShortcut(),
71 KShortcut(Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Down),
72 KShortcut(Qt::Key_VolumeDown) } },
73 { "mute",
74 { KShortcut(),
75 KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_M),
76 KShortcut(Qt::Key_VolumeMute) } },
77 { "showHide",
78 { KShortcut(),
79 KShortcut(),
80 KShortcut() } }
83 const uint KeyDialog::keyInfoCount = sizeof(KeyDialog::keyInfo) / sizeof(KeyDialog::keyInfo[0]);
85 KeyDialog::KeyDialog(KActionCollection *actionCollection, QWidget *parent)
86 : KDialog(parent)
88 setCaption(i18n("Configure Shortcuts"));
89 setButtons(Default | Ok | Cancel);
91 // Read key group from configuration
93 KConfigGroup config(KGlobal::config(), "Shortcuts");
94 int selectedButton = config.readEntry("GlobalKeys", int(StandardKeys));
96 // Create widgets for key chooser - widget stack used to replace key chooser
98 KVBox *vbox = new KVBox(this);
99 vbox->setSpacing(KDialog::spacingHint());
100 m_widgetStack = new Q3WidgetStack(vbox);
102 vbox->setStretchFactor(m_widgetStack, 1);
104 // Create buttons to select key group
106 m_group = new QButtonGroup(vbox);
107 QGroupBox *buttonBox = new QGroupBox(i18n("Global Shortcuts"), vbox);
109 m_group->addButton(new QRadioButton(i18n("&No keys"), buttonBox), NoKeys);
110 m_group->addButton(new QRadioButton(i18n("&Standard keys"), buttonBox), StandardKeys);
111 m_group->addButton(new QRadioButton(i18n("&Multimedia keys"), buttonBox), MultimediaKeys);
113 connect(m_group, SIGNAL(buttonClicked(int)), this, SLOT(slotKeys(int)));
114 buttonBox->setWhatsThis(
115 i18n("Here you can select the keys used as global shortcuts to control the player"));
117 // Create the key chooser
119 setMainWidget(vbox);
120 newDialog(actionCollection, selectedButton);
123 KeyDialog::~KeyDialog()
128 void KeyDialog::newDialog(KActionCollection *actionCollection, int selectedButton)
130 m_actionCollection = actionCollection;
132 // Create key chooser and show it in the widget stack
133 m_pKeyChooser = new KShortcutsEditor(actionCollection, this);
134 m_widgetStack->addWidget(m_pKeyChooser);
135 m_widgetStack->raiseWidget(m_pKeyChooser);
137 m_group->button(selectedButton)->setChecked(true);
139 connect(this, SIGNAL(defaultClicked()), this, SLOT(slotDefault()));
142 int KeyDialog::configure()
144 // Show the dialog and save configuration if accepted
146 int retcode = exec();
147 if(retcode == Accepted) {
148 KConfigGroup config(KGlobal::config(), "Shortcuts");
150 config.writeEntry("GlobalKeys", m_group->checkedId());
151 KGlobal::config()->sync();
153 m_pKeyChooser->save();
155 return retcode;
158 void KeyDialog::slotKeys(int group)
160 // Set modifier keys according to key group and modifier keys
162 for(uint i = 0; i < keyInfoCount; i++) {
163 KAction *a = dynamic_cast<KAction*>(ActionCollection::action(keyInfo[i].action));
164 if(a)
165 a->setGlobalShortcut(keyInfo[i].shortcut[group]);
168 // Create a new key chooser to show the keys, and delete the old one
170 QWidget *w = m_widgetStack->visibleWidget();
171 newDialog(m_actionCollection, group);
172 m_widgetStack->removeWidget(w);
173 delete w;
176 void KeyDialog::slotDefault()
178 // Select default keys - standard key group
180 m_group->button(StandardKeys)->setChecked(true);
181 m_pKeyChooser->allDefault();
184 int KeyDialog::configure(KActionCollection *actionCollection, QWidget *parent)
186 // Create and show dialog - update connections if accepted
188 return KeyDialog(actionCollection, parent).configure();
191 void KeyDialog::setupActionShortcut(const QString &actionName)
193 // Find and insert a standard key
194 KShortcut shortcut = KShortcut();
196 for(uint i = 0; i < keyInfoCount; i++) {
197 if(keyInfo[i].action == actionName) {
198 shortcut = keyInfo[i].shortcut[StandardKeys];
199 break;
203 if(shortcut.isEmpty())
204 return; // We have no shortcut to set.
206 KAction *a = dynamic_cast<KAction*>(ActionCollection::action(actionName));
207 if(a)
208 a->setGlobalShortcut(shortcut);
211 #include "keydialog.moc"
213 // vim: set et sw=4 tw=0 sta: