Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qxtprogresslabel.h"
00026 #include <QProgressBar>
00027 #include <QBasicTimer>
00028 #include <QTime>
00029
00030 class QxtProgressLabelPrivate : public QxtPrivate<QxtProgressLabel>
00031 {
00032 public:
00033 QXT_DECLARE_PUBLIC(QxtProgressLabel);
00034 QxtProgressLabelPrivate();
00035
00036 QTime start;
00037 int interval;
00038 int cachedMin;
00039 int cachedMax;
00040 int cachedVal;
00041 QString cformat;
00042 QString tformat;
00043 QBasicTimer timer;
00044 };
00045
00046 QxtProgressLabelPrivate::QxtProgressLabelPrivate()
00047 : interval(-1), cachedMin(0), cachedMax(0), cachedVal(0)
00048 {}
00049
00072 QxtProgressLabel::QxtProgressLabel(QWidget* parent, Qt::WindowFlags flags)
00073 : QLabel(parent, flags)
00074 {
00075 QXT_INIT_PRIVATE(QxtProgressLabel);
00076 refresh();
00077 }
00078
00082 QxtProgressLabel::QxtProgressLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags)
00083 : QLabel(text, parent, flags)
00084 {
00085 QXT_INIT_PRIVATE(QxtProgressLabel);
00086 refresh();
00087 }
00088
00092 QxtProgressLabel::~QxtProgressLabel()
00093 {}
00094
00111 QString QxtProgressLabel::contentFormat() const
00112 {
00113 return qxt_d().cformat;
00114 }
00115
00116 void QxtProgressLabel::setContentFormat(const QString& format)
00117 {
00118 if (qxt_d().cformat != format)
00119 {
00120 qxt_d().cformat = format;
00121 refresh();
00122 }
00123 }
00124
00134 QString QxtProgressLabel::timeFormat() const
00135 {
00136 return qxt_d().tformat;
00137 }
00138
00139 void QxtProgressLabel::setTimeFormat(const QString& format)
00140 {
00141 if (qxt_d().tformat != format)
00142 {
00143 qxt_d().tformat = format;
00144 refresh();
00145 }
00146 }
00147
00156 int QxtProgressLabel::updateInterval() const
00157 {
00158 return qxt_d().interval;
00159 }
00160
00161 void QxtProgressLabel::setUpdateInterval(int msecs)
00162 {
00163 qxt_d().interval = msecs;
00164 if (msecs < 0)
00165 {
00166 if (qxt_d().timer.isActive())
00167 qxt_d().timer.stop();
00168 }
00169 else
00170 {
00171 if (!qxt_d().timer.isActive())
00172 qxt_d().timer.start(msecs, this);
00173 }
00174 }
00175
00182 void QxtProgressLabel::setValue(int value)
00183 {
00184 QProgressBar* bar = qobject_cast<QProgressBar*>(sender());
00185 if (bar)
00186 {
00187 if (!qxt_d().start.isValid())
00188 restart();
00189
00190 qxt_d().cachedMin = bar->minimum();
00191 qxt_d().cachedMax = bar->maximum();
00192 qxt_d().cachedVal = value;
00193
00194 refresh();
00195 }
00196 }
00197
00201 void QxtProgressLabel::restart()
00202 {
00203 qxt_d().cachedMin = 0;
00204 qxt_d().cachedMax = 0;
00205 qxt_d().cachedVal = 0;
00206 qxt_d().start.restart();
00207 refresh();
00208 }
00209
00213 void QxtProgressLabel::refresh()
00214 {
00215
00216 qreal elapsed = 0;
00217 if (qxt_d().start.isValid())
00218 elapsed = qxt_d().start.elapsed() / 1000.0;
00219 QTime etime(0, 0);
00220 etime = etime.addSecs(static_cast<int>(elapsed));
00221
00222
00223 qreal percent = 0;
00224 if (qxt_d().cachedMax != 0)
00225 percent = (qxt_d().cachedVal - qxt_d().cachedMin) / static_cast<qreal>(qxt_d().cachedMax);
00226 qreal total = 0;
00227 if (percent != 0)
00228 total = elapsed / percent;
00229
00230
00231 QTime rtime(0, 0);
00232 rtime = rtime.addSecs(static_cast<int>(total - elapsed));
00233
00234
00235 QString tformat = qxt_d().tformat;
00236 if (tformat.isEmpty())
00237 tformat = tr("mm:ss");
00238 QString cformat = qxt_d().cformat;
00239 if (cformat.isEmpty())
00240 cformat = tr("ETA: %r");
00241
00242 QString result = QString(cformat).replace("%e", etime.toString(tformat));
00243 result = result.replace("%r", rtime.toString(tformat));
00244 setText(result);
00245 }
00246
00250 void QxtProgressLabel::timerEvent(QTimerEvent* event)
00251 {
00252 Q_UNUSED(event);
00253 refresh();
00254 }