File indexing completed on 2024-04-14 03:52:02

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include <KGlobalAccel>
0010 #include <QAction>
0011 #include <QApplication>
0012 #include <QQmlApplicationEngine>
0013 #include <QTest>
0014 
0015 class GlobalAction : public QAction, public QQmlParserStatus
0016 {
0017     Q_OBJECT
0018     Q_INTERFACES(QQmlParserStatus)
0019     Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
0020 public:
0021     GlobalAction()
0022     {
0023         connect(this, &QAction::changed, this, &GlobalAction::refresh);
0024         connect(KGlobalAccel::self(), &KGlobalAccel::globalShortcutActiveChanged, this, [this](QAction *action, bool active) {
0025             if (action != this) {
0026                 return;
0027             }
0028             qDebug() << "active changed" << action << active;
0029             m_active = active;
0030             Q_EMIT activeChanged(active);
0031         });
0032     }
0033 
0034     void classBegin() override
0035     {
0036     }
0037     void componentComplete() override
0038     {
0039         m_done = true;
0040         refresh();
0041     }
0042 
0043     void refresh()
0044     {
0045         if (!m_done) {
0046             return;
0047         }
0048         bool added = KGlobalAccel::self()->setShortcut(this, {shortcut()}, KGlobalAccel::NoAutoloading);
0049 
0050         if (!added) {
0051             qWarning() << "could not set the global shortcut" << shortcut();
0052         } else {
0053             qDebug() << "shortcut set correctly" << shortcut();
0054         }
0055     }
0056 
0057     bool isActive() const
0058     {
0059         return m_active;
0060     }
0061 
0062 Q_SIGNALS:
0063     void activeChanged(bool active);
0064 
0065 private:
0066     bool m_done = false;
0067     bool m_active = false;
0068 };
0069 
0070 int main(int argc, char **argv)
0071 {
0072     QApplication app(argc, argv);
0073     QQmlApplicationEngine engine;
0074 
0075     qmlRegisterType<GlobalAction>("org.kde.globalaccel", 1, 0, "GlobalAction");
0076 
0077     engine.load(QFINDTESTDATA("kglobalacceltest.qml"));
0078 
0079     return app.exec();
0080 }
0081 
0082 #include "kglobalacceltest.moc"