2 Source : inspired by https://www.kdab.com/qt-input-method-virtual-keyboard/
10 #include <QSignalMapper>
11 #include <QPushButton>
12 #include <QImageReader>
14 #define NEXT_ROW_MARKER '\0'
16 #define SIZE_FACTOR 1 /* looks nice on AGL demo */
17 //#define SIZE_FACTOR 0.5 /* looks nice on PC build */
19 #define OFFSET_H ( 78 * SIZE_FACTOR )
20 #define KEY_WIDTH ( 78 * SIZE_FACTOR )
21 #define KEY_HEIGHT ( 94 * SIZE_FACTOR )
22 #define MARGIN_H ( 12 * SIZE_FACTOR )
23 #define MARGIN_V ( 18 * SIZE_FACTOR )
24 #define SPACE_BAR_WIDTH ( 502 * SIZE_FACTOR )
26 struct KeyboardLayoutEntry
32 static KeyboardLayoutEntry keyboardLayout[] = {
43 { NEXT_ROW_MARKER, NULL },
54 { NEXT_ROW_MARKER, NULL },
64 { NEXT_ROW_MARKER, NULL },
72 { '!' , "exclam.png" },
74 { NEXT_ROW_MARKER, NULL },
75 { '\'',"apostrophe.png" },
77 { ' ' , "space.png" },
78 { '-' , "minus.png" },
82 const static int layoutSize = (sizeof(keyboardLayout) /
83 sizeof(KeyboardLayoutEntry));
85 Keyboard::Keyboard(QRect r, QWidget *parent):QWidget(parent),background(parent),
86 rect(QRect(r.x() + (r.width()-(r.width()*SIZE_FACTOR))/2, r.y(), r.width()*SIZE_FACTOR, r.height()*SIZE_FACTOR)),
87 mapper(new QSignalMapper(this))
90 for (int i = 0; i < layoutSize; ++i)
91 if (keyboardLayout[i].key == NEXT_ROW_MARKER)
94 connect(mapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
97 int offset_h = KEY_WIDTH;
98 int offset_v = (rect.height() - (KEY_HEIGHT*nbRowMarkers + MARGIN_V*(nbRowMarkers-1))) / 2;
100 background.setGeometry(rect);
101 QImageReader reader(QString(":/images/background.png"));
102 background.setPixmap(QPixmap::fromImage(reader.read()).scaled(rect.width(), rect.height(), Qt::IgnoreAspectRatio));
106 for (int i = 0; i < layoutSize; ++i)
108 int key_width = KEY_WIDTH;
109 if (keyboardLayout[i].key == NEXT_ROW_MARKER)
114 offset_h += (KEY_WIDTH + MARGIN_H)/2;
116 offset_h += (KEY_WIDTH + MARGIN_H);
117 offset_v += (KEY_HEIGHT+MARGIN_V);
120 else if (keyboardLayout[i].key == ' ')
122 key_width = SPACE_BAR_WIDTH;
125 QPushButton *button = new QPushButton(QIcon(tr(":/images/")+QString(keyboardLayout[i].image)), tr(""), &background);
126 button->setMinimumSize(QSize(key_width, KEY_HEIGHT));
127 button->setMaximumSize(QSize(key_width, KEY_HEIGHT));
128 button->setIconSize(button->size());
129 /* geometry of button is relative to its parent, ie 'background' : */
130 button->setGeometry(QRect(offset_h, offset_v, button->width(), button->height()));
133 mapper->setMapping(button, keyboardLayout[i].key);
134 connect(button, SIGNAL(clicked()), mapper, SLOT(map()));
136 offset_h += (key_width+MARGIN_H);
140 void Keyboard::buttonClicked(int key)
142 if (key == '\b') /* backspace */
143 emit specialKeyClicked(key);
145 emit keyClicked(QString(key));
148 Keyboard::~Keyboard()