Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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