File indexing completed on 2025-01-26 05:09:00

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "keystate.h"
0008 
0009 #include <QDebug>
0010 
0011 #include "keyservice.h"
0012 #include <klocalizedstring.h>
0013 
0014 KeyStatesEngine::KeyStatesEngine(QObject *parent)
0015     : Plasma5Support::DataEngine(parent)
0016 {
0017     qWarning("KeyStatesEngine is deprecated. Use KeyState from org.kde.plasma.private.keyboardindicator instead.");
0018     m_mods.insert(Qt::Key_Shift, kli18n("Shift"));
0019     m_mods.insert(Qt::Key_Control, kli18n("Ctrl"));
0020     m_mods.insert(Qt::Key_Alt, kli18n("Alt"));
0021     m_mods.insert(Qt::Key_Meta, kli18n("Meta"));
0022     m_mods.insert(Qt::Key_Super_L, kli18n("Super"));
0023     m_mods.insert(Qt::Key_Hyper_L, kli18n("Hyper"));
0024     m_mods.insert(Qt::Key_AltGr, kli18n("AltGr"));
0025     m_mods.insert(Qt::Key_NumLock, kli18n("Num Lock"));
0026     m_mods.insert(Qt::Key_CapsLock, kli18n("Caps Lock"));
0027     m_mods.insert(Qt::Key_ScrollLock, kli18n("Scroll Lock"));
0028 
0029     m_buttons.insert(Qt::LeftButton, kli18n("Left Button"));
0030     m_buttons.insert(Qt::RightButton, kli18n("Right Button"));
0031     m_buttons.insert(Qt::MiddleButton, kli18n("Middle Button"));
0032     m_buttons.insert(Qt::XButton1, kli18n("First X Button"));
0033     m_buttons.insert(Qt::XButton2, kli18n("Second X Button"));
0034     init();
0035 }
0036 
0037 KeyStatesEngine::~KeyStatesEngine()
0038 {
0039 }
0040 
0041 void KeyStatesEngine::init()
0042 {
0043     const auto end = m_mods.constEnd();
0044     for (auto it = m_mods.constBegin(); it != end; ++it) {
0045         if (m_keyInfo.knowsKey(it.key())) {
0046             Data data;
0047             data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isKeyPressed(it.key()));
0048             data.insert(QString::fromLatin1(kli18n("Latched").untranslatedText()), m_keyInfo.isKeyLatched(it.key()));
0049             data.insert(QString::fromLatin1(kli18n("Locked").untranslatedText()), m_keyInfo.isKeyLocked(it.key()));
0050             setData(QString::fromLatin1(it.value().untranslatedText()), data);
0051         }
0052     }
0053 
0054     const auto end2 = m_buttons.constEnd();
0055     for (auto it2 = m_buttons.constBegin(); it2 != end2; ++it2) {
0056         Data data;
0057         data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isButtonPressed(it2.key()));
0058         setData(QString::fromLatin1(it2.value().untranslatedText()), data);
0059     }
0060 
0061     connect(&m_keyInfo, &KModifierKeyInfo::keyPressed, this, &KeyStatesEngine::keyPressed);
0062     connect(&m_keyInfo, &KModifierKeyInfo::keyLatched, this, &KeyStatesEngine::keyLatched);
0063     connect(&m_keyInfo, &KModifierKeyInfo::keyLocked, this, &KeyStatesEngine::keyLocked);
0064     connect(&m_keyInfo, &KModifierKeyInfo::buttonPressed, this, &KeyStatesEngine::mouseButtonPressed);
0065     connect(&m_keyInfo, &KModifierKeyInfo::keyAdded, this, &KeyStatesEngine::keyAdded);
0066     connect(&m_keyInfo, &KModifierKeyInfo::keyRemoved, this, &KeyStatesEngine::keyRemoved);
0067 }
0068 
0069 Plasma5Support::Service *KeyStatesEngine::serviceForSource(const QString &source)
0070 {
0071     const auto end = m_mods.constEnd();
0072     for (auto it = m_mods.constBegin(); it != end; ++it) {
0073         if (QLatin1String(it.value().untranslatedText()) == source) {
0074             return new KeyService(this, &m_keyInfo, it.key());
0075         }
0076     }
0077 
0078     return Plasma5Support::DataEngine::serviceForSource(source);
0079 }
0080 
0081 void KeyStatesEngine::keyPressed(Qt::Key key, bool state)
0082 {
0083     if (m_mods.contains(key)) {
0084         setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Pressed").untranslatedText()), state);
0085     }
0086 }
0087 
0088 void KeyStatesEngine::keyLatched(Qt::Key key, bool state)
0089 {
0090     if (m_mods.contains(key)) {
0091         setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Latched").untranslatedText()), state);
0092     }
0093 }
0094 
0095 void KeyStatesEngine::keyLocked(Qt::Key key, bool state)
0096 {
0097     if (m_mods.contains(key)) {
0098         setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), QString::fromLatin1(kli18n("Locked").untranslatedText()), state);
0099     }
0100 }
0101 
0102 void KeyStatesEngine::mouseButtonPressed(Qt::MouseButton button, bool state)
0103 {
0104     if (m_buttons.contains(button)) {
0105         setData(QString::fromLatin1(m_buttons.value(button).untranslatedText()), QString::fromLatin1(kli18n("Pressed").untranslatedText()), state);
0106     }
0107 }
0108 
0109 void KeyStatesEngine::keyAdded(Qt::Key key)
0110 {
0111     if (m_mods.contains(key)) {
0112         Data data;
0113         data.insert(QString::fromLatin1(kli18n("Pressed").untranslatedText()), m_keyInfo.isKeyPressed(key));
0114         data.insert(QString::fromLatin1(kli18n("Latched").untranslatedText()), m_keyInfo.isKeyLatched(key));
0115         data.insert(QString::fromLatin1(kli18n("Locked").untranslatedText()), m_keyInfo.isKeyLocked(key));
0116         setData(QString::fromLatin1(m_mods.value(key).untranslatedText()), data);
0117     }
0118 }
0119 
0120 void KeyStatesEngine::keyRemoved(Qt::Key key)
0121 {
0122     if (m_mods.contains(key)) {
0123         removeSource(QString::fromLatin1(m_mods.value(key).untranslatedText()));
0124     }
0125 }
0126 
0127 K_PLUGIN_CLASS_WITH_JSON(KeyStatesEngine, "plasma-dataengine-keystate.json")
0128 
0129 #include "keystate.moc"