File indexing completed on 2024-11-10 04:57:19

0001 /*
0002     SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "shortcuthandler.h"
0008 #include "utils/common.h"
0009 
0010 #include <KGlobalAccel>
0011 
0012 #include <QAction>
0013 
0014 namespace KWin
0015 {
0016 
0017 ShortcutHandler::ShortcutHandler(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 void ShortcutHandler::classBegin()
0023 {
0024 }
0025 
0026 void ShortcutHandler::componentComplete()
0027 {
0028     if (m_name.isEmpty()) {
0029         qCWarning(KWIN_CORE) << "ShortcutHandler.name is required";
0030         return;
0031     }
0032     if (m_text.isEmpty()) {
0033         qCWarning(KWIN_CORE) << "ShortcutHandler.text is required";
0034         return;
0035     }
0036 
0037     QAction *action = new QAction(this);
0038     connect(action, &QAction::triggered, this, &ShortcutHandler::activated);
0039     action->setObjectName(m_name);
0040     action->setText(m_text);
0041     KGlobalAccel::self()->setShortcut(action, {m_keySequence});
0042 }
0043 
0044 QString ShortcutHandler::name() const
0045 {
0046     return m_name;
0047 }
0048 
0049 void ShortcutHandler::setName(const QString &name)
0050 {
0051     if (m_action) {
0052         qCWarning(KWIN_CORE) << "ShortcutHandler.name cannot be changed";
0053         return;
0054     }
0055     if (m_name != name) {
0056         m_name = name;
0057         Q_EMIT nameChanged();
0058     }
0059 }
0060 
0061 QString ShortcutHandler::text() const
0062 {
0063     return m_text;
0064 }
0065 
0066 void ShortcutHandler::setText(const QString &text)
0067 {
0068     if (m_text != text) {
0069         m_text = text;
0070         if (m_action) {
0071             m_action->setText(text);
0072         }
0073         Q_EMIT textChanged();
0074     }
0075 }
0076 
0077 QVariant ShortcutHandler::sequence() const
0078 {
0079     return m_userSequence;
0080 }
0081 
0082 void ShortcutHandler::setSequence(const QVariant &sequence)
0083 {
0084     if (m_action) {
0085         qCWarning(KWIN_CORE) << "ShortcutHandler.sequence cannot be changed";
0086         return;
0087     }
0088     if (m_userSequence != sequence) {
0089         m_userSequence = sequence;
0090         m_keySequence = QKeySequence::fromString(sequence.toString());
0091         Q_EMIT sequenceChanged();
0092     }
0093 }
0094 
0095 } // namespace KWin
0096 
0097 #include "moc_shortcuthandler.cpp"