keydialog.cpp 6.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (C) 2003 Stefan Asserhall <stefan.asserhall@telia.com>
 * Copyright (C) 2007, 2008 Michael Pyne <mpyne@kde.org>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17 18

#include "keydialog.h"
19
#include "actioncollection.h"
20 21 22

#include <kconfig.h>
#include <klocale.h>
23
#include <kshortcutseditor.h>
24
#include <kglobal.h>
25
#include <kaction.h>
Laurent Montel's avatar
Laurent Montel committed
26
#include <kvbox.h>
27
#include <kconfiggroup.h>
28
#include <KShortcut>
Laurent Montel's avatar
Laurent Montel committed
29

30 31
#include <QHBoxLayout>
#include <QTimer>
32 33 34
#include <QButtonGroup>
#include <QRadioButton>
#include <QGroupBox>
35
#include <QString>
36 37

// Table of shortcut keys for each action, key group and three or four button modifier
Scott Wheeler's avatar
Scott Wheeler committed
38

39 40 41 42 43
struct KeyDialog::KeyInfo {
    QString action;
    KShortcut shortcut[3];
};

44
const KeyDialog::KeyInfo KeyDialog::keyInfo[] = {
45
    { "playPause",
Laurent Montel's avatar
Laurent Montel committed
46 47 48
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_P),
        KShortcut(Qt::Key_MediaPlay) } },
49
    { "stop",
Laurent Montel's avatar
Laurent Montel committed
50 51 52
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_S),
        KShortcut(Qt::Key_MediaStop) } },
53
    { "back",
Laurent Montel's avatar
Laurent Montel committed
54 55 56
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Left),
        KShortcut(Qt::Key_MediaPrevious) } },
57
    { "forward",
Laurent Montel's avatar
Laurent Montel committed
58 59 60
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Right),
        KShortcut(Qt::Key_MediaNext) } },
61
    { "forwardAlbum",
Laurent Montel's avatar
Laurent Montel committed
62 63 64
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_Up),
        KShortcut(Qt::CTRL+Qt::Key_MediaNext) } },
65
    { "seekBack",
Laurent Montel's avatar
Laurent Montel committed
66 67 68
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Left),
        KShortcut(Qt::SHIFT+Qt::Key_MediaPrevious) } },
69
    { "seekForward",
Laurent Montel's avatar
Laurent Montel committed
70 71 72
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Right),
        KShortcut(Qt::SHIFT+Qt::Key_MediaNext) } },
73
    { "volumeUp",
Laurent Montel's avatar
Laurent Montel committed
74 75 76
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Up),
        KShortcut(Qt::Key_VolumeUp) } },
77
    { "volumeDown",
Laurent Montel's avatar
Laurent Montel committed
78 79 80
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Down),
        KShortcut(Qt::Key_VolumeDown) } },
81
    { "mute",
Laurent Montel's avatar
Laurent Montel committed
82 83 84
      { KShortcut(),
        KShortcut(Qt::CTRL+Qt::ALT+Qt::Key_M),
        KShortcut(Qt::Key_VolumeMute) } },
85
    { "showHide",
Laurent Montel's avatar
Laurent Montel committed
86 87 88
      { KShortcut(),
        KShortcut(),
        KShortcut() } }
89 90
};

91
const uint KeyDialog::keyInfoCount = sizeof(KeyDialog::keyInfo) / sizeof(KeyDialog::keyInfo[0]);
92

93
KeyDialog::KeyDialog(KActionCollection *actionCollection, QWidget *parent)
94
    : KDialog(parent), m_actionCollection(actionCollection)
95
{
Richard Lärkäng's avatar
Richard Lärkäng committed
96 97 98
    setCaption(i18n("Configure Shortcuts"));
    setButtons(Default | Ok | Cancel);

99
    // Read key group from configuration
Scott Wheeler's avatar
Scott Wheeler committed
100

101
    KConfigGroup config(KGlobal::config(), "Shortcuts");
102
    int selectedButton = config.readEntry("GlobalKeys", int(StandardKeys));
103

104
    // Create widgets for key chooser
Scott Wheeler's avatar
Scott Wheeler committed
105

Laurent Montel's avatar
Laurent Montel committed
106
    KVBox *vbox = new KVBox(this);
107 108
    vbox->setSpacing(KDialog::spacingHint());

109
    m_pKeyChooser = new KShortcutsEditor(actionCollection, vbox);
110

111
    // Create buttons to select key group
Scott Wheeler's avatar
Scott Wheeler committed
112

113
    QGroupBox *buttonBox = new QGroupBox(i18n("Global Shortcuts"), vbox);
114 115 116 117 118 119 120
    m_group = new QButtonGroup(buttonBox);
    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonBox->setLayout(buttonLayout);

    QRadioButton *radioButton = new QRadioButton(i18n("&No keys"), buttonBox);
    m_group->addButton(radioButton, NoKeys);
    buttonLayout->addWidget(radioButton);
121

122 123 124 125 126 127 128
    radioButton = new QRadioButton(i18n("&Standard keys"), buttonBox);
    m_group->addButton(radioButton, StandardKeys);
    buttonLayout->addWidget(radioButton);

    radioButton = new QRadioButton(i18n("&Multimedia keys"), buttonBox);
    m_group->addButton(radioButton, MultimediaKeys);
    buttonLayout->addWidget(radioButton);
129 130 131

    connect(m_group, SIGNAL(buttonClicked(int)), this, SLOT(slotKeys(int)));
    buttonBox->setWhatsThis(
132
	i18n("Here you can select the keys used as global shortcuts to control the player"));
133

134 135
    m_group->button(selectedButton)->setChecked(true);
    connect(this, SIGNAL(defaultClicked()), this, SLOT(slotDefault()));
Scott Wheeler's avatar
Scott Wheeler committed
136

137
    setMainWidget(vbox);
138
    resize(400, 500); // Make it bigger!
139 140 141 142 143 144 145 146 147
}

KeyDialog::~KeyDialog()
{
}

int KeyDialog::configure()
{
    // Show the dialog and save configuration if accepted
Scott Wheeler's avatar
Scott Wheeler committed
148

149 150
    int retcode = exec();
    if(retcode == Accepted) {
151
        KConfigGroup config(KGlobal::config(), "Shortcuts");
152 153

        config.writeEntry("GlobalKeys", m_group->checkedId());
154
        KGlobal::config()->sync();
155

156
        m_pKeyChooser->save();
157
    }
158

159 160 161 162 163 164
    return retcode;
}

void KeyDialog::slotKeys(int group)
{
    // Set modifier keys according to key group and modifier keys
Scott Wheeler's avatar
Scott Wheeler committed
165

166
    for(uint i = 0; i < keyInfoCount; i++) {
167 168 169 170 171 172 173
        KAction *a = ActionCollection::action<KAction>(keyInfo[i].action);

        if(a) {
            KShortcut shortcut(keyInfo[i].shortcut[group]);

            a->setGlobalShortcut(shortcut, KAction::ActiveShortcut, KAction::NoAutoloading);
        }
174
    }
175

176 177 178
    // Update the key chooser widget.
    // TODO: When widget is fixed to note the update remove this bit so that
    // we don't drop user changes for no reason.
Scott Wheeler's avatar
Scott Wheeler committed
179

180 181
    m_pKeyChooser->clearCollections();
    m_pKeyChooser->addCollection(m_actionCollection);
182 183 184 185 186
}

void KeyDialog::slotDefault()
{
    // Select default keys - standard key group
Scott Wheeler's avatar
Scott Wheeler committed
187

188
    m_group->button(StandardKeys)->setChecked(true);
189
    slotKeys(StandardKeys);
190 191 192
    m_pKeyChooser->allDefault();
}

193
int KeyDialog::configure(KActionCollection *actionCollection, QWidget *parent)
194 195
{
    // Create and show dialog - update connections if accepted
Scott Wheeler's avatar
Scott Wheeler committed
196

197
    return KeyDialog(actionCollection, parent).configure();
198 199
}

200
void KeyDialog::setupActionShortcut(const QString &actionName)
201 202
{
    // Find and insert a standard key
Laurent Montel's avatar
Laurent Montel committed
203
    KShortcut shortcut = KShortcut();
Scott Wheeler's avatar
Scott Wheeler committed
204

205 206 207 208
    // Find out what type is selected so we know what keys to setup.
    KConfigGroup config(KGlobal::config(), "Shortcuts");
    int selectedKeys = config.readEntry("GlobalKeys", int(StandardKeys));

Scott Wheeler's avatar
Scott Wheeler committed
209
    for(uint i = 0; i < keyInfoCount; i++) {
210
        if(keyInfo[i].action == actionName) {
211
            shortcut = keyInfo[i].shortcut[selectedKeys];
212 213
            break;
        }
Scott Wheeler's avatar
Scott Wheeler committed
214
    }
215

Laurent Montel's avatar
Laurent Montel committed
216
    if(shortcut.isEmpty())
217 218
        return; // We have no shortcut to set.

219
    KAction *a = ActionCollection::action<KAction>(actionName);
220 221
    if(a)
        a->setGlobalShortcut(shortcut);
222
}
223

224
// vim: set et sw=4 tw=0 sta: