Delete COPYING as LICENSE is to be used
[apps/poi-yelp.git] / Keyboard.cpp
1 /**
2   Source : inspired by https://www.kdab.com/qt-input-method-virtual-keyboard/
3   */
4
5 #include "Keyboard.h"
6
7 #include <QWidget>
8 #include <QRect>
9 #include <QString>
10 #include <QSignalMapper>
11 #include <QPushButton>
12 #include <QImageReader>
13
14 #define NEXT_ROW_MARKER '\0'
15
16 #define SIZE_FACTOR 1       /* looks nice on AGL demo */
17 //#define SIZE_FACTOR 0.5   /* looks nice on PC build */
18
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 )
25
26 struct KeyboardLayoutEntry
27 {
28     int key;
29     const char *image;
30 };
31
32 static KeyboardLayoutEntry keyboardLayout[] = {
33     { '1' , "1.png" },
34     { '2' , "2.png" },
35     { '3' , "3.png" },
36     { '4' , "4.png" },
37     { '5' , "5.png" },
38     { '6' , "6.png" },
39     { '7' , "7.png" },
40     { '8' , "8.png" },
41     { '9' , "9.png" },
42     { '0' , "0.png" },
43     { NEXT_ROW_MARKER, NULL },
44     { 'q' , "Q.png" },
45     { 'w' , "W.png" },
46     { 'e' , "E.png" },
47     { 'r' , "R.png" },
48     { 't' , "T.png" },
49     { 'y' , "Y.png" },
50     { 'u' , "U.png" },
51     { 'i' , "I.png" },
52     { 'o' , "O.png" },
53     { 'p' , "P.png" },
54     { NEXT_ROW_MARKER, NULL },
55     { 'a' , "A.png" },
56     { 's' , "S.png" },
57     { 'd' , "D.png" },
58     { 'f' , "F.png" },
59     { 'g' , "G.png" },
60     { 'h' , "H.png" },
61     { 'j' , "J.png" },
62     { 'k' , "K.png" },
63     { 'l' , "L.png" },
64     { NEXT_ROW_MARKER, NULL },
65     { 'z' , "Z.png" },
66     { 'x' , "X.png" },
67     { 'c' , "C.png" },
68     { 'v' , "V.png" },
69     { 'b' , "B.png" },
70     { 'n' , "N.png" },
71     { 'm' , "M.png" },
72     { '!' , "exclam.png" },
73     { '\b',"back.png" },
74     { NEXT_ROW_MARKER, NULL },
75     { '\'',"apostrophe.png" },
76     { '&' , "et.png" },
77     { ' ' , "space.png" },
78     { '-' , "minus.png" },
79     { '/' , "slash.png" }
80 };
81
82 const static int layoutSize = (sizeof(keyboardLayout) /
83                                sizeof(KeyboardLayoutEntry));
84
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))
88 {
89     int nbRowMarkers = 1;
90     for (int i = 0; i < layoutSize; ++i)
91         if (keyboardLayout[i].key == NEXT_ROW_MARKER)
92             nbRowMarkers++;
93
94     connect(mapper, SIGNAL(mapped(int)), SLOT(buttonClicked(int)));
95
96     int row = 0;
97     int offset_h = KEY_WIDTH;
98     int offset_v = (rect.height() - (KEY_HEIGHT*nbRowMarkers + MARGIN_V*(nbRowMarkers-1))) / 2;
99
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));
103
104     background.show();
105
106     for (int i = 0; i < layoutSize; ++i)
107     {
108         int key_width = KEY_WIDTH;
109         if (keyboardLayout[i].key == NEXT_ROW_MARKER)
110         {
111             row++;
112             offset_h = OFFSET_H;
113             if (row == 2)
114                 offset_h += (KEY_WIDTH + MARGIN_H)/2;
115             else if (row == 3)
116                 offset_h += (KEY_WIDTH + MARGIN_H);
117             offset_v += (KEY_HEIGHT+MARGIN_V);
118             continue;
119         }
120         else if (keyboardLayout[i].key ==  ' ')
121         {
122             key_width = SPACE_BAR_WIDTH;
123         }
124
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()));
131         button->show();
132
133         mapper->setMapping(button, keyboardLayout[i].key);
134         connect(button, SIGNAL(clicked()), mapper, SLOT(map()));
135
136         offset_h += (key_width+MARGIN_H);
137     }
138 }
139
140 void Keyboard::buttonClicked(int key)
141 {
142     if (key == '\b') /* backspace */
143         emit specialKeyClicked(key);
144     else
145         emit keyClicked(QString(key));
146 }
147
148 Keyboard::~Keyboard()
149 {
150     disconnect();
151     delete mapper;
152 }