Added to the Plasma widgets library the ProgressBar class. It's included a small program test for the progressbar.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=712543
This commit is contained in:
Jon de Andres Frias 2007-09-14 18:19:42 +00:00
parent c24d23c1f9
commit e726cd04c6
5 changed files with 520 additions and 0 deletions

View File

@ -43,6 +43,7 @@ set(plasma_LIB_SRCS
widgets/layoutanimator.cpp widgets/layoutanimator.cpp
widgets/layoutitem.cpp widgets/layoutitem.cpp
widgets/lineedit.cpp widgets/lineedit.cpp
widgets/progressbar.cpp
widgets/pushbutton.cpp widgets/pushbutton.cpp
widgets/radiobutton.cpp widgets/radiobutton.cpp
# widgets/rectangle.cpp # widgets/rectangle.cpp
@ -115,6 +116,7 @@ install(FILES
widgets/layoutanimator.h widgets/layoutanimator.h
widgets/layoutitem.h widgets/layoutitem.h
widgets/lineedit.h widgets/lineedit.h
widgets/progressbar.cpp
widgets/pushbutton.h widgets/pushbutton.h
widgets/checkbox.h widgets/checkbox.h
widgets/radiobutton.h widgets/radiobutton.h

254
widgets/progressbar.cpp Normal file
View File

@ -0,0 +1,254 @@
/*
* Copyright (C) 2007 Jon de Andres Frias <jondeandres@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "progressbar.h"
#include <QWidget>
#include <QStyleOptionProgressBarV2>
#include <QPainter>
#include <QStyle>
#include <QtGlobal>
#include <QApplication>
#include <kdebug.h>
namespace Plasma
{
class ProgressBar::Private
{
public:
Private() {}
~Private() {}
QString format;
QString text;
Qt::Alignment alignment;
bool invertedAppearance;
bool textVisible;
int maximum;
int minimum;
int value;
int lastPaintedValue;
};
ProgressBar::ProgressBar(Widget *parent)
: Plasma::Widget(parent),
d(new Private)
{
init();
}
ProgressBar::~ProgressBar() {}
void ProgressBar::init()
{
setAlignment(Qt::AlignLeft);
setFormat(QLatin1String("%p%"));
setInvertedAppearance(false);
setTextVisible(true);
setMinimum(0);
setMaximum(100);
setValue(-1);
d->lastPaintedValue = -1;
}
void ProgressBar::setAlignment(Qt::Alignment alignment)
{
if (d->alignment != alignment) {
d->alignment = alignment;
update();
}
}
Qt::Alignment ProgressBar::alignment() const
{
return d->alignment;
}
void ProgressBar::setFormat(const QString &format)
{
if (d->format != format) {
d->format = format;
update();
}
}
QString ProgressBar::format() const
{
return d->format;
}
void ProgressBar::setInvertedAppearance(bool invert)
{
if (d->invertedAppearance & invert) {
return;
}
d->invertedAppearance = invert;
update();
}
bool ProgressBar::invertedAppearance()
{
return d->invertedAppearance;
}
void ProgressBar::setTextVisible(bool visible)
{
if (d->textVisible & visible) {
return;
}
d->textVisible = visible;
}
bool ProgressBar::isTextVisible() const
{
return d->textVisible;
}
void ProgressBar::reset()
{
d->value = d->minimum - 1;
update();
}
void ProgressBar::setRange(int minimum, int maximum)
{
d->minimum = minimum;
d->maximum = maximum;
if (d->value < (d->minimum - 1) || d->value > d->maximum) {
reset();
}
}
void ProgressBar::setMinimum(int minimum)
{
setRange(minimum, qMax(d->maximum, minimum));
}
void ProgressBar::setMaximum(int maximum)
{
setRange(qMin(d->minimum, maximum), maximum);
}
int ProgressBar::maximum() const
{
return d->maximum;
}
int ProgressBar::minimum() const
{
return d->minimum;
}
void ProgressBar::setValue(int value)
{
//This line is from qprogressbar.cpp by Qt.
if (d->value == value
|| ((value > d->maximum || value < d->minimum)
&& (d->maximum != 0 || d->minimum != 0)))
return;
d->value = value;
emit valueChanged(value);
update();
}
int ProgressBar::value() const
{
return d->value;
}
void ProgressBar::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QStyle *style = widget ? widget->style() : QApplication::style();
QStyleOptionProgressBarV2 options;
options.minimum = d->minimum;
options.maximum = d->maximum;
options.progress = d->value;
options.textAlignment = d->alignment;
options.textVisible = d->textVisible;
options.text = text();
options.invertedAppearance = d->invertedAppearance;
options.rect = geometry().toRect();
style->drawControl(QStyle::CE_ProgressBar, &options, painter, widget);
}
QSizeF ProgressBar::sizeHint() const
{
QFontMetrics fm(font());
QStyleOptionProgressBarV2 options;
options.minimum = d->minimum;
options.maximum = d->maximum;
options.progress = d->value;
options.textAlignment = d->alignment;
options.textVisible = d->textVisible;
options.text = text();
options.invertedAppearance = d->invertedAppearance;
QStyle *style = QApplication::style();
int cw = style->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &options);
QSize size = QSize(cw * 7 + fm.width(QLatin1Char('0')) * 4, fm.height() + 8);
return style->sizeFromContents(QStyle::CT_ProgressBar, &options, size);
}
/*This method has the same code that QProgressBar::text() */
QString ProgressBar::text() const
{
qint64 totalSteps = qint64(d->maximum) - qint64(d->minimum);
QString result = d->format;
result.replace(QLatin1String("%m"), QString::fromLatin1("%1").arg(totalSteps));
result.replace(QLatin1String("%v"), QString::fromLatin1("%1").arg(d->value));
if (totalSteps == 0) {
result.replace(QLatin1String("%p"), QString::fromLatin1("%1").arg(100));
return result;
}
int progress = int(((qreal(d->value) - qreal(d->minimum)) * 100.0) / totalSteps);
result.replace(QLatin1String("%p"), QString::fromLatin1("%1").arg(progress));
return result;
}
}
#include "progressbar.moc"

143
widgets/progressbar.h Normal file
View File

@ -0,0 +1,143 @@
/*
* Copyright (C) 2007 Jon de Andres Frias <jondeandres@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 as
* published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __PLASMA_PROGRESSBAR__
#define __PLASMA_PROGRESSBAR__
#include <plasma/widgets/widget.h>
namespace Plasma
{
class PLASMA_EXPORT ProgressBar : public Plasma::Widget
{
Q_OBJECT
Q_PROPERTY(QString format READ format WRITE setFormat)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
Q_PROPERTY(bool textVisible READ isTextVisible WRITE setTextVisible)
Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
Q_PROPERTY(int value READ value WRITE setValue)
public:
/**
* Constructor
*/
ProgressBar(Widget *parent);
/**
* Virtual Destructor
*/
virtual ~ProgressBar();
/**
* @return alignment of the progressbar
*/
Qt::Alignment alignment() const;
/**
* @return the format for the shown text
*/
QString format() const;
/**
* @return wether the Plasma::ProgressBar is inverted or not.
*/
bool invertedAppearance();
/**
* @return wether the text is visible or not.
*/
bool isTextVisible() const;
/**
* @return the maximum value in the range of Plasma::ProgressBar
*/
int maximum() const;
/**
* @return the minimum value in the range of Plasma:ProgressBar
*/
int minimum() const;
/**
* Changes the alignment of Plasma::ProgressBar
*
* %p - is replaced by the percentage completed
* %v - is replaced by the current value
* %m - is replaced by the total number of steps
* @param alignment the alignment for the progressbar.
*/
void setAlignment(Qt::Alignment alignment);
/**
* Sets the format for the shown text in Plasma::ProgressBar;
* @format the format for the shown text in Plasma:ProgressBar
*/
void setFormat(const QString &format);
/**
* Inverts the appearance of Plasma::ProgressBar
* @param invert true inverts the appearance
*/
void setInvertedAppearance(bool invert);
/**
* Change the text to be visible or not.
* @param visible if true then visible
*/
void setTextVisible(bool visible);
/**
* @return the text to be shown in Plasma::ProgressBar
*/
QString text() const;
/**
* @return the actual value of the progress
*/
int value() const;
void paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
QSizeF sizeHint() const;
public Q_SLOTS:
void reset();
void setRange(int minimum, int maximum);
void setMinimum(int minimum);
void setMaximum(int maximum);
void setValue(int value);
Q_SIGNALS:
void valueChanged(int value);
private:
void init();
class Private;
Private * const d;
};
}
#endif /*__PLASMA_PROGRESSBAR__*/

View File

@ -8,3 +8,10 @@ set(testlayouts_SRCS
kde4_add_executable(testLayouts ${testlayouts_SRCS} ) kde4_add_executable(testLayouts ${testlayouts_SRCS} )
target_link_libraries( testLayouts ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} plasma) target_link_libraries( testLayouts ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} plasma)
set(testProgressBar_SRCS
testProgressBar.cpp
)
kde4_add_executable( testProgressBar ${testProgressBar_SRCS} )
target_link_libraries( testProgressBar ${KDE4_KDECORE_LIBS} ${KDE4_KDEUI_LIBS} plasma)

View File

@ -0,0 +1,114 @@
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include <KUniqueApplication>
#include <KCmdLineArgs>
#include <KCmdLineOptions>
#include <KAboutData>
#include <KIcon>
#include "../boxlayout.h"
#include "../widget.h"
#include "../progressbar.h"
class Counter : QObject
{
Q_OBJECT
public:
Counter(Plasma::ProgressBar *p);
~Counter();
void start();
private Q_SLOTS:
void updateValue();
private:
QTimer timer;
Plasma::ProgressBar *pbar;
int seconds;
};
Counter::Counter(Plasma::ProgressBar *p)
: QObject(), timer(0)
{
seconds = 10;
pbar = p;
}
Counter::~Counter()
{
}
void Counter::start()
{
connect(&timer, SIGNAL(timeout()),
this, SLOT(updateValue()));
timer.start(100);
}
void Counter::updateValue()
{
int inc = 1;
pbar->setValue(pbar->value() + inc);
if (pbar->value() >= 100) {
timer.stop();
}
}
int main(int argc, char **argv)
{
KAboutData aboutData(QByteArray("test"), 0, ki18n("test"),
KDE_VERSION_STRING, ki18n("test"), KAboutData::License_BSD,
ki18n("test"));
KCmdLineArgs::init(argc, argv, &aboutData);
KApplication app;
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
Plasma::VBoxLayout *widgetLayout = new Plasma::VBoxLayout(0);
widgetLayout->setGeometry(QRectF(0.0, 0.0, 350.0, 30.0));
Plasma::HBoxLayout *h1 = new Plasma::HBoxLayout(0);
widgetLayout->addItem(h1);
Plasma::ProgressBar *progressBar = new Plasma::ProgressBar(0);
h1->addItem(progressBar);
scene.addItem(progressBar);
widgetLayout->update();
view.show();
Counter *c = new Counter(progressBar);
c->start();
// Uncomment for hide the text.
// progressBar->setTextVisible( false );
//
// Uncomment for change the progressbar alignment
// progressBar->setAlignment( Qt::AlignRight );
//
// Uncomment in order to invert the progress, from right to left.
// progressBar->setInvertedAppearance( true );;
progressBar->setGeometry(QRectF(0, 0, 200, 30));
return app.exec();
}
#include "testLayouts.moc"