1 /**
2 * \file progresswidget.cpp
3 * Widget showing progress, similar to QProgressDialog.
4 *
5 * \b Project: Kid3
6 * \author Urs Fleisch
7 * \date 13 Jan 2017
8 *
9 * Copyright (C) 2017-2018 Urs Fleisch
10 *
11 * This file is part of Kid3.
12 *
13 * Kid3 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Kid3 is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 #include "progresswidget.h"
28 #include <QVBoxLayout>
29 #include <QHBoxLayout>
30 #include <QLabel>
31 #include <QProgressBar>
32 #include <QPushButton>
33
34 /**
35 * Constructor.
36 * @param parent parent widget
37 */
ProgressWidget(QWidget * parent)38 ProgressWidget::ProgressWidget(QWidget* parent) : QFrame(parent),
39 m_percentage(0), m_wasCanceled(false)
40 {
41 setFrameShape(QFrame::StyledPanel);
42 setFrameShadow(QFrame::Sunken);
43 auto layout = new QVBoxLayout(this);
44 m_title = new QLabel;
45 QFont titleFont = font();
46 titleFont.setPointSize(titleFont.pointSize() + 3);
47 titleFont.setBold(true);
48 m_title->setFont(titleFont);
49
50 layout->addWidget(m_title);
51 m_label = new QLabel;
52 layout->addWidget(m_label);
53 m_progress = new QProgressBar;
54 layout->addWidget(m_progress);
55 auto buttonLayout = new QHBoxLayout;
56 m_cancelButton = new QPushButton(tr("&Cancel"));
57 connect(m_cancelButton, &QAbstractButton::clicked, this, &ProgressWidget::onCancelClicked);
58 buttonLayout->addStretch();
59 buttonLayout->addWidget(m_cancelButton);
60 layout->addLayout(buttonLayout);
61 layout->addStretch();
62 }
63
64 /**
65 * Set title.
66 * @param text title
67 */
setWindowTitle(const QString & text)68 void ProgressWidget::setWindowTitle(const QString& text)
69 {
70 m_title->setText(text);
71 }
72
73 /**
74 * Set text of label.
75 * @param text label, default is empty
76 */
setLabelText(const QString & text)77 void ProgressWidget::setLabelText(const QString& text)
78 {
79 m_label->setText(text);
80 }
81
82 /**
83 * Set text of cancel button.
84 * @param text button text, default is "Cancel"
85 */
setCancelButtonText(const QString & text)86 void ProgressWidget::setCancelButtonText(const QString& text)
87 {
88 m_cancelButton->setText(text);
89 }
90
91 /**
92 * Set minimum value.
93 * @param minimum minimum value, default is 0
94 */
setMinimum(int minimum)95 void ProgressWidget::setMinimum(int minimum)
96 {
97 m_progress->setMinimum(minimum);
98 }
99
100 /**
101 * Set maximum value.
102 * @param maximum maximum value, default is 100
103 */
setMaximum(int maximum)104 void ProgressWidget::setMaximum(int maximum)
105 {
106 m_progress->setMaximum(maximum);
107 }
108
109 /**
110 * Set current amount of progress made.
111 * @param value progress value
112 */
setValue(int value)113 void ProgressWidget::setValue(int value)
114 {
115 m_progress->setValue(value);
116 }
117
118 /**
119 * Set value and maximum, but only if it changes the current percentage.
120 *
121 * This will have better performance by avoiding too many UI updates.
122 *
123 * @param value progress value
124 * @param maximum maximum value
125 */
setValueAndMaximum(int value,int maximum)126 void ProgressWidget::setValueAndMaximum(int value, int maximum)
127 {
128 int percentage = maximum > 0 ? value * 100 / maximum : 0;
129 if (m_percentage != percentage) {
130 m_percentage = percentage;
131 m_progress->setMaximum(maximum);
132 m_progress->setValue(value);
133 }
134 }
135
136 /**
137 * Set format used for progress text.
138 * @param format format string, can contain %p, %v, %m for
139 * percentage, value, total
140 */
setFormat(const QString & format)141 void ProgressWidget::setFormat(const QString& format)
142 {
143 m_progress->setFormat(format);
144 }
145
146 /**
147 * Reset the progress widget.
148 */
reset()149 void ProgressWidget::reset()
150 {
151 m_progress->reset();
152 m_percentage = 0;
153 m_wasCanceled = false;
154 }
155
onCancelClicked()156 void ProgressWidget::onCancelClicked()
157 {
158 m_wasCanceled = true;
159 emit canceled();
160 }
161