Krita Source Code Documentation
Loading...
Searching...
No Matches
KisShortcutsEditor.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org>
2 SPDX-FileCopyrightText: 1997 Nicolas Hadacek <hadacek@kde.org>
3 SPDX-FileCopyrightText: 1998 Matthias Ettrich <ettrich@kde.org>
4 SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
5 SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
6 SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org>
7 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
8 SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
9
10 SPDX-License-Identifier: LGPL-2.0-or-later
11*/
12
13#include "KisShortcutsEditor.h"
16#include "config-xmlgui.h"
17#include "kis_action_registry.h"
18
19// The following is needed for KisShortcutsEditorPrivate and QTreeWidgetHack
20// #include "KisShortcutsDialog_p.h"
21
22#include <QAction>
23#include <QList>
24#include <QObject>
25#include <QTimer>
26#include <QTextDocument>
27#include <QTextTable>
28#include <QTextCursor>
29#include <QTextTableFormat>
30#include <QPrinter>
31#include <QDebug>
32
33#include <kconfig.h>
34#include <kconfiggroup.h>
35#include <ksharedconfig.h>
36#include <kmessagebox.h>
37#include "kactioncollection.h"
38#include "kactioncategory.h"
39#include <ktreewidgetsearchline.h>
40
41//---------------------------------------------------------------------
42// KisShortcutsEditor
43//---------------------------------------------------------------------
44
46
47
48KisShortcutsEditor::KisShortcutsEditor(QWidget *parent, ActionTypes actionType, LetterShortcuts allowLetterShortcuts)
49 : QWidget(parent)
50 , d(new KisShortcutsEditorPrivate(this))
51{
52 d->initGUI(actionType, allowLetterShortcuts);
53}
54
59
61{
62 // Iterate over all items
63 QTreeWidgetItemIterator it(d->ui.list, QTreeWidgetItemIterator::NoChildren);
64
65 for (; (*it); ++it) {
66 KisShortcutsEditorItem *item = dynamic_cast<KisShortcutsEditorItem *>(*it);
67 if (item && item->isModified()) {
68 return true;
69 }
70 }
71 return false;
72}
73
75{
76 d->delegate->contractAll();
77 d->ui.list->clear();
78 d->actionCollections.clear();
79 QTimer::singleShot(0, this, SLOT(resizeColumns()));
80}
81
83{
84 d->ui.searchFilter->searchLine()->clear();
85}
86
87
88void KisShortcutsEditor::addCollection(KisKActionCollection *collection, const QString &title)
89{
90 // KXmlGui add action collections unconditionally. If some plugin doesn't
91 // provide actions we don't want to create empty subgroups.
92 if (collection->isEmpty()) {
93 return;
94 }
95
96 // Pause updating.
97 setUpdatesEnabled(false);
98
99
104 d->actionCollections.append(collection);
106
107
108 // Determine how we should label this collection in the widget.
109 QString collectionTitle;
110 if (!title.isEmpty()) {
111 collectionTitle = title;
112 } else {
113 // Use the programName (Translated).
114 collectionTitle = collection->componentDisplayName();
115 }
116
117 // Create the collection root node.
118 QTreeWidgetItem *hierarchy[3];
119 hierarchy[KisShortcutsEditorPrivate::Root] = d->ui.list->invisibleRootItem();
121 d->findOrMakeItem(hierarchy[KisShortcutsEditorPrivate::Root], collectionTitle);
123
124 // Remember which actions we have seen. We will be adding categorized
125 // actions first, so this will help us keep track of which actions haven't
126 // been categorized yet, so we can add them as uncategorized at the end.
127 QSet<QAction *> actionsSeen;
128
129 // Add a subtree for each category? Perhaps easier to think that this
130 // doesn't exist. Basically you add KisKActionCategory as a QObject child of
131 // KisKActionCollection, and then tag objects as belonging to the category.
132 foreach (KisKActionCategory *category, collection->categories()) {
133
134 // Don't display empty categories.
135 if (category->actions().isEmpty()) {
136 continue;
137 }
138
141
142 // Add every item from the category.
143 foreach (QAction *action, category->actions()) {
144 actionsSeen.insert(action);
145 d->addAction(action, hierarchy, KisShortcutsEditorPrivate::Action);
146 }
147
148 // Fold in each KisKActionCategory by default.
149 hierarchy[KisShortcutsEditorPrivate::Action]->setExpanded(false);
150
151 }
152
153 // Finally, tack on any uncategorized actions.
154 foreach (QAction *action, collection->actions()) {
155 if (!actionsSeen.contains(action)) {
157 }
158 }
159
160 // sort the list
161 d->ui.list->sortItems(Name, Qt::AscendingOrder);
162
163 // Now turn on updating again.
164 setUpdatesEnabled(true);
165
166 QTimer::singleShot(0, this, SLOT(resizeColumns()));
167}
168
173
174void KisShortcutsEditor::importConfiguration(KConfigBase *config, bool isScheme)
175{
176 Q_ASSERT(config);
177 if (!config) {
178 return;
179 }
180
181 // If this is a shortcut scheme, apply it
182 if (isScheme) {
184 }
185
186 // Update the dialog entry items
187 const KConfigGroup schemeShortcuts(config, QStringLiteral("Shortcuts"));
188 for (QTreeWidgetItemIterator it(d->ui.list); (*it); ++it) {
189
190 if (!(*it)->parent()) {
191 continue;
192 }
193 KisShortcutsEditorItem *item = static_cast<KisShortcutsEditorItem *>(*it);
194 const QString actionId = item->data(Id).toString();
195 if (!schemeShortcuts.hasKey(actionId))
196 continue;
197
198 QList<QKeySequence> sc = QKeySequence::listFromString(schemeShortcuts.readEntry(actionId, QString()));
201 }
202}
203
204void KisShortcutsEditor::exportConfiguration(KConfigBase *config) const
205{
206 Q_ASSERT(config);
207 if (!config) {
208 return;
209 }
210
211 if (d->actionTypes) {
212 KConfigGroup group(config,QStringLiteral("Shortcuts"));
213 foreach (KisKActionCollection *collection, d->actionCollections) {
214 collection->writeSettings(&group, true);
215 }
216 }
217
219}
220
221void KisShortcutsEditor::saveShortcuts(KConfigGroup *config) const
222{
223 // This is a horrible mess with pointers...
224 KConfigGroup cg;
225 if (config == 0) {
226 cg = KConfigGroup(KSharedConfig::openConfig("kritashortcutsrc"),
227 QStringLiteral("Shortcuts"));
228 config = &cg;
229 }
230
231 // Clear and reset temporary shortcuts
232 config->deleteGroup();
233 foreach (KisKActionCollection *collection, d->actionCollections) {
234 collection->writeSettings(config, false);
235 }
236
238}
239
240//slot
242{
243 for (int i = 0; i < d->ui.list->columnCount(); i++) {
244 d->ui.list->resizeColumnToContents(i);
245 }
246}
247
248
249
250
252{
253 for (QTreeWidgetItemIterator it(d->ui.list); (*it); ++it) {
254 if (KisShortcutsEditorItem *item = dynamic_cast<KisShortcutsEditorItem *>(*it)) {
255 item->commit();
256 }
257 }
258}
259
261{
263 commit(); // Not doing this would be bad
264}
265
267{
268 // TODO: is this working?
269 for (QTreeWidgetItemIterator it(d->ui.list); (*it); ++it) {
270 if (KisShortcutsEditorItem *item = dynamic_cast<KisShortcutsEditorItem *>(*it)) {
271 item->undo();
272 }
273 }
274}
275
280
285
287{
288 if (s.isEmpty()) {
289 // Reset the tree area
290 d->ui.list->collapseAll();
291 d->ui.list->expandToDepth(0);
292 } else {
293 d->ui.list->expandAll();
294 }
295}
296
297KisShortcutsEditor::ActionTypes KisShortcutsEditor::actionTypes() const
298{
299 return d->actionTypes;
300}
301
302void KisShortcutsEditor::setActionTypes(ActionTypes actionTypes)
303{
305}
306
307
308#include "moc_KisShortcutsEditor.cpp"
QKeySequence alternateSequence(const QList< QKeySequence > &sequences)
QKeySequence primarySequence(const QList< QKeySequence > &sequences)
@ LocalAlternate
@ LocalPrimary
void applyShortcutScheme(const KConfigBase *config=0)
static KisActionRegistry * instance()
const QList< QAction * > actions() const
A container for a set of QAction objects.
QList< KisKActionCategory * > categories() const
QString componentDisplayName() const
void writeSettings(KConfigGroup *config=0, bool writeScheme=false, QAction *oneAction=0) const
QList< QAction * > actions() const
void setCheckActionCollections(const QList< KisKActionCollection * > checkActionCollections)
bool isModified(uint column) const
QVariant data(int column, int role=Qt::DisplayRole) const override
QList< KisKActionCollection * > actionCollections
KisShortcutsEditorDelegate * delegate
KisShortcutsEditor::ActionTypes actionTypes
Ui::KisShortcutsDialog ui
QTreeWidgetItem * findOrMakeItem(QTreeWidgetItem *parent, const QString &name)
bool addAction(QAction *action, QTreeWidgetItem *hier[], hierarchyLevel level)
void changeKeyShortcut(KisShortcutsEditorItem *item, uint column, const QKeySequence &capture)
void setActionTypes(KisShortcutsEditor::ActionTypes actionTypes)
Widget for configuration of KAccel and KGlobalAccel.
void saveShortcuts(KConfigGroup *config=0) const
void addCollection(KisKActionCollection *, const QString &title=QString())
~KisShortcutsEditor() override
Destructor.
void searchUpdated(QString s)
void setActionTypes(ActionTypes actionTypes)
void importConfiguration(KConfigBase *config, bool isScheme)
void exportConfiguration(KConfigBase *config) const
KisShortcutsEditorPrivate *const d
Q_DECLARE_METATYPE(KisPaintopLodLimitations)