Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
396 andreas 1
/*
2
 * Copyright (C) 2023 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
#include <QToolButton>
21
#include <QLabel>
22
#include <QLineEdit>
23
 
24
#include "tqtinputline.h"
25
#include "terror.h"
26
#include "ui_tqtinputline.h"
27
 
28
using std::string;
29
 
30
TQtInputLine::TQtInputLine(QWidget* parent)
31
    : QDialog(parent),
32
      ui(new Ui::TQtInputLine)
33
{
34
    DECL_TRACER("TQtInputLine::TQtInputLine(QWidget* parent)");
35
 
36
    ui->setupUi(this);
37
 
38
    ui->labelMessage->setText("Input information");
39
    ui->lineEdit->setClearButtonEnabled(true);
40
    ui->lineEdit->setFocus();
41
#ifdef Q_OS_IOS
42
    setAttribute(Qt::WA_ContentsMarginsRespectsSafeArea, true);
43
#endif
44
}
45
 
46
TQtInputLine::~TQtInputLine()
47
{
48
    DECL_TRACER("TQtInputLine::~TQtInputLine()");
49
}
50
 
51
void TQtInputLine::setText(const string& text)
52
{
53
    DECL_TRACER("TQtInputLine::setText(const string& text)");
54
 
55
    mText = text;
56
    ui->lineEdit->setText(text.c_str());
57
}
58
 
59
void TQtInputLine::setMessage(const string& msg)
60
{
61
    DECL_TRACER("TQtInputLine::setMessage(const string& msg)");
62
 
63
    ui->labelMessage->setText(msg.c_str());
64
}
65
 
66
void TQtInputLine::setPassword(bool pw)
67
{
68
    DECL_TRACER("TQtInputLine::setPassword(bool pw)");
69
 
70
    ui->lineEdit->setEchoMode(pw ? QLineEdit::Password : QLineEdit::Normal);
71
}
72
 
73
void TQtInputLine::doResize()
74
{
75
    DECL_TRACER("TQtInputLine::doResize()");
76
 
77
    // The main dialog window
78
    QSize size = this->size();
79
    QRect rect = this->geometry();
80
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
81
    this->resize(size);
82
    this->move(scale(rect.left()), scale(rect.top()));
83
    QWidget *parent = this->parentWidget();
84
 
85
    if (parent)
86
    {
87
        rect = parent->geometry();
88
        this->move(rect.center() - this->rect().center());
89
    }
90
 
91
    // Layout
92
    // Iterate through childs and resize them
93
    QObjectList childs = children();
94
    QList<QObject *>::Iterator iter;
95
 
96
    for (iter = childs.begin(); iter != childs.end(); ++iter)
97
    {
98
        QObject *obj = *iter;
99
        QString name = obj->objectName();
100
 
101
        if (name.startsWith("toolButton"))
102
            scaleObject(dynamic_cast<QToolButton *>(obj));
103
        else if (name.startsWith("pushButton"))
104
            scaleObject(dynamic_cast<QPushButton *>(obj));
105
        else if (name.startsWith("label"))
106
            scaleObject(dynamic_cast<QLabel *>(obj));
107
        else if (name.startsWith("line"))
108
            scaleObject(dynamic_cast<QFrame *>(obj));
109
        else if (name.startsWith("lineEdit"))
110
            scaleObject(dynamic_cast<QLineEdit *>(obj));
111
    }
112
}
113
 
114
void TQtInputLine::on_lineEdit_textChanged(const QString& arg1)
115
{
116
    DECL_TRACER("TQtInputLine::on_lineEdit_textChanged(const QString& arg1)");
117
 
118
    if (arg1.compare(mText.c_str()) == 0)
119
        return;
120
 
121
    mText = arg1.toStdString();
122
}
123
 
124
template<typename T>
125
void TQtInputLine::scaleObject(T *obj)
126
{
127
    DECL_TRACER("TQtInputLine::scaleObject(T *obj): " + obj->objectName().toStdString());
128
 
129
    QSize size = obj->size();
130
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
131
    obj->resize(size);
132
    QRect rect = obj->geometry();
133
    obj->move(scale(rect.left()), scale(rect.top()));
134
}
135
 
136
int TQtInputLine::scale(int value)
137
{
138
    if (value <= 0 || mScaleFactor == 1.0)
139
        return value;
140
 
141
    return (int)((double)value * mScaleFactor);
142
}