forked from luebking/qarma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gprogressdialog.cpp
137 lines (116 loc) · 3.21 KB
/
gprogressdialog.cpp
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
#include "gprogressdialog.h"
#include "dimagebutton.h"
DWIDGET_USE_NAMESPACE
GProgressDialog::GProgressDialog(QWidget *parent)
: DDialog(parent)
{
m_progressbar = new DWaterProgress;
m_tipsText = new QLabel();
m_cancelButton = new QPushButton(tr("Cancel"));
m_progressbar->setFixedSize(60,60);
// 开启动画
m_progressbar->start();
m_layout = new QGridLayout();
m_layout->addWidget(m_progressbar, 0, 0);
m_layout->addWidget(m_tipsText, 0, 1);
m_dialogWidget = new QWidget();
m_dialogWidget->setLayout(m_layout);
this->addContent(m_dialogWidget);
addButton(tr("Cancel"), true, ButtonType::ButtonWarning);
installEventFilter(this);
setFocusPolicy(Qt::StrongFocus);
}
void GProgressDialog::reject()
{
// 通过重写 reject 来禁用 ESC 事件
if (!m_isEnabledCloseButton) {
// 如果设置为禁用关闭按钮且当前进度值不等于最大值(非繁忙状态),则不退出窗口
if (m_value != m_maximum || m_busy) {
return;
}
}
qDebug() << m_value << m_maximum;
DDialog::reject();
}
void GProgressDialog::setLabelText(QString text)
{
// 设置文本居中
m_tipsText->setText(" " + text.replace("\\n", "\n"));
}
void GProgressDialog::setValue(int value)
{
if (m_busy) {
// 显示繁忙动画
m_progressbar->setValue(50);
m_progressbar->setTextVisible(false);
return;
}
m_value = value;
m_progressbar->setTextVisible(true);
m_progressbar->setValue((m_value - m_minimum) * 100 / (m_maximum - m_minimum));
// 如果设置自动关闭则在等于最大值时自动关闭对话框
if (m_maximum == m_value && property("Garma_autoclose").toBool()) {
done(QDialog::Accepted);
}
}
int GProgressDialog::value()
{
return m_value;
}
void GProgressDialog::setCancelButtonText(QString text)
{
m_cancelButton->setText(text);
}
void GProgressDialog::setRange(int min, int max)
{
m_maximum = max;
m_minimum = min;
m_busy = false;
if (min == 0 && max == 0) {
// 繁忙动画
m_busy = true;
}
setValue(m_value);
}
void GProgressDialog::setCloseButtonVisiable(bool status)
{
m_isEnabledCloseButton = status;
if (QPushButton *btn = this->findChild<QPushButton*>()) {
if (m_isEnabledCloseButton) {
btn->show();
btn->setEnabled(true);
btn->setAutoDefault(true);
btn->setDefault(true);
}
else {
btn->hide();
btn->setDisabled(true);
btn->setAutoDefault(false);
btn->setDefault(false);
}
}
// 关闭对话框右上角的关闭按钮
if (DImageButton *diabtn = this->findChild<DImageButton*>("CloseButton")) {
if (m_isEnabledCloseButton) {
diabtn->show();
diabtn->setEnabled(true);
}
else {
diabtn->hide();
diabtn->setDisabled(true);
}
}
setOnButtonClickedClose(m_isEnabledCloseButton);
}
bool GProgressDialog::closeButtonVisiable() const
{
return m_isEnabledCloseButton;
}
int GProgressDialog::minimum()
{
return m_minimum;
}
int GProgressDialog::maximum()
{
return m_maximum;
}