2
* Copyright 2008 Michael Jansen <kde@michael-jansen.biz>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
#include "standard_actions_module.h"
23
#include <KActionCollection>
24
#include <KConfigGroup>
26
#include <KPluginFactory>
27
#include <KShortcutsEditor>
28
#include <KStandardAction>
29
#include <KMessageBox>
32
#include <QVBoxLayout>
35
K_PLUGIN_FACTORY(StandardActionsModuleFactory, registerPlugin<StandardActionsModule>();)
36
K_EXPORT_PLUGIN(StandardActionsModuleFactory("kcm_standard_actions"))
38
static void dressUpAction(KAction *action, KStandardShortcut::StandardShortcut shortcutId)
40
// Remember the shortcutId so we know where to save changes.
41
action->setData(shortcutId);
42
// We have to manually adjust the action. We want to show the
43
// hardcoded default and the user set shortcut. But action currently
44
// only contain the active shortcuts as default shortcut. So we
45
// have to fill it correctly
46
KShortcut hardcoded = KStandardShortcut::hardcodedDefaultShortcut(shortcutId);
47
KShortcut active = KStandardShortcut::shortcut(shortcutId);
48
// Set the hardcoded default shortcut as default shortcut
49
action->setShortcut(hardcoded, KAction::DefaultShortcut);
50
// Set the user defined values as active shortcuts. If the user only
51
// has overwritten the primary shortcut make sure the alternate one
53
if (active.alternate()==QKeySequence())
55
active.setAlternate(hardcoded.alternate());
57
action->setShortcut(active, KAction::ActiveShortcut);
60
StandardActionsModule::StandardActionsModule(
62
const QVariantList &args )
63
: KCModule(StandardActionsModuleFactory::componentData(), parent, args )
65
,m_actionCollection(NULL)
67
KAboutData about("kcm_standard_actions", 0, ki18n("Standard Shortcuts"), "0.1");
68
StandardActionsModuleFactory::componentData().setAboutData(about);
71
KCModule::setButtons(KCModule::Buttons(KCModule::Default | KCModule::Apply | KCModule::Help));
73
// Create and configure the editor
74
m_editor = new KShortcutsEditor(this, KShortcutsEditor::AllActions);
75
connect(m_editor, SIGNAL(keyChange()), this, SLOT(keyChanged()));
78
QVBoxLayout *global = new QVBoxLayout;
79
global->addWidget(m_editor);
84
StandardActionsModule::~StandardActionsModule()
88
void StandardActionsModule::defaults()
90
m_editor->allDefault();
94
void StandardActionsModule::keyChanged()
100
void StandardActionsModule::load()
102
// Create a collection to handle the shortcuts
103
m_actionCollection = new KActionCollection(
105
StandardActionsModuleFactory::componentData());
107
// Keeps track of which shortcut IDs have been added
108
QSet<int> shortcutIdsAdded;
110
// Put all shortcuts for standard actions into the collection
111
Q_FOREACH(KStandardAction::StandardAction id, KStandardAction::actionIds())
113
KStandardShortcut::StandardShortcut shortcutId = KStandardAction::shortcutForActionId(id);
114
// If the StandardShortcutId is AccelNone skip configuration for this
116
if (shortcutId == KStandardShortcut::AccelNone || shortcutIdsAdded.contains(shortcutId))
121
KAction *action = KStandardAction::create(id, NULL, NULL, m_actionCollection);
122
dressUpAction(action, shortcutId);
123
shortcutIdsAdded << shortcutId;
126
// Put in the remaining standard shortcuts too...
127
for(int i = int(KStandardShortcut::AccelNone) + 1; i < KStandardShortcut::StandardShortcutCount; ++i)
129
KStandardShortcut::StandardShortcut shortcutId = static_cast<KStandardShortcut::StandardShortcut>(i);
130
if(!shortcutIdsAdded.contains(shortcutId))
132
KAction *action = new KAction(KStandardShortcut::label(shortcutId), this);
133
action->setWhatsThis(KStandardShortcut::whatsThis(shortcutId));
134
dressUpAction(action, shortcutId);
135
m_actionCollection->addAction(KStandardShortcut::name(shortcutId), action);
139
// Hand the collection to the editor
140
m_editor->addCollection(m_actionCollection);
144
void StandardActionsModule::save()
148
Q_FOREACH(QAction* action, m_actionCollection->actions())
150
KAction *kaction = qobject_cast<KAction*>(action);
152
KStandardShortcut::saveShortcut(
153
static_cast<KStandardShortcut::StandardShortcut>(action->data().toInt())
154
, kaction->shortcut());
157
KGlobal::config()->sync();
158
KConfigGroup cg(KGlobal::config(), "Shortcuts");
161
QString title = i18n("Standard Actions successfully saved");
162
QString message = i18n(
163
"The changes have been saved. Please note that:"
164
"<ul><li>Applications need to be restarted to see the changes.</li>"
165
" <li>This change could introduce shortcut conflicts in some applications.</li>"
167
KMessageBox::information(this, message, title, "shortcuts_saved_info");
170
#include "standard_actions_module.moc"