1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
Copyright 2009 Michael Leupold <lemma@confuego.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License or (at your option) version 3 or any later version
accepted by the membership of KDE e.V. (or its successor approved
by the membership of KDE e.V.), which shall act as a proxy
defined in Section 14 of version 3 of the license.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMap>
#include <QWidget>
#include <QPushButton>
#include <QDebug>
#include <QCheckBox>
#include "kmodifierkeyinfo.h"
template <typename A, typename B, typename C>
class Triple
{
public:
Triple() {}
Triple(const A _first, const B _second, const C _third)
: first(_first), second(_second), third(_third) {};
A first;
B second;
C third;
};
class TestWidget : public QWidget
{
Q_OBJECT
public:
TestWidget();
protected Q_SLOTS:
void keyPressed(Qt::Key key, bool state);
void keyLatched(Qt::Key key, bool state);
void keyLocked(Qt::Key key, bool state);
void mouseButtonPressed(Qt::MouseButton button, bool state);
void latch();
void lock();
void keyAdded(Qt::Key)
{
qDebug() << "Key added";
}
void keyRemoved(Qt::Key)
{
qDebug() << "Key removed";
}
private:
KModifierKeyInfo m_lock;
QMap<Qt::Key, Triple<QCheckBox *, QCheckBox *, QCheckBox *> > m_leds;
QMap<Qt::MouseButton, QCheckBox *> m_mouseLeds;
};
TestWidget::TestWidget() : QWidget(0), m_lock(this)
{
QMap<Qt::Key, QString> mods;
mods.insert(Qt::Key_Shift, QStringLiteral("Shift"));
mods.insert(Qt::Key_Control, QStringLiteral("Ctrl"));
mods.insert(Qt::Key_Alt, QStringLiteral("Alt"));
mods.insert(Qt::Key_Meta, QStringLiteral("Meta"));
mods.insert(Qt::Key_Super_L, QStringLiteral("Super"));
mods.insert(Qt::Key_Hyper_L, QStringLiteral("Hyper"));
mods.insert(Qt::Key_AltGr, QStringLiteral("AltGr"));
mods.insert(Qt::Key_NumLock, QStringLiteral("NumLock"));
mods.insert(Qt::Key_CapsLock, QStringLiteral("CapsLock"));
mods.insert(Qt::Key_ScrollLock, QStringLiteral("ScrollLock"));
QMap<Qt::MouseButton, QString> buttons;
buttons.insert(Qt::LeftButton, QStringLiteral("Left Button"));
buttons.insert(Qt::RightButton, QStringLiteral("Right Button"));
buttons.insert(Qt::MidButton, QStringLiteral("Middle Button"));
buttons.insert(Qt::XButton1, QStringLiteral("First X Button"));
buttons.insert(Qt::XButton2, QStringLiteral("Second X Button"));
QVBoxLayout *layout = new QVBoxLayout(this);
QMap<Qt::Key, QString>::const_iterator it;
QMap<Qt::Key, QString>::const_iterator end = mods.constEnd();
for (it = mods.constBegin(); it != end; ++it) {
if (m_lock.knowsKey(it.key())) {
QHBoxLayout *hlayout = new QHBoxLayout;
QCheckBox *pressed = new QCheckBox(this);
QCheckBox *latched = new QCheckBox(this);
QCheckBox *locked = new QCheckBox(this);
QPushButton *latch = new QPushButton(QStringLiteral("latch"), this);
latch->setProperty("modifier", it.key());
connect(latch, SIGNAL(clicked()), SLOT(latch()));
QPushButton *lock = new QPushButton(QStringLiteral("lock"), this);
lock->setProperty("modifier", it.key());
connect(lock, SIGNAL(clicked()), SLOT(lock()));
pressed->setChecked(m_lock.isKeyPressed(it.key()));
latched->setChecked(m_lock.isKeyLatched(it.key()));
locked->setChecked(m_lock.isKeyLocked(it.key()));
m_leds.insert(it.key(), Triple<QCheckBox *, QCheckBox *, QCheckBox *>(pressed, latched, locked));
hlayout->addWidget(pressed);
hlayout->addWidget(latched);
hlayout->addWidget(locked);
hlayout->addWidget(new QLabel(it.value()));
hlayout->addWidget(latch);
hlayout->addWidget(lock);
layout->addLayout(hlayout);
}
}
QMap<Qt::MouseButton, QString>::const_iterator it2;
QMap<Qt::MouseButton, QString>::const_iterator end2 = buttons.constEnd();
for (it2 = buttons.constBegin(); it2 != end2; ++it2) {
QHBoxLayout *hlayout = new QHBoxLayout;
QCheckBox *pressed = new QCheckBox(this);
pressed->setChecked(m_lock.isButtonPressed(it2.key()));
m_mouseLeds.insert(it2.key(), pressed);
hlayout->addWidget(pressed);
hlayout->addWidget(new QLabel(it2.value()));
layout->addLayout(hlayout);
}
setLayout(layout);
connect(&m_lock, SIGNAL(keyPressed(Qt::Key,bool)), SLOT(keyPressed(Qt::Key,bool)));
connect(&m_lock, SIGNAL(keyLatched(Qt::Key,bool)), SLOT(keyLatched(Qt::Key,bool)));
connect(&m_lock, SIGNAL(keyLocked(Qt::Key,bool)), SLOT(keyLocked(Qt::Key,bool)));
connect(&m_lock, SIGNAL(buttonPressed(Qt::MouseButton,bool)),
SLOT(mouseButtonPressed(Qt::MouseButton,bool)));
connect(&m_lock, SIGNAL(keyAdded(Qt::Key)), SLOT(keyAdded(Qt::Key)));
connect(&m_lock, SIGNAL(keyRemoved(Qt::Key)), SLOT(keyRemoved(Qt::Key)));
}
void TestWidget::keyPressed(Qt::Key key, bool pressed)
{
if (m_leds.contains(key)) {
m_leds[key].first->setChecked(pressed);
}
}
void TestWidget::keyLatched(Qt::Key key, bool latched)
{
if (m_leds.contains(key)) {
m_leds[key].second->setChecked(latched);
}
}
void TestWidget::keyLocked(Qt::Key key, bool locked)
{
if (m_leds.contains(key)) {
m_leds[key].third->setChecked(locked);
}
}
void TestWidget::mouseButtonPressed(Qt::MouseButton button, bool pressed)
{
if (m_mouseLeds.contains(button)) {
m_mouseLeds[button]->setChecked(pressed);
}
}
void TestWidget::latch()
{
Qt::Key key = (Qt::Key)sender()->property("modifier").toInt();
m_lock.setKeyLatched(key, !m_lock.isKeyLatched(key));
}
void TestWidget::lock()
{
Qt::Key key = (Qt::Key)sender()->property("modifier").toInt();
m_lock.setKeyLocked(key, !m_lock.isKeyLocked(key));
}
int main(int argc, char *argv[])
{
QApplication::setApplicationName(QStringLiteral("simple"));
QApplication app(argc, argv);
TestWidget mainWidget;
mainWidget.show();
return app.exec();
}
#include "kmodifierkeyinfotest.moc"
|