File: numpad.cpp

package info (click to toggle)
tipp10 3.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,984 kB
  • sloc: cpp: 8,343; xml: 70; ansic: 60; makefile: 11
file content (246 lines) | stat: -rw-r--r-- 6,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (c) 2006-2009, Tom Thielicke IT Solutions

SPDX-License-Identifier: GPL-2.0-only
*/

/****************************************************************
**
** Implementation of the KeyBoard class
** File name: numpad.cpp
**
****************************************************************/

#include <QCoreApplication>
#include <QPainter>
#include <QPen>
#include <QSettings>

#include "../def/defines.h"
#include "errormessage.h"
#include "numpad.h"

NumPad::NumPad(QWidget* parent)
    : QWidget(parent)
    , isStarted(false)
    , isPaused(false)
    , keyLayoutFound(false) // Flag whether the key properties where found
    , activeKey('0')
    , activeX(0)
    , activeY(0)
    , activeColor(0)
    , activeForm(0)
    , activeFinger(8)
{
    // Read settings (keyboard layout and key coloring)
    readSettings();

    // Load all images used in the keyboard
    loadKeyImages();

    colorIndex[0] = 4; // yellow
    colorIndex[1] = 3; // green
    colorIndex[2] = 2; // blue
    colorIndex[3] = 1; // red
    colorIndex[4] = 0; // grey (no additional key)

    keyColors[0] = QColor(253, 232, 151);
    keyColors[1] = QColor(181, 235, 157);
    keyColors[2] = QColor(172, 215, 254);
    keyColors[3] = QColor(249, 174, 174);

    currentPen.setWidth(3);
    currentPen.setStyle(Qt::DotLine);

    // Sql class to get the key layout of a given char
    keyboardSql = new KeyboardSql(layout);
    // Fix the size of this class because of using fix sized images
    setFixedSize(180, 228);
    setAttribute(Qt::WA_NoSystemBackground);
}

void NumPad::startBoard()
{
    // Turn start flag true
    isStarted = true;
    // Turn pause flag false
    isPaused = false;
    // Call the paint event (refresh the keyboard)
    repaint();
}

void NumPad::pauseBoard()
{
    // Turn start flag false
    isStarted = false;
    // Turn pause flag true
    isPaused = true;
    // Call the paint event (refresh the keyboard)
    update();
}

void NumPad::stopBoard()
{
    // Turn start flag false
    isStarted = false;
    // Call the paint event (refresh the keyboard)
    update();
}

void NumPad::paintEvent([[maybe_unused]] QPaintEvent* event)
{
    // Draw empty grey keyboard
    cleanKeyboard();

    if (isStarted) {
        // Color current key and modifier
        colorKey();
    }
    // Draw keyboard labeling
    drawLayout();
}

void NumPad::loadKeyImages()
{
    // Load background
    if (!loadImage(background, ":/img/numpadbg.png")) {
        return;
    }
    // Keys
    // -> four different key colors
    for (int x = 0; x < 5; x++) {
        // -> six different key forms
        for (int y = 0; y < 10; y++) {
            // Load key
            if (!loadImage(keyImage[x][y],
                    ":/img/key" + QString::number(x) + "_" + QString::number(y)
                        + ".png")) {
                return;
            }
        }
    }
    // Load key labeling
    if (!loadImage(keyLayout,
            ":/img/" + layout.left(2) + "_numpad_" + layout.right(3)
                + ".png")) {

        return;
    }
}

bool NumPad::loadImage(QPixmap& img, QString filename)
{
    // Check whether loading the image is correct
    if (!img.load(filename)) {
        // Error message
        ErrorMessage* errorMessage = new ErrorMessage(this);
        errorMessage->showMessage(Error::key_pic, ErrorMessage::Type::Warning,
            ErrorMessage::Cancel::Operation, filename);
        return false;
    }
    return true;
}

void NumPad::cleanKeyboard()
{
    QPainter painter(this);
    // Background image
    painter.drawPixmap(0, 0, background);

    // Keys first row
    for (int i = 0; i < 4; i++) {
        painter.drawPixmap(10 + (40 * i), 15, keyImage[0][0]);
    }

    // Keys second row
    for (int i = 0; i < 3; i++) {
        painter.drawPixmap(10 + (40 * i), 55, keyImage[0][0]);
    }
    if (!isMac) {
        painter.drawPixmap(130, 55, keyImage[0][9]);
    } else {
        painter.drawPixmap(130, 55, keyImage[0][0]);
    }

    // Keys third row
    for (int i = 0; i < 3; i++) {
        painter.drawPixmap(10 + (40 * i), 95, keyImage[0][0]);
    }
    if (layout.right(3) == "mac") {
        painter.drawPixmap(130, 95, keyImage[0][0]);
    }

    // Keys fourth row
    for (int i = 0; i < 3; i++) {
        painter.drawPixmap(10 + (40 * i), 135, keyImage[0][0]);
    }
    painter.drawPixmap(130, 135, keyImage[0][9]);

    // Keys fifth row
    painter.drawPixmap(10, 175, keyImage[0][7]);
    painter.drawPixmap(90, 175, keyImage[0][0]);
}

void NumPad::drawLayout()
{
    QPainter painter(this);
    // Draw the labeling of the keyboard
    painter.drawPixmap(10, 15, keyLayout);
}

// Places the current colored keys
void NumPad::colorKey()
{
    QPainter painter(this);

    if (keyLayoutFound) {

        if (keyColoring) {
            // Color current key
            painter.drawPixmap(activeX, activeY,
                keyImage[colorIndex[4 - activeColor]][activeForm]);
        }
    }
}

void NumPad::setKey(QChar key)
{
    QString statusText = "0";

    // Check whether key is already colored and whether keyboard is started
    if (activeKey != key && isStarted) {
        activeFinger = 8;
        // New current key
        activeKey = key;
        // Get the properties of the key
        // If false, no properties were found
        keyLayoutFound = keyboardSql->getNumLayout(activeKey, &activeX,
            &activeY, &activeColor, &activeForm, &activeFinger, &statusText);
        // Emit the help text of current key(s)
        emit statusRefreshed(statusText);
        // Call the paint event (refresh the keyboard)
        if (keyColoring) {
            repaint();
        }
    }
}

void NumPad::readSettings()
{
#if APP_PORTABLE
    QSettings settings(
        QCoreApplication::applicationDirPath() + "/portable/settings.ini",
        QSettings::IniFormat);
#else
    QSettings settings;
#endif
    // Check user's keyboard layout
    settings.beginGroup("main");
    layout = settings.value("language_layout", t10::app_std_language_layout)
                 .toString();
    settings.endGroup();
    // Check if key coloring is turned on
    settings.beginGroup("support");
    keyColoring = settings.value("check_selection", true).toBool();
    settings.endGroup();
}