~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/standard_actions/standard_actions_module.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008 Michael Jansen <kde@michael-jansen.biz>
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
#include "standard_actions_module.h"
 
19
 
 
20
 
 
21
#include <KAboutData>
 
22
#include <KAction>
 
23
#include <KActionCollection>
 
24
#include <KConfigGroup>
 
25
#include <KDebug>
 
26
#include <KPluginFactory>
 
27
#include <KShortcutsEditor>
 
28
#include <KStandardAction>
 
29
#include <KMessageBox>
 
30
#include <KLocale>
 
31
 
 
32
#include <QVBoxLayout>
 
33
#include <QSet>
 
34
 
 
35
K_PLUGIN_FACTORY(StandardActionsModuleFactory, registerPlugin<StandardActionsModule>();)
 
36
K_EXPORT_PLUGIN(StandardActionsModuleFactory("kcm_standard_actions"))
 
37
 
 
38
static void dressUpAction(KAction *action, KStandardShortcut::StandardShortcut shortcutId)
 
39
    {
 
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
 
52
    // still get's shown
 
53
    if (active.alternate()==QKeySequence())
 
54
        {
 
55
        active.setAlternate(hardcoded.alternate());
 
56
        }
 
57
    action->setShortcut(active, KAction::ActiveShortcut);
 
58
    }
 
59
 
 
60
StandardActionsModule::StandardActionsModule(
 
61
        QWidget *parent,
 
62
        const QVariantList &args )
 
63
    : KCModule(StandardActionsModuleFactory::componentData(), parent, args )
 
64
      ,m_editor(NULL)
 
65
      ,m_actionCollection(NULL)
 
66
    {
 
67
    KAboutData about("kcm_standard_actions", 0, ki18n("Standard Shortcuts"), "0.1");
 
68
    StandardActionsModuleFactory::componentData().setAboutData(about);
 
69
 
 
70
    // Configure the KCM
 
71
    KCModule::setButtons(KCModule::Buttons(KCModule::Default | KCModule::Apply | KCModule::Help));
 
72
 
 
73
    // Create and configure the editor
 
74
    m_editor = new KShortcutsEditor(this, KShortcutsEditor::AllActions);
 
75
    connect(m_editor, SIGNAL(keyChange()), this, SLOT(keyChanged()));
 
76
 
 
77
    // Make a layout
 
78
    QVBoxLayout *global = new QVBoxLayout;
 
79
    global->addWidget(m_editor);
 
80
    setLayout(global);
 
81
    }
 
82
 
 
83
 
 
84
StandardActionsModule::~StandardActionsModule()
 
85
    {}
 
86
 
 
87
 
 
88
void StandardActionsModule::defaults()
 
89
    {
 
90
    m_editor->allDefault();
 
91
    }
 
92
 
 
93
 
 
94
void StandardActionsModule::keyChanged()
 
95
    {
 
96
    emit changed(true);
 
97
    }
 
98
 
 
99
 
 
100
void StandardActionsModule::load()
 
101
    {
 
102
    // Create a collection to handle the shortcuts
 
103
    m_actionCollection = new KActionCollection(
 
104
            this,
 
105
            StandardActionsModuleFactory::componentData());
 
106
 
 
107
    // Keeps track of which shortcut IDs have been added
 
108
    QSet<int> shortcutIdsAdded;
 
109
 
 
110
    // Put all shortcuts for standard actions into the collection
 
111
    Q_FOREACH(KStandardAction::StandardAction id, KStandardAction::actionIds())
 
112
        {
 
113
        KStandardShortcut::StandardShortcut shortcutId = KStandardAction::shortcutForActionId(id);
 
114
        // If the StandardShortcutId is AccelNone skip configuration for this
 
115
        // action.
 
116
        if (shortcutId == KStandardShortcut::AccelNone || shortcutIdsAdded.contains(shortcutId))
 
117
            {
 
118
            continue;
 
119
            }
 
120
        // Create the action
 
121
        KAction *action = KStandardAction::create(id, NULL, NULL, m_actionCollection);
 
122
        dressUpAction(action, shortcutId);
 
123
        shortcutIdsAdded << shortcutId;
 
124
        }
 
125
 
 
126
    // Put in the remaining standard shortcuts too...
 
127
    for(int i = int(KStandardShortcut::AccelNone) + 1; i < KStandardShortcut::StandardShortcutCount; ++i)
 
128
        {
 
129
        KStandardShortcut::StandardShortcut shortcutId = static_cast<KStandardShortcut::StandardShortcut>(i);
 
130
        if(!shortcutIdsAdded.contains(shortcutId))
 
131
            {
 
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);
 
136
            }
 
137
        }
 
138
 
 
139
    // Hand the collection to the editor
 
140
    m_editor->addCollection(m_actionCollection);
 
141
    }
 
142
 
 
143
 
 
144
void StandardActionsModule::save()
 
145
    {
 
146
    m_editor->commit();
 
147
 
 
148
    Q_FOREACH(QAction* action, m_actionCollection->actions())
 
149
        {
 
150
        KAction *kaction = qobject_cast<KAction*>(action);
 
151
 
 
152
        KStandardShortcut::saveShortcut(
 
153
                static_cast<KStandardShortcut::StandardShortcut>(action->data().toInt())
 
154
                , kaction->shortcut());
 
155
        }
 
156
 
 
157
    KGlobal::config()->sync();
 
158
    KConfigGroup cg(KGlobal::config(), "Shortcuts");
 
159
    cg.sync();
 
160
 
 
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>"
 
166
        "</ul>" );
 
167
    KMessageBox::information(this, message, title, "shortcuts_saved_info");
 
168
    }
 
169
 
 
170
#include "standard_actions_module.moc"