1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtprogresslabel.h"
25 #include <QProgressBar>
26 #include <QBasicTimer>
29 class QxtProgressLabelPrivate : public QxtPrivate<QxtProgressLabel>
32 QXT_DECLARE_PUBLIC(QxtProgressLabel);
33 QxtProgressLabelPrivate();
45 QxtProgressLabelPrivate::QxtProgressLabelPrivate()
46 : interval(-1), cachedMin(0), cachedMax(0), cachedVal(0)
50 \class QxtProgressLabel QxtProgressLabel
52 \brief A label showing progress related time values.
54 QxtProgressLabel is a label widget able to show elapsed and remaining
55 time of a progress. Usage is as simple as connecting signal
56 \b QProgressBar::valueChanged() to slot QxtProgressLabel::setValue().
60 QProgressBar* bar = new QProgressBar(this);
61 QxtProgressLabel* label = new QxtProgressLabel(this);
62 connect(bar, SIGNAL(valueChanged(int)), label, SLOT(setValue(int)));
65 \image html qxtprogresslabel.png "QxtProgressLabel in action."
69 Constructs a new QxtProgressLabel with \a parent and \a flags.
71 QxtProgressLabel::QxtProgressLabel(QWidget* parent, Qt::WindowFlags flags)
72 : QLabel(parent, flags)
74 QXT_INIT_PRIVATE(QxtProgressLabel);
79 Constructs a new QxtProgressLabel with \a text, \a parent and \a flags.
81 QxtProgressLabel::QxtProgressLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags)
82 : QLabel(text, parent, flags)
84 QXT_INIT_PRIVATE(QxtProgressLabel);
89 Destructs the progress label.
91 QxtProgressLabel::~QxtProgressLabel()
95 \property QxtProgressLabel::contentFormat
96 \brief This property holds the content format of the progress label
98 The content of the label is formatted according to this property.
99 The default value is an empty string which defaults to \b "ETA: %r".
101 The following variables may be used in the format string:
103 <tr><td><b>Variable</b></td><td><b>Output</b></td></tr>
104 <tr><td>\%e</td><td>elapsed time</td></tr>
105 <tr><td>\%r</td><td>remaining time</td></tr>
110 QString QxtProgressLabel::contentFormat() const
112 return qxt_d().cformat;
115 void QxtProgressLabel::setContentFormat(const QString& format)
117 if (qxt_d().cformat != format)
119 qxt_d().cformat = format;
125 \property QxtProgressLabel::timeFormat
126 \brief This property holds the time format of the progress label
128 Time values are formatted according to this property.
129 The default value is an empty string which defaults to \b "mm:ss".
131 \sa contentFormat, QTime::toString()
133 QString QxtProgressLabel::timeFormat() const
135 return qxt_d().tformat;
138 void QxtProgressLabel::setTimeFormat(const QString& format)
140 if (qxt_d().tformat != format)
142 qxt_d().tformat = format;
148 \property QxtProgressLabel::updateInterval
149 \brief This property holds the update interval of the progress label
151 The content of the progress label is updated according to this interval.
152 A negative interval makes the content to update only during value changes.
153 The default value is \b -1.
155 int QxtProgressLabel::updateInterval() const
157 return qxt_d().interval;
160 void QxtProgressLabel::setUpdateInterval(int msecs)
162 qxt_d().interval = msecs;
165 if (qxt_d().timer.isActive())
166 qxt_d().timer.stop();
170 if (!qxt_d().timer.isActive())
171 qxt_d().timer.start(msecs, this);
176 Sets the current value to \a value.
178 \note Calling this slot by hand has no effect.
179 Connect this slot to \b QProgressBar::valueChange().
181 void QxtProgressLabel::setValue(int value)
183 QProgressBar* bar = qobject_cast<QProgressBar*>(sender());
186 if (!qxt_d().start.isValid())
189 qxt_d().cachedMin = bar->minimum();
190 qxt_d().cachedMax = bar->maximum();
191 qxt_d().cachedVal = value;
198 Restarts the calculation of elapsed and remaining times.
200 void QxtProgressLabel::restart()
202 qxt_d().cachedMin = 0;
203 qxt_d().cachedMax = 0;
204 qxt_d().cachedVal = 0;
205 qxt_d().start.restart();
210 Refreshes the content.
212 void QxtProgressLabel::refresh()
216 if (qxt_d().start.isValid())
217 elapsed = qxt_d().start.elapsed() / 1000.0;
219 etime = etime.addSecs(static_cast<int>(elapsed));
223 if (qxt_d().cachedMax != 0)
224 percent = (qxt_d().cachedVal - qxt_d().cachedMin) / static_cast<qreal>(qxt_d().cachedMax);
227 total = elapsed / percent;
231 rtime = rtime.addSecs(static_cast<int>(total - elapsed));
234 QString tformat = qxt_d().tformat;
235 if (tformat.isEmpty())
236 tformat = tr("mm:ss");
237 QString cformat = qxt_d().cformat;
238 if (cformat.isEmpty())
239 cformat = tr("ETA: %r");
241 QString result = QString(cformat).replace("%e", etime.toString(tformat));
242 result = result.replace("%r", rtime.toString(tformat));
246 void QxtProgressLabel::timerEvent(QTimerEvent* event)