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
00059 QLabel *schemesLabel = new QLabel(i18n("Current scheme:"), this);
00060 l->addWidget(schemesLabel);
00061
00062 m_schemesList = new KComboBox(this);
00063 m_schemesList->setEditable(false);
00064 m_schemesList->addItems(schemes);
00065 m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
00066 schemesLabel->setBuddy(m_schemesList);
00067 l->addWidget(m_schemesList);
00068
00069 m_newScheme = new KPushButton(i18n("New..."));
00070 l->addWidget(m_newScheme);
00071
00072 m_deleteScheme = new KPushButton(i18n("Delete"));
00073 l->addWidget(m_deleteScheme);
00074
00075 KPushButton *moreActions = new KPushButton(i18n("More Actions"));
00076 l->addWidget(moreActions);
00077
00078 QMenu *moreActionsMenu = new QMenu(this);
00079 moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
00080 this, SLOT(saveAsDefaultsForScheme()));
00081 moreActionsMenu->addAction(i18n("Export Scheme..."),
00082 this, SLOT(exportShortcutsScheme()));
00083
00084 moreActions->setMenu(moreActionsMenu);
00085
00086 l->addStretch(1);
00087
00088 connect(m_schemesList, SIGNAL(activated(const QString&)),
00089 this, SIGNAL(shortcutsSchemeChanged(const QString&)));
00090 connect(m_newScheme, SIGNAL(clicked()), this, SLOT(newScheme()));
00091 connect(m_deleteScheme, SIGNAL(clicked()), this, SLOT(deleteScheme()));
00092 updateDeleteButton();
00093 }
00094
00095 void KShortcutSchemesEditor::newScheme()
00096 {
00097 bool ok;
00098 QString newName = KInputDialog::getText(i18n("Name for New Scheme"),
00099 i18n("Name for new scheme:"), i18n("New Scheme"), &ok,this);
00100 if (!ok )
00101 return;
00102
00103 if (m_schemesList->findText(newName) != -1)
00104 {
00105 KMessageBox::sorry(this, i18n("A scheme with this name already exists."));
00106 return;
00107 }
00108
00109 QString newSchemeFileName = KShortcutSchemesHelper::applicationShortcutSchemeFileName(newName);
00110
00111 QFile schemeFile(newSchemeFileName);
00112 if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate))
00113 return;
00114
00115 QDomDocument doc;
00116 QDomElement docElem = doc.createElement("kpartgui");
00117 doc.appendChild(docElem);
00118 QDomElement elem = doc.createElement("ActionProperties");
00119 docElem.appendChild(elem);
00120
00121 QTextStream out(&schemeFile);
00122 out << doc.toString(4);
00123
00124 m_schemesList->addItem(newName);
00125 m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
00126 updateDeleteButton();
00127 emit shortcutsSchemeChanged(newName);
00128 }
00129
00130 void KShortcutSchemesEditor::deleteScheme()
00131 {
00132 if (KMessageBox::questionYesNo(this,
00133 i18n("Do you really want to delete the scheme %1?\n\
00134 Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No)
00135 return;
00136
00137
00138 QFile::remove(KShortcutSchemesHelper::applicationShortcutSchemeFileName(currentScheme()));
00139
00140
00141 foreach (KActionCollection *collection, m_dialog->actionCollections())
00142 {
00143 const KXMLGUIClient *client = collection->parentGUIClient();
00144 if (!client)
00145 continue;
00146 QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(client, currentScheme()));
00147 }
00148
00149 m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
00150 updateDeleteButton();
00151 emit shortcutsSchemeChanged(currentScheme());
00152 }
00153
00154 QString KShortcutSchemesEditor::currentScheme()
00155 {
00156 return m_schemesList->currentText();
00157 }
00158
00159 void KShortcutSchemesEditor::exportShortcutsScheme()
00160 {
00161
00162 QString exportTo = QFileDialog::getExistingDirectory(this, i18n("Export to Location"),
00163 QDir::currentPath());
00164 if (exportTo.isEmpty())
00165 return;
00166
00167 QDir schemeRoot(exportTo);
00168
00169 if (!schemeRoot.exists(exportTo))
00170 {
00171 KMessageBox::error(this, i18n("Could not export shortcuts scheme because the location is invalid."));
00172 return;
00173 }
00174
00175 foreach (KActionCollection *collection, m_dialog->actionCollections())
00176 {
00177 const KXMLGUIClient *client = collection->parentGUIClient();
00178 if (!client) continue;
00179 QString fileDir = QLatin1String("shortcuts/share/apps/")
00180 + client->componentData().componentName() + '/';
00181 schemeRoot.mkpath(fileDir);
00182 KShortcutSchemesHelper::exportActionCollection(collection,
00183 currentScheme(), exportTo + '/' + fileDir);
00184 }
00185 }
00186
00187 void KShortcutSchemesEditor::saveAsDefaultsForScheme()
00188 {
00189 foreach (KActionCollection *collection, m_dialog->actionCollections())
00190 KShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
00191 }
00192
00193
00194 void KShortcutSchemesEditor::updateDeleteButton()
00195 {
00196 m_deleteScheme->setEnabled(m_schemesList->count()>=1);
00197 }