KDEUI
kshortcutschemeseditor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kshortcutsdialog_p.h"
00020
00021 #include <QDir>
00022 #include <QLabel>
00023 #include <QMenu>
00024 #include <QFile>
00025 #include <QTextStream>
00026 #include <QtXml/QDomDocument>
00027 #include <QFileDialog>
00028
00029 #include <kcombobox.h>
00030 #include <kpushbutton.h>
00031 #include <kstandarddirs.h>
00032 #include <kactioncollection.h>
00033 #include <kmessagebox.h>
00034 #include <kxmlguiclient.h>
00035 #include <kinputdialog.h>
00036
00037 #include "kshortcutsdialog.h"
00038 #include "kshortcutschemeshelper_p.h"
00039
00040 KShortcutSchemesEditor::KShortcutSchemesEditor(KShortcutsDialog *parent)
00041 :QGroupBox(i18n("Shortcut Schemes"), parent), m_dialog(parent)
00042 {
00043 KConfigGroup group( KGlobal::config(), "Shortcut Schemes" );
00044
00045 QStringList schemeFiles = KGlobal::dirs()->findAllResources("appdata", "*shortcuts.rc");
00046 QStringList schemes;
00047 schemes << "Default";
00048 foreach (QString schemeFile, schemeFiles)
00049 {
00050 schemes << schemeFile.remove(QRegExp("^.*/"+KGlobal::mainComponent().componentName())).
00051 remove("shortcuts.rc");
00052 }
00053
00054 QString currentScheme = group.readEntry("Current Scheme", "Default");
00055
00056 QHBoxLayout *l = new QHBoxLayout(this);
00057 l->setMargin(0);
00058 l->setSpacing(KDialog::spacingHint());
00059
00060 QLabel *schemesLabel = new QLabel(i18n("Current scheme:"), this);
00061 l->addWidget(schemesLabel);
00062
00063 m_schemesList = new KComboBox(this);
00064 m_schemesList->setEditable(false);
00065 m_schemesList->addItems(schemes);
00066 m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
00067 schemesLabel->setBuddy(m_schemesList);
00068 l->addWidget(m_schemesList);
00069
00070 m_newScheme = new KPushButton(i18n("New..."));
00071 l->addWidget(m_newScheme);
00072
00073 m_deleteScheme = new KPushButton(i18n("Delete"));
00074 l->addWidget(m_deleteScheme);
00075
00076 KPushButton *moreActions = new KPushButton(i18n("More Actions"));
00077 l->addWidget(moreActions);
00078
00079 QMenu *moreActionsMenu = new QMenu(this);
00080 moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
00081 this, SLOT(saveAsDefaultsForScheme()));
00082 moreActionsMenu->addAction(i18n("Export Scheme..."),
00083 this, SLOT(exportShortcutsScheme()));
00084
00085 moreActions->setMenu(moreActionsMenu);
00086
00087 l->addStretch(1);
00088
00089 connect(m_schemesList, SIGNAL(activated(const QString&)),
00090 this, SIGNAL(shortcutsSchemeChanged(const QString&)));
00091 connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme()));
00092 connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme()));
00093 updateDeleteButton();
00094 }
00095
00096 void KShortcutSchemesEditor::newScheme()
00097 {
00098 bool ok;
00099 QString newName = KInputDialog::getText(i18n("Name for New Scheme"),
00100 i18n("Name for new scheme:"), i18n("New Scheme"), &ok,this);
00101 if (!ok )
00102 return;
00103
00104 if (m_schemesList->findText(newName) != -1)
00105 {
00106 KMessageBox::sorry(this, i18n("A scheme with this name already exists."));
00107 return;
00108 }
00109
00110 QString newSchemeFileName = KShortcutSchemesHelper::applicationShortcutSchemeFileName(newName);
00111
00112 QFile schemeFile(newSchemeFileName);
00113 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate))
00114 return;
00115
00116 QDomDocument doc;
00117 QDomElement docElem = doc.createElement("kpartgui");
00118 doc.appendChild(docElem);
00119 QDomElement elem = doc.createElement("ActionProperties");
00120 docElem.appendChild(elem);
00121
00122 QTextStream out(&schemeFile);
00123 out << doc.toString(4);
00124
00125 m_schemesList->addItem(newName);
00126 m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
00127 updateDeleteButton();
00128 emit shortcutsSchemeChanged(newName);
00129 }
00130
00131 void KShortcutSchemesEditor::deleteScheme()
00132 {
00133 if (KMessageBox::questionYesNo(this,
00134 i18n("Do you really want to delete the scheme %1?\n\
00135 Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No)
00136 return;
00137
00138
00139 QFile::remove(KShortcutSchemesHelper::applicationShortcutSchemeFileName(currentScheme()));
00140
00141
00142 foreach (KActionCollection *collection, m_dialog->actionCollections())
00143 {
00144 const KXMLGUIClient *client = collection->parentGUIClient();
00145 if (!client)
00146 continue;
00147 QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(client, currentScheme()));
00148 }
00149
00150 m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
00151 updateDeleteButton();
00152 emit shortcutsSchemeChanged(currentScheme());
00153 }
00154
00155 QString KShortcutSchemesEditor::currentScheme()
00156 {
00157 return m_schemesList->currentText();
00158 }
00159
00160 void KShortcutSchemesEditor::exportShortcutsScheme()
00161 {
00162
00163 QString exportTo = QFileDialog::getExistingDirectory(this, i18n("Export to Location"),
00164 QDir::currentPath());
00165 if (exportTo.isEmpty())
00166 return;
00167
00168 QDir schemeRoot(exportTo);
00169
00170 if (!schemeRoot.exists(exportTo))
00171 {
00172 KMessageBox::error(this, i18n("Could not export shortcuts scheme because the location is invalid."));
00173 return;
00174 }
00175
00176 foreach (KActionCollection *collection, m_dialog->actionCollections())
00177 {
00178 const KXMLGUIClient *client = collection->parentGUIClient();
00179 if (!client) continue;
00180 QString fileDir = QLatin1String("shortcuts/share/apps/")
00181 + client->componentData().componentName() + '/';
00182 schemeRoot.mkpath(fileDir);
00183 KShortcutSchemesHelper::exportActionCollection(collection,
00184 currentScheme(), exportTo + '/' + fileDir);
00185 }
00186 }
00187
00188 void KShortcutSchemesEditor::saveAsDefaultsForScheme()
00189 {
00190 foreach (KActionCollection *collection, m_dialog->actionCollections())
00191 KShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
00192 }
00193
00194
00195 void KShortcutSchemesEditor::updateDeleteButton()
00196 {
00197 m_deleteScheme->setEnabled(m_schemesList->count()>=1);
00198 }