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