Debian Codegraph @

kysdk-qtwidgets/src/kprogressdialog.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kprogressdialog.h"
24 #include "parmscontroller.h"
25 #include <QLayoutItem>
26 #include "accessinfohelper.h"
27
28 namespace kdk
29 {
30 class KProgressDialogPrivate : public QObject
31 {
32 Q_OBJECT
33 Q_DECLARE_PUBLIC(KProgressDialog)
34
35 public:
36 KProgressDialogPrivate(KProgressDialog *parent);
37 void adjustNormalMode();
38 void infoReset();
39 void setAccessInfo();
40
41 private:
42 KProgressDialog *const q_ptr;
43
44 QProgressBar *m_pProgressBar;
45 QLabel *m_pMainLabel;
46 QLabel *m_pPercentLabel;
47 QLabel *m_pSubContentLabel;
48 QLabel *m_pProgressLabel;
49 QPushButton *m_pCanelButton;
50 QList<QPushButton *> m_pButtons;
51 QVBoxLayout *m_pMainLayout;
52
53 QString m_suffix;
54 QString m_numerator;
55 QString m_denominator;
56 QString m_speed;
57
58 BarType m_type;
59 bool m_autoClose;
60 bool m_autoReset;
61 bool m_forceHide;
62
63 int m_maxValue;
64 int m_minVlaue;
65 };
66
67 KProgressDialog::KProgressDialog(QWidget *parent)
68 : KDialog(parent)
69 , d_ptr(new KProgressDialogPrivate(this))
70 {
71 Q_D(KProgressDialog);
72 // setFixedSize(440,265);
73 this->layout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
74 d->m_pMainLayout = new QVBoxLayout();
75 d->m_pMainLayout->setContentsMargins(24, 0, 24, 24);
76 d->m_pMainLayout->setSpacing(0);
77 d->m_pMainLabel = new QLabel(this);
78
79 d->m_pMainLabel->setAlignment(Qt::AlignLeft);
80 d->m_pProgressBar = new QProgressBar(this);
81 d->m_pCanelButton = new QPushButton(this);
82 d->m_pCanelButton->setText(tr("cancel"));
83 d->m_pSubContentLabel = new QLabel(this);
84 d->m_pPercentLabel = new QLabel(this);
85 d->m_pProgressLabel = new QLabel(this);
86
87 d->m_autoClose = true;
88 d->m_autoReset = true;
89 d->m_forceHide = false;
90
91 d->m_maxValue = maximum();
92 d->m_minVlaue = minimum();
93
94 d->m_type = BarType::NormalBar;
95
96 d->adjustNormalMode();
97 setShowDetail(false);
98 changeTheme();
99 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
100 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
101 {
102 changeTheme();
103 }
104 });
105 connect(this, SIGNAL(canceled()), this, SLOT(cancel()));
106 connect(d->m_pCanelButton, SIGNAL(clicked()), this, SIGNAL(canceled()));
107
108 d->setAccessInfo();
109 }
110
111 KProgressDialog::KProgressDialog(const QString &labelText, const QString &cancelButtonText, int minimum, int maximum, QWidget *parent)
112 : KProgressDialog(parent)
113 {
114 Q_D(KProgressDialog);
115 d->m_pMainLabel->setText(labelText);
116 d->m_pProgressBar->setRange(minimum, maximum);
117 d->m_pCanelButton->setText(cancelButtonText);
118 d->adjustNormalMode();
119 setShowDetail(true);
120 }
121
122 KProgressDialog::~KProgressDialog()
123 {
124 }
125
126 void KProgressDialog::setLabel(QLabel *label)
127 {
128 Q_D(KProgressDialog);
129 if (!label)
130 return;
131 if (d->m_pMainLabel)
132 delete d->m_pMainLabel;
133 d->m_pMainLabel = label;
134 d->m_pMainLabel->setVisible(true);
135 }
136
137 void KProgressDialog::setCancelButton(QPushButton *button)
138 {
139 Q_D(KProgressDialog);
140 if (!button)
141 return;
142 if (d->m_pCanelButton)
143 delete d->m_pCanelButton;
144 d->m_pCanelButton = button;
145 }
146
147 void KProgressDialog::setBar(QProgressBar *bar)
148 {
149 Q_D(KProgressDialog);
150 if (!bar)
151 return;
152 if (d->m_pProgressBar)
153 delete d->m_pProgressBar;
154 d->m_pProgressBar = bar;
155 }
156
157 void KProgressDialog::setSuffix(const QString &suffix)
158 {
159 Q_D(KProgressDialog);
160 if (myType() == BarType::NormalBar) {
161 d->m_suffix = suffix;
162 }
163
164 d->adjustNormalMode();
165 }
166
167 void KProgressDialog::setSuffix(const QString &numerator, const QString &denominator)
168 {
169 Q_D(KProgressDialog);
170 // 加一些type的判断
171 if (myType() == BarType::SpeedBar) {
172 d->m_numerator = numerator;
173 d->m_denominator = denominator;
174 }
175
176 d->adjustNormalMode();
177 }
178
179 void KProgressDialog::setSpeed(const float &speed)
180 {
181 Q_D(KProgressDialog);
182 d->m_speed = QString::number(speed);
183 if (d->m_type == BarType::SpeedBar)
184 d->m_pProgressLabel->setText(QString("%1 %2/%3").arg(d->m_speed).arg(d->m_numerator).arg(d->m_denominator));
185 }
186
187 void KProgressDialog::setShowDetail(bool flag)
188 {
189 Q_D(KProgressDialog);
190 if (flag) {
191 d->m_pSubContentLabel->setVisible(true);
192 d->m_pProgressLabel->setVisible(true);
193 d->m_pPercentLabel->setVisible(true);
194 } else {
195 d->m_pSubContentLabel->setVisible(false);
196 d->m_pProgressLabel->setVisible(false);
197 d->m_pPercentLabel->setVisible(false);
198 }
199 }
200
201 int KProgressDialog::minimum() const
202 {
203 Q_D(const KProgressDialog);
204 if (d->m_pProgressBar)
205 return d->m_pProgressBar->minimum();
206 else
207 return -1;
208 }
209
210 int KProgressDialog::maximum() const
211 {
212 Q_D(const KProgressDialog);
213 if (d->m_pProgressBar)
214 return d->m_pProgressBar->maximum();
215 else
216 return -1;
217 }
218
219 int KProgressDialog::value() const
220 {
221 Q_D(const KProgressDialog);
222 if (d->m_pProgressBar)
223 return d->m_pProgressBar->value();
224 else
225 return -1;
226 }
227
228 QString KProgressDialog::labelText() const
229 {
230 Q_D(const KProgressDialog);
231 if (d->m_pMainLabel)
232 return d->m_pMainLabel->text();
233 else {
234 return QString();
235 }
236 }
237
238 void KProgressDialog::setAutoReset(bool reset)
239 {
240 Q_D(KProgressDialog);
241 d->m_autoReset = reset;
242 }
243
244 bool KProgressDialog::autoReset() const
245 {
246 Q_D(const KProgressDialog);
247 return d->m_autoReset;
248 }
249
250 void KProgressDialog::setAutoClose(bool close)
251 {
252 Q_D(KProgressDialog);
253 d->m_autoClose = close;
254 }
255
256 bool KProgressDialog::autoClose() const
257 {
258 Q_D(const KProgressDialog);
259 return d->m_autoClose;
260 }
261
262 QProgressBar *KProgressDialog::progressBar()
263 {
264 Q_D(KProgressDialog);
265 return d->m_pProgressBar;
266 }
267
268 void KProgressDialog::setBarType(const BarType &type)
269 {
270 Q_D(KProgressDialog);
271 d->m_type = type;
272 if (type == BarType::ScrollBar) {
273 if (d->m_pProgressBar->minimum() != d->m_pProgressBar->maximum() != 0) {
274 d->m_maxValue = d->m_pProgressBar->maximum();
275 d->m_minVlaue = d->m_pProgressBar->minimum();
276 }
277 }
278 if (type == BarType::NormalBar)
279 d->m_pProgressLabel->setText(QString("%1%2/%3%4").arg(d->m_pProgressBar->value()).arg(d->m_suffix).arg(maximum()).arg(d->m_suffix));
280 else if (type == BarType::SpeedBar)
281 d->m_pProgressLabel->setText(QString("%1 %2/%3").arg(d->m_speed).arg(d->m_numerator).arg(d->m_denominator));
282
283 d->adjustNormalMode();
284 }
285
286 void KProgressDialog::addButton(QPushButton *button)
287 {
288 Q_D(KProgressDialog);
289 if (!d->m_pButtons.contains(button)) {
290 if (d->m_pButtons.count() >= 3)
291 return;
292 if (button)
293 d->m_pButtons.append(button);
294 d->adjustNormalMode();
295 }
296 }
297
298 void KProgressDialog::removeButton(QPushButton *button)
299 {
300 Q_D(KProgressDialog);
301 if (d->m_pButtons.contains(button)) {
302 d->m_pButtons.removeAll(button);
303 }
304 d->adjustNormalMode();
305 }
306
307 BarType KProgressDialog::myType()
308 {
309 Q_D(KProgressDialog);
310 return d->m_type;
311 }
312
313 void KProgressDialog::cancel()
314 {
315 Q_D(KProgressDialog);
316 d->m_forceHide = true;
317 reset();
318 d->m_forceHide = false;
319 }
320
321 void KProgressDialog::reset()
322 {
323 Q_D(KProgressDialog);
324 if (d->m_autoClose || d->m_forceHide)
325 hide();
326 d->m_pProgressBar->reset();
327 d->infoReset();
328 }
329
330 void KProgressDialog::setMaximum(int maximum)
331 {
332 Q_D(KProgressDialog);
333 if (d->m_pProgressBar)
334 d->m_pProgressBar->setMaximum(maximum);
335 }
336
337 void KProgressDialog::setMinimum(int minimum)
338 {
339 Q_D(KProgressDialog);
340 if (d->m_pProgressBar)
341 d->m_pProgressBar->setMinimum(minimum);
342 }
343
344 void KProgressDialog::setRange(int minimum, int maximum)
345 {
346 Q_D(KProgressDialog);
347 if (d->m_pProgressBar)
348 d->m_pProgressBar->setRange(minimum, maximum);
349 }
350
351 void KProgressDialog::setValue(int progress)
352 {
353 Q_D(KProgressDialog);
354 if (d->m_pProgressBar)
355 d->m_pProgressBar->setValue(progress);
356 if (d->m_pPercentLabel)
357 d->m_pPercentLabel->setText(QString::number(progress * 100 / maximum()) + "%");
358 if (d->m_pProgressLabel) {
359 if (d->m_type != BarType::SpeedBar)
360 d->m_pProgressLabel->setText(QString("%1%2/%3%4").arg(progress).arg(d->m_suffix).arg(maximum()).arg(d->m_suffix));
361 // else
362 // d->m_pProgressLabel->setText(QString("%1 %2/%3").arg(d->m_speed).arg(d->m_numerator).arg(d->m_denominator));
363 }
364 }
365
366 void KProgressDialog::setLabelText(const QString &text)
367 {
368 Q_D(KProgressDialog);
369 if (d->m_pMainLabel)
370 d->m_pMainLabel->setText(text);
371 }
372
373 void KProgressDialog::setCancelButtonText(const QString &text)
374 {
375 Q_D(KProgressDialog);
376 if (d->m_pCanelButton)
377 d->m_pCanelButton->setText(text);
378 }
379
380 void KProgressDialog::setSubContent(const QString &text)
381 {
382 Q_D(KProgressDialog);
383 if (d->m_pSubContentLabel) {
384 d->m_pSubContentLabel->setText(text);
385 setShowDetail(true);
386 }
387 }
388
389 void KProgressDialog::changeTheme()
390 {
391 Q_D(KProgressDialog);
392 KDialog::changeTheme();
393 QFont font;
394 font.setPixelSize(ThemeController::systemFontSize() * 1.7);
395 d->m_pMainLabel->setFont(font);
396 font.setPixelSize(ThemeController::systemFontSize() * 1.2);
397 d->m_pPercentLabel->setFont(font);
398 d->m_pSubContentLabel->setFont(font);
399 d->m_pProgressLabel->setFont(font);
400 }
401
402 KProgressDialogPrivate::KProgressDialogPrivate(KProgressDialog *parent)
403 : q_ptr(parent)
404 {
405 setParent(parent);
406 }
407
408 void KProgressDialogPrivate::adjustNormalMode()
409 {
410 Q_Q(KProgressDialog);
411
412 QLayoutItem *child;
413 while ((child = m_pMainLayout->takeAt(0)) != 0) {
414 // 删除Stretch(弹簧)等布局
415 if (child->spacerItem()) {
416 m_pMainLayout->removeItem(child);
417 continue;
418 }
419 // 删除layout
420 else if (child->layout()) {
421 QLayoutItem *item = child->layout()->takeAt(0);
422 while (item != 0) {
423 child->layout()->removeItem(item);
424 delete item;
425 item = child->layout()->takeAt(0);
426 }
427 } else // 删除布局
428 {
429 m_pMainLayout->removeWidget(child->widget());
430 child->widget()->setParent(nullptr);
431 }
432
433 delete child;
434 child = nullptr;
435 }
436
437 q->mainWidget()->setLayout(m_pMainLayout);
438 switch (m_type) {
439 case BarType::NothingBar: {
440 m_pProgressBar->setTextVisible(false);
441
442 m_pMainLayout->addStretch();
443 m_pMainLayout->addSpacing(24);
444 m_pMainLayout->addWidget(m_pMainLabel);
445 m_pMainLayout->addSpacing(8);
446
447 m_pProgressBar->setMaximum(m_maxValue);
448 m_pProgressBar->setMinimum(m_minVlaue);
449 m_pProgressBar->setFixedHeight(8);
450 QHBoxLayout *hLayout = new QHBoxLayout;
451 hLayout->setSpacing(0);
452 hLayout->addWidget(m_pProgressBar);
453 hLayout->addSpacing(8);
454 hLayout->addWidget(m_pPercentLabel);
455 m_pMainLayout->addLayout(hLayout);
456 m_pProgressBar->show();
457 m_pSubContentLabel->hide();
458 m_pPercentLabel->show();
459 m_pProgressLabel->hide();
460 m_pCanelButton->hide();
461
462 q->iconBar()->setFixedHeight(0);
463 q->windowButtonBar()->setVisible(false);
464 break;
465 }
466 case BarType::NormalBar: {
467 q->iconBar()->setFixedHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_IconbarHeight));
468 q->iconBar()->show();
469 m_pProgressBar->setTextVisible(true);
470 m_pMainLayout->addStretch();
471 m_pMainLayout->addSpacing(30);
472 m_pMainLayout->addWidget(m_pMainLabel);
473 m_pMainLayout->addSpacing(10);
474 m_pMainLayout->addWidget(m_pProgressBar);
475 m_pProgressBar->setMaximum(m_maxValue);
476 m_pProgressBar->setMinimum(m_minVlaue);
477 m_pProgressBar->setFixedHeight(8);
478
479 QHBoxLayout *hLayout = new QHBoxLayout;
480 hLayout->setSpacing(0);
481 hLayout->addWidget(m_pSubContentLabel);
482 hLayout->addStretch();
483 hLayout->addWidget(m_pPercentLabel);
484 hLayout->addSpacing(15);
485 hLayout->addWidget(m_pProgressLabel);
486 m_pMainLayout->addLayout(hLayout);
487
488 hLayout = new QHBoxLayout;
489 hLayout->addStretch();
490 hLayout->addWidget(m_pCanelButton);
491
492 m_pMainLayout->addSpacing(30);
493 m_pMainLayout->addLayout(hLayout);
494
495 m_pProgressBar->show();
496 m_pSubContentLabel->show();
497 m_pPercentLabel->show();
498 m_pProgressLabel->show();
499 m_pCanelButton->show();
500 q->windowButtonBar()->setVisible(true);
501 break;
502 }
503 case BarType::ScrollBar: {
504
505 m_pProgressBar->setTextVisible(false);
506 m_pProgressBar->setMaximum(0);
507 m_pProgressBar->setMinimum(0);
508 m_pProgressBar->setFixedHeight(30);
509 m_pMainLayout->addStretch();
510 m_pMainLayout->addSpacing(24);
511 m_pMainLayout->addWidget(m_pMainLabel);
512 m_pMainLayout->addSpacing(8);
513 m_pMainLayout->addWidget(m_pProgressBar);
514 q->iconBar()->setFixedHeight(0);
515
516 m_pSubContentLabel->hide();
517 m_pPercentLabel->hide();
518 m_pProgressLabel->hide();
519 m_pCanelButton->hide();
520
521 q->windowButtonBar()->setVisible(false);
522 break;
523 }
524 case BarType::SpeedBar: {
525
526 q->iconBar()->setFixedHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_IconbarHeight));
527 q->iconBar()->hide();
528 m_pProgressBar->setTextVisible(false);
529 m_pMainLayout->addStretch();
530 m_pMainLayout->addSpacing(4);
531 m_pMainLayout->addWidget(m_pMainLabel);
532 m_pMainLayout->addSpacing(8);
533 m_pProgressBar->setMaximum(m_maxValue);
534 m_pProgressBar->setMinimum(m_minVlaue);
535
536 QHBoxLayout *hLayout = new QHBoxLayout;
537 hLayout->setSpacing(0);
538 hLayout->addWidget(m_pProgressBar);
539 hLayout->addSpacing(8);
540 hLayout->addWidget(m_pPercentLabel);
541 m_pMainLayout->addLayout(hLayout);
542
543 hLayout = new QHBoxLayout;
544 hLayout->setSpacing(0);
545 hLayout->addWidget(m_pSubContentLabel);
546 hLayout->addStretch();
547 hLayout->addWidget(m_pProgressLabel);
548 hLayout->addSpacing(36);
549 m_pMainLayout->addSpacing(4);
550 m_pMainLayout->addLayout(hLayout);
551
552 hLayout = new QHBoxLayout;
553 if (!m_pButtons.isEmpty()) {
554 if (m_pButtons.count() == 1) {
555 hLayout->addStretch();
556 hLayout->addWidget((m_pButtons.first()));
557 } else if (m_pButtons.count() == 2) {
558 hLayout->addStretch();
559 hLayout->addWidget(m_pButtons[1]);
560 hLayout->addSpacing(8);
561 hLayout->addWidget(m_pButtons[0]);
562 } else {
563 hLayout->addWidget(m_pButtons[2]);
564 hLayout->addStretch();
565 hLayout->addWidget(m_pButtons[1]);
566 hLayout->addSpacing(8);
567 hLayout->addWidget(m_pButtons[0]);
568 }
569 }
570
571 m_pMainLayout->addSpacing(32);
572 m_pMainLayout->addLayout(hLayout);
573 m_pMainLayout->addStretch();
574
575 m_pProgressBar->show();
576 m_pSubContentLabel->show();
577 m_pPercentLabel->show();
578 m_pProgressLabel->show();
579
580 m_pCanelButton->hide();
581
582 q->windowButtonBar()->setVisible(true);
583 break;
584 }
585 default:
586 break;
587 }
588 }
589
590 void KProgressDialogPrivate::infoReset()
591 {
592 Q_Q(KProgressDialog);
593
594 if (m_pPercentLabel)
595 m_pPercentLabel->setText(+"0%");
596 if (m_pProgressLabel) {
597 if (m_type != BarType::SpeedBar)
598 m_pProgressLabel->setText(QString("%1%2/%3%4").arg(0).arg(m_suffix).arg(q->maximum()).arg(m_suffix));
599 else
600 m_pProgressLabel->setText(QString("%1 %2/%3").arg(0).arg(m_numerator).arg(m_denominator));
601 }
602 }
603
604 void KProgressDialogPrivate::setAccessInfo()
605 {
606 Q_Q(KProgressDialog);
607
608 KDK_ALL_INFO_FORMAT(m_pProgressBar,"");
609 KDK_ALL_INFO_FORMAT(m_pMainLabel,"");
610 KDK_ALL_INFO_FORMAT(m_pPercentLabel,"");
611 KDK_ALL_INFO_FORMAT(m_pSubContentLabel,"");
612 KDK_ALL_INFO_FORMAT(m_pProgressLabel,"");
613 KDK_ALL_INFO_FORMAT(m_pCanelButton,"");
614 }
615
616 }
617 #include "kprogressdialog.moc"
618 #include "moc_kprogressdialog.cpp"