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>
23 #include "ui_kglobalshortcutseditor.h"
24 #include "kactioncollection.h"
25 #include "kshortcut.h"
27 #include <kglobalaccel.h>
32 * This class was created because the kshortcutseditor class has some shortcomings. That class uses
33 * QTreeWidget and therefore makes it impossible for an outsider to switch the models. But the
34 * global shortcuts editor did that. Each global component ( kded, krunner, kopete ... ) was
35 * destined to be separately edited. If you selected another component the kshortcutseditor was
36 * cleared and refilled. But the items take care of undoing. Therefore when switching the component
37 * you lost the undo history.
39 * To solve that problem this class keeps one kshortcuteditor for each component. That is easier
40 * than rewrite that dialog to a model/view framework.
42 * It perfectly covers a bug of KExtedableItemDelegate when clearing and refilling the associated
46 class KGlobalShortcutsEditor::KGlobalShortcutsEditorPrivate
50 KGlobalShortcutsEditorPrivate( KGlobalShortcutsEditor
*q
)
57 KGlobalShortcutsEditor
*q
;
58 Ui::KGlobalShortcutsEditor ui
;
59 QStackedWidget
*stack
;
60 KShortcutsEditor::ActionTypes actionTypes
;
61 QHash
<QString
,KShortcutsEditor
*> components
;
65 void KGlobalShortcutsEditor::KGlobalShortcutsEditorPrivate::initGUI()
68 // Create a stacked widget.
69 stack
= new QStackedWidget(q
);
70 q
->layout()->addWidget(stack
);
71 // Connect our components
73 ui
.components
, SIGNAL(activated(const QString
&)),
74 q
, SLOT(activateComponent(const QString
&))
79 KGlobalShortcutsEditor::KGlobalShortcutsEditor(
81 KShortcutsEditor::ActionTypes actionTypes
84 d(new KGlobalShortcutsEditorPrivate(this))
86 d
->actionTypes
= actionTypes
;
92 KGlobalShortcutsEditor::~KGlobalShortcutsEditor()
94 // Before closing the door, undo all changes
100 void KGlobalShortcutsEditor::activateComponent(const QString
&component
)
102 QHash
<QString
,KShortcutsEditor
*>::Iterator iter
= d
->components
.find( component
);
103 if (iter
==d
->components
.end())
105 // Unknown component. Its a bad bad world
106 kWarning() << "The component " << component
<< " is unknown";
107 Q_ASSERT(iter
!=d
->components
.end());
112 // Known component. Get it.
113 d
->stack
->setCurrentWidget(iter
.value());
114 KGlobalAccel::self()->overrideMainComponentData(KComponentData(component
.toAscii()));
119 void KGlobalShortcutsEditor::addCollection(
120 KActionCollection
*collection
,
121 const QString
&component
,
122 const QString
&title
)
124 kDebug() << "adding collection " << component
;
125 KShortcutsEditor
*editor
;
126 // Check if this component is known
127 QHash
<QString
,KShortcutsEditor
*>::Iterator iter
= d
->components
.find( component
);
128 if (iter
==d
->components
.end())
130 // Unknown component. Create a editor.
131 editor
= new KShortcutsEditor( this, d
->actionTypes
);
132 d
->stack
->addWidget( editor
);
133 // Add it to the combobox
134 d
->ui
.components
->addItem(component
);
135 // And to our registry
136 d
->components
.insert(component
, editor
);
138 connect( editor
, SIGNAL(keyChange()), this, SLOT(_k_key_changed()) );
142 // Known component. Get it.
143 editor
= iter
.value();
146 // Add the collection to the editor of the component
147 editor
->addCollection( collection
, title
);
149 if (d
->ui
.components
->count()>-1)
151 kDebug() << "Activate item " << d
->ui
.components
->itemText(0);
152 d
->ui
.components
->setCurrentIndex(0);
153 activateComponent( d
->ui
.components
->itemText(0) );
158 void KGlobalShortcutsEditor::allDefault()
160 // The editors are responsible for the reset
162 Q_FOREACH (KShortcutsEditor
*editor
, d
->components
.values())
164 editor
->allDefault();
169 void KGlobalShortcutsEditor::clear()
171 // Remove all components and their associated editors
172 Q_FOREACH (KShortcutsEditor
*editor
, d
->components
.values())
176 d
->components
.clear();
177 d
->ui
.components
->clear();
181 void KGlobalShortcutsEditor::save()
183 // The editors are responsible for the saving
184 kDebug() << "Save the changes";
185 Q_FOREACH (KShortcutsEditor
*editor
, d
->components
.values())
192 void KGlobalShortcutsEditor::undo()
194 // The editors are responsible for the undo
195 kDebug() << "Undo the changes";
196 Q_FOREACH (KShortcutsEditor
*editor
, d
->components
.values())
198 editor
->undoChanges();
203 void KGlobalShortcutsEditor::_k_key_changed()
208 #include "kglobalshortcutseditor.moc"