• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • dialogs
kshortcutschemeseditor.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2008 Alexander Dymo <adymo@kdevelop.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19#include "kshortcutsdialog_p.h"
20
21#include <QDir>
22#include <QLabel>
23#include <QMenu>
24#include <QFile>
25#include <QTextStream>
26#include <QtXml/QDomDocument>
27#include <QFileDialog>
28
29#include <kcombobox.h>
30#include <kpushbutton.h>
31#include <kstandarddirs.h>
32#include <kactioncollection.h>
33#include <kmessagebox.h>
34#include <kxmlguiclient.h>
35#include <kinputdialog.h>
36
37#include "kshortcutsdialog.h"
38#include "kshortcutschemeshelper_p.h"
39
40KShortcutSchemesEditor::KShortcutSchemesEditor(KShortcutsDialog *parent)
41 :QGroupBox(i18n("Shortcut Schemes"), parent), m_dialog(parent)
42{
43 KConfigGroup group( KGlobal::config(), "Shortcut Schemes" );
44
45 const QStringList schemeFiles = KGlobal::dirs()->findAllResources("appdata", "*shortcuts.rc");
46 QStringList schemes;
47 schemes << "Default";
48 foreach (QString schemeFile, schemeFiles)
49 {
50 schemes << schemeFile.remove(QRegExp("^.*/"+KGlobal::mainComponent().componentName())).
51 remove("shortcuts.rc");
52 }
53
54 QString currentScheme = group.readEntry("Current Scheme", "Default");
55
56 QHBoxLayout *l = new QHBoxLayout(this);
57 l->setMargin(0);
58
59 QLabel *schemesLabel = new QLabel(i18n("Current scheme:"), this);
60 l->addWidget(schemesLabel);
61
62 m_schemesList = new KComboBox(this);
63 m_schemesList->setEditable(false);
64 m_schemesList->addItems(schemes);
65 m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
66 schemesLabel->setBuddy(m_schemesList);
67 l->addWidget(m_schemesList);
68
69 m_newScheme = new KPushButton(i18n("New..."));
70 l->addWidget(m_newScheme);
71
72 m_deleteScheme = new KPushButton(i18n("Delete"));
73 l->addWidget(m_deleteScheme);
74
75 KPushButton *moreActions = new KPushButton(i18n("More Actions"));
76 l->addWidget(moreActions);
77
78 QMenu *moreActionsMenu = new QMenu(this);
79 moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
80 this, SLOT(saveAsDefaultsForScheme()));
81 moreActionsMenu->addAction(i18n("Export Scheme..."),
82 this, SLOT(exportShortcutsScheme()));
83
84 moreActions->setMenu(moreActionsMenu);
85
86 l->addStretch(1);
87
88 connect(m_schemesList, SIGNAL(activated(QString)),
89 this, SIGNAL(shortcutsSchemeChanged(QString)));
90 connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme()));
91 connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme()));
92 updateDeleteButton();
93}
94
95void KShortcutSchemesEditor::newScheme()
96{
97 bool ok;
98 const QString newName = KInputDialog::getText(i18n("Name for New Scheme"),
99 i18n("Name for new scheme:"), i18n("New Scheme"), &ok,this);
100 if (!ok )
101 return;
102
103 if (m_schemesList->findText(newName) != -1)
104 {
105 KMessageBox::sorry(this, i18n("A scheme with this name already exists."));
106 return;
107 }
108
109 const QString newSchemeFileName = KShortcutSchemesHelper::applicationShortcutSchemeFileName(newName);
110
111 QFile schemeFile(newSchemeFileName);
112 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate))
113 return;
114
115 QDomDocument doc;
116 QDomElement docElem = doc.createElement("kpartgui");
117 doc.appendChild(docElem);
118 QDomElement elem = doc.createElement("ActionProperties");
119 docElem.appendChild(elem);
120
121 QTextStream out(&schemeFile);
122 out << doc.toString(4);
123
124 m_schemesList->addItem(newName);
125 m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
126 updateDeleteButton();
127 emit shortcutsSchemeChanged(newName);
128}
129
130void KShortcutSchemesEditor::deleteScheme()
131{
132 if (KMessageBox::questionYesNo(this,
133 i18n("Do you really want to delete the scheme %1?\n\
134Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No)
135 return;
136
137 //delete the scheme for the app itself
138 QFile::remove(KShortcutSchemesHelper::applicationShortcutSchemeFileName(currentScheme()));
139
140 //delete all scheme files we can find for xmlguiclients in the user directories
141 foreach (KActionCollection *collection, m_dialog->actionCollections())
142 {
143 const KXMLGUIClient *client = collection->parentGUIClient();
144 if (!client)
145 continue;
146 QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(client, currentScheme()));
147 }
148
149 m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
150 updateDeleteButton();
151 emit shortcutsSchemeChanged(currentScheme());
152}
153
154QString KShortcutSchemesEditor::currentScheme()
155{
156 return m_schemesList->currentText();
157}
158
159void KShortcutSchemesEditor::exportShortcutsScheme()
160{
161 //ask user about dir
162 QString exportTo = QFileDialog::getExistingDirectory(this, i18n("Export to Location"), //krazy:exclude=qclasses it is not possible to use KDirSelectDialog here because kfile links against kdeui; the dialog gets replaced anyway with the KDE one at runtime
163 QDir::currentPath());
164 if (exportTo.isEmpty())
165 return;
166
167 QDir schemeRoot(exportTo);
168
169 if (!schemeRoot.exists(exportTo))
170 {
171 KMessageBox::error(this, i18n("Could not export shortcuts scheme because the location is invalid."));
172 return;
173 }
174
175 foreach (KActionCollection *collection, m_dialog->actionCollections())
176 {
177 const KXMLGUIClient *client = collection->parentGUIClient();
178 if (!client) continue;
179 KShortcutSchemesHelper::exportActionCollection(collection,
180 currentScheme(), exportTo + '/');
181 }
182}
183
184void KShortcutSchemesEditor::saveAsDefaultsForScheme()
185{
186 foreach (KActionCollection *collection, m_dialog->actionCollections())
187 KShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
188}
189
190
191void KShortcutSchemesEditor::updateDeleteButton()
192{
193 m_deleteScheme->setEnabled(m_schemesList->count()>=1);
194}
KActionCollection
A container for a set of QAction objects.
Definition: kactioncollection.h:57
KActionCollection::parentGUIClient
const KXMLGUIClient * parentGUIClient() const
The parent KXMLGUIClient, or null if not available.
Definition: kactioncollection.cpp:181
KComboBox
An enhanced combo box.
Definition: kcombobox.h:149
KConfigGroup
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Display an "Error" dialog.
Definition: kmessagebox.cpp:818
KMessageBox::No
@ No
Definition: kmessagebox.h:73
KMessageBox::sorry
static void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
Display an "Sorry" dialog.
Definition: kmessagebox.cpp:904
KMessageBox::questionYesNo
static int 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)
Display a simple "question" dialog.
Definition: kmessagebox.cpp:353
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:47
KShortcutsDialog
Dialog for configuration of KActionCollection and KGlobalAccel.
Definition: kshortcutsdialog.h:70
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter, SearchOptions options, QStringList &relPaths) const
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document,...
Definition: kxmlguiclient.h:47
QGroupBox
QLabel
QMenu
kactioncollection.h
kcombobox.h
kinputdialog.h
i18n
QString i18n(const char *text)
kmessagebox.h
kpushbutton.h
kshortcutsdialog.h
kstandarddirs.h
kxmlguiclient.h
KGlobal::mainComponent
const KComponentData & mainComponent()
KGlobal::dirs
KStandardDirs * dirs()
KGlobal::config
KSharedConfigPtr config()
KInputDialog::getText
QString getText(const QString &caption, const QString &label, const QString &value, bool *ok, QWidget *parent, QValidator *validator, const QString &mask, const QString &whatsThis, const QStringList &completionList)
Static convenience function to get a string from the user.
Definition: kinputdialog.cpp:330
group
group
KStandardGuiItem::remove
KGuiItem remove()
Returns the 'Remove' gui item.
Definition: kstandardguiitem.cpp:289
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal