kdeui: simplify KActionCollection settings reading and writing

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-25 23:10:57 +03:00
parent 11e40bd6d2
commit d74d369e5e
7 changed files with 60 additions and 191 deletions

View file

@ -49,7 +49,6 @@ public:
KActionCollectionPrivate()
: m_parentGUIClient(nullptr),
configGroup("Shortcuts"),
configIsGlobal(false),
connectTriggered(false),
connectHovered(false),
q(nullptr)
@ -57,7 +56,7 @@ public:
}
void setComponentForAction(KAction *kaction)
{ kaction->d->maybeSetComponentData(m_componentData); }
{ kaction->d->maybeSetComponentData(componentData); }
static QList<KActionCollection*> s_allCollections;
@ -66,7 +65,7 @@ public:
bool writeKXMLGUIConfigFile();
KComponentData m_componentData;
KComponentData componentData;
//! Remove a action from our internal bookkeeping. Returns NULL if the
//! action doesn't belong to us.
@ -78,7 +77,6 @@ public:
const KXMLGUIClient *m_parentGUIClient;
QString configGroup;
bool configIsGlobal;
bool connectTriggered;
bool connectHovered;
@ -108,7 +106,7 @@ KActionCollection::KActionCollection(const KXMLGUIClient *parent)
KActionCollectionPrivate::s_allCollections.append(this);
d->m_parentGUIClient = parent;
d->m_componentData = parent->componentData();
d->componentData = parent->componentData();
}
KActionCollection::~KActionCollection()
@ -162,15 +160,15 @@ void KActionCollection::setComponentData(const KComponentData &cData)
}
if (cData.isValid()) {
d->m_componentData = cData;
d->componentData = cData;
} else {
d->m_componentData = KGlobal::mainComponent();
d->componentData = KGlobal::mainComponent();
}
}
KComponentData KActionCollection::componentData() const
{
return d->m_componentData;
return d->componentData;
}
const KXMLGUIClient *KActionCollection::parentGUIClient() const
@ -365,45 +363,14 @@ void KActionCollection::setConfigGroup(const QString &group)
d->configGroup = group;
}
bool KActionCollection::configIsGlobal() const
{
return d->configIsGlobal;
}
void KActionCollection::setConfigGlobal(bool global)
{
d->configIsGlobal = global;
}
void KActionCollection::importGlobalShortcuts(KConfigGroup *config)
{
Q_ASSERT(config);
if (!config || !config->exists()) {
return;
}
for (QMap<QString, QAction *>::ConstIterator it = d->actionByName.constBegin();
it != d->actionByName.constEnd(); ++it) {
KAction *kaction = qobject_cast<KAction*>(it.value());
if (!kaction) {
continue;
}
const QString actionName = it.key();
if (kaction->isShortcutConfigurable()) {
const QString entry = config->readEntry(actionName, QString());
if (!entry.isEmpty()) {
kaction->setGlobalShortcut(QKeySequence(entry), KAction::ActiveShortcut);
} else {
kaction->setGlobalShortcut(kaction->shortcut(KAction::DefaultShortcut), KAction::ActiveShortcut);
}
}
}
}
void KActionCollection::readSettings(KConfigGroup *config)
{
KConfigGroup cg(KGlobal::config(), configGroup());
KConfigGroup cg;
if (d->componentData.isValid()) {
cg = KConfigGroup(d->componentData.config(), configGroup());
} else {
cg = KConfigGroup(KGlobal::config(), configGroup());
}
if (!config) {
config = &cg;
}
@ -419,70 +386,28 @@ void KActionCollection::readSettings(KConfigGroup *config)
continue;
}
if (kaction->isShortcutConfigurable() ) {
const QString actionName = it.key();
const QString entry = config->readEntry(actionName, QString());
QString entry = config->readEntry(actionName, QString());
if (!entry.isEmpty()) {
kaction->setShortcut(QKeySequence(entry), KAction::ActiveShortcut);
} else {
kaction->setShortcut(kaction->shortcut(KAction::DefaultShortcut));
}
const QString globalActionName = actionName + QLatin1String("_global");
entry = config->readEntry(globalActionName, QString());
if (!entry.isEmpty()) {
kaction->setGlobalShortcut(QKeySequence(entry), KAction::ActiveShortcut);
} else {
kaction->setGlobalShortcut(kaction->shortcut(KAction::DefaultShortcut), KAction::ActiveShortcut);
}
}
}
// kDebug(125) << "done";
}
void KActionCollection::exportGlobalShortcuts(KConfigGroup *config, bool writeAll) const
{
Q_ASSERT(config);
if (!config) {
return;
}
for (QMap<QString, QAction *>::ConstIterator it = d->actionByName.constBegin();
it != d->actionByName.constEnd(); ++it) {
KAction *kaction = qobject_cast<KAction*>(it.value());
if (!kaction) {
continue;
}
const QString actionName = it.key();
// If the action name starts with unnamed- spit out a warning. That name
// will change at will and will break loading writing
if (actionName.startsWith(QLatin1String("unnamed-"))) {
kError() << "Skipped exporting Shortcut for action without name " << kaction->text() << "!";
continue;
}
if (kaction->isShortcutConfigurable() && kaction->isGlobalShortcutEnabled() ) {
bool bConfigHasAction = !config->readEntry(actionName, QString()).isEmpty();
bool bSameAsDefault = (kaction->globalShortcut() == kaction->globalShortcut(KAction::DefaultShortcut));
// If we're using a global config or this setting
// differs from the default, then we want to write.
KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent;
if (configIsGlobal()) {
flags |= KConfigGroup::Global;
}
if (writeAll || !bSameAsDefault) {
QString s = kaction->globalShortcut().toString();
if (s.isEmpty()) {
s = "none";
}
kDebug(125) << "\twriting " << actionName << " = " << s;
config->writeEntry(actionName, s, flags);
} else if (bConfigHasAction) {
// Otherwise, this key is the same as default but exists in config file. Remove it.
kDebug(125) << "\tremoving " << actionName << " because == default";
config->deleteEntry( actionName, flags );
}
}
}
config->sync();
}
bool KActionCollectionPrivate::writeKXMLGUIConfigFile()
{
const KXMLGUIClient *kxmlguiClient = q->parentGUIClient();
@ -559,7 +484,12 @@ void KActionCollection::writeSettings(KConfigGroup *config, bool writeAll, QActi
return;
}
KConfigGroup cg(KGlobal::config(), configGroup());
KConfigGroup cg;
if (d->componentData.isValid()) {
cg = KConfigGroup(d->componentData.config(), configGroup());
} else {
cg = KConfigGroup(KGlobal::config(), configGroup());
}
if (!config) {
config = &cg;
}
@ -593,29 +523,33 @@ void KActionCollection::writeSettings(KConfigGroup *config, bool writeAll, QActi
if (kaction->isShortcutConfigurable()) {
bool bConfigHasAction = !config->readEntry(actionName, QString()).isEmpty();
bool bSameAsDefault = (kaction->shortcut() == kaction->shortcut(KAction::DefaultShortcut));
// If we're using a global config or this setting
// differs from the default, then we want to write.
KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent;
// Honor the configIsGlobal() setting
if (configIsGlobal()) {
flags |= KConfigGroup::Global;
}
if (writeAll || !bSameAsDefault) {
// We are instructed to write all shortcuts or the shortcut is
// not set to its default value. Write it
QString s = kaction->shortcut().toString();
if( s.isEmpty() )
s = "none";
kDebug(125) << "\twriting " << actionName << " = " << s;
config->writeEntry(actionName, s, flags);
const QString s = kaction->shortcut().toString();
kDebug(125) << "writing " << actionName << " = " << s;
config->writeEntry(actionName, s);
} else if (bConfigHasAction) {
// Otherwise, this key is the same as default but exists in
// config file. Remove it.
kDebug(125) << "\tremoving " << actionName << " because == default";
config->deleteEntry(actionName, flags);
kDebug(125) << "removing " << actionName << " because == default";
config->deleteEntry(actionName);
}
}
if (kaction->isShortcutConfigurable() && kaction->isGlobalShortcutEnabled() ) {
const QString globalActionName = actionName + QLatin1String("_global");
bool bConfigHasAction = !config->readEntry(globalActionName, QString()).isEmpty();
bool bSameAsDefault = (kaction->globalShortcut() == kaction->globalShortcut(KAction::DefaultShortcut));
if (writeAll || !bSameAsDefault) {
const QString s = kaction->globalShortcut().toString();
kDebug(125) << "writing " << globalActionName << " = " << s;
config->writeEntry(globalActionName, s);
} else if (bConfigHasAction) {
// Otherwise, this key is the same as default but exists in config file. Remove it.
kDebug(125) << "removing " << globalActionName << " because == default";
config->deleteEntry( globalActionName );
}
}
}
@ -639,7 +573,6 @@ void KActionCollection::slotActionHovered()
}
}
void KActionCollectionPrivate::_k_actionDestroyed(QObject *obj)
{
// obj isn't really a QAction anymore. So make sure we don't do fancy stuff

View file

@ -57,9 +57,7 @@ class KDEUI_EXPORT KActionCollection : public QObject
friend class KXMLGUIClient;
Q_OBJECT
Q_PROPERTY(QString configGroup READ configGroup WRITE setConfigGroup)
Q_PROPERTY(bool configIsGlobal READ configIsGlobal WRITE setConfigGlobal)
public:
/**
@ -123,23 +121,11 @@ public:
*/
QString configGroup() const;
/**
* Returns whether this action collection's configuration should be global to KDE ( \e true ),
* or specific to the application ( \e false ).
*/
bool configIsGlobal() const;
/**
* Sets \a group as the KConfig group with which settings will be loaded and saved.
*/
void setConfigGroup(const QString &group);
/**
* Set whether this action collection's configuration should be global to KDE ( \e true ),
* or specific to the application ( \e false ).
*/
void setConfigGlobal(bool global);
/**
* Read all key associations from @p config.
*
@ -149,25 +135,6 @@ public:
*/
void readSettings(KConfigGroup *config = nullptr);
/**
* Import from @p config all configurable global key associations.
*
* \since 4.1
*
* \param config Config object to read from
*/
void importGlobalShortcuts(KConfigGroup *config);
/**
* Export the current configurable global key associations to @p config.
*
* \since 4.1
*
* \param config Config object to save to
* \param writeDefaults set to true to write settings which are already at defaults.
*/
void exportGlobalShortcuts(KConfigGroup *config, bool writeDefaults = false) const;
/**
* Write the current configurable key associations to @a config. What the
* function does if @a config is zero depends. If this action collection

View file

@ -297,24 +297,11 @@ void KShortcutsEditor::addCollection(KActionCollection *collection, const QStrin
treeheader->setSectionHidden(2, !addglobal || globalcounter < 1);
}
void KShortcutsEditor::importConfiguration(KConfigBase *config)
void KShortcutsEditor::importConfiguration(KConfigGroup *config)
{
if (!config) {
config = KGlobal::config().data();
}
const QList<KActionCollection*> actioncollections = d->actioncollections.keys();
if (d->actiontypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, actioncollections) {
collection->readSettings(&group);
}
}
if (d->actiontypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts");
foreach (KActionCollection* collection, actioncollections) {
collection->importGlobalShortcuts(&group);
}
foreach (KActionCollection* collection, actioncollections) {
collection->readSettings(config);
}
// start all over, it is unknown what changed in the configuration
@ -325,12 +312,8 @@ void KShortcutsEditor::importConfiguration(KConfigBase *config)
}
}
void KShortcutsEditor::exportConfiguration(KConfigBase *config) const
void KShortcutsEditor::exportConfiguration(KConfigGroup *config) const
{
if (!config) {
config = KGlobal::config().data();
}
foreach (const KKeySequenceWidget *kswidget, d->keysequencewidgets) {
QAction* action = qvariant_cast<QAction*>(kswidget->property("_k_action"));
Q_ASSERT(action != nullptr);
@ -345,19 +328,9 @@ void KShortcutsEditor::exportConfiguration(KConfigBase *config) const
}
const QList<KActionCollection*> actioncollections = d->actioncollections.keys();
if (d->actiontypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, actioncollections) {
collection->writeSettings(&group, true);
}
foreach (KActionCollection* collection, actioncollections) {
collection->writeSettings(config);
}
if (d->actiontypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts");
foreach (KActionCollection* collection, actioncollections) {
collection->exportGlobalShortcuts(&group, true);
}
}
config->sync();
d->modified = false;
}

View file

@ -25,7 +25,6 @@
#include <QWidget>
class KActionCollection;
class KConfigBase;
class KConfigGroup;
class KShortcutsEditorPrivate;
@ -117,7 +116,7 @@ public:
*
* @param config Config object
*/
void exportConfiguration(KConfigBase *config = nullptr) const;
void exportConfiguration(KConfigGroup *config = nullptr) const;
/**
* Import the settings from configuration @p config.
@ -127,7 +126,7 @@ public:
*
* @param config Config object
*/
void importConfiguration(KConfigBase *config = nullptr);
void importConfiguration(KConfigGroup *config = nullptr);
Q_SIGNALS:
/**

View file

@ -261,10 +261,7 @@ void KApplicationPrivate::_k_checkAppStartedSlot()
// because it is not done anywhere else. unfortunately that magic also means any collections
// created afterwards will need an explicit settings read
foreach (KActionCollection* collection, KActionCollection::allCollections()) {
KConfigGroup group(KGlobal::config(), "Shortcuts");
collection->readSettings(&group);
group = KConfigGroup(KGlobal::config(), "Global Shortcuts");
collection->importGlobalShortcuts(&group);
collection->readSettings();
}
}

View file

@ -141,10 +141,10 @@ KGlobalAccelFilter::KGlobalAccelFilter()
bool KGlobalAccelFilter::x11Event(XEvent *xevent)
{
if (m_block) {
return false;
}
if (xevent->type == KeyPress) {
if (m_block) {
return false;
}
foreach (const KGlobalAccelStruct &shortcut, shortcuts) {
if (xevent->xkey.state == shortcut.keyModX && xevent->xkey.keycode == shortcut.keyCodeX) {
kDebug(s_kglobalaccelarea) << "triggering action" << shortcut.keyModX << shortcut.keyCodeX << shortcut.action;

View file

@ -275,7 +275,7 @@ public Q_SLOTS:
/**
* Actually remove the shortcut that the user wanted to steal, from the
* action that was using it. This only applies to actions provided to us
* by setCheckActionCollections() and setCheckActionList().
* by setCheckActionCollections().
*
* Global and Standard Shortcuts have to be stolen immediately when the
* user gives his consent (technical reasons). That means those changes