Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2021, 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include <QPushButton>
20
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21
#include <QSound>
22
#else
23
#include <QMediaPlayer>
24
#include <QAudioOutput>
25
#endif
26
 
27
#include "tqkeyboard.h"
28
#include "ui_keyboard.h"
29
#include "terror.h"
30
#include "tresources.h"
31
#include "tconfig.h"
32
#include "tpagemanager.h"
33
 
34
extern TPageManager *gPageManager;              //!< The pointer to the global defined main class.
35
 
36
TQKeyboard::TQKeyboard(const std::string& init, const std::string& prompt, QWidget *parent, bool priv)
37
    : QDialog(parent),
38
      TSystemSound(TConfig::getSystemPath(TConfig::SOUNDS)),
39
      ui(new Ui::TQKeyboard),
40
      mPrivate(priv),
41
      mText(init)
42
{
43
    DECL_TRACER("TQKeyboard::TQKeyboard(const std::string& init, const std::string& prompt, QWidget *parent, bool priv)");
44
 
45
    ui->setupUi(this);
46
    connect(ui->key_Enter, SIGNAL(pressed()), this, SLOT(accept()));
47
    connect(ui->key_Cancel, SIGNAL(pressed()), this, SLOT(reject()));
48
 
49
    connect(ui->key_A, SIGNAL(pressed()), this, SLOT(keyA()));
50
    connect(ui->key_B, SIGNAL(pressed()), this, SLOT(keyB()));
51
    connect(ui->key_C, SIGNAL(pressed()), this, SLOT(keyC()));
52
    connect(ui->key_D, SIGNAL(pressed()), this, SLOT(keyD()));
53
    connect(ui->key_E, SIGNAL(pressed()), this, SLOT(keyE()));
54
    connect(ui->key_F, SIGNAL(pressed()), this, SLOT(keyF()));
55
    connect(ui->key_G, SIGNAL(pressed()), this, SLOT(keyG()));
56
    connect(ui->key_H, SIGNAL(pressed()), this, SLOT(keyH()));
57
    connect(ui->key_I, SIGNAL(pressed()), this, SLOT(keyI()));
58
    connect(ui->key_J, SIGNAL(pressed()), this, SLOT(keyJ()));
59
    connect(ui->key_K, SIGNAL(pressed()), this, SLOT(keyK()));
60
    connect(ui->key_L, SIGNAL(pressed()), this, SLOT(keyL()));
61
    connect(ui->key_M, SIGNAL(pressed()), this, SLOT(keyM()));
62
    connect(ui->key_N, SIGNAL(pressed()), this, SLOT(keyN()));
63
    connect(ui->key_O, SIGNAL(pressed()), this, SLOT(keyO()));
64
    connect(ui->key_P, SIGNAL(pressed()), this, SLOT(keyP()));
65
    connect(ui->key_Q, SIGNAL(pressed()), this, SLOT(keyQ()));
66
    connect(ui->key_R, SIGNAL(pressed()), this, SLOT(keyR()));
67
    connect(ui->key_S, SIGNAL(pressed()), this, SLOT(keyS()));
68
    connect(ui->key_T, SIGNAL(pressed()), this, SLOT(keyT()));
69
    connect(ui->key_U, SIGNAL(pressed()), this, SLOT(keyU()));
70
    connect(ui->key_V, SIGNAL(pressed()), this, SLOT(keyV()));
71
    connect(ui->key_W, SIGNAL(pressed()), this, SLOT(keyW()));
72
    connect(ui->key_X, SIGNAL(pressed()), this, SLOT(keyX()));
73
    connect(ui->key_Y, SIGNAL(pressed()), this, SLOT(keyY()));
74
    connect(ui->key_Z, SIGNAL(pressed()), this, SLOT(keyZ()));
75
    connect(ui->key_AE, SIGNAL(pressed()), this, SLOT(keyAE()));
76
    connect(ui->key_OE, SIGNAL(pressed()), this, SLOT(keyOE()));
77
    connect(ui->key_UE, SIGNAL(pressed()), this, SLOT(keyUE()));
78
    connect(ui->key_SS, SIGNAL(pressed()), this, SLOT(keySS()));
79
    connect(ui->key_Caret, SIGNAL(pressed()), this, SLOT(keyCaret()));
80
    connect(ui->key_SQ, SIGNAL(pressed()), this, SLOT(keySQ()));
81
    connect(ui->key_Backspace, SIGNAL(pressed()), this, SLOT(keyBackspace()));
82
    connect(ui->key_Plus, SIGNAL(pressed()), this, SLOT(keyPlus()));
83
    connect(ui->key_Hash, SIGNAL(pressed()), this, SLOT(keyHash()));
84
    connect(ui->key_Komma, SIGNAL(pressed()), this, SLOT(keyKomma()));
85
    connect(ui->key_Dot, SIGNAL(pressed()), this, SLOT(keyDot()));
86
    connect(ui->key_Dash, SIGNAL(pressed()), this, SLOT(keyDash()));
87
    connect(ui->key_GtLt, SIGNAL(pressed()), this, SLOT(keyGtLt()));
88
    connect(ui->key_Tab, SIGNAL(pressed()), this, SLOT(keyTab()));
89
    connect(ui->key_Caps, SIGNAL(pressed()), this, SLOT(keyCaps()));
90
    connect(ui->key_Shift, SIGNAL(pressed()), this, SLOT(keyShift()));
91
    connect(ui->key_Blank, SIGNAL(pressed()), this, SLOT(keyBlank()));
92
    connect(ui->key_Clear, SIGNAL(pressed()), this, SLOT(keyClear()));
93
    connect(ui->key_AltGR, SIGNAL(pressed()), this, SLOT(keyAltGr()));
94
    connect(ui->key_1, SIGNAL(pressed()), this, SLOT(key1()));
95
    connect(ui->key_2, SIGNAL(pressed()), this, SLOT(key2()));
96
    connect(ui->key_3, SIGNAL(pressed()), this, SLOT(key3()));
97
    connect(ui->key_4, SIGNAL(pressed()), this, SLOT(key4()));
98
    connect(ui->key_5, SIGNAL(pressed()), this, SLOT(key5()));
99
    connect(ui->key_6, SIGNAL(pressed()), this, SLOT(key6()));
100
    connect(ui->key_7, SIGNAL(pressed()), this, SLOT(key7()));
101
    connect(ui->key_8, SIGNAL(pressed()), this, SLOT(key8()));
102
    connect(ui->key_9, SIGNAL(pressed()), this, SLOT(key9()));
103
    connect(ui->key_0, SIGNAL(pressed()), this, SLOT(key0()));
104
 
105
    ui->label_Prompt->setText(prompt.c_str());
106
 
107
    if (!mPrivate)
108
        ui->label_TextLine->setText(init.c_str());
109
    else
110
        ui->label_TextLine->setText(fillString('*', (int)mText.length()).c_str());
111
}
112
 
113
TQKeyboard::~TQKeyboard()
114
{
115
    DECL_TRACER("TQKeyboard::~TQKeyboard()");
116
    delete ui;
117
}
118
 
119
void TQKeyboard::doResize()
120
{
121
    DECL_TRACER("TQKeyboard::doResize()");
122
 
123
    QRect rect = this->geometry();
124
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
125
    QSize size = this->size();
126
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
127
    this->resize(size);
128
#endif
129
    QWidget *parent = this->parentWidget();
130
 
131
    if (parent)     // Move window to lower left corner
132
    {
133
        rect = parent->geometry();
134
        this->move(0, rect.height() - this->geometry().height());
135
    }
136
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
137
    // Iterate through childs and resize them
138
    QObjectList childs = children();
139
    QList<QObject *>::Iterator iter;
140
 
141
    for (iter = childs.begin(); iter != childs.end(); ++iter)
142
    {
143
        QObject *obj = *iter;
144
        QString name = obj->objectName();
145
 
146
        if (name.startsWith("key_"))
147
        {
148
            QPushButton *bt = dynamic_cast<QPushButton *>(obj);
149
            size = bt->size();
150
            size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
151
            bt->resize(size);
152
            rect = bt->geometry();
153
            bt->move(scale(rect.left()), scale(rect.top()));
154
        }
155
        else    // It's a label
156
        {
157
            QLabel *lb = dynamic_cast<QLabel *>(obj);
158
            size = lb->size();
159
            size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
160
            lb->resize(size);
161
            rect = lb->geometry();
162
            lb->move(scale(rect.left()), scale(rect.top()));
163
        }
164
    }
165
#endif
166
}
167
 
168
void TQKeyboard::setKey(Ui::KEYS_t key)
169
{
170
    DECL_TRACER("TQKeyboard::setKey(Ui::KEYS_t key)");
171
 
172
    switch(key)
173
    {
174
        case Ui::KEY_0:         mText += ((mShift || mCaps) ? "=" : (mGr ? "}" : "0")); break;
175
        case Ui::KEY_1:         mText += ((mShift || mCaps) ? "!" : "1"); break;
176
        case Ui::KEY_2:         mText += ((mShift || mCaps) ? "\"" : (mGr ? "²" : "2")); break;
177
        case Ui::KEY_3:         mText += ((mShift || mCaps) ? "§" : (mGr ? "³" : "3")); break;
178
        case Ui::KEY_4:         mText += ((mShift || mCaps) ? "$" : "4"); break;
179
        case Ui::KEY_5:         mText += ((mShift || mCaps) ? "%" : "5"); break;
180
        case Ui::KEY_6:         mText += ((mShift || mCaps) ? "&" : "6"); break;
181
        case Ui::KEY_7:         mText += ((mShift || mCaps) ? "/" : (mGr ? "{" : "7")); break;
182
        case Ui::KEY_8:         mText += ((mShift || mCaps) ? "(" : (mGr ? "[" : "8")); break;
183
        case Ui::KEY_9:         mText += ((mShift || mCaps) ? ")" : (mGr ? "]" : "9")); break;
184
        case Ui::KEY_A:         mText += ((mShift || mCaps) ? "A" : "a"); break;
185
        case Ui::KEY_B:         mText += ((mShift || mCaps) ? "B" : "b"); break;
186
        case Ui::KEY_C:         mText += ((mShift || mCaps) ? "C" : "c"); break;
187
        case Ui::KEY_D:         mText += ((mShift || mCaps) ? "D" : "d"); break;
188
        case Ui::KEY_E:         mText += ((mShift || mCaps) ? "E" : (mGr ? "€" : "e")); break;
189
        case Ui::KEY_F:         mText += ((mShift || mCaps) ? "F" : "f"); break;
190
        case Ui::KEY_G:         mText += ((mShift || mCaps) ? "G" : "g"); break;
191
        case Ui::KEY_H:         mText += ((mShift || mCaps) ? "H" : "h"); break;
192
        case Ui::KEY_I:         mText += ((mShift || mCaps) ? "I" : "i"); break;
193
        case Ui::KEY_J:         mText += ((mShift || mCaps) ? "J" : "j"); break;
194
        case Ui::KEY_K:         mText += ((mShift || mCaps) ? "K" : "k"); break;
195
        case Ui::KEY_L:         mText += ((mShift || mCaps) ? "L" : "l"); break;
196
        case Ui::KEY_M:         mText += ((mShift || mCaps) ? "M" : (mGr ? "µ" : "m")); break;
197
        case Ui::KEY_N:         mText += ((mShift || mCaps) ? "N" : "n"); break;
198
        case Ui::KEY_O:         mText += ((mShift || mCaps) ? "O" : "o"); break;
199
        case Ui::KEY_P:         mText += ((mShift || mCaps) ? "P" : "p"); break;
200
        case Ui::KEY_Q:         mText += ((mShift || mCaps) ? "Q" : (mGr ? "@" : "q")); break;
201
        case Ui::KEY_R:         mText += ((mShift || mCaps) ? "R" : "r"); break;
202
        case Ui::KEY_S:         mText += ((mShift || mCaps) ? "S" : "s"); break;
203
        case Ui::KEY_T:         mText += ((mShift || mCaps) ? "T" : "t"); break;
204
        case Ui::KEY_U:         mText += ((mShift || mCaps) ? "U" : "u"); break;
205
        case Ui::KEY_V:         mText += ((mShift || mCaps) ? "V" : "v"); break;
206
        case Ui::KEY_W:         mText += ((mShift || mCaps) ? "W" : "w"); break;
207
        case Ui::KEY_X:         mText += ((mShift || mCaps) ? "X" : "x"); break;
208
        case Ui::KEY_Y:         mText += ((mShift || mCaps) ? "Y" : "y"); break;
209
        case Ui::KEY_Z:         mText += ((mShift || mCaps) ? "Z" : "z"); break;
210
        case Ui::KEY_AE:        mText += ((mShift || mCaps) ? "Ä" : "ä"); break;
211
        case Ui::KEY_OE:        mText += ((mShift || mCaps) ? "Ö" : "ö"); break;
212
        case Ui::KEY_UE:        mText += ((mShift || mCaps) ? "Ü" : "ü"); break;
213
        case Ui::KEY_SS:        mText += ((mShift || mCaps) ? "?" : (mGr ? "\\" : "ß")); break;
214
        case Ui::KEY_SQ:        mText += ((mShift || mCaps) ? "`" : "´"); break;
215
        case Ui::KEY_Caret:     mText += ((mShift || mCaps) ? "°" : "^"); break;
216
        case Ui::KEY_Clear:     mText.clear(); break;
217
        case Ui::KEY_Backspace: mText = mText.substr(0, mText.length() -1); break;
218
        case Ui::KEY_Plus:      mText += ((mShift || mCaps) ? "*" : (mGr ? "~" : "+")); break;
219
        case Ui::KEY_Hash:      mText += ((mShift || mCaps) ? "'" : "#"); break;
220
        case Ui::KEY_Komma:     mText += ((mShift || mCaps) ? ";" : ","); break;
221
        case Ui::KEY_Dot:       mText += ((mShift || mCaps) ? ":" : "."); break;
222
        case Ui::KEY_Dash:      mText += ((mShift || mCaps) ? "_" : "-"); break;
223
        case Ui::KEY_GtLt:      mText += ((mShift || mCaps) ? ">" : (mGr ? "|" : "<")); break;
224
        case Ui::KEY_Tab:       mText += "\t"; break;
225
        case Ui::KEY_Caps:      mCaps = !mCaps; break;
226
        case Ui::KEY_Shift:     mShift = true; break;
227
        case Ui::KEY_AltGR:     mGr = true; break;
228
        case Ui::KEY_Blank:     mText += " "; break;
229
    }
230
 
231
    if (key != Ui::KEY_Shift)
232
        mShift = false;
233
 
234
    if (key != Ui::KEY_AltGR)
235
        mGr = false;
236
 
237
    if (mMaxLen > 0 && mText.length() > (size_t)mMaxLen)
238
        mText = mText.substr(0, mMaxLen);
239
 
240
    if (!mPrivate)
241
        ui->label_TextLine->setText(mText.c_str());
242
    else
243
        ui->label_TextLine->setText(fillString('*', (int)mText.length()).c_str());
244
 
245
    if (getSystemSoundState())
246
    {
247
        std::string snd = getTouchFeedbackSound();
248
        MSG_DEBUG("Playing sound: " << snd);
249
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
250
        QMediaPlayer *player = new QMediaPlayer;
251
        QAudioOutput *audioOutput = new QAudioOutput;
252
        player->setAudioOutput(audioOutput);
253
        // ...
254
        player->setSource(QUrl::fromLocalFile(snd.c_str()));
255
        player->play();
256
        delete player;
257
        delete audioOutput;
258
#else
259
        QSound::play(snd.c_str());
260
#endif
261
    }
262
 
263
    if (gPageManager && gPageManager->getPassThrough() && !mText.empty() &&
264
        key != Ui::KEY_Backspace && key != Ui::KEY_Clear &&
265
        key != Ui::KEY_Shift && key != Ui::KEY_AltGR && key != Ui::KEY_Caps)
266
    {
267
        size_t pos = mText.length() - 1;
268
        gPageManager->sendKeyStroke(mText[pos]);
269
    }
270
}
271
 
272
void TQKeyboard::setString(const std::string& str)
273
{
274
    DECL_TRACER("TQKeyboard::setString(const string& str)");
275
 
276
    mText += str;
277
 
278
    if (mMaxLen > 0 && mText.length() > (size_t)mMaxLen)
279
        mText = mText.substr(0, mMaxLen);
280
 
281
    if (!mPrivate)
282
        ui->label_TextLine->setText(mText.c_str());
283
    else
284
        ui->label_TextLine->setText(fillString('*', (int)mText.length()).c_str());
285
}
286
 
287
int TQKeyboard::scale(int value)
288
{
289
    if (value <= 0 || mScaleFactor == 1.0)
290
        return value;
291
 
292
    return (int)((double)value * mScaleFactor);
293
}