We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtprogresslabel.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtprogresslabel.h"
25 #include <QProgressBar>
26 #include <QBasicTimer>
27 #include <QTime>
28
29 class QxtProgressLabelPrivate : public QxtPrivate<QxtProgressLabel>
30 {
31 public:
32     QXT_DECLARE_PUBLIC(QxtProgressLabel);
33     QxtProgressLabelPrivate();
34
35     QTime start;
36     int interval;
37     int cachedMin;
38     int cachedMax;
39     int cachedVal;
40     QString cformat;
41     QString tformat;
42     QBasicTimer timer;
43 };
44
45 QxtProgressLabelPrivate::QxtProgressLabelPrivate()
46         : interval(-1), cachedMin(0), cachedMax(0), cachedVal(0)
47 {}
48
49 /*!
50     \class QxtProgressLabel QxtProgressLabel
51     \ingroup QxtGui
52     \brief A label showing progress related time values.
53
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().
57
58     Example usage:
59     \code
60     QProgressBar* bar = new QProgressBar(this);
61     QxtProgressLabel* label = new QxtProgressLabel(this);
62     connect(bar, SIGNAL(valueChanged(int)), label, SLOT(setValue(int)));
63     \endcode
64
65     \image html qxtprogresslabel.png "QxtProgressLabel in action."
66  */
67
68 /*!
69     Constructs a new QxtProgressLabel with \a parent and \a flags.
70  */
71 QxtProgressLabel::QxtProgressLabel(QWidget* parent, Qt::WindowFlags flags)
72         : QLabel(parent, flags)
73 {
74     QXT_INIT_PRIVATE(QxtProgressLabel);
75     refresh();
76 }
77
78 /*!
79     Constructs a new QxtProgressLabel with \a text, \a parent and \a flags.
80  */
81 QxtProgressLabel::QxtProgressLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags)
82         : QLabel(text, parent, flags)
83 {
84     QXT_INIT_PRIVATE(QxtProgressLabel);
85     refresh();
86 }
87
88 /*!
89     Destructs the progress label.
90  */
91 QxtProgressLabel::~QxtProgressLabel()
92 {}
93
94 /*!
95     \property QxtProgressLabel::contentFormat
96     \brief This property holds the content format of the progress label
97
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".
100
101     The following variables may be used in the format string:
102     <table>
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>
106     </table>
107
108     \sa timeFormat
109  */
110 QString QxtProgressLabel::contentFormat() const
111 {
112     return qxt_d().cformat;
113 }
114
115 void QxtProgressLabel::setContentFormat(const QString& format)
116 {
117     if (qxt_d().cformat != format)
118     {
119         qxt_d().cformat = format;
120         refresh();
121     }
122 }
123
124 /*!
125     \property QxtProgressLabel::timeFormat
126     \brief This property holds the time format of the progress label
127
128     Time values are formatted according to this property.
129     The default value is an empty string which defaults to \b "mm:ss".
130
131     \sa contentFormat, QTime::toString()
132  */
133 QString QxtProgressLabel::timeFormat() const
134 {
135     return qxt_d().tformat;
136 }
137
138 void QxtProgressLabel::setTimeFormat(const QString& format)
139 {
140     if (qxt_d().tformat != format)
141     {
142         qxt_d().tformat = format;
143         refresh();
144     }
145 }
146
147 /*!
148     \property QxtProgressLabel::updateInterval
149     \brief This property holds the update interval of the progress label
150
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.
154  */
155 int QxtProgressLabel::updateInterval() const
156 {
157     return qxt_d().interval;
158 }
159
160 void QxtProgressLabel::setUpdateInterval(int msecs)
161 {
162     qxt_d().interval = msecs;
163     if (msecs < 0)
164     {
165         if (qxt_d().timer.isActive())
166             qxt_d().timer.stop();
167     }
168     else
169     {
170         if (!qxt_d().timer.isActive())
171             qxt_d().timer.start(msecs, this);
172     }
173 }
174
175 /*!
176     Sets the current value to \a value.
177
178     \note Calling this slot by hand has no effect.
179     Connect this slot to \b QProgressBar::valueChange().
180  */
181 void QxtProgressLabel::setValue(int value)
182 {
183     QProgressBar* bar = qobject_cast<QProgressBar*>(sender());
184     if (bar)
185     {
186         if (!qxt_d().start.isValid())
187             restart();
188
189         qxt_d().cachedMin = bar->minimum();
190         qxt_d().cachedMax = bar->maximum();
191         qxt_d().cachedVal = value;
192
193         refresh();
194     }
195 }
196
197 /*!
198     Restarts the calculation of elapsed and remaining times.
199  */
200 void QxtProgressLabel::restart()
201 {
202     qxt_d().cachedMin = 0;
203     qxt_d().cachedMax = 0;
204     qxt_d().cachedVal = 0;
205     qxt_d().start.restart();
206     refresh();
207 }
208
209 /*!
210     Refreshes the content.
211  */
212 void QxtProgressLabel::refresh()
213 {
214     // elapsed
215     qreal elapsed = 0;
216     if (qxt_d().start.isValid())
217         elapsed = qxt_d().start.elapsed() / 1000.0;
218     QTime etime(0, 0);
219     etime = etime.addSecs(static_cast<int>(elapsed));
220
221     // percentage
222     qreal percent = 0;
223     if (qxt_d().cachedMax != 0)
224         percent = (qxt_d().cachedVal - qxt_d().cachedMin) / static_cast<qreal>(qxt_d().cachedMax);
225     qreal total = 0;
226     if (percent != 0)
227         total = elapsed / percent;
228
229     // remaining
230     QTime rtime(0, 0);
231     rtime = rtime.addSecs(static_cast<int>(total - elapsed));
232
233     // format
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");
240
241     QString result = QString(cformat).replace("%e", etime.toString(tformat));
242     result = result.replace("%r", rtime.toString(tformat));
243     setText(result);
244 }
245
246 void QxtProgressLabel::timerEvent(QTimerEvent* event)
247 {
248     Q_UNUSED(event);
249     refresh();
250 }