2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* 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>
|
2009-04-16 00:21:29 +02:00
|
|
|
#include <QTextOption>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
//Plasma
|
|
|
|
#include "plasma/theme.h"
|
|
|
|
#include "plasma/svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class BusyWidgetPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BusyWidgetPrivate()
|
|
|
|
: svg(0),
|
2009-01-29 18:04:46 +01:00
|
|
|
timerId(0),
|
|
|
|
rotationAngle(0),
|
2009-04-16 00:21:29 +02:00
|
|
|
rotation(0),
|
|
|
|
running(true)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BusyWidgetPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void themeChanged()
|
|
|
|
{
|
2008-11-04 13:04:54 +01:00
|
|
|
frames.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
rotationAngle = svg->elementSize("hint-rotation-angle").width();
|
2008-11-04 13:04:54 +01:00
|
|
|
|
|
|
|
//use an angle near to rotationAngle but that it fits an integer number of times in 360
|
|
|
|
int nFrames = 360/rotationAngle;
|
|
|
|
rotationAngle = 360/nFrames;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Svg *svg;
|
|
|
|
QString styleSheet;
|
|
|
|
int timerId;
|
2008-11-04 13:04:54 +01:00
|
|
|
QHash<int, QPixmap> frames;
|
2008-11-04 00:08:39 +01:00
|
|
|
qreal rotationAngle;
|
|
|
|
qreal rotation;
|
2009-04-16 00:21:29 +02:00
|
|
|
bool running;
|
|
|
|
QString label;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
BusyWidget::BusyWidget(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new BusyWidgetPrivate)
|
|
|
|
{
|
|
|
|
d->svg = new Plasma::Svg(this);
|
|
|
|
d->svg->setImagePath("widgets/busywidget");
|
|
|
|
d->svg->setContainsMultipleImages(true);
|
2008-11-04 13:04:54 +01:00
|
|
|
d->themeChanged();
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
|
|
|
|
}
|
|
|
|
|
|
|
|
BusyWidget::~BusyWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-04-16 00:21:29 +02:00
|
|
|
void BusyWidget::setRunning(bool running)
|
|
|
|
{
|
|
|
|
if (running && !d->timerId && isVisible()) {
|
|
|
|
d->timerId = startTimer(150);
|
|
|
|
} else if (!running && d->timerId) {
|
|
|
|
killTimer(d->timerId);
|
|
|
|
d->timerId = 0;
|
|
|
|
}
|
|
|
|
d->running = running;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BusyWidget::isRunning() const
|
|
|
|
{
|
|
|
|
return d->running;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BusyWidget::setLabel(const QString &label)
|
|
|
|
{
|
|
|
|
d->label = label;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BusyWidget::label() const
|
|
|
|
{
|
|
|
|
return d->label;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void BusyWidget::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
2009-03-25 20:10:02 +01:00
|
|
|
if (event->timerId() != d->timerId) {
|
|
|
|
QObject::timerEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
d->rotation += d->rotationAngle;
|
|
|
|
|
|
|
|
qreal overflow = d->rotation - 360;
|
|
|
|
if ( overflow > 0) {
|
|
|
|
d->rotation = overflow;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
void BusyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-11-04 13:04:54 +01:00
|
|
|
int intRotation = int(d->rotation);
|
|
|
|
|
2009-03-05 14:05:12 +01:00
|
|
|
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));
|
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
if (!isEnabled()) {
|
|
|
|
painter->setOpacity(painter->opacity() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->running && d->svg->hasElement("paused")) {
|
|
|
|
d->svg->paint(painter, spinnerRect, "paused");
|
|
|
|
} else {
|
|
|
|
if (!d->frames[intRotation]) {
|
|
|
|
QPointF translatedPos(spinnerRect.width()/2, spinnerRect.height()/2);
|
|
|
|
|
|
|
|
d->frames[intRotation] = QPixmap(spinnerRect.size());
|
|
|
|
d->frames[intRotation].fill(Qt::transparent);
|
2008-11-04 13:04:54 +01:00
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
QPainter buffPainter(&d->frames[intRotation]);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
buffPainter.setRenderHints(QPainter::SmoothPixmapTransform);
|
|
|
|
buffPainter.translate(translatedPos);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
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();
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 13:04:54 +01:00
|
|
|
buffPainter.rotate(intRotation);
|
2009-05-06 20:35:31 +02:00
|
|
|
d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), spinnerRect.size()), "busywidget");
|
2008-11-04 13:04:54 +01:00
|
|
|
}
|
|
|
|
|
2009-05-06 20:35:31 +02:00
|
|
|
painter->drawPixmap(spinnerRect.topLeft(), d->frames[intRotation]);
|
2008-11-04 13:04:54 +01:00
|
|
|
}
|
|
|
|
|
2009-04-16 00:21:29 +02:00
|
|
|
painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::HighlightColor));
|
2009-05-10 18:15:42 +02:00
|
|
|
Qt::Alignment align(Qt::AlignVCenter | Qt::AlignHCenter);
|
|
|
|
painter->drawText(boundingRect(), d->label, QTextOption(align));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BusyWidget::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
2009-04-16 00:21:29 +02:00
|
|
|
if (d->running) {
|
|
|
|
d->timerId = startTimer(150);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BusyWidget::hideEvent(QHideEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
2009-03-25 20:10:02 +01:00
|
|
|
if (d->timerId) {
|
2009-01-08 12:27:39 +01:00
|
|
|
killTimer(d->timerId);
|
2009-03-25 20:10:02 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->timerId = 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 13:04:54 +01:00
|
|
|
void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
2009-01-08 12:27:39 +01:00
|
|
|
Q_UNUSED(event)
|
2008-11-04 13:04:54 +01:00
|
|
|
d->frames.clear();
|
|
|
|
}
|
|
|
|
|
2009-04-16 00:21:29 +02:00
|
|
|
void BusyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
emit clicked();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <busywidget.moc>
|
|
|
|
|