KXmlGui

kshortcutsdialog.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org>
4 SPDX-FileCopyrightText: 1997 Nicolas Hadacek <hadacek@kde.org>
5 SPDX-FileCopyrightText: 1998 Matthias Ettrich <ettrich@kde.org>
6 SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
7 SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
8 SPDX-FileCopyrightText: 2007 Roberto Raggi <roberto@kdevelop.org>
9 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
10 SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
11 SPDX-FileCopyrightText: 2008 Alexander Dymo <adymo@kdevelop.org>
12 SPDX-FileCopyrightText: 2009 Chani Armitage <chani@kde.org>
13
14 SPDX-License-Identifier: LGPL-2.0-or-later
15*/
16
17#include "kshortcutsdialog.h"
18#include "kshortcutschemeshelper_p.h"
19#include "kshortcutsdialog_p.h"
20
21#include <QApplication>
22#include <QDialogButtonBox>
23#include <QDomDocument>
24
25#include <KConfigGroup>
26#include <KLocalizedString>
27#include <KMessageBox>
28#include <KSharedConfig>
29
30#include "kactioncollection.h"
31#include "kxmlguiclient.h"
32#include "kxmlguifactory.h"
33
34/************************************************************************/
35/* KShortcutsDialog */
36/* */
37/* Originally by Nicolas Hadacek <hadacek@via.ecp.fr> */
38/* */
39/* Substantially revised by Mark Donohoe <donohoe@kde.org> */
40/* */
41/* And by Espen Sand <espen@kde.org> 1999-10-19 */
42/* (by using KDialog there is almost no code left ;) */
43/* */
44/************************************************************************/
45
46QKeySequence primarySequence(const QList<QKeySequence> &sequences)
47{
48 return sequences.isEmpty() ? QKeySequence() : sequences.at(0);
49}
50
51QKeySequence alternateSequence(const QList<QKeySequence> &sequences)
52{
53 return sequences.size() <= 1 ? QKeySequence() : sequences.at(1);
54}
55
56class KShortcutsDialogPrivate
57{
58public:
59 KShortcutsDialogPrivate(KShortcutsDialog *qq)
60 : q(qq)
61 {
62 }
63
64 QList<KActionCollection *> m_collections;
65
66 void changeShortcutScheme(const QString &scheme)
67 {
68 if (m_keyChooser->isModified()
70 i18n("The current shortcut scheme is modified. Save before switching to the new one?"),
71 QString(),
75 m_keyChooser->save();
76 } else {
77 m_keyChooser->undo();
78 }
79
81 m_keyChooser->clearCollections();
82
83 for (KActionCollection *collection : std::as_const(m_collections)) {
84 // passing an empty stream forces the clients to reread the XML
85 KXMLGUIClient *client = const_cast<KXMLGUIClient *>(collection->parentGUIClient());
86 if (client) {
87 client->setXMLGUIBuildDocument(QDomDocument());
88 }
89 }
90
91 // get xmlguifactory
92 if (!m_collections.isEmpty()) {
93 const KXMLGUIClient *client = m_collections.first()->parentGUIClient();
94 if (client) {
95 KXMLGUIFactory *factory = client->factory();
96 if (factory) {
97 factory->changeShortcutScheme(scheme);
98 }
99 }
100 }
101
102 for (KActionCollection *collection : std::as_const(m_collections)) {
103 m_keyChooser->addCollection(collection);
104 }
105
107 }
108
109 void undo()
110 {
111 m_keyChooser->undo();
112 }
113
114 void toggleDetails()
115 {
116 const bool isVisible = m_schemeEditor->isVisible();
117
118 m_schemeEditor->setVisible(!isVisible);
119 m_detailsButton->setText(detailsButtonText() + (isVisible ? QStringLiteral(" >>") : QStringLiteral(" <<")));
120 }
121
122 static QString detailsButtonText()
123 {
124 return i18n("Manage &Schemes");
125 }
126
127 void save()
128 {
129 m_keyChooser->save();
130 Q_EMIT q->saved();
131 }
132
133 KShortcutsDialog *const q;
134 KShortcutsEditor *m_keyChooser = nullptr; // ### move
135 KShortcutSchemesEditor *m_schemeEditor = nullptr;
136 QPushButton *m_detailsButton = nullptr;
137 bool m_saveSettings = false;
138};
139
144
146 : QDialog(parent)
147 , d(new KShortcutsDialogPrivate(this))
148{
149 setWindowTitle(i18nc("@title:window", "Configure Keyboard Shortcuts"));
150 setModal(true);
151
152 QVBoxLayout *layout = new QVBoxLayout(this);
153
154 d->m_keyChooser = new KShortcutsEditor(this, types, allowLetterShortcuts);
155 layout->addWidget(d->m_keyChooser);
156
157 d->m_schemeEditor = new KShortcutSchemesEditor(this);
158 connect(d->m_schemeEditor, &KShortcutSchemesEditor::shortcutsSchemeChanged, this, [this](const QString &scheme) {
159 d->changeShortcutScheme(scheme);
160 });
161 d->m_schemeEditor->hide();
162 layout->addWidget(d->m_schemeEditor);
163
164 d->m_detailsButton = new QPushButton;
165 d->m_detailsButton->setText(KShortcutsDialogPrivate::detailsButtonText() + QLatin1String(" >>"));
166
167 QPushButton *printButton = new QPushButton;
169
170 QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
171 buttonBox->addButton(d->m_detailsButton, QDialogButtonBox::ActionRole);
172 buttonBox->addButton(printButton, QDialogButtonBox::ActionRole);
177 layout->addWidget(buttonBox);
178
180 connect(d->m_detailsButton, &QPushButton::clicked, this, [this]() {
181 d->toggleDetails();
182 });
183 connect(printButton, &QPushButton::clicked, d->m_keyChooser, &KShortcutsEditor::printShortcuts);
184 connect(buttonBox, &QDialogButtonBox::rejected, this, [this]() {
185 d->undo();
186 });
187
190
191 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("KShortcutsDialog Settings"));
192 resize(group.readEntry("Dialog Size", sizeHint()));
193}
194
196{
197 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("KShortcutsDialog Settings"));
199}
200
202{
203 d->m_keyChooser->addCollection(collection, title);
204 d->m_collections << collection;
205}
206
208{
209 return d->m_collections;
210}
211
212// TODO KF6: remove this method, always save settings, and open the
213// dialog with show() not exec()
214bool KShortcutsDialog::configure(bool saveSettings)
215{
216 d->m_saveSettings = saveSettings;
217 if (isModal()) {
218 int retcode = exec();
219 return retcode;
220 } else {
221 show();
222 return false;
223 }
224}
225
227{
228 if (d->m_saveSettings) {
229 d->save();
230 }
232}
233
235{
236 return QSize(600, 480);
237}
238
239// static
241{
242 auto *dlg = new KShortcutsDialog(KShortcutsEditor::AllActions, allowLetterShortcuts, parent);
243 dlg->setAttribute(Qt::WA_DeleteOnClose);
244
245 dlg->d->m_saveSettings = true; // Always save settings if the dialog is accepted
246
247 dlg->addCollection(collection);
248 dlg->show();
249}
250
252{
253 KConfig config(path);
254 d->m_keyChooser->importConfiguration(static_cast<KConfigBase *>(&config));
255}
256
258{
259 KConfig config(path);
260 d->m_keyChooser->exportConfiguration(static_cast<KConfigBase *>(&config));
261}
262
264{
265 d->m_schemeEditor->refreshSchemes();
266}
267
269{
270 d->m_schemeEditor->addMoreMenuAction(action);
271}
272
273#include "moc_kshortcutsdialog.cpp"
274#include "moc_kshortcutsdialog_p.cpp"
A container for a set of QAction objects.
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static void assign(QPushButton *button, const KGuiItem &item)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KShortcutsDialog(KShortcutsEditor::ActionTypes actionTypes=KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcuts allowLetterShortcuts=KShortcutsEditor::LetterShortcutsAllowed, QWidget *parent=nullptr)
Constructs a KShortcutsDialog as a child of parent.
void importConfiguration(const QString &path)
Imports a shortcuts set up from path.
void addActionToSchemesMoreButton(QAction *action)
This adds a QAction to the "More Actions" menu.
QSize sizeHint() const override
~KShortcutsDialog() override
Destructor.
void exportConfiguration(const QString &path) const
Exports a shortcuts set up from path.
QList< KActionCollection * > actionCollections() const
void refreshSchemes()
Reloads the list of schemes in the "Manage Schemes" section.
bool configure(bool saveSettings=true)
Run the dialog and call writeSettings() on the action collections that were added if saveSettings is ...
void accept() override
static void showDialog(KActionCollection *collection, KShortcutsEditor::LetterShortcuts allowLetterShortcuts=KShortcutsEditor::LetterShortcutsAllowed, QWidget *parent=nullptr)
This static method shows a modal dialog that can be used to configure the shortcuts associated with e...
void addCollection(KActionCollection *collection, const QString &title={})
Add all actions of the collection to the ones displayed and configured by the dialog.
Widget for configuration of KAccel and KGlobalAccel.
@ AllActions
All actions.
void allDefault()
Set all shortcuts to their default values (bindings).
QFlags< ActionType > ActionTypes
Stores a combination of ActionType values.
KXMLGUIFactory * factory() const
Retrieves a pointer to the KXMLGUIFactory this client is associated with (will return nullptr if the ...
void setXMLGUIBuildDocument(const QDomDocument &doc)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem print()
KGuiItem cancel()
KGuiItem defaults()
KGuiItem save()
KGuiItem discard()
void clicked(bool checked)
void setText(const QString &text)
QDialog(QWidget *parent, Qt::WindowFlags f)
virtual void accept()
virtual int exec()
void setModal(bool modal)
virtual void reject()
QPushButton * addButton(StandardButton button)
QPushButton * button(StandardButton which) const const
void setStandardButtons(StandardButtons buttons)
void restoreOverrideCursor()
void setOverrideCursor(const QCursor &cursor)
bool isEmpty() const const
qsizetype size() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
WaitCursor
WA_DeleteOnClose
QWidget(QWidget *parent, Qt::WindowFlags f)
QLayout * layout() const const
bool isModal() const const
void show()
void resize(const QSize &)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:54:16 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.