Update preset.
[bertos.git] / bertos / emul / emulkbd.cpp
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2001 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  *
37  * \brief QT-based widget for keyboard emulation (implementation)
38  */
39
40 #include "emulkbd.h"
41 #include "emul.h"
42
43 #include <QtGui/QPainter>
44 #include <QtGui/QPixmap>
45 #include <QtGui/QSizePolicy>
46 #include <QtGui/QLayout>
47 #include <QtGui/QKeyEvent>
48 #include <QtCore/QEvent>
49 #include <QtCore/QSize>
50 #include <QtCore/QRect>
51
52
53 EmulKey::EmulKey(EmulKbd *kbd, const char *label, int _keycode, int _row, int _col) :
54         QPushButton(label, kbd),
55         row(_row), col(_col),
56         keycode(_keycode)
57 {
58         // don't let the widget get focus
59         setFocusPolicy(Qt::NoFocus);
60
61         // unused
62         connect(this, SIGNAL(pressed()), this, SLOT(keyPressed()));
63         connect(this, SIGNAL(released()), this, SLOT(keyReleased()));
64 }
65
66
67 EmulKey::~EmulKey()
68 {
69         // nop
70 }
71
72
73 /**
74  * Override standad QButton behaviour: we must also emit the signals.
75  */
76 // unused
77 void EmulKey::setDown(bool enable)
78 {
79         // let our superclass do everything else
80         QPushButton::setDown(enable);
81
82         if (enable)
83                 emit pressed();
84         else
85                 emit released();
86 }
87
88
89 // unused
90 void EmulKey::keyPressed(void)
91 {
92         static_cast<EmulKbd *>(parent())->setKey(row, col, true);
93 }
94
95
96 // unused
97 void EmulKey::keyReleased(void)
98 {
99         static_cast<EmulKbd *>(parent())->setKey(row, col, false);
100 }
101
102
103 EmulKbd::EmulKbd(QWidget *parent, Qt::WFlags f) :
104         QFrame(parent, f),
105         layout(new QGridLayout(this)),
106         active_row(0)
107 {
108         setFrameStyle(QFrame::Box | QFrame::Sunken);
109         setLineWidth(1);
110         setFocusPolicy(Qt::StrongFocus);
111         frame_width = frameWidth();
112 }
113
114
115 EmulKbd::~EmulKbd()
116 {
117         delete layout;
118 }
119
120
121 QSizePolicy EmulKbd::sizePolicy() const
122 {
123         return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
124 }
125
126
127 void EmulKbd::resizeEvent(QResizeEvent *event)
128 {
129         // Let our superclass process the event first
130         QFrame::resizeEvent(event);
131 }
132
133
134 // handle key presses for all keys in keyboard
135 bool EmulKbd::event(QEvent *_e)
136 {
137         switch (_e->type())
138         {
139                 case QEvent::KeyPress:
140                 case QEvent::KeyRelease:
141                 {
142                         QKeyEvent *e = static_cast<QKeyEvent *>(_e);
143                         int keycode = e->key();
144                         EmulKey *key;
145
146                         // ignore repeated keys
147                         if (!e->isAutoRepeat())
148                         {
149                                 // scan all children
150                                 for (QObjectList::const_iterator it(children().begin()); it != children().end(); ++it)
151                                 {
152                                         // only keys, not other children!
153                                         if ((*it)->metaObject() == &EmulKey::staticMetaObject)
154                                         // if ((key = dynamic_cast<EmulKey *>(*it)))
155                                         {
156                                                 key = static_cast<EmulKey *>(*it);
157
158                                                 // same key?
159                                                 if (key->keycode == keycode)
160                                                 {
161                                                         // yes, tell key to go down (or up)
162                                                         key->setDown(_e->type() == QEvent::KeyPress);
163                                                         break;
164                                                 }
165                                         }
166                                 }
167                         }
168                         return true;
169                 }
170
171                 default:
172                         // let superclass process this event
173                         return QFrame::event(_e);
174
175         } // end switch(_e->type())
176 }
177
178
179 void EmulKbd::addKey(const char *label, int keycode, int row, int col, int matrix_row, int matrix_col)
180 {
181         if (matrix_row == -1)
182                 matrix_row = row;
183         if (matrix_col == -1)
184                 matrix_col = col;
185
186         layout->addWidget(new EmulKey(this, label, keycode, matrix_row, matrix_col), row, col);
187 }
188
189
190 // unused
191 void EmulKbd::setKey(int /*row*/, int /*col*/, bool /*on*/)
192 {
193 }
194
195
196 void EmulKbd::setRow(int r)
197 {
198         active_row = r;
199 }
200
201 int EmulKbd::readCols(void)
202 {
203         QLayoutItem *item;
204         EmulKey *key;
205         int cols = 0;
206
207         for (int i = 0; (item = layout->itemAt(i)); ++i)
208         {
209                 key = static_cast<EmulKey *>(item->widget());
210                 if (key->row == active_row)
211                 {
212                         if (key->isDown())
213                                 cols |= (1<<key->col);
214                 }
215         }
216         return cols;
217 }
218
219 extern "C" void emul_kbdSetRows(int r)
220 {
221         emul->emulKbd->setRow(r);
222 }
223
224
225 extern "C" int emul_kbdReadCols(void)
226 {
227         return emul->emulKbd->readCols();
228 }
229
230 #include "emulkbd_moc.cpp"