File indexing completed on 2024-05-12 16:02:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisActionsSnapshot.h"
0008 
0009 #include "kis_action_registry.h"
0010 #include "./kactioncollection.h"
0011 
0012 #include "kis_debug.h"
0013 
0014 //#define ACTIONS_CHECKSUM_SANITY_CHECK
0015 
0016 
0017 struct KisActionsSnapshot::Private
0018 {
0019     QMap<QString, KisKActionCollection*> actionCollections;
0020 
0021     ~Private() {
0022         qDeleteAll(actionCollections);
0023         qDeleteAll(fakeActions);
0024     }
0025 
0026     QSet<QString> nonRegisteredShortcuts;
0027     QVector<QAction*> fakeActions;
0028 };
0029 
0030 KisActionsSnapshot::KisActionsSnapshot()
0031     : m_d(new Private)
0032 {
0033     QList<QString> registeredShortcutIds = KisActionRegistry::instance()->registeredShortcutIds();
0034 #if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
0035     m_d->nonRegisteredShortcuts = QSet<QString>(registeredShortcutIds.begin(), registeredShortcutIds.end());
0036 #else
0037     m_d->nonRegisteredShortcuts = QSet<QString>::fromList(registeredShortcutIds);
0038 #endif
0039 
0040 }
0041 
0042 KisActionsSnapshot::~KisActionsSnapshot()
0043 {
0044 }
0045 
0046 void KisActionsSnapshot::addAction(const QString &name, QAction *action)
0047 {
0048     m_d->nonRegisteredShortcuts.remove(name);
0049     KisActionRegistry::ActionCategory cat = KisActionRegistry::instance()->fetchActionCategory(name);
0050 
0051     if (!cat.isValid()) {
0052         warnKrita << "WARNING: Uncategorized action" << name << "Dropping...";
0053         return;
0054     }
0055 
0056 #ifdef ACTIONS_CHECKSUM_SANITY_CHECK
0057     if (!KisActionRegistry::instance()->sanityCheckPropertized(action->objectName())) {
0058         warnKrita << "WARNING: action" << name  << "was not propertized!"  << ppVar(action->property("isShortcutConfigurable").toBool());
0059     }
0060 #endif /* ACTIONS_CHECKSUM_SANITY_CHECK */
0061 
0062     KisKActionCollection *collection =  m_d->actionCollections[cat.componentName];
0063 
0064     if (!collection) {
0065         collection = new KisKActionCollection(0, cat.componentName);
0066         m_d->actionCollections.insert(cat.componentName, collection);
0067     }
0068 
0069     collection->addCategorizedAction(name, action, cat.categoryName);
0070 }
0071 
0072 QMap<QString, KisKActionCollection *> KisActionsSnapshot::actionCollections()
0073 {
0074     /**
0075      * A small heuristics to show warnings only when unknown shortcuts appear
0076      * in the non-registered list
0077      */
0078     if (m_d->nonRegisteredShortcuts.size() > 4 &&
0079         m_d->nonRegisteredShortcuts.size() < 160) {
0080 
0081         warnKrita << "WARNING: The following shortcuts are not registered in the collection, "
0082                      "they might have wrong shortcuts in the end:";
0083         Q_FOREACH (const QString &str, m_d->nonRegisteredShortcuts) {
0084             warnKrita << str;
0085         }
0086         warnKrita << "=== end ===";
0087     }
0088 
0089     // try to workaround non-registered shortcuts by faking them manually
0090     Q_FOREACH (const QString &str, m_d->nonRegisteredShortcuts) {
0091         QAction *action = KisActionRegistry::instance()->makeQAction(str, 0);
0092         m_d->fakeActions << action;
0093         addAction(action->objectName(), action);
0094     }
0095 
0096     return m_d->actionCollections;
0097 }
0098