-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrfxprogressdialog.cpp
More file actions
141 lines (127 loc) · 3.95 KB
/
drfxprogressdialog.cpp
File metadata and controls
141 lines (127 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "drfxprogressdialog.h"
#include "ui_drfxprogressdialog.h"
#include <QMessageBox>
#include <QMessageBox>
#include <QPushButton>
#include <QScreen>
DRFXProgressDialog::DRFXProgressDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::DRFXProgressDialog)
, m_isError(false)
{
ui->setupUi(this);
ui->progressBar->reset();
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(100);
ui->progressBar->setValue(0);
QScreen *screen = qApp->primaryScreen();
int width = geometry().width();
int hight = geometry().height();
if (width > 0 && width < screen->size().width() && hight > 0 && hight < screen->size().height()) {
uint centerX = screen->size().width() / 2 - width / 2;
uint centerY = screen->size().height() / 2 - hight / 2;
setGeometry(centerX, centerY, width, hight);
}
// call all events in main thread context!ss
connect(this, &DRFXProgressDialog::showComplete, this, &DRFXProgressDialog::onComplete, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::progressReset, ui->progressBar, &QProgressBar::reset, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::updateRange, ui->progressBar, &QProgressBar::setRange, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::updateMinimum, ui->progressBar, &QProgressBar::setMinimum, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::updateMaximum, ui->progressBar, &QProgressBar::setMaximum, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::updateValue, ui->progressBar, &QProgressBar::setValue, Qt::QueuedConnection);
connect(this, &DRFXProgressDialog::updateText, ui->label, &QLabel::setText, Qt::QueuedConnection);
connect(
this,
&DRFXProgressDialog::showError,
this,
[parent](const QString &message) { //
QMessageBox::critical(parent, qApp->applicationDisplayName(), message);
},
Qt::QueuedConnection);
}
DRFXProgressDialog::~DRFXProgressDialog()
{
delete ui;
}
void DRFXProgressDialog::complete(const QString& message) {
emit showComplete(message);
}
void DRFXProgressDialog::run(const TWorkerCallback &worker, const TCompleteCallback &completion)
{
show();
QThread *t = new QThread(this);
connect(
ui->buttonBox,
&QDialogButtonBox::rejected,
this,
[this, t]() {
QPushButton *b;
if ((b = ui->buttonBox->button(QDialogButtonBox::Cancel))) {
b->setEnabled(false);
}
t->requestInterruption();
t->wait(5000);
},
Qt::QueuedConnection);
connect(
t,
&QThread::started,
this,
[this, worker, t]() {
worker(this, t);
t->exit(0);
},
Qt::DirectConnection);
connect(
t,
&QThread::finished,
this,
[t]() { //
t->deleteLater();
},
Qt::DirectConnection);
connect(
t,
&QThread::destroyed,
this,
[this, completion](QObject *) {
completion(this);
this->reject();
},
Qt::QueuedConnection);
t->start(QThread::HighPriority);
}
void DRFXProgressDialog::reset()
{
m_isError = false;
emit progressReset();
}
void DRFXProgressDialog::setRange(int minimum, int maximum)
{
emit updateRange(minimum, maximum);
}
void DRFXProgressDialog::setMinimum(int minimum)
{
emit updateMinimum(minimum);
}
void DRFXProgressDialog::setMaximum(int maximum)
{
emit updateMaximum(maximum);
}
void DRFXProgressDialog::setValue(int value)
{
emit updateValue(value);
}
void DRFXProgressDialog::setMessage(const QString &message)
{
emit updateText(message);
}
void DRFXProgressDialog::setError(const QString &message)
{
m_isError = true;
emit showError(message);
}
void DRFXProgressDialog::onComplete(const QString &message)
{
QMessageBox::information(this, qApp->applicationDisplayName(), message);
}