Commit f0d60f5e authored by Michael Abrahams's avatar Michael Abrahams

Port shortcut scheme file search to KoResourcePaths

Also prettify the shortcut scheme options
parent bdefad45
......@@ -121,6 +121,8 @@ KisActionRegistry::KisActionRegistry()
{
d->loadActionFiles();
d->loadCustomShortcuts();
KoResourcePaths::addResourceType("kis_shortcuts", "data", "krita/shortcuts/");
}
// Not the most efficient logic, but simple and readable.
......
......@@ -60,44 +60,38 @@ KisShortcutsDialog::KisShortcutsDialog(KisShortcutsEditor::ActionTypes types,
setWindowTitle(i18n("Configure Shortcuts"));
setModal(true);
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
layout->addWidget(d->m_shortcutsEditor);
QVBoxLayout *mainLayout = new QVBoxLayout;
setLayout(mainLayout);
mainLayout->addWidget(d->m_shortcutsEditor);
QHBoxLayout *bottomLayout = new QHBoxLayout;
d->m_schemeEditor = new KShortcutSchemesEditor(this);
connect(d->m_schemeEditor, SIGNAL(shortcutsSchemeChanged(QString)),
this, SLOT(changeShortcutScheme(QString)));
d->m_schemeEditor->hide();
layout->addWidget(d->m_schemeEditor);
d->m_detailsButton = new QPushButton;
d->m_detailsButton->setText(i18n("&Details") + QStringLiteral(" >>"));
bottomLayout->addLayout(d->m_schemeEditor);
QPushButton *printButton = new QPushButton;
KGuiItem::assign(printButton, KStandardGuiItem::print());
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
buttonBox->addButton(printButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(d->m_detailsButton, QDialogButtonBox::ActionRole);
buttonBox->setStandardButtons(QDialogButtonBox::Ok |
QDialogButtonBox::Cancel |
QDialogButtonBox::RestoreDefaults);
QDialogButtonBox::Cancel);
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok),
KStandardGuiItem::ok());
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel),
KStandardGuiItem::cancel());
KGuiItem::assign(buttonBox->button(QDialogButtonBox::RestoreDefaults),
KStandardGuiItem::defaults());
layout->addWidget(buttonBox);
bottomLayout->addWidget(buttonBox);
mainLayout->addLayout(bottomLayout);
connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()),
d->m_shortcutsEditor, SLOT(allDefault()));
connect(printButton, SIGNAL(clicked()), d->m_shortcutsEditor, SLOT(printShortcuts()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(undo()));
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
connect(d->m_detailsButton, SIGNAL(clicked()), this, SLOT(toggleDetails()));
KConfigGroup group(KSharedConfig::openConfig(), "KisShortcutsDialog Settings");
resize(group.readEntry("Dialog Size", sizeHint()));
}
......@@ -154,6 +148,11 @@ int KisShortcutsDialog::configure(KActionCollection *collection,
return dlg.configure(saveSettings);
}
void KisShortcutsDialog::allDefault()
{
d->m_shortcutsEditor->allDefault();
}
void KisShortcutsDialog::importConfiguration(const QString &path)
{
KConfig config(path);
......
......@@ -179,6 +179,8 @@ public Q_SLOTS:
///@reimp
void accept() Q_DECL_OVERRIDE;
void allDefault();
Q_SIGNALS:
///emitted after OK is clicked and settings are saved
void saved();
......@@ -186,7 +188,6 @@ Q_SIGNALS:
private:
Q_PRIVATE_SLOT(d, void changeShortcutScheme(const QString &))
Q_PRIVATE_SLOT(d, void undo())
Q_PRIVATE_SLOT(d, void toggleDetails())
class KisShortcutsDialogPrivate;
class KisShortcutsDialogPrivate *const d;
......
......@@ -99,12 +99,4 @@ void KisShortcutsDialog::KisShortcutsDialogPrivate::save()
emit q->saved();
};
void KisShortcutsDialog::KisShortcutsDialogPrivate::toggleDetails()
{
const bool isVisible = m_schemeEditor->isVisible();
m_schemeEditor->setVisible(!isVisible);
m_detailsButton->setText(i18n("&Details") + (isVisible ? QStringLiteral(" >>") : QLatin1String(" <<")));
}
#include "moc_KisShortcutsDialog_p.cpp"
......@@ -35,7 +35,7 @@
#include <QModelIndex>
#include <QtCore/QList>
#include <QtCore/QCollator>
#include <QGroupBox>
#include <QHBoxLayout>
class QLabel;
class QTreeWidget;
......@@ -97,9 +97,7 @@ public:
KisShortcutsDialog *q;
KisShortcutsEditor *m_shortcutsEditor;
bool m_saveSettings{false};
QPushButton *m_detailsButton;
void toggleDetails();
KShortcutSchemesEditor *m_schemeEditor{0};
};
......@@ -238,7 +236,7 @@ private:
QObject *m_action;
};
class KShortcutSchemesEditor: public QGroupBox
class KShortcutSchemesEditor: public QHBoxLayout
{
Q_OBJECT
public:
......@@ -267,6 +265,7 @@ private:
QComboBox *m_schemesList;
KisShortcutsDialog *m_dialog;
QHash<QString, QString> m_schemeFileLocations;
};
......
......@@ -30,6 +30,7 @@
#include <QInputDialog>
#include <QComboBox>
#include <QHBoxLayout>
#include <QDebug>
#include <kconfiggroup.h>
#include <kmessagebox.h>
......@@ -40,55 +41,64 @@
#include "kactioncollection.h"
#include "kxmlguiclient.h"
#include "KoResourcePaths.h"
KShortcutSchemesEditor::KShortcutSchemesEditor(KisShortcutsDialog *parent)
: QGroupBox(i18n("Shortcut Schemes"), parent), m_dialog(parent)
: QHBoxLayout(parent)
, m_dialog(parent)
{
KConfigGroup group(KSharedConfig::openConfig(), "Shortcut Schemes");
QStringList schemes;
schemes << QStringLiteral("Default");
// List files in the shortcuts subdir, each one is a theme. See KShortcutSchemesHelper::{shortcutSchemeFileName,exportActionCollection}
const QStringList shortcutsDir = QStandardPaths::locateAll(QStandardPaths::DataLocation, QStringLiteral("shortcuts"), QStandardPaths::LocateDirectory);
Q_FOREACH (const QString &dir, shortcutsDir) {
Q_FOREACH (const QString &file, QDir(dir).entryList(QDir::NoDotAndDotDot)) {
schemes << file;
// List files in the shortcuts subdirs.
// See KShortcutSchemesHelper functions shortcutSchemeFileName, exportActionCollection
const QStringList shortcutFiles = KoResourcePaths::findAllResources("kis_shortcuts", "*.shortcuts");
Q_FOREACH (const QString &file, shortcutFiles) {
QFileInfo fileInfo(file);
QString schemeName = fileInfo.completeBaseName();
if (!schemes.contains(schemeName)) {
schemes << schemeName;
m_schemeFileLocations.insert(schemeName, fileInfo.canonicalFilePath());
}
}
const QString currentScheme = group.readEntry("Current Scheme", "Default");
setMargin(0);
QHBoxLayout *l = new QHBoxLayout(this);
l->setMargin(0);
QLabel *schemesLabel = new QLabel(i18n("Current scheme:"), this);
l->addWidget(schemesLabel);
QLabel *schemesLabel = new QLabel(i18n("Shortcut Schemes:"), m_dialog);
addWidget(schemesLabel);
m_schemesList = new QComboBox(this);
m_schemesList = new QComboBox(m_dialog);
m_schemesList->setEditable(false);
m_schemesList->addItems(schemes);
m_schemesList->setCurrentIndex(m_schemesList->findText(currentScheme));
schemesLabel->setBuddy(m_schemesList);
l->addWidget(m_schemesList);
addWidget(m_schemesList);
m_newScheme = new QPushButton(i18n("New..."));
l->addWidget(m_newScheme);
addWidget(m_newScheme);
m_deleteScheme = new QPushButton(i18n("Delete"));
l->addWidget(m_deleteScheme);
addWidget(m_deleteScheme);
QPushButton *moreActions = new QPushButton(i18n("More Actions"));
l->addWidget(moreActions);
addWidget(moreActions);
QMenu *moreActionsMenu = new QMenu(this);
QMenu *moreActionsMenu = new QMenu(m_dialog);
moreActionsMenu->addAction(i18n("Save as Scheme Defaults"),
this, SLOT(saveAsDefaultsForScheme()));
moreActionsMenu->addAction(i18n("Export Scheme..."),
this, SLOT(exportShortcutsScheme()));
moreActionsMenu->addAction(i18n("Import Scheme..."),
this, SLOT(importShortcutsScheme()));
moreActionsMenu->addAction(i18n("Restore Defaults"),
m_dialog, SLOT(allDefault()));
moreActions->setMenu(moreActionsMenu);
l->addStretch(1);
addStretch(1);
connect(m_schemesList, SIGNAL(activated(QString)),
this, SIGNAL(shortcutsSchemeChanged(QString)));
......@@ -100,18 +110,18 @@ KShortcutSchemesEditor::KShortcutSchemesEditor(KisShortcutsDialog *parent)
void KShortcutSchemesEditor::newScheme()
{
bool ok;
const QString newName = QInputDialog::getText(this, i18n("Name for New Scheme"),
const QString newName = QInputDialog::getText(m_dialog, i18n("Name for New Scheme"),
i18n("Name for new scheme:"), QLineEdit::Normal, i18n("New Scheme"), &ok);
if (!ok) {
return;
}
if (m_schemesList->findText(newName) != -1) {
KMessageBox::sorry(this, i18n("A scheme with this name already exists."));
KMessageBox::sorry(m_dialog, i18n("A scheme with this name already exists."));
return;
}
const QString newSchemeFileName = KShortcutSchemesHelper::applicationShortcutSchemeFileName(newName);
const QString newSchemeFileName = KShortcutSchemesHelper::shortcutSchemeFileName(newName);
QFile schemeFile(newSchemeFileName);
if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) {
......@@ -135,23 +145,14 @@ void KShortcutSchemesEditor::newScheme()
void KShortcutSchemesEditor::deleteScheme()
{
if (KMessageBox::questionYesNo(this,
if (KMessageBox::questionYesNo(m_dialog,
i18n("Do you really want to delete the scheme %1?\n\
Note that this will not remove any system wide shortcut schemes.", currentScheme())) == KMessageBox::No) {
return;
}
//delete the scheme for the app itself
QFile::remove(KShortcutSchemesHelper::applicationShortcutSchemeFileName(currentScheme()));
//delete all scheme files we can find for xmlguiclients in the user directories
foreach (KActionCollection *collection, m_dialog->actionCollections()) {
const KXMLGUIClient *client = collection->parentGUIClient();
if (!client) {
continue;
}
QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(client, currentScheme()));
}
QFile::remove(KShortcutSchemesHelper::shortcutSchemeFileName(currentScheme()));
m_schemesList->removeItem(m_schemesList->findText(currentScheme()));
updateDeleteButton();
......@@ -166,7 +167,7 @@ QString KShortcutSchemesEditor::currentScheme()
void KShortcutSchemesEditor::exportShortcutsScheme()
{
//ask user about dir
QString path = QFileDialog::getSaveFileName(this, i18n("Export Shortcuts"), QDir::currentPath(), i18n("Shortcuts (*.shortcuts)"));
QString path = QFileDialog::getSaveFileName(m_dialog, i18n("Export Shortcuts"), QDir::currentPath(), i18n("Shortcuts (*.shortcuts)"));
if (path.isEmpty()) {
return;
}
......@@ -177,7 +178,7 @@ void KShortcutSchemesEditor::exportShortcutsScheme()
void KShortcutSchemesEditor::importShortcutsScheme()
{
//ask user about dir
QString path = QFileDialog::getOpenFileName(this, i18n("Import Shortcuts"), QDir::currentPath(), i18n("Shortcuts (*.shortcuts)"));
QString path = QFileDialog::getOpenFileName(m_dialog, i18n("Import Shortcuts"), QDir::currentPath(), i18n("Shortcuts (*.shortcuts)"));
if (path.isEmpty()) {
return;
}
......@@ -187,9 +188,12 @@ void KShortcutSchemesEditor::importShortcutsScheme()
void KShortcutSchemesEditor::saveAsDefaultsForScheme()
{
// XXX: Not implemented
#if 0
foreach (KActionCollection *collection, m_dialog->actionCollections()) {
KShortcutSchemesHelper::exportActionCollection(collection, currentScheme());
}
#endif
}
void KShortcutSchemesEditor::updateDeleteButton()
......
......@@ -32,22 +32,13 @@
#include "kactioncollection.h"
#include "kxmlguiclient.h"
#include "KoResourcePaths.h"
bool KShortcutSchemesHelper::exportActionCollection(KActionCollection *collection,
const QString &schemeName, const QString &dir)
{
const KXMLGUIClient *client = collection->parentGUIClient();
if (!client) {
return false;
}
QString schemeFileName;
if (!dir.isEmpty()) {
const QString dirPath = dir + QStringLiteral("shortcuts/");
QDir().mkpath(dirPath);
schemeFileName = dirPath + client->componentName() + schemeName;
} else {
schemeFileName = shortcutSchemeFileName(client, schemeName);
}
QString schemeFileName = shortcutSchemeFileName(schemeName);
QFile schemeFile(schemeFileName);
if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) {
return false;
......@@ -55,7 +46,7 @@ bool KShortcutSchemesHelper::exportActionCollection(KActionCollection *collectio
QDomDocument doc;
QDomElement docElem = doc.createElement(QStringLiteral("kpartgui"));
docElem.setAttribute(QStringLiteral("version"), QLatin1String("1"));
docElem.setAttribute(QStringLiteral("name"), client->componentName());
docElem.setAttribute(QStringLiteral("name"), QCoreApplication::applicationName());
doc.appendChild(docElem);
QDomElement elem = doc.createElement(QStringLiteral("ActionProperties"));
docElem.appendChild(elem);
......@@ -86,15 +77,8 @@ QString KShortcutSchemesHelper::currentShortcutSchemeName()
return KSharedConfig::openConfig()->group("Shortcut Schemes").readEntry("Current Scheme", "Default");
}
QString KShortcutSchemesHelper::shortcutSchemeFileName(const KXMLGUIClient *client, const QString &schemeName)
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') +
client->componentName() + QStringLiteral("/shortcuts/") +
client->componentName() + schemeName;
}
QString KShortcutSchemesHelper::applicationShortcutSchemeFileName(const QString &schemeName)
QString KShortcutSchemesHelper::shortcutSchemeFileName(const QString &schemeName)
{
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') +
QCoreApplication::applicationName() + schemeName;
// Create a directory if one cannot be found.
return KoResourcePaths::locateLocal("kis_shortcuts", schemeName, true);
}
......@@ -49,12 +49,7 @@ public:
/**
* @return the name of the (writable) file to save the shortcut scheme to.
*/
static QString shortcutSchemeFileName(const KXMLGUIClient *client, const QString &schemeName);
/**
* @return the name of the scheme file for application itself.
*/
static QString applicationShortcutSchemeFileName(const QString &schemeName);
static QString shortcutSchemeFileName(const QString &schemeName);
static bool saveShortcutScheme();
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment