4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2001 Bernie Innocenti <bernie@codewiz.org>
36 * \author Bernie Innocenti <bernie@codewiz.org>
38 * \brief QT-based widget for keyboard emulation (implementation)
44 #include <QtGui/QPainter>
45 #include <QtGui/QPixmap>
46 #include <QtGui/QSizePolicy>
47 #include <QtGui/QLayout>
48 #include <QtGui/QKeyEvent>
49 #include <QtCore/QEvent>
50 #include <QtCore/QSize>
51 #include <QtCore/QRect>
54 EmulKey::EmulKey(EmulKbd *kbd, const char *label, int _keycode, int _row, int _col) :
55 QPushButton(label, kbd),
59 // don't let the widget get focus
60 setFocusPolicy(Qt::NoFocus);
63 connect(this, SIGNAL(pressed()), this, SLOT(keyPressed()));
64 connect(this, SIGNAL(released()), this, SLOT(keyReleased()));
75 * Override standad QButton behaviour: we must also emit the signals.
78 void EmulKey::setDown(bool enable)
80 // let our superclass do everything else
81 QPushButton::setDown(enable);
91 void EmulKey::keyPressed(void)
93 static_cast<EmulKbd *>(parent())->setKey(row, col, true);
98 void EmulKey::keyReleased(void)
100 static_cast<EmulKbd *>(parent())->setKey(row, col, false);
104 EmulKbd::EmulKbd(QWidget *parent, Qt::WFlags f) :
106 layout(new QGridLayout(this)),
109 setFrameStyle(QFrame::Box | QFrame::Sunken);
111 setFocusPolicy(Qt::StrongFocus);
112 frame_width = frameWidth();
122 QSizePolicy EmulKbd::sizePolicy() const
124 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
128 void EmulKbd::resizeEvent(QResizeEvent *event)
130 // Let our superclass process the event first
131 QFrame::resizeEvent(event);
135 // handle key presses for all keys in keyboard
136 bool EmulKbd::event(QEvent *_e)
140 case QEvent::KeyPress:
141 case QEvent::KeyRelease:
143 QKeyEvent *e = static_cast<QKeyEvent *>(_e);
144 int keycode = e->key();
147 // ignore repeated keys
148 if (!e->isAutoRepeat())
151 for (QObjectList::const_iterator it(children().begin()); it != children().end(); ++it)
153 // only keys, not other children!
154 if ((*it)->metaObject() == &EmulKey::staticMetaObject)
155 // if ((key = dynamic_cast<EmulKey *>(*it)))
157 key = static_cast<EmulKey *>(*it);
160 if (key->keycode == keycode)
162 // yes, tell key to go down (or up)
163 key->setDown(_e->type() == QEvent::KeyPress);
173 // let superclass process this event
174 return QFrame::event(_e);
176 } // end switch(_e->type())
180 void EmulKbd::addKey(const char *label, int keycode, int row, int col, int matrix_row, int matrix_col)
182 if (matrix_row == -1)
184 if (matrix_col == -1)
187 layout->addWidget(new EmulKey(this, label, keycode, matrix_row, matrix_col), row, col);
192 void EmulKbd::setKey(int /*row*/, int /*col*/, bool /*on*/)
197 void EmulKbd::setRow(int r)
202 int EmulKbd::readCols(void)
208 for (int i = 0; (item = layout->itemAt(i)); ++i)
210 key = static_cast<EmulKey *>(item->widget());
211 if (key->row == active_row)
214 cols |= (1<<key->col);
220 extern "C" void emul_kbdSetRows(int r)
222 emul->emulKbd->setRow(r);
226 extern "C" int emul_kbdReadCols(void)
228 return emul->emulKbd->readCols();
231 #include "emulkbd_moc.cpp"