Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
260 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 <QLabel>
20
#include <QProgressBar>
21
#include <QTimer>
22
 
23
#include "tqtwait.h"
24
#include "terror.h"
25
#include "tpagemanager.h"
26
#include "ui_wait.h"
27
 
28
extern TPageManager *gPageManager;
29
 
30
using std::string;
31
 
32
TQtWait::TQtWait(QWidget *parent)
33
    : QDialog(parent),
34
      ui(new Ui::TQtWait)
35
{
36
    DECL_TRACER("TQtWait::TQtWait(QWidget *parent = nullptr)");
37
 
38
    ui->setupUi(this);
39
    ui->labelText->setText("Please wait ...");
40
}
41
 
42
TQtWait::TQtWait(QWidget *parent, const string& text)
43
    : QDialog(parent),
44
      ui(new Ui::TQtWait),
45
      mText(text)
46
{
47
    DECL_TRACER("TQtWait::TQtWait(QWidget *parent, const string& text)");
48
 
49
    ui->setupUi(this);
50
    ui->labelText->setText(mText.c_str());
51
}
52
 
53
TQtWait::~TQtWait()
54
{
55
    DECL_TRACER("TQtWait::~TQtWait()");
56
 
57
    if (mTimer)
58
        mTimer->stop();
59
}
60
 
61
void TQtWait::setText(const string& text)
62
{
63
    DECL_TRACER("TQtWait::setText(const string& text)");
64
 
65
    mText = text;
66
    ui->labelText->setText(text.c_str());
67
}
68
 
69
void TQtWait::startTimer()
70
{
71
    DECL_TRACER("TQtWait::startTimer()");
72
 
73
    if (!mTimer)
74
        mTimer = new QTimer(this);
75
 
76
    connect(mTimer, &QTimer::timeout, this, &TQtWait::onTimerEvent);
77
    mTimer->start(200);
78
}
79
 
80
void TQtWait::onTimerEvent()
81
{
82
    DECL_TRACER("TQtWait::onTimerEvent(QTimerEvent *e)");
83
 
84
    mPosition++;
85
 
86
    if (mPosition > 100)
87
    {
88
        mPosition = 0;
89
 
90
        if (!mDir)
91
            mDir = !mDir;
92
 
93
        ui->progressBarWait->setInvertedAppearance(mDir);
94
    }
95
 
96
    ui->progressBarWait->setValue(mPosition);
97
}
98
 
99
void TQtWait::start()
100
{
101
    DECL_TRACER("TQtWait::start()");
102
 
103
    show();
104
    startTimer();
105
}
106
 
107
void TQtWait::end()
108
{
109
    DECL_TRACER("TQtWait::end()");
110
 
111
    if (mTimer)
112
    {
113
        mTimer->stop();
114
        delete mTimer;
115
        mTimer = nullptr;
116
    }
117
 
118
    close();
119
}
120
 
121
void TQtWait::doResize()
122
{
123
    DECL_TRACER("TQtWait::doResize()");
124
 
125
    // The main dialog window
126
    QSize size = this->size();
127
    QRect rect = this->geometry();
128
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
129
    this->resize(size);
130
    this->move(scale(rect.left()), scale(rect.top()));
131
    QWidget *parent = this->parentWidget();
132
 
133
    if (parent)
134
    {
135
        rect = parent->geometry();
136
        this->move(rect.center() - this->rect().center());
137
    }
138
 
139
    // Layout
140
    // Iterate through childs and resize them
141
    QObjectList childs = children();
142
    QList<QObject *>::Iterator iter;
143
 
144
    for (iter = childs.begin(); iter != childs.end(); ++iter)
145
    {
146
        QObject *obj = *iter;
147
        QString name = obj->objectName();
148
 
149
        if (name.startsWith("progressBar"))
150
            scaleObject(dynamic_cast<QProgressBar *>(obj));
151
        else if (name.startsWith("label"))
152
            scaleObject(dynamic_cast<QLabel *>(obj));
153
    }
154
}
155
 
156
int TQtWait::scale(int value)
157
{
158
    if (value <= 0 || mScaleFactor == 1.0)
159
        return value;
160
 
161
    return (int)((double)value * mScaleFactor);
162
}
163
 
164
template<typename T>
165
void TQtWait::scaleObject(T *obj)
166
{
167
    DECL_TRACER("TQtWait::scaleObject(T *obj): " + obj->objectName().toStdString());
168
 
169
    QSize size = obj->size();
170
    size.scale(scale(size.width()), scale(size.height()), Qt::KeepAspectRatio);
171
    obj->resize(size);
172
    QRect rect = obj->geometry();
173
    obj->move(scale(rect.left()), scale(rect.top()));
174
}