X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcontrib%2Flibqxt-2007-10-24%2Fsrc%2Fgui%2Fqxtprogresslabel.cpp;fp=src%2Fcontrib%2Flibqxt-2007-10-24%2Fsrc%2Fgui%2Fqxtprogresslabel.cpp;h=26591122808e7d86557ce44fcd0a1b67f162c0bf;hb=a634acadbcf6017474f68a3eaf7cb632660e9e49;hp=0000000000000000000000000000000000000000;hpb=cd122ca8e0d2c0ffc5397e0a813c75d791a7e6e3;p=quassel.git diff --git a/src/contrib/libqxt-2007-10-24/src/gui/qxtprogresslabel.cpp b/src/contrib/libqxt-2007-10-24/src/gui/qxtprogresslabel.cpp new file mode 100644 index 00000000..26591122 --- /dev/null +++ b/src/contrib/libqxt-2007-10-24/src/gui/qxtprogresslabel.cpp @@ -0,0 +1,250 @@ +/**************************************************************************** +** +** Copyright (C) Qxt Foundation. Some rights reserved. +** +** This file is part of the QxtGui module of the Qt eXTension library +** +** This library is free software; you can redistribute it and/or modify it +** under the terms of th Common Public License, version 1.0, as published by +** IBM. +** +** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY +** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY +** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR +** FITNESS FOR A PARTICULAR PURPOSE. +** +** You should have received a copy of the CPL along with this file. +** See the LICENSE file and the cpl1.0.txt file included with the source +** distribution for more information. If you did not receive a copy of the +** license, contact the Qxt Foundation. +** +** +** +****************************************************************************/ +#include "qxtprogresslabel.h" +#include +#include +#include + +class QxtProgressLabelPrivate : public QxtPrivate +{ +public: + QXT_DECLARE_PUBLIC(QxtProgressLabel); + QxtProgressLabelPrivate(); + + QTime start; + int interval; + int cachedMin; + int cachedMax; + int cachedVal; + QString cformat; + QString tformat; + QBasicTimer timer; +}; + +QxtProgressLabelPrivate::QxtProgressLabelPrivate() + : interval(-1), cachedMin(0), cachedMax(0), cachedVal(0) +{} + +/*! + \class QxtProgressLabel QxtProgressLabel + \ingroup QxtGui + \brief A label showing progress related time values. + + QxtProgressLabel is a label widget able to show elapsed and remaining + time of a progress. Usage is as simple as connecting signal + \b QProgressBar::valueChanged() to slot QxtProgressLabel::setValue(). + + Example usage: + \code + QProgressBar* bar = new QProgressBar(this); + QxtProgressLabel* label = new QxtProgressLabel(this); + connect(bar, SIGNAL(valueChanged(int)), label, SLOT(setValue(int))); + \endcode + + \image html qxtprogresslabel.png "QxtProgressLabel in action." + */ + +/*! + Constructs a new QxtProgressLabel with \a parent and \a flags. + */ +QxtProgressLabel::QxtProgressLabel(QWidget* parent, Qt::WindowFlags flags) + : QLabel(parent, flags) +{ + QXT_INIT_PRIVATE(QxtProgressLabel); + refresh(); +} + +/*! + Constructs a new QxtProgressLabel with \a text, \a parent and \a flags. + */ +QxtProgressLabel::QxtProgressLabel(const QString& text, QWidget* parent, Qt::WindowFlags flags) + : QLabel(text, parent, flags) +{ + QXT_INIT_PRIVATE(QxtProgressLabel); + refresh(); +} + +/*! + Destructs the progress label. + */ +QxtProgressLabel::~QxtProgressLabel() +{} + +/*! + \property QxtProgressLabel::contentFormat + \brief This property holds the content format of the progress label + + The content of the label is formatted according to this property. + The default value is an empty string which defaults to \b "ETA: %r". + + The following variables may be used in the format string: + + + + +
VariableOutput
\%eelapsed time
\%rremaining time
+ + \sa timeFormat + */ +QString QxtProgressLabel::contentFormat() const +{ + return qxt_d().cformat; +} + +void QxtProgressLabel::setContentFormat(const QString& format) +{ + if (qxt_d().cformat != format) + { + qxt_d().cformat = format; + refresh(); + } +} + +/*! + \property QxtProgressLabel::timeFormat + \brief This property holds the time format of the progress label + + Time values are formatted according to this property. + The default value is an empty string which defaults to \b "mm:ss". + + \sa contentFormat, QTime::toString() + */ +QString QxtProgressLabel::timeFormat() const +{ + return qxt_d().tformat; +} + +void QxtProgressLabel::setTimeFormat(const QString& format) +{ + if (qxt_d().tformat != format) + { + qxt_d().tformat = format; + refresh(); + } +} + +/*! + \property QxtProgressLabel::updateInterval + \brief This property holds the update interval of the progress label + + The content of the progress label is updated according to this interval. + A negative interval makes the content to update only during value changes. + The default value is \b -1. + */ +int QxtProgressLabel::updateInterval() const +{ + return qxt_d().interval; +} + +void QxtProgressLabel::setUpdateInterval(int msecs) +{ + qxt_d().interval = msecs; + if (msecs < 0) + { + if (qxt_d().timer.isActive()) + qxt_d().timer.stop(); + } + else + { + if (!qxt_d().timer.isActive()) + qxt_d().timer.start(msecs, this); + } +} + +/*! + Sets the current value to \a value. + + \note Calling this slot by hand has no effect. + Connect this slot to \b QProgressBar::valueChange(). + */ +void QxtProgressLabel::setValue(int value) +{ + QProgressBar* bar = qobject_cast(sender()); + if (bar) + { + if (!qxt_d().start.isValid()) + restart(); + + qxt_d().cachedMin = bar->minimum(); + qxt_d().cachedMax = bar->maximum(); + qxt_d().cachedVal = value; + + refresh(); + } +} + +/*! + Restarts the calculation of elapsed and remaining times. + */ +void QxtProgressLabel::restart() +{ + qxt_d().cachedMin = 0; + qxt_d().cachedMax = 0; + qxt_d().cachedVal = 0; + qxt_d().start.restart(); + refresh(); +} + +/*! + Refreshes the content. + */ +void QxtProgressLabel::refresh() +{ + // elapsed + qreal elapsed = 0; + if (qxt_d().start.isValid()) + elapsed = qxt_d().start.elapsed() / 1000.0; + QTime etime(0, 0); + etime = etime.addSecs(static_cast(elapsed)); + + // percentage + qreal percent = 0; + if (qxt_d().cachedMax != 0) + percent = (qxt_d().cachedVal - qxt_d().cachedMin) / static_cast(qxt_d().cachedMax); + qreal total = 0; + if (percent != 0) + total = elapsed / percent; + + // remaining + QTime rtime(0, 0); + rtime = rtime.addSecs(static_cast(total - elapsed)); + + // format + QString tformat = qxt_d().tformat; + if (tformat.isEmpty()) + tformat = tr("mm:ss"); + QString cformat = qxt_d().cformat; + if (cformat.isEmpty()) + cformat = tr("ETA: %r"); + + QString result = QString(cformat).replace("%e", etime.toString(tformat)); + result = result.replace("%r", rtime.toString(tformat)); + setText(result); +} + +void QxtProgressLabel::timerEvent(QTimerEvent* event) +{ + Q_UNUSED(event); + refresh(); +}