add more spacing
[personal-kdebase.git] / workspace / kcontrol / keys / kglobalshortcutseditor.cpp
blob351f7666b4dd324d1cf1a58338b8d0fe96b21ef2
1 /*
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 "kglobalshortcutseditor.h"
20 #include <QStackedWidget>
21 #include <QHash>
23 #include "ui_kglobalshortcutseditor.h"
24 #include "kactioncollection.h"
25 #include "kshortcut.h"
26 #include "kdebug.h"
27 #include <kglobalaccel.h>
29 //### copied over from kdedglobalaccel.cpp (figure out a header file to put it in!)
30 enum actionIdFields
32 ComponentUnique = 0,
33 ActionUnique = 1,
34 ComponentFriendly = 2,
35 ActionFriendly = 3
39 * README
41 * This class was created because the kshortcutseditor class has some shortcomings. That class uses
42 * QTreeWidget and therefore makes it impossible for an outsider to switch the models. But the
43 * global shortcuts editor did that. Each global component ( kded, krunner, kopete ... ) was
44 * destined to be separately edited. If you selected another component the kshortcutseditor was
45 * cleared and refilled. But the items take care of undoing. Therefore when switching the component
46 * you lost the undo history.
48 * To solve that problem this class keeps one kshortcuteditor for each component. That is easier
49 * than rewrite that dialog to a model/view framework.
51 * It perfectly covers a bug of KExtedableItemDelegate when clearing and refilling the associated
52 * model.
55 struct componentData
57 KShortcutsEditor *editor;
58 QString uniqueName;
61 class KGlobalShortcutsEditor::KGlobalShortcutsEditorPrivate
63 public:
65 KGlobalShortcutsEditorPrivate(KGlobalShortcutsEditor *q)
66 : q(q),
67 stack(0)
70 void initGUI();
72 KGlobalShortcutsEditor *q;
73 Ui::KGlobalShortcutsEditor ui;
74 QStackedWidget *stack;
75 KShortcutsEditor::ActionTypes actionTypes;
76 QHash<QString, componentData> components;
80 void KGlobalShortcutsEditor::KGlobalShortcutsEditorPrivate::initGUI()
82 ui.setupUi(q);
83 // Create a stacked widget.
84 stack = new QStackedWidget(q);
85 q->layout()->addWidget(stack);
86 // Connect our components
87 connect(ui.components, SIGNAL(activated(const QString&)),
88 q, SLOT(activateComponent(const QString&)));
92 KGlobalShortcutsEditor::KGlobalShortcutsEditor(QWidget *parent, KShortcutsEditor::ActionTypes actionTypes)
93 : QWidget(parent),
94 d(new KGlobalShortcutsEditorPrivate(this))
96 d->actionTypes = actionTypes;
97 // Setup the ui
98 d->initGUI();
102 KGlobalShortcutsEditor::~KGlobalShortcutsEditor()
104 // Before closing the door, undo all changes
105 undo();
106 delete d;
110 void KGlobalShortcutsEditor::activateComponent(const QString &component)
112 QHash<QString, componentData>::Iterator iter = d->components.find(component);
113 if (iter == d->components.end()) {
114 // Unknown component. Its a bad bad world
115 kWarning() << "The component " << component << " is unknown";
116 Q_ASSERT(iter != d->components.end());
117 return;
118 } else {
119 // Known component. Get it.
120 d->stack->setCurrentWidget((*iter).editor);
125 void KGlobalShortcutsEditor::addCollection(
126 KActionCollection *collection,
127 const QString &id,
128 const QString &friendlyName)
130 KShortcutsEditor *editor;
131 // Check if this component is known
132 QHash<QString, componentData>::Iterator iter = d->components.find(friendlyName);
133 if (iter == d->components.end()) {
134 // Unknown component. Create an editor.
135 editor = new KShortcutsEditor(this, d->actionTypes);
136 d->stack->addWidget(editor);
137 // Add to the component combobox
138 d->ui.components->addItem(friendlyName);
139 // Add to our component registry
140 componentData cd;
141 cd.editor = editor;
142 cd.uniqueName = id;
143 d->components.insert(friendlyName, cd);
144 connect(editor, SIGNAL(keyChange()), this, SLOT(_k_key_changed()));
145 } else {
146 // Known component.
147 editor = (*iter).editor;
150 // Add the collection to the editor of the component
151 editor->addCollection(collection, friendlyName);
153 if (d->ui.components->count() > -1) {
154 d->ui.components->setCurrentIndex(0);
155 activateComponent(d->ui.components->itemText(0));
160 void KGlobalShortcutsEditor::allDefault()
162 // The editors are responsible for the reset
163 kDebug() << "Reset";
164 foreach (const componentData &cd, d->components) {
165 cd.editor->allDefault();
170 void KGlobalShortcutsEditor::clear()
172 // Remove all components and their associated editors
173 foreach (const componentData &cd, d->components) {
174 delete cd.editor;
176 d->components.clear();
177 d->ui.components->clear();
182 void KGlobalShortcutsEditor::save()
184 // The editors are responsible for the saving
185 kDebug() << "Save the changes";
186 foreach (const componentData &cd, d->components) {
187 cd.editor->commit();
192 void KGlobalShortcutsEditor::importConfiguration(KConfig *config)
194 // The editors are responsible for the writing of the scheme
195 foreach (const componentData &cd, d->components) {
196 cd.editor->importConfiguration(config);
200 void KGlobalShortcutsEditor::exportConfiguration(KConfig *config) const
202 // The editors are responsible for the writing of the scheme
203 foreach (const componentData &cd, d->components) {
204 cd.editor->exportConfiguration(config);
209 void KGlobalShortcutsEditor::undo()
211 // The editors are responsible for the undo
212 kDebug() << "Undo the changes";
213 foreach (const componentData &cd, d->components) {
214 cd.editor->undoChanges();
219 bool KGlobalShortcutsEditor::isModified() const
221 foreach (const componentData &cd, d->components) {
222 if (cd.editor->isModified()) {
223 return true;
226 return false;
230 void KGlobalShortcutsEditor::_k_key_changed()
232 emit changed(isModified());
235 #include "kglobalshortcutseditor.moc"