File indexing completed on 2024-12-08 03:39:28
0001 /* 0002 SPDX-FileCopyrightText: 2009 Michael Leupold <lemma@confuego.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include <QApplication> 0008 #include <QCheckBox> 0009 #include <QDebug> 0010 #include <QHBoxLayout> 0011 #include <QLabel> 0012 #include <QMap> 0013 #include <QPushButton> 0014 #include <QVBoxLayout> 0015 #include <QWidget> 0016 0017 #include "kmodifierkeyinfo.h" 0018 0019 template<typename A, typename B, typename C> 0020 class Triple 0021 { 0022 public: 0023 Triple() 0024 { 0025 } 0026 Triple(const A _first, const B _second, const C _third) 0027 : first(_first) 0028 , second(_second) 0029 , third(_third){}; 0030 A first; 0031 B second; 0032 C third; 0033 }; 0034 0035 class TestWidget : public QWidget 0036 { 0037 Q_OBJECT 0038 0039 public: 0040 TestWidget(); 0041 0042 protected Q_SLOTS: 0043 void keyPressed(Qt::Key key, bool state); 0044 void keyLatched(Qt::Key key, bool state); 0045 void keyLocked(Qt::Key key, bool state); 0046 void mouseButtonPressed(Qt::MouseButton button, bool state); 0047 void latch(); 0048 void lock(); 0049 void keyAdded(Qt::Key) 0050 { 0051 qDebug() << "Key added"; 0052 } 0053 void keyRemoved(Qt::Key) 0054 { 0055 qDebug() << "Key removed"; 0056 } 0057 0058 private: 0059 KModifierKeyInfo m_lock; 0060 QMap<Qt::Key, Triple<QCheckBox *, QCheckBox *, QCheckBox *>> m_leds; 0061 QMap<Qt::MouseButton, QCheckBox *> m_mouseLeds; 0062 }; 0063 0064 TestWidget::TestWidget() 0065 : QWidget(nullptr) 0066 , m_lock(this) 0067 { 0068 QMap<Qt::Key, QString> mods; 0069 mods.insert(Qt::Key_Shift, QStringLiteral("Shift")); 0070 mods.insert(Qt::Key_Control, QStringLiteral("Ctrl")); 0071 mods.insert(Qt::Key_Alt, QStringLiteral("Alt")); 0072 mods.insert(Qt::Key_Meta, QStringLiteral("Meta")); 0073 mods.insert(Qt::Key_Super_L, QStringLiteral("Super")); 0074 mods.insert(Qt::Key_Hyper_L, QStringLiteral("Hyper")); 0075 mods.insert(Qt::Key_AltGr, QStringLiteral("AltGr")); 0076 mods.insert(Qt::Key_NumLock, QStringLiteral("NumLock")); 0077 mods.insert(Qt::Key_CapsLock, QStringLiteral("CapsLock")); 0078 mods.insert(Qt::Key_ScrollLock, QStringLiteral("ScrollLock")); 0079 0080 QMap<Qt::MouseButton, QString> buttons; 0081 buttons.insert(Qt::LeftButton, QStringLiteral("Left Button")); 0082 buttons.insert(Qt::RightButton, QStringLiteral("Right Button")); 0083 buttons.insert(Qt::MiddleButton, QStringLiteral("Middle Button")); 0084 buttons.insert(Qt::XButton1, QStringLiteral("First X Button")); 0085 buttons.insert(Qt::XButton2, QStringLiteral("Second X Button")); 0086 0087 QVBoxLayout *layout = new QVBoxLayout(this); 0088 0089 QMap<Qt::Key, QString>::const_iterator it; 0090 QMap<Qt::Key, QString>::const_iterator end = mods.constEnd(); 0091 for (it = mods.constBegin(); it != end; ++it) { 0092 if (m_lock.knowsKey(it.key())) { 0093 QHBoxLayout *hlayout = new QHBoxLayout; 0094 QCheckBox *pressed = new QCheckBox(this); 0095 QCheckBox *latched = new QCheckBox(this); 0096 QCheckBox *locked = new QCheckBox(this); 0097 QPushButton *latch = new QPushButton(QStringLiteral("latch"), this); 0098 latch->setProperty("modifier", it.key()); 0099 connect(latch, &QAbstractButton::clicked, this, &TestWidget::latch); 0100 QPushButton *lock = new QPushButton(QStringLiteral("lock"), this); 0101 lock->setProperty("modifier", it.key()); 0102 connect(lock, &QAbstractButton::clicked, this, &TestWidget::lock); 0103 pressed->setChecked(m_lock.isKeyPressed(it.key())); 0104 latched->setChecked(m_lock.isKeyLatched(it.key())); 0105 locked->setChecked(m_lock.isKeyLocked(it.key())); 0106 m_leds.insert(it.key(), Triple<QCheckBox *, QCheckBox *, QCheckBox *>(pressed, latched, locked)); 0107 hlayout->addWidget(pressed); 0108 hlayout->addWidget(latched); 0109 hlayout->addWidget(locked); 0110 hlayout->addWidget(new QLabel(it.value())); 0111 hlayout->addWidget(latch); 0112 hlayout->addWidget(lock); 0113 layout->addLayout(hlayout); 0114 } 0115 } 0116 0117 QMap<Qt::MouseButton, QString>::const_iterator it2; 0118 QMap<Qt::MouseButton, QString>::const_iterator end2 = buttons.constEnd(); 0119 for (it2 = buttons.constBegin(); it2 != end2; ++it2) { 0120 QHBoxLayout *hlayout = new QHBoxLayout; 0121 QCheckBox *pressed = new QCheckBox(this); 0122 pressed->setChecked(m_lock.isButtonPressed(it2.key())); 0123 m_mouseLeds.insert(it2.key(), pressed); 0124 hlayout->addWidget(pressed); 0125 hlayout->addWidget(new QLabel(it2.value())); 0126 layout->addLayout(hlayout); 0127 } 0128 0129 connect(&m_lock, &KModifierKeyInfo::keyPressed, this, &TestWidget::keyPressed); 0130 connect(&m_lock, &KModifierKeyInfo::keyLatched, this, &TestWidget::keyLatched); 0131 connect(&m_lock, &KModifierKeyInfo::keyLocked, this, &TestWidget::keyLocked); 0132 connect(&m_lock, &KModifierKeyInfo::buttonPressed, this, &TestWidget::mouseButtonPressed); 0133 connect(&m_lock, &KModifierKeyInfo::keyAdded, this, &TestWidget::keyAdded); 0134 connect(&m_lock, &KModifierKeyInfo::keyRemoved, this, &TestWidget::keyRemoved); 0135 } 0136 0137 void TestWidget::keyPressed(Qt::Key key, bool pressed) 0138 { 0139 if (m_leds.contains(key)) { 0140 m_leds[key].first->setChecked(pressed); 0141 } 0142 } 0143 0144 void TestWidget::keyLatched(Qt::Key key, bool latched) 0145 { 0146 if (m_leds.contains(key)) { 0147 m_leds[key].second->setChecked(latched); 0148 } 0149 } 0150 0151 void TestWidget::keyLocked(Qt::Key key, bool locked) 0152 { 0153 if (m_leds.contains(key)) { 0154 m_leds[key].third->setChecked(locked); 0155 } 0156 } 0157 0158 void TestWidget::mouseButtonPressed(Qt::MouseButton button, bool pressed) 0159 { 0160 if (m_mouseLeds.contains(button)) { 0161 m_mouseLeds[button]->setChecked(pressed); 0162 } 0163 } 0164 0165 void TestWidget::latch() 0166 { 0167 Qt::Key key = (Qt::Key)sender()->property("modifier").toInt(); 0168 m_lock.setKeyLatched(key, !m_lock.isKeyLatched(key)); 0169 } 0170 0171 void TestWidget::lock() 0172 { 0173 Qt::Key key = (Qt::Key)sender()->property("modifier").toInt(); 0174 m_lock.setKeyLocked(key, !m_lock.isKeyLocked(key)); 0175 } 0176 0177 int main(int argc, char *argv[]) 0178 { 0179 QApplication::setApplicationName(QStringLiteral("simple")); 0180 0181 QApplication app(argc, argv); 0182 TestWidget mainWidget; 0183 mainWidget.show(); 0184 0185 return app.exec(); 0186 } 0187 0188 #include "kmodifierkeyinfotest.moc"