Plasma5Support

keystate.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "keystate.h"
8
9#include <QDebug>
10
11#include "keyservice.h"
12#include <klocalizedstring.h>
13
14KeyStatesEngine::KeyStatesEngine(QObject *parent)
15 : Plasma5Support::DataEngine(parent)
16{
17 qWarning("KeyStatesEngine is deprecated. Use KeyState from org.kde.plasma.private.keyboardindicator instead.");
18 m_mods.insert(Qt::Key_Shift, kli18n("Shift"));
19 m_mods.insert(Qt::Key_Control, kli18n("Ctrl"));
20 m_mods.insert(Qt::Key_Alt, kli18n("Alt"));
21 m_mods.insert(Qt::Key_Meta, kli18n("Meta"));
22 m_mods.insert(Qt::Key_Super_L, kli18n("Super"));
23 m_mods.insert(Qt::Key_Hyper_L, kli18n("Hyper"));
24 m_mods.insert(Qt::Key_AltGr, kli18n("AltGr"));
25 m_mods.insert(Qt::Key_NumLock, kli18n("Num Lock"));
26 m_mods.insert(Qt::Key_CapsLock, kli18n("Caps Lock"));
27 m_mods.insert(Qt::Key_ScrollLock, kli18n("Scroll Lock"));
28
29 m_buttons.insert(Qt::LeftButton, kli18n("Left Button"));
30 m_buttons.insert(Qt::RightButton, kli18n("Right Button"));
31 m_buttons.insert(Qt::MiddleButton, kli18n("Middle Button"));
32 m_buttons.insert(Qt::XButton1, kli18n("First X Button"));
33 m_buttons.insert(Qt::XButton2, kli18n("Second X Button"));
34 init();
35}
36
37KeyStatesEngine::~KeyStatesEngine()
38{
39}
40
41void KeyStatesEngine::init()
42{
43 const auto end = m_mods.constEnd();
44 for (auto it = m_mods.constBegin(); it != end; ++it) {
45 if (m_keyInfo.knowsKey(it.key())) {
46 Data data;
47 data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isKeyPressed(it.key()));
48 data.insert(QString::fromLatin1(kli18n("Latched").untranslatedText()), m_keyInfo.isKeyLatched(it.key()));
49 data.insert(QString::fromLatin1(kli18n("Locked").untranslatedText()), m_keyInfo.isKeyLocked(it.key()));
50 setData(QString::fromLatin1(it.value().untranslatedText()), data);
51 }
52 }
53
54 const auto end2 = m_buttons.constEnd();
55 for (auto it2 = m_buttons.constBegin(); it2 != end2; ++it2) {
56 Data data;
57 data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isButtonPressed(it2.key()));
58 setData(QString::fromLatin1(it2.value().untranslatedText()), data);
59 }
60
61 connect(&m_keyInfo, &KModifierKeyInfo::keyPressed, this, &KeyStatesEngine::keyPressed);
62 connect(&m_keyInfo, &KModifierKeyInfo::keyLatched, this, &KeyStatesEngine::keyLatched);
63 connect(&m_keyInfo, &KModifierKeyInfo::keyLocked, this, &KeyStatesEngine::keyLocked);
64 connect(&m_keyInfo, &KModifierKeyInfo::buttonPressed, this, &KeyStatesEngine::mouseButtonPressed);
65 connect(&m_keyInfo, &KModifierKeyInfo::keyAdded, this, &KeyStatesEngine::keyAdded);
66 connect(&m_keyInfo, &KModifierKeyInfo::keyRemoved, this, &KeyStatesEngine::keyRemoved);
67}
68
70{
71 const auto end = m_mods.constEnd();
72 for (auto it = m_mods.constBegin(); it != end; ++it) {
73 if (QLatin1String(it.value().untranslatedText()) == source) {
74 return new KeyService(this, &m_keyInfo, it.key());
75 }
76 }
77
79}
80
81void KeyStatesEngine::keyPressed(Qt::Key key, bool state)
82{
83 if (m_mods.contains(key)) {
84 setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Pressed").untranslatedText()), state);
85 }
86}
87
88void KeyStatesEngine::keyLatched(Qt::Key key, bool state)
89{
90 if (m_mods.contains(key)) {
91 setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Latched").untranslatedText()), state);
92 }
93}
94
95void KeyStatesEngine::keyLocked(Qt::Key key, bool state)
96{
97 if (m_mods.contains(key)) {
98 setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Locked").untranslatedText()), state);
99 }
100}
101
102void KeyStatesEngine::mouseButtonPressed(Qt::MouseButton button, bool state)
103{
104 if (m_buttons.contains(button)) {
105 setData(QString::fromLatin1(m_buttons.value(button).untranslatedText()), QString::fromLatin1(kli18n("Pressed").untranslatedText()), state);
106 }
107}
108
109void KeyStatesEngine::keyAdded(Qt::Key key)
110{
111 if (m_mods.contains(key)) {
112 Data data;
113 data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isKeyPressed(key));
114 data.insert(QString::fromLatin1(kli18n("Latched").untranslatedText()), m_keyInfo.isKeyLatched(key));
115 data.insert(QString::fromLatin1(kli18n("Locked").untranslatedText()), m_keyInfo.isKeyLocked(key));
116 setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), data);
117 }
118}
119
120void KeyStatesEngine::keyRemoved(Qt::Key key)
121{
122 if (m_mods.contains(key)) {
123 removeSource(QString::fromLatin1(m_mods.value(key).untranslatedText()));
124 }
125}
126
127K_PLUGIN_CLASS_WITH_JSON(KeyStatesEngine, "plasma-dataengine-keystate.json")
128
129#include "keystate.moc"
void keyPressed(Qt::Key key, bool pressed)
void keyAdded(Qt::Key key)
void keyRemoved(Qt::Key key)
void keyLocked(Qt::Key key, bool locked)
bool knowsKey(Qt::Key key) const
void keyLatched(Qt::Key key, bool latched)
bool isButtonPressed(Qt::MouseButton button) const
bool isKeyLatched(Qt::Key key) const
bool isKeyPressed(Qt::Key key) const
bool isKeyLocked(Qt::Key key) const
void buttonPressed(Qt::MouseButton button, bool pressed)
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
This engine provides the current state of the keyboard modifiers and mouse buttons,...
Definition keystate.h:19
Plasma5Support::Service * serviceForSource(const QString &source) override
Definition keystate.cpp:69
void removeSource(const QString &source)
Removes a data source.
virtual Q_INVOKABLE Service * serviceForSource(const QString &source)
void setData(const QString &source, const QVariant &value)
Sets a value for a data source.
This class provides a generic API for write access to settings or services.
Definition service.h:78
const QList< QKeySequence > & end()
Namespace for everything in libplasma.
Definition datamodel.cpp:15
QCA_EXPORT void init()
const_iterator constBegin() const const
const_iterator constEnd() const const
bool contains(const Key &key) const const
T value(const Key &key, const T &defaultValue) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
Key_Shift
LeftButton
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.