keydialog.cpp 7.27 KB
Newer Older
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
/***************************************************************************
                          keydialog.cpp  -  description
                             -------------------
    begin                : Tue Mar 11 19:00:00 CET 2003
    copyright            : (C) 2003 by Stefan Asserhall
    email                : stefan.asserhall@telia.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 "keydialog.h"

#include <kconfig.h>
#include <klocale.h>
#include <kshortcut.h>

#include <qlayout.h>
#include <qradiobutton.h>
#include <qvbox.h>
#include <qwhatsthis.h>


// Table of shortcut keys for each action, key group and three or four button modifier
31
const KeyDialog::KeyInfo KeyDialog::keyInfo[] = {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    {"PlayPause",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::Key_P,              KKey::QtWIN+Qt::ALT+Qt::Key_P},
       {Qt::Key_MediaPlay,                       Qt::Key_MediaPlay}}},
    {"Stop",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::Key_S,              KKey::QtWIN+Qt::ALT+Qt::Key_S},
       {Qt::Key_MediaStop,                       Qt::Key_MediaStop}}},
    {"Back",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::Key_Left,           KKey::QtWIN+Qt::ALT+Qt::Key_Left},
       {Qt::Key_MediaPrev,                       Qt::Key_MediaPrev}}},
    {"Forward",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::Key_Right,          KKey::QtWIN+Qt::ALT+Qt::Key_Right},
       {Qt::Key_MediaNext,                       Qt::Key_MediaNext}}},
48 49 50 51 52 53 54 55
    {"SeekBack",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Left, KKey::QtWIN+Qt::SHIFT+Qt::ALT+Qt::Key_Left},
       {Qt::SHIFT+Qt::Key_MediaPrev,             Qt::SHIFT+Qt::Key_MediaPrev}}},
    {"SeekForward",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::SHIFT+Qt::ALT+Qt::Key_Right,KKey::QtWIN+Qt::SHIFT+Qt::ALT+Qt::Key_Right},
       {Qt::SHIFT+Qt::Key_MediaNext,             Qt::SHIFT+Qt::Key_MediaNext}}},
56 57 58 59 60 61 62 63 64 65 66 67 68 69
    {"VolumeUp",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Up,   KKey::QtWIN+Qt::ALT+Qt::SHIFT+Qt::Key_Up},
       {Qt::Key_VolumeUp,                        Qt::Key_VolumeUp}}},
    {"VolumeDown",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::SHIFT+Qt::Key_Down, KKey::QtWIN+Qt::ALT+Qt::SHIFT+Qt::Key_Down},
       {Qt::Key_VolumeDown,                      Qt::Key_VolumeDown}}},
    {"Mute",
      {{KShortcut::null(),                       KShortcut::null()},
       {Qt::CTRL+Qt::ALT+Qt::Key_M,              KKey::QtWIN+Qt::ALT+Qt::Key_M},
       {Qt::Key_VolumeMute,                      Qt::Key_VolumeMute}}}
};

70
const uint KeyDialog::keyInfoCount = sizeof(KeyDialog::keyInfo) / sizeof(KeyDialog::keyInfo[0]);
71

72 73
KeyDialog::KeyDialog(KGlobalAccel *keys, KActionCollection *actionCollection, QWidget *parent, const char* name)
    : KDialogBase(parent, name, true, i18n("Configure Shortcuts"), Default | Ok | Cancel, Ok)
74 75 76 77 78 79 80 81 82 83 84 85
{
    // Read key group from configuration
    int selectedButton;
    KConfig *config = KGlobal::config();
    {
	KConfigGroupSaver saver(config, "Shortcuts");
	selectedButton = config->readNumEntry("GlobalKeys", StandardKeys);
    }

    // Create widgets for key chooser - widget stack used to replace key chooser
    QVBox *vbox = new QVBox(this);
    vbox->setSpacing(KDialog::spacingHint());
86
    m_widgetStack = new QWidgetStack(vbox);
87 88 89 90 91 92 93 94 95 96 97 98

    // Create buttons to select key group
    m_group = new QHButtonGroup(i18n("Global shortcuts"), vbox);
    new QRadioButton(i18n("&No keys"),         m_group);
    new QRadioButton(i18n("&Standard keys"),   m_group);
    new QRadioButton(i18n("&Multimedia keys"), m_group);
    connect(m_group, SIGNAL(clicked(int)), this, SLOT(slotKeys(int)));
    QWhatsThis::add( m_group,
      i18n("Here you can select the keys used as global shortcuts to control the player"));

    // Create the key chooser
    setMainWidget(vbox);
99
    newDialog(keys, actionCollection, selectedButton);
100 101 102 103 104 105
}

KeyDialog::~KeyDialog()
{
}

106
void KeyDialog::newDialog(KGlobalAccel* keys, KActionCollection* actionCollection, int selectedButton)
107 108
{
    m_keys = keys;
109
    m_actionCollection = actionCollection;
110 111 112

    // Create key chooser and show it in the widget stack
    m_pKeyChooser = new KKeyChooser(keys, this);
113 114 115
    m_pKeyChooser->insert(actionCollection);
    m_widgetStack->addWidget(m_pKeyChooser);
    m_widgetStack->raiseWidget(m_pKeyChooser);
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
    m_group->setButton(selectedButton);

    connect(this, SIGNAL(defaultClicked()), this, SLOT(slotDefault()));
}

int KeyDialog::configure()
{
    // Show the dialog and save configuration if accepted
    int retcode = exec();
    if(retcode == Accepted) {
	KConfig *config = KGlobal::config();
	{
	    KConfigGroupSaver saver(config, "Shortcuts");
	    config->writeEntry("GlobalKeys", m_group->id(m_group->selected()));
	    config->sync();
	}
	m_pKeyChooser->save();
    }
    return retcode;
}

void KeyDialog::slotKeys(int group)
{
    bool fourModKeys = KGlobalAccel::useFourModifierKeys();

    // Set modifier keys according to key group and modifier keys
142
    for (uint i = 0; i < keyInfoCount; i++)
143 144 145
	m_keys->setShortcut(keyInfo[i].action, keyInfo[i].shortcut[group][fourModKeys]);

    // Create a new key chooser to show the keys, and delete the old one
146 147 148
    QWidget *w = m_widgetStack->visibleWidget();
    newDialog(m_keys, m_actionCollection, group);
    m_widgetStack->removeWidget(w);
149 150 151 152 153 154 155 156 157 158
    delete w;
}

void KeyDialog::slotDefault()
{
    // Select default keys - standard key group
    m_group->setButton(StandardKeys);
    m_pKeyChooser->allDefault();
}

159
int KeyDialog::configure(KGlobalAccel *keys, KActionCollection *actionCollection, QWidget* parent)
160 161
{
    // Create and show dialog - update connections if accepted
162
    KeyDialog dlg(keys, actionCollection, parent);
163 164 165 166 167 168 169 170 171 172 173 174 175
    int retcode = dlg.configure();
    if (retcode == Accepted)
	keys->updateConnections();
    return retcode;
}

void KeyDialog::insert(KGlobalAccel* keys, const QString& action, const QString& label,
                       const QObject* objSlot, const char* methodSlot)
{
    KShortcut def3 = KShortcut::null();
    KShortcut def4 = KShortcut::null();

    // Find and insert a standard key
176
    for (uint i = 0; i < keyInfoCount; i++)
177 178 179 180 181 182 183
	if (keyInfo[i].action == action) {
	    def3 = keyInfo[i].shortcut[StandardKeys][0];
	    def4 = keyInfo[i].shortcut[StandardKeys][1];
	    break;
	}
    keys->insert(action, label, QString::null, def3, def4, objSlot, methodSlot);
}
184 185

#include "keydialog.moc"