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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
40 KShortcutSchemesEditor::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 
95 void 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 
130 void KShortcutSchemesEditor::deleteScheme()
131 {
132  if (KMessageBox::questionYesNo(this,
133  i18n("Do you really want to delete the scheme %1?\n\
134 Note 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 
154 QString KShortcutSchemesEditor::currentScheme()
155 {
156  return m_schemesList->currentText();
157 }
158 
159 void 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 
184 void KShortcutSchemesEditor::saveAsDefaultsForScheme()
185 {
186  foreach (KActionCollection *collection, m_dialog->actionCollections())
187  KShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
188 }
189 
190 
191 void KShortcutSchemesEditor::updateDeleteButton()
192 {
193  m_deleteScheme->setEnabled(m_schemesList->count()>=1);
194 }
i18n
QString i18n(const char *text)
QPushButton::setMenu
void setMenu(QMenu *menu)
KMessageBox::No
Definition: kmessagebox.h:73
kcombobox.h
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:46
KActionCollection
A container for a set of QAction objects.
Definition: kactioncollection.h:56
QFileDialog::getExistingDirectory
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFlags< QFileDialog::Option > options)
KXMLGUIClient
A KXMLGUIClient can be used with KXMLGUIFactory to create a GUI from actions and an XML document...
Definition: kxmlguiclient.h:46
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
group
QFile::remove
bool remove()
QDomDocument::toString
QString toString(int indent) const
kactioncollection.h
QMenu::addAction
void addAction(QAction *action)
KGlobal::dirs
KStandardDirs * dirs()
QHBoxLayout
QString::remove
QString & remove(int position, int n)
QDir::currentPath
QString currentPath()
QFile
QTextStream
KGlobal::config
KSharedConfigPtr config()
QLabel::setBuddy
void setBuddy(QWidget *buddy)
kshortcutsdialog.h
QRegExp
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QGroupBox
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
QString::isEmpty
bool isEmpty() const
QString
QLayout::setMargin
void setMargin(int margin)
QStringList
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
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
QMenu
QDomDocument
kpushbutton.h
KStandardGuiItem::ok
KGuiItem ok()
Returns the 'Ok' gui item.
Definition: kstandardguiitem.cpp:107
kinputdialog.h
QDir
KConfigGroup
kxmlguiclient.h
QBoxLayout::addStretch
void addStretch(int stretch)
kstandarddirs.h
KComboBox
An enhanced combo box.
Definition: kcombobox.h:148
KGlobal::mainComponent
const KComponentData & mainComponent()
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter=QString(), SearchOptions options=NoSearchOptions) const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
KActionCollection::parentGUIClient
const KXMLGUIClient * parentGUIClient() const
The parent KXMLGUIClient, or null if not available.
Definition: kactioncollection.cpp:181
kmessagebox.h
QLabel
QDomElement
KShortcutsDialog
Dialog for configuration of KActionCollection and KGlobalAccel.
Definition: kshortcutsdialog.h:69
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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