KXmlGui

kshortcutsdialog.cpp
1 /* This file is part of the KDE libraries Copyright (C) 1998 Mark Donohoe <[email protected]>
2  Copyright (C) 1997 Nicolas Hadacek <[email protected]>
3  Copyright (C) 1998 Matthias Ettrich <[email protected]>
4  Copyright (C) 2001 Ellis Whitehead <[email protected]>
5  Copyright (C) 2006 Hamish Rodda <[email protected]>
6  Copyright (C) 2007 Roberto Raggi <[email protected]>
7  Copyright (C) 2007 Andreas Hartmetz <[email protected]>
8  Copyright (C) 2008 Michael Jansen <[email protected]>
9  Copyright (C) 2008 Alexander Dymo <[email protected]>
10  Copyright (C) 2009 Chani Armitage <[email protected]>
11 
12  This library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU Library General Public
14  License as published by the Free Software Foundation; either
15  version 2 of the License, or (at your option) any later version.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Library General Public License for more details.
21 
22  You should have received a copy of the GNU Library General Public License
23  along with this library; see the file COPYING.LIB. If not, write to
24  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  Boston, MA 02110-1301, USA.
26 */
27 
28 #include "kshortcutsdialog.h"
29 #include "kshortcutsdialog_p.h"
30 #include "kshortcutschemeshelper_p.h"
31 
32 #include <QApplication>
33 #include <QDialogButtonBox>
34 #include <QDomDocument>
35 
36 #include <klocalizedstring.h>
37 #include <kconfiggroup.h>
38 #include <kmessagebox.h>
39 #include <ksharedconfig.h>
40 
41 #include "kxmlguiclient.h"
42 #include "kxmlguifactory.h"
43 #include "kactioncollection.h"
44 
45 /************************************************************************/
46 /* KShortcutsDialog */
47 /* */
48 /* Originally by Nicolas Hadacek <[email protected]> */
49 /* */
50 /* Substantially revised by Mark Donohoe <[email protected]> */
51 /* */
52 /* And by Espen Sand <[email protected]> 1999-10-19 */
53 /* (by using KDialog there is almost no code left ;) */
54 /* */
55 /************************************************************************/
56 
57 QKeySequence primarySequence(const QList<QKeySequence> &sequences)
58 {
59  return sequences.isEmpty() ? QKeySequence() : sequences.at(0);
60 }
61 
62 QKeySequence alternateSequence(const QList<QKeySequence> &sequences)
63 {
64  return sequences.size() <= 1 ? QKeySequence() : sequences.at(1);
65 }
66 
67 class Q_DECL_HIDDEN KShortcutsDialog::KShortcutsDialogPrivate
68 {
69 public:
70 
71  KShortcutsDialogPrivate(KShortcutsDialog *q)
72  : q(q),
73  m_keyChooser(nullptr),
74  m_schemeEditor(nullptr),
75  m_detailsButton(nullptr),
76  m_saveSettings(false)
77  {
78  }
79 
80  QList<KActionCollection *> m_collections;
81 
82  void changeShortcutScheme(const QString &scheme)
83  {
84  if (m_keyChooser->isModified() && KMessageBox::questionYesNo(q,
85  i18n("The current shortcut scheme is modified. Save before switching to the new one?")) == KMessageBox::Yes) {
86  m_keyChooser->save();
87  } else {
88  m_keyChooser->undoChanges();
89  }
90 
92  m_keyChooser->clearCollections();
93 
94  for (KActionCollection *collection : qAsConst(m_collections)) {
95  // passing an empty stream forces the clients to reread the XML
96  KXMLGUIClient *client = const_cast<KXMLGUIClient *>(collection->parentGUIClient());
97  if (client) {
99  }
100  }
101 
102  //get xmlguifactory
103  if (!m_collections.isEmpty()) {
104  const KXMLGUIClient *client = m_collections.first()->parentGUIClient();
105  if (client) {
106  KXMLGUIFactory *factory = client->factory();
107  if (factory) {
108  factory->changeShortcutScheme(scheme);
109  }
110  }
111  }
112 
113  for (KActionCollection *collection : qAsConst(m_collections)) {
114  m_keyChooser->addCollection(collection);
115  }
116 
118  }
119 
120  void undoChanges()
121  {
122  m_keyChooser->undoChanges();
123  }
124 
125  void toggleDetails()
126  {
127  const bool isVisible = m_schemeEditor->isVisible();
128 
129  m_schemeEditor->setVisible(!isVisible);
130  m_detailsButton->setText(detailsButtonText() + (isVisible ? QStringLiteral(" >>") : QStringLiteral(" <<")));
131  }
132 
133  static QString detailsButtonText()
134  {
135  return i18n("Manage &Schemes");
136  }
137 
138  void save()
139  {
140  m_keyChooser->save();
141  emit q->saved();
142  }
143 
144  KShortcutsDialog *q;
145  KShortcutsEditor *m_keyChooser; // ### move
146  KShortcutSchemesEditor *m_schemeEditor;
147  QPushButton *m_detailsButton;
148  bool m_saveSettings;
149 };
150 
152  : QDialog(parent), d(new KShortcutsDialogPrivate(this))
153 {
154  setWindowTitle(i18nc("@title:window", "Configure Keyboard Shortcuts"));
155  setModal(true);
156 
158  setLayout(layout);
159 
160  d->m_keyChooser = new KShortcutsEditor(this, types, allowLetterShortcuts);
161  layout->addWidget(d->m_keyChooser);
162 
163  d->m_schemeEditor = new KShortcutSchemesEditor(this);
164  connect(d->m_schemeEditor, SIGNAL(shortcutsSchemeChanged(QString)),
165  this, SLOT(changeShortcutScheme(QString)));
166  d->m_schemeEditor->hide();
167  layout->addWidget(d->m_schemeEditor);
168 
169  d->m_detailsButton = new QPushButton;
170  d->m_detailsButton->setText(KShortcutsDialogPrivate::detailsButtonText() + QLatin1String(" >>"));
171 
172  QPushButton *printButton = new QPushButton;
173  KGuiItem::assign(printButton, KStandardGuiItem::print());
174 
175  QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
176  buttonBox->addButton(d->m_detailsButton, QDialogButtonBox::ActionRole);
177  buttonBox->addButton(printButton, QDialogButtonBox::ActionRole);
178  buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
179  KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
180  KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
181  KGuiItem::assign(buttonBox->button(QDialogButtonBox::RestoreDefaults), KStandardGuiItem::defaults());
182  layout->addWidget(buttonBox);
183 
184  connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked,
185  d->m_keyChooser, &KShortcutsEditor::allDefault);
186  connect(d->m_detailsButton, SIGNAL(clicked()), this, SLOT(toggleDetails()));
187  connect(printButton, &QPushButton::clicked,
188  d->m_keyChooser, &KShortcutsEditor::printShortcuts);
189  connect(buttonBox, SIGNAL(rejected()), this, SLOT(undoChanges()));
190 
193 
194  KConfigGroup group(KSharedConfig::openConfig(), "KShortcutsDialog Settings");
195  resize(group.readEntry("Dialog Size", sizeHint()));
196 }
197 
199 {
200  KConfigGroup group(KSharedConfig::openConfig(), "KShortcutsDialog Settings");
202  delete d;
203 }
204 
206 {
207  d->m_keyChooser->addCollection(collection, title);
208  d->m_collections << collection;
209 }
210 
212 {
213  return d->m_collections;
214 }
215 
216 //FIXME should there be a setSaveSettings method?
217 bool KShortcutsDialog::configure(bool saveSettings)
218 {
219  d->m_saveSettings = saveSettings;
220  if (isModal()) {
221  int retcode = exec();
222  return retcode;
223  } else {
224  show();
225  return false;
226  }
227 }
228 
230 {
231  if (d->m_saveSettings) {
232  d->save();
233  }
234  QDialog::accept();
235 }
236 
238 {
239  return QSize(600, 480);
240 }
241 
243  QWidget *parent, bool saveSettings)
244 {
245  //qDebug(125) << "KShortcutsDialog::configureKeys( KActionCollection*, " << saveSettings << " )";
246  KShortcutsDialog dlg(KShortcutsEditor::AllActions, allowLetterShortcuts, parent);
247  dlg.d->m_keyChooser->addCollection(collection);
248  return dlg.configure(saveSettings);
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 
263 #include "moc_kshortcutsdialog.cpp"
QLayout * layout() const const
KGuiItem defaults()
void resize(int w, int h)
A container for a set of QAction objects.
virtual void reject()
void setModal(bool modal)
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document...
Definition: kxmlguiclient.h:48
void rejected()
void addButton(QAbstractButton *button, QDialogButtonBox::ButtonRole role)
virtual int exec()
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
KXMLGUIFactory * factory() const
Retrieves a pointer to the KXMLGUIFactory this client is associated with (will return nullptr if the ...
~KShortcutsDialog() override
Destructor.
KGuiItem cancel()
void restoreOverrideCursor()
int size() const const
void printShortcuts() const
Opens a printing dialog to print all the shortcuts.
QSize sizeHint() const override
QSize size() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
void clicked(bool checked)
const QList< QKeySequence > & save()
T & first()
Widget for configuration of KAccel and KGlobalAccel.
bool isModal() const const
QList< KActionCollection * > actionCollections() const
virtual void accept()
void addCollection(KActionCollection *, const QString &title=QString())
Add all actions of the collection to the ones displayed and configured by the dialog.
void accept() override
void setOverrideCursor(const QCursor &cursor)
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
KXMLGUIFactory, together with KXMLGUIClient objects, can be used to create a GUI of container widgets...
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void allDefault()
Set all shortcuts to their default values (bindings).
QString i18n(const char *text, const TYPE &arg...)
void setStandardButtons(QDialogButtonBox::StandardButtons buttons)
KShortcutsDialog(KShortcutsEditor::ActionTypes types=KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcuts allowLetterShortcuts=KShortcutsEditor::LetterShortcutsAllowed, QWidget *parent=nullptr)
Constructs a KShortcutsDialog as a child of parent.
void setWindowTitle(const QString &)
QPushButton * button(QDialogButtonBox::StandardButton which) const const
void show()
void importConfiguration(const QString &path)
Imports a shortcuts set up from path.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
KGuiItem print()
T readEntry(const QString &key, const T &aDefault) const
void exportConfiguration(const QString &path) const
Exports a shortcuts set up from path.
Dialog for configuration of KActionCollection and KGlobalAccel.
void setXMLGUIBuildDocument(const QDomDocument &doc)
bool configure(bool saveSettings=true)
Run the dialog and call writeSettings() on the action collections that were added if saveSettings is ...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:56:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.