Commit 10981d68 authored by Michael Abrahams's avatar Michael Abrahams

Restore changeShortcutScheme()

Move shortcut schemes work from XMLGUI to KisActionRegistry.
parent ee309b9c
......@@ -156,6 +156,9 @@ KisPart::KisPart()
connect(this, SIGNAL(documentClosed(QString)),
this, SLOT(updateIdleWatcherConnections()));
connect(KisActionRegistry::instance(), SIGNAL(shortcutsUpdated()),
this, SLOT(updateShortcuts()));
#ifdef HAVE_OPENGL
connect(&d->idleWatcher, SIGNAL(startedIdleMode()),
&d->animationCachePopulator, SLOT(slotRequestRegeneration()));
......@@ -429,8 +432,11 @@ void KisPart::configureShortcuts()
auto actionRegistry = KisActionRegistry::instance();
actionRegistry->configureShortcuts(d->actionCollection);
}
// Update the non-UI actions. That includes:
void KisPart::updateShortcuts()
{
// Update any non-UI actionCollections. That includes:
// - Shortcuts called inside of tools
// - Perhaps other things?
KoToolManager::instance()->updateToolShortcuts();
......
......@@ -181,6 +181,8 @@ private Q_SLOTS:
void updateIdleWatcherConnections();
void updateShortcuts();
Q_SIGNALS:
/**
* emitted when a new document is opened.
......
......@@ -98,8 +98,11 @@ public:
void loadActionFiles();
void loadActionCollections();
void loadCustomShortcuts(QString filename = QStringLiteral("kritashortcutsrc"));
ActionInfoItem actionInfo(const QString &name) {
return actionInfoList.value(name, emptyActionInfo);
ActionInfoItem &actionInfo(const QString &name) {
if (!actionInfoList.contains(name)) {
dbgAction << "Tried to look up info for unknown action" << name;
}
return actionInfoList[name];
};
KisActionRegistry *q;
......@@ -152,7 +155,7 @@ void KisActionRegistry::addAction(const QString &name, QAction *a)
KActionCollection *collection = d->actionCollections.value(info.collectionName);
if (!collection) {
qDebug() << "No collection found for action" << name;
dbgAction << "No collection found for action" << name;
return;
}
if (collection->action(name)) {
......@@ -200,19 +203,48 @@ void KisActionRegistry::configureShortcuts(KActionCollection *ac)
dlg.configure(); // Show the dialog.
d->loadCustomShortcuts();
emit shortcutsUpdated();
}
void KisActionRegistry::applyShortcutScheme(const KConfigBase *config)
{
/**
* Stuff from XMLGUI starts here
*/
// First, update the things in KisActionRegistry
if (config == 0) {
// Simplest just to reload everything
d->loadActionFiles();
} else {
const auto schemeEntries = config->group(QStringLiteral("Shortcuts")).entryMap();
// Load info item for each shortcut, reset custom shortcuts
auto it = schemeEntries.constBegin();
while (it != schemeEntries.end()) {
ActionInfoItem &info = d->actionInfo(it.key());
info.defaultShortcut = it.value();
info.customShortcut = QKeySequence();
it++;
}
}
}
void KisActionRegistry::updateShortcut(const QString &name, QAction *action)
{
action->setShortcut(preferredShortcut(d->actionInfo(name)));
const ActionInfoItem info = d->actionInfo(name);
action->setShortcut(preferredShortcut(info));
auto propertizedShortcut = qVariantFromValue(QList<QKeySequence>() << info.defaultShortcut);
action->setProperty("defaultShortcuts", propertizedShortcut);
}
bool KisActionRegistry::propertizeAction(const QString &name, QAction * a)
{
ActionInfoItem info = d->actionInfo(name);
const ActionInfoItem info = d->actionInfo(name);
QDomElement actionXml = info.xmlData;
if (actionXml.text().isEmpty()) {
dbgAction << "No XML data found for action" << name;
......@@ -245,10 +277,7 @@ bool KisActionRegistry::propertizeAction(const QString &name, QAction * a)
a->setIconText(iconText);
a->setCheckable(isCheckable);
a->setShortcut(preferredShortcut(info));
auto propertizedShortcut = qVariantFromValue(QList<QKeySequence>() << info.defaultShortcut);
a->setProperty("defaultShortcuts", propertizedShortcut);
updateShortcut(name, a);
......
......@@ -27,6 +27,7 @@
class KActionCollection;
class QDomElement;
class KConfigBase;
/**
* KisShortcutRegistry is intended to manage the global shortcut configuration
......@@ -126,6 +127,11 @@ public:
KActionCollection * getDefaultCollection();
// If config == 0, reload defaults
void applyShortcutScheme(const KConfigBase *config = 0);
Q_SIGNALS:
void shortcutsUpdated();
private:
class Private;
......
......@@ -106,12 +106,12 @@ KisShortcutsDialog::~KisShortcutsDialog()
void KisShortcutsDialog::addCollection(KActionCollection *collection, const QString &title)
{
d->m_shortcutsEditor->addCollection(collection, title);
d->m_collections << collection;
d->m_collections.insert(title, collection);
}
QList<KActionCollection *> KisShortcutsDialog::actionCollections() const
{
return d->m_collections;
return d->m_collections.values();
}
bool KisShortcutsDialog::configure(bool saveSettings)
......
......@@ -18,6 +18,7 @@
#include "KisShortcutsDialog_p.h"
#include "kshortcutschemeshelper_p.h"
#include "kxmlguiclient.h"
#include <QDomDocument>
#include "kactioncollection.h"
......@@ -25,6 +26,9 @@
#include <QAction>
#include <QApplication>
#include <QDebug>
#include "kis_action_registry.h"
#include <KSharedConfig>
#include <KConfigGroup>
......@@ -43,11 +47,8 @@ KisShortcutsDialog::KisShortcutsDialogPrivate::KisShortcutsDialogPrivate(KisShor
: q(q)
{ }
void KisShortcutsDialog::KisShortcutsDialogPrivate::changeShortcutScheme(const QString &scheme)
void KisShortcutsDialog::KisShortcutsDialogPrivate::changeShortcutScheme(const QString &schemeName)
{
qDebug() << "Not implemented";
#if 0
QString dialogText = i18n("The current shortcut scheme is modified. Save before switching to the new one?");
if (m_shortcutsEditor->isModified() &&
......@@ -60,32 +61,33 @@ void KisShortcutsDialog::KisShortcutsDialogPrivate::changeShortcutScheme(const Q
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
m_shortcutsEditor->clearCollections();
foreach (KActionCollection *collection, m_collections) {
// passing an empty stream forces the clients to reread the XML
KXMLGUIClient *client = const_cast<KXMLGUIClient *>(collection->parentGUIClient());
if (client) {
client->setXMLGUIBuildDocument(QDomDocument());
}
}
KConfigGroup cg = KSharedConfig::openConfig()->group("Shortcut Schemes");
cg.writeEntry("Current Scheme", schemeName);
//get xmlguifactory
if (!m_collections.isEmpty()) {
const KXMLGUIClient *client = m_collections.first()->parentGUIClient();
if (client) {
KXMLGUIFactory *factory = client->factory();
if (factory) {
factory->changeShortcutScheme(scheme);
}
// Load scheme file
if (schemeName != QStringLiteral("Default")) {
QString schemeFileName = KShortcutSchemesHelper::schemeFileLocations().value(schemeName);
if (schemeFileName.isEmpty()) {
// qDebug() << "No configuration file found for scheme" << schemeName;
return;
}
KConfig schemeConfig(schemeFileName, KConfig::SimpleConfig);
KisActionRegistry::instance()->applyShortcutScheme(&schemeConfig);
} else {
// Apply default scheme, updating KisActionRegistry data
KisActionRegistry::instance()->applyShortcutScheme();
}
foreach (KActionCollection *collection, m_collections) {
m_shortcutsEditor->addCollection(collection);
// Update actions themselves, and re-add to dialog box to refresh
auto it = m_collections.constBegin();
while (it != m_collections.constEnd()) {
it.value()->updateShortcuts();
m_shortcutsEditor->addCollection(it.value(), it.key());
it++;
}
QApplication::restoreOverrideCursor();
#endif
}
void KisShortcutsDialog::KisShortcutsDialogPrivate::undo()
......
......@@ -93,7 +93,7 @@ public:
void undo();
void save();
QList<KActionCollection *> m_collections;
QHash<QString, KActionCollection *> m_collections;
KisShortcutsDialog *q;
KisShortcutsEditor *m_shortcutsEditor;
bool m_saveSettings{false};
......
......@@ -25,6 +25,7 @@
#include "KisShortcutsEditor.h"
#include "KisShortcutsEditor_p.h"
#include "kshortcutschemeshelper_p.h"
#include "config-xmlgui.h"
#include "kis_action_registry.h"
......@@ -188,7 +189,34 @@ void KisShortcutsEditor::clearConfiguration()
void KisShortcutsEditor::importConfiguration(KConfigBase *config)
{
d->importConfiguration(config);
Q_ASSERT(config);
if (!config) {
return;
}
// Reload the configuration file
KisActionRegistry::instance()->applyShortcutScheme(config);
// Update the actions themselves
// TODO
// Update the dialog entry items
const KConfigGroup schemeShortcuts(config, QStringLiteral("Shortcuts"));
for (QTreeWidgetItemIterator it(d->ui.list); (*it); ++it) {
if (!(*it)->parent()) {
continue;
}
KisShortcutsEditorItem *item = static_cast<KisShortcutsEditorItem *>(*it);
const QString actionId = item->data(Id).toString();
if (!schemeShortcuts.hasKey(actionId))
continue;
QList<QKeySequence> sc = QKeySequence::listFromString(schemeShortcuts.readEntry(actionId, QString()));
d->changeKeyShortcut(item, LocalPrimary, primarySequence(sc));
d->changeKeyShortcut(item, LocalAlternate, alternateSequence(sc));
}
}
void KisShortcutsEditor::exportConfiguration(KConfigBase *config) const
......@@ -252,6 +280,7 @@ void KisShortcutsEditor::save()
void KisShortcutsEditor::undo()
{
// TODO: is this working?
for (QTreeWidgetItemIterator it(d->ui.list); (*it); ++it) {
if (KisShortcutsEditorItem *item = dynamic_cast<KisShortcutsEditorItem *>(*it)) {
item->undo();
......
......@@ -34,6 +34,7 @@
#include <QPrintDialog>
#include <ksharedconfig.h>
#include <KConfigGroup>
#include "kis_action_registry.h"
//---------------------------------------------------------------------
// KisShortcutsEditorPrivate
......@@ -212,31 +213,6 @@ void KisShortcutsEditorPrivate::clearConfiguration()
}
}
void KisShortcutsEditorPrivate::importConfiguration(KConfigBase *config)
{
Q_ASSERT(config);
if (!config) {
return;
}
const KConfigGroup localShortcutsGroup(config, QStringLiteral("Shortcuts"));
for (QTreeWidgetItemIterator it(ui.list); (*it); ++it) {
if (!(*it)->parent()) {
continue;
}
KisShortcutsEditorItem *item = static_cast<KisShortcutsEditorItem *>(*it);
const QString actionId = item->data(Id).toString();
if (!localShortcutsGroup.hasKey(actionId))
continue;
QList<QKeySequence> sc = QKeySequence::listFromString(localShortcutsGroup.readEntry(actionId, QString()));
changeKeyShortcut(item, LocalPrimary, primarySequence(sc));
changeKeyShortcut(item, LocalAlternate, alternateSequence(sc));
}
}
/*TODO for the printShortcuts function
Nice to have features (which I'm not sure I can do before may due to
more important things):
......
......@@ -69,9 +69,6 @@ public:
// clear all shortcuts
void clearConfiguration();
// Import shortcuts from file
void importConfiguration(KConfigBase *config);
//changeXXX were described as "conflict resolution functions"
void changeKeyShortcut(KisShortcutsEditorItem *item, uint column, const QKeySequence &capture);
......
......@@ -53,17 +53,8 @@ KShortcutSchemesEditor::KShortcutSchemesEditor(KisShortcutsDialog *parent)
QStringList schemes;
schemes << QStringLiteral("Default");
// 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());
}
}
auto schemeFileLocations = KShortcutSchemesHelper::schemeFileLocations();
schemes << schemeFileLocations.keys();
const QString currentScheme = group.readEntry("Current Scheme", "Default");
setMargin(0);
......@@ -127,18 +118,12 @@ void KShortcutSchemesEditor::newScheme()
if (!schemeFile.open(QFile::WriteOnly | QFile::Truncate)) {
return;
}
schemeFile.close();
QDomDocument doc;
QDomElement docElem = doc.createElement(QStringLiteral("kpartgui"));
doc.appendChild(docElem);
QDomElement elem = doc.createElement(QStringLiteral("ActionProperties"));
docElem.appendChild(elem);
QTextStream out(&schemeFile);
out << doc.toString(4);
m_dialog->exportConfiguration(newSchemeFileName);
m_schemesList->addItem(newName);
m_schemesList->setCurrentIndex(m_schemesList->findText(newName));
m_schemeFileLocations.insert(newName, newSchemeFileName);
updateDeleteButton();
emit shortcutsSchemeChanged(newName);
}
......
......@@ -33,6 +33,7 @@
#include "kxmlguiclient.h"
#include "KoResourcePaths.h"
#include "kis_action_registry.h"
bool KShortcutSchemesHelper::exportActionCollection(KActionCollection *collection,
const QString &schemeName, const QString &dir)
......@@ -82,3 +83,21 @@ QString KShortcutSchemesHelper::shortcutSchemeFileName(const QString &schemeName
// Create a directory if one cannot be found.
return KoResourcePaths::locateLocal("kis_shortcuts", schemeName, true);
}
QHash<QString, QString> KShortcutSchemesHelper::schemeFileLocations()
{
QStringList schemes;
schemes << QString("Default"); // Forbid "Default.shortcuts"
QHash<QString, QString> schemeFileLocations;
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;
schemeFileLocations.insert(schemeName, fileInfo.canonicalFilePath());
}
}
return schemeFileLocations;
}
......@@ -20,9 +20,11 @@
#define KSHORTCUTSCHEMESHELPER_P_H
#include <QString>
#include <QHash>
class KActionCollection;
class KXMLGUIClient;
class KConfigBase;
class KShortcutSchemesHelper
{
......@@ -52,6 +54,13 @@ public:
static QString shortcutSchemeFileName(const QString &schemeName);
static bool saveShortcutScheme();
/**
* @return a list of files to save the shortcut scheme to. Does not include "Default"
* @see shortcutSchemeFileName, exportActionCollection
*/
static QHash<QString, QString> schemeFileLocations();
};
#endif
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