Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
62 andreas 1
/*
111 andreas 2
 * Copyright (C) 2021, 2022 by Andreas Theofilu <andreas@theosys.at>
62 andreas 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
 */
71 andreas 18
#include <QPushButton>
19
#include <QLabel>
264 andreas 20
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
181 andreas 21
#   include <QSound>
22
#else
23
#include <QMediaPlayer>
24
#include <QAudioOutput>
25
#endif
62 andreas 26
 
27
#include "tqkeypad.h"
28
#include "ui_keypad.h"
29
#include "terror.h"
63 andreas 30
#include "tresources.h"
71 andreas 31
#include "tconfig.h"
111 andreas 32
#include "tpagemanager.h"
62 andreas 33
 
111 andreas 34
extern TPageManager *gPageManager;              //!< The pointer to the global defined main class.
35
 
63 andreas 36
TQKeypad::TQKeypad(const std::string& init, const std::string& prompt, QWidget *parent, bool priv)
62 andreas 37
    : QDialog(parent),
71 andreas 38
      TSystemSound(TConfig::getSystemPath(TConfig::SOUNDS)),
62 andreas 39
      ui(new Ui::TQKeypad),
63 andreas 40
      mPrivate(priv),
62 andreas 41
      mText(init)
42
{
43
    DECL_TRACER("TQKeypad::TQKeypad(const std::string& init, const std::string& prompt, QWidget *parent)");
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_0, SIGNAL(pressed()), this, SLOT(key0()));
50
    connect(ui->key_1, SIGNAL(pressed()), this, SLOT(key1()));
51
    connect(ui->key_2, SIGNAL(pressed()), this, SLOT(key2()));
52
    connect(ui->key_3, SIGNAL(pressed()), this, SLOT(key3()));
53
    connect(ui->key_4, SIGNAL(pressed()), this, SLOT(key4()));
54
    connect(ui->key_5, SIGNAL(pressed()), this, SLOT(key5()));
55
    connect(ui->key_6, SIGNAL(pressed()), this, SLOT(key6()));
56
    connect(ui->key_7, SIGNAL(pressed()), this, SLOT(key7()));
57
    connect(ui->key_8, SIGNAL(pressed()), this, SLOT(key8()));
58
    connect(ui->key_9, SIGNAL(pressed()), this, SLOT(key9()));
59
    connect(ui->key_Plus, SIGNAL(pressed()), this, SLOT(keyPlus()));
60
    connect(ui->key_Minus, SIGNAL(pressed()), this, SLOT(keyMinus()));
61
    connect(ui->key_Clear, SIGNAL(pressed()), this, SLOT(keyClear()));
62
    connect(ui->key_Dot, SIGNAL(pressed()), this, SLOT(keyDot()));
63
    connect(ui->key_DoubleDot, SIGNAL(pressed()), this, SLOT(keyDoubleDot()));
64
    connect(ui->key_Komma, SIGNAL(pressed()), this, SLOT(keyKomma()));
65
 
66
    ui->label_Prompt->setText(prompt.c_str());
63 andreas 67
 
68
    if (!mPrivate)
69
        ui->label_TextLine->setText(init.c_str());
70
    else
71
        ui->label_TextLine->setText(fillString('*', mText.length()).c_str());
65 andreas 72
 
73
    MSG_DEBUG("Dialog was initialized.");
62 andreas 74
}
75
 
76
TQKeypad::~TQKeypad()
77
{
78
    DECL_TRACER("TQKeypad::~TQKeypad()");
79
    delete ui;
80
}
81
 
82
void TQKeypad::doResize()
83
{
84
    DECL_TRACER("TQKeypad::doResize()");
85
 
243 andreas 86
#ifndef Q_OS_IOS
65 andreas 87
    QRect rect = this->geometry();
243 andreas 88
#if defined(Q_OS_ANDROID)
62 andreas 89
    QSize size = this->size();
90
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
91
    this->resize(size);
92
    this->move(scale(rect.left()), scale(rect.top()));
243 andreas 93
#endif  // Q_OS_ANDROID
62 andreas 94
    QWidget *parent = this->parentWidget();
95
 
96
    if (parent)
97
    {
98
        rect = parent->geometry();
99
        this->move(rect.center() - this->rect().center());
100
    }
101
 
243 andreas 102
#if defined(Q_OS_ANDROID)
62 andreas 103
    // Iterate through childs and resize them
104
    QObjectList childs = children();
105
    QList<QObject *>::Iterator iter;
106
 
107
    for (iter = childs.begin(); iter != childs.end(); ++iter)
108
    {
182 andreas 109
        QObject *obj = *iter;
110
        QString name = obj->objectName();
62 andreas 111
 
112
        if (name.startsWith("key_"))
113
        {
114
            QPushButton *bt = dynamic_cast<QPushButton *>(obj);
115
            size = bt->size();
116
            size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
117
            bt->resize(size);
118
            rect = bt->geometry();
119
            bt->move(scale(rect.left()), scale(rect.top()));
120
        }
121
        else    // It's a label
122
        {
123
            QLabel *lb = dynamic_cast<QLabel *>(obj);
124
            size = lb->size();
125
            size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
126
            lb->resize(size);
127
            rect = lb->geometry();
128
            lb->move(scale(rect.left()), scale(rect.top()));
129
        }
130
    }
243 andreas 131
#endif  // Q_OS_ANDROID
132
#endif  // ! Q_OS_IOS
62 andreas 133
}
134
 
135
void TQKeypad::setKey(Ui::KEYSP_t key)
136
{
71 andreas 137
    DECL_TRACER("TQKeypad::setKey(Ui::KEYSP_t key)");
138
 
62 andreas 139
    switch(key)
140
    {
141
        case Ui::KEYP_0:        mText += "0"; break;
142
        case Ui::KEYP_1:        mText += "1"; break;
143
        case Ui::KEYP_2:        mText += "2"; break;
144
        case Ui::KEYP_3:        mText += "3"; break;
145
        case Ui::KEYP_4:        mText += "4"; break;
146
        case Ui::KEYP_5:        mText += "5"; break;
147
        case Ui::KEYP_6:        mText += "6"; break;
148
        case Ui::KEYP_7:        mText += "7"; break;
149
        case Ui::KEYP_8:        mText += "8"; break;
150
        case Ui::KEYP_9:        mText += "9"; break;
151
        case Ui::KEYP_Plus:     mText += "+"; break;
152
        case Ui::KEYP_Minus:    mText += "-"; break;
153
        case Ui::KEYP_Komma:    mText += ","; break;
154
        case Ui::KEYP_Dot:      mText += "."; break;
155
        case Ui::KEYP_DoubleDot:mText += ":"; break;
156
        case Ui::KEYP_Clear:    mText.clear(); break;
157
    }
158
 
111 andreas 159
    if (mMaxLen > 0 && mText.length() > (size_t)mMaxLen)
160
        mText = mText.substr(0, mMaxLen);
161
 
63 andreas 162
    if (!mPrivate)
163
        ui->label_TextLine->setText(mText.c_str());
164
    else
165
        ui->label_TextLine->setText(fillString('*', mText.length()).c_str());
71 andreas 166
 
167
    if (getSystemSoundState())
168
    {
169
        std::string snd = getTouchFeedbackSound();
170
        MSG_DEBUG("Playing sound: " << snd);
264 andreas 171
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
181 andreas 172
        QMediaPlayer *player = new QMediaPlayer;
173
        QAudioOutput *audioOutput = new QAudioOutput;
174
        player->setAudioOutput(audioOutput);
175
        player->setSource(QUrl::fromLocalFile(snd.c_str()));
176
        player->play();
177
        delete audioOutput;
178
        delete player;
179
#else
71 andreas 180
        QSound::play(snd.c_str());
181 andreas 181
#endif
71 andreas 182
    }
111 andreas 183
 
184
    if (gPageManager && gPageManager->getPassThrough() && !mText.empty() &&
185
        key != Ui::KEYP_Clear)
186
    {
187
        size_t pos = mText.length() - 1;
188
        gPageManager->sendKeyStroke(mText[pos]);
189
    }
62 andreas 190
}
191
 
111 andreas 192
void TQKeypad::setString(const std::string& str)
193
{
194
    DECL_TRACER("TQKeypad::setString(const string& str)");
195
 
196
    mText += str;
197
 
198
    if (mMaxLen > 0 && mText.length() > (size_t)mMaxLen)
199
        mText = mText.substr(0, mMaxLen);
200
 
201
    if (!mPrivate)
202
        ui->label_TextLine->setText(mText.c_str());
203
    else
204
        ui->label_TextLine->setText(fillString('*', mText.length()).c_str());
205
}
206
 
62 andreas 207
int TQKeypad::scale(int value)
208
{
209
    if (value <= 0 || mScaleFactor == 1.0)
210
        return value;
211
 
212
    return (int)((double)value * mScaleFactor);
213
}