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