4 * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2001 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief QT-based widget for keyboard emulation (implementation)
22 #include <qsizepolicy.h>
26 #include <qobjectlist.h>
28 #include <QtGui/QPainter>
29 #include <QtGui/QPixmap>
30 #include <QtGui/QSizePolicy>
31 #include <QtGui/QLayout>
32 #include <QtGui/QKeyEvent>
33 #include <QtCore/QEvent>
34 #include <QtCore/QSize>
35 #include <QtCore/QRect>
39 EmulKey::EmulKey(EmulKbd *kbd, const char *label, int _keycode, int _row, int _col) :
40 QPushButton(label, kbd),
44 // don't let the widget get focus
45 setFocusPolicy(Qt::NoFocus);
48 connect(this, SIGNAL(pressed()), this, SLOT(keyPressed()));
49 connect(this, SIGNAL(released()), this, SLOT(keyReleased()));
60 * Override standad QButton behaviour: we must also emit the signals.
63 void EmulKey::setDown(bool enable)
65 // let our superclass do everything else
66 QPushButton::setDown(enable);
76 void EmulKey::keyPressed(void)
78 static_cast<EmulKbd *>(parent())->setKey(row, col, true);
83 void EmulKey::keyReleased(void)
85 static_cast<EmulKbd *>(parent())->setKey(row, col, false);
89 EmulKbd::EmulKbd(QWidget *parent, const char *name, Qt::WFlags f) :
90 QFrame(parent, name, Qt::WRepaintNoErase | Qt::WResizeNoErase | f),
91 layout(new QGridLayout(this, 4, 4, 4)),
94 setFrameStyle(QFrame::Box | QFrame::Sunken);
96 setFocusPolicy(Qt::StrongFocus);
97 frame_width = frameWidth();
107 QSizePolicy EmulKbd::sizePolicy() const
109 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, false);
113 void EmulKbd::resizeEvent(QResizeEvent *event)
115 // Let our superclass process the event first
116 QFrame::resizeEvent(event);
120 // handle key presses for all keys in keyboard
121 bool EmulKbd::event(QEvent *_e)
125 case QEvent::KeyPress:
126 case QEvent::KeyRelease:
128 QKeyEvent *e = static_cast<QKeyEvent *>(_e);
129 int keycode = e->key();
132 // ignore repeated keys
133 if (!e->isAutoRepeat())
136 for (QObjectList::const_iterator it(children().begin()); it != children().end(); ++it)
138 // only keys, not other children!
139 if ((*it)->metaObject() == &EmulKey::staticMetaObject)
140 // if ((key = dynamic_cast<EmulKey *>(*it)))
142 key = static_cast<EmulKey *>(*it);
145 if (key->keycode == keycode)
147 // yes, tell key to go down (or up)
148 key->setDown(_e->type() == QEvent::KeyPress);
158 // let superclass process this event
159 return QFrame::event(_e);
161 } // end switch(_e->type())
165 void EmulKbd::addKey(const char *label, int keycode, int row, int col, int matrix_row, int matrix_col)
167 if (matrix_row == -1)
169 if (matrix_col == -1)
172 layout->addWidget(new EmulKey(this, label, keycode, matrix_row, matrix_col), row, col);
177 void EmulKbd::setKey(int /*row*/, int /*col*/, bool /*on*/)
182 void EmulKbd::setRow(int r)
187 int EmulKbd::readCols(void)
193 for(QLayoutIterator it(layout->iterator()); (item = it.current()); ++it)
195 key = static_cast<EmulKey *>(item->widget());
196 if (key->row == active_row)
199 cols |= (1<<key->col);
205 extern "C" void emul_kbdSetRows(int r)
207 emul->emulKbd->setRow(r);
211 extern "C" int emul_kbdReadCols(void)
213 return emul->emulKbd->readCols();
216 #include "emulkbd_moc.cpp"