KWin
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
shortcuthandler.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "shortcuthandler.h"
8#include "utils/common.h"
9
10#include <KGlobalAccel>
11
12#include <QAction>
13
14namespace KWin
15{
16
18 : QObject(parent)
19{
20}
21
25
27{
28 if (m_name.isEmpty()) {
29 qCWarning(KWIN_CORE) << "ShortcutHandler.name is required";
30 return;
31 }
32 if (m_text.isEmpty()) {
33 qCWarning(KWIN_CORE) << "ShortcutHandler.text is required";
34 return;
35 }
36
37 QAction *action = new QAction(this);
38 connect(action, &QAction::triggered, this, &ShortcutHandler::activated);
39 action->setObjectName(m_name);
40 action->setText(m_text);
41 KGlobalAccel::self()->setShortcut(action, {m_keySequence});
42}
43
44QString ShortcutHandler::name() const
45{
46 return m_name;
47}
48
49void ShortcutHandler::setName(const QString &name)
50{
51 if (m_action) {
52 qCWarning(KWIN_CORE) << "ShortcutHandler.name cannot be changed";
53 return;
54 }
55 if (m_name != name) {
56 m_name = name;
57 Q_EMIT nameChanged();
58 }
59}
60
61QString ShortcutHandler::text() const
62{
63 return m_text;
64}
65
66void ShortcutHandler::setText(const QString &text)
67{
68 if (m_text != text) {
69 m_text = text;
70 if (m_action) {
71 m_action->setText(text);
72 }
73 Q_EMIT textChanged();
74 }
75}
76
78{
79 return m_userSequence;
80}
81
82void ShortcutHandler::setSequence(const QVariant &sequence)
83{
84 if (m_action) {
85 qCWarning(KWIN_CORE) << "ShortcutHandler.sequence cannot be changed";
86 return;
87 }
88 if (m_userSequence != sequence) {
89 m_userSequence = sequence;
90 m_keySequence = QKeySequence::fromString(sequence.toString());
91 Q_EMIT sequenceChanged();
92 }
93}
94
95} // namespace KWin
96
97#include "moc_shortcuthandler.cpp"
void setText(const QString &text)
void setSequence(const QVariant &sequence)
void componentComplete() override
void setName(const QString &name)
void classBegin() override
ShortcutHandler(QObject *parent=nullptr)