plasma-framework/widgets/busywidget.cpp
Aaron J. Seigo 599fcdd138 ignore timer id's that aren't ours
svn path=/trunk/KDE/kdelibs/; revision=944581
2009-03-25 19:10:02 +00:00

165 lines
4.1 KiB
C++

/*
* Copyright 2008 Marco Martin <notmart@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 as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 "busywidget.h"
//Qt
#include <QPainter>
#include <QTimer>
#include <QGraphicsSceneResizeEvent>
//Plasma
#include "plasma/theme.h"
#include "plasma/svg.h"
namespace Plasma
{
class BusyWidgetPrivate
{
public:
BusyWidgetPrivate()
: svg(0),
timerId(0),
rotationAngle(0),
rotation(0)
{
}
~BusyWidgetPrivate()
{
}
void themeChanged()
{
frames.clear();
rotationAngle = svg->elementSize("hint-rotation-angle").width();
//use an angle near to rotationAngle but that it fits an integer number of times in 360
int nFrames = 360/rotationAngle;
rotationAngle = 360/nFrames;
}
Svg *svg;
QString styleSheet;
int timerId;
QHash<int, QPixmap> frames;
qreal rotationAngle;
qreal rotation;
};
BusyWidget::BusyWidget(QGraphicsWidget *parent)
: QGraphicsWidget(parent),
d(new BusyWidgetPrivate)
{
d->svg = new Plasma::Svg(this);
d->svg->setImagePath("widgets/busywidget");
d->svg->setContainsMultipleImages(true);
d->themeChanged();
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
}
BusyWidget::~BusyWidget()
{
delete d;
}
void BusyWidget::timerEvent(QTimerEvent *event)
{
if (event->timerId() != d->timerId) {
QObject::timerEvent(event);
return;
}
d->rotation += d->rotationAngle;
qreal overflow = d->rotation - 360;
if ( overflow > 0) {
d->rotation = overflow;
}
update();
}
void BusyWidget::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
int intRotation = int(d->rotation);
QRect spinnerRect(QPoint(0, 0), QSize(qMin(size().width(), size().height()), qMin(size().width(), size().height())));
spinnerRect.moveCenter(QPoint(size().width()/2, size().height()/2));
if (!d->frames[intRotation]) {
QPointF translatedPos(spinnerRect.width()/2, spinnerRect.height()/2);
d->frames[intRotation] = QPixmap(spinnerRect.size());
d->frames[intRotation].fill(Qt::transparent);
QPainter buffPainter(&d->frames[intRotation]);
buffPainter.setRenderHints(QPainter::SmoothPixmapTransform);
buffPainter.translate(translatedPos);
if (d->svg->hasElement("busywidget-shadow")) {
buffPainter.save();
buffPainter.translate(2,2);
buffPainter.rotate(intRotation);
d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), spinnerRect.size()), "busywidget-shadow");
buffPainter.restore();
}
buffPainter.rotate(intRotation);
d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), spinnerRect.size()), "busywidget");
}
painter->drawPixmap(spinnerRect.topLeft(), d->frames[intRotation]);
}
void BusyWidget::showEvent(QShowEvent *event)
{
Q_UNUSED(event)
d->timerId = startTimer(150);
}
void BusyWidget::hideEvent(QHideEvent *event)
{
Q_UNUSED(event)
if (d->timerId) {
killTimer(d->timerId);
}
d->timerId = 0;
}
void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
Q_UNUSED(event)
d->frames.clear();
}
} // namespace Plasma
#include <busywidget.moc>