Plasma
busywidget.cpp
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 #include "busywidget.h"
00021
00022
00023 #include <QPainter>
00024 #include <QTimer>
00025 #include <QGraphicsSceneResizeEvent>
00026
00027
00028 #include "plasma/theme.h"
00029 #include "plasma/svg.h"
00030
00031 namespace Plasma
00032 {
00033
00034 class BusyWidgetPrivate
00035 {
00036 public:
00037 BusyWidgetPrivate()
00038 : svg(0),
00039 timerId(0)
00040 {
00041 }
00042
00043 ~BusyWidgetPrivate()
00044 {
00045 }
00046
00047 void themeChanged()
00048 {
00049 frames.clear();
00050 rotationAngle = svg->elementSize("hint-rotation-angle").width();
00051
00052
00053 int nFrames = 360/rotationAngle;
00054 rotationAngle = 360/nFrames;
00055 }
00056
00057 Svg *svg;
00058 QString styleSheet;
00059 int timerId;
00060 QHash<int, QPixmap> frames;
00061 qreal rotationAngle;
00062 qreal rotation;
00063 };
00064
00065
00066 BusyWidget::BusyWidget(QGraphicsWidget *parent)
00067 : QGraphicsWidget(parent),
00068 d(new BusyWidgetPrivate)
00069 {
00070 d->svg = new Plasma::Svg(this);
00071 d->svg->setImagePath("widgets/busywidget");
00072 d->svg->setContainsMultipleImages(true);
00073 d->themeChanged();
00074
00075 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
00076 }
00077
00078 BusyWidget::~BusyWidget()
00079 {
00080 delete d;
00081 }
00082
00083 void BusyWidget::timerEvent(QTimerEvent *event)
00084 {
00085 Q_UNUSED(event)
00086
00087 d->rotation += d->rotationAngle;
00088
00089 qreal overflow = d->rotation - 360;
00090 if ( overflow > 0) {
00091 d->rotation = overflow;
00092 }
00093 update();
00094 }
00095
00096 void BusyWidget::paint(QPainter *painter,
00097 const QStyleOptionGraphicsItem *option,
00098 QWidget *widget)
00099 {
00100 Q_UNUSED(option)
00101 Q_UNUSED(widget)
00102
00103 int intRotation = int(d->rotation);
00104
00105 if (!d->frames[intRotation]) {
00106 QPointF translatedPos(size().width()/2.0, size().height()/2.0);
00107
00108 d->frames[intRotation] = QPixmap(size().toSize());
00109 d->frames[intRotation].fill(Qt::transparent);
00110
00111 QPainter buffPainter(&d->frames[intRotation]);
00112
00113 buffPainter.setRenderHints(QPainter::SmoothPixmapTransform);
00114 buffPainter.translate(translatedPos);
00115
00116 if (d->svg->hasElement("busywidget-shadow")) {
00117 buffPainter.save();
00118 buffPainter.translate(2,2);
00119 buffPainter.rotate(intRotation);
00120 d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), size().toSize()), "busywidget-shadow");
00121 buffPainter.restore();
00122 }
00123
00124 buffPainter.rotate(intRotation);
00125 d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), size().toSize()), "busywidget");
00126 }
00127
00128 painter->drawPixmap(QPoint(0,0), d->frames[intRotation]);
00129 }
00130
00131 void BusyWidget::showEvent(QShowEvent *event)
00132 {
00133 Q_UNUSED(event)
00134 d->timerId = startTimer(150);
00135 }
00136
00137 void BusyWidget::hideEvent(QHideEvent *event)
00138 {
00139 Q_UNUSED(event)
00140 if (d->timerId)
00141 killTimer(d->timerId);
00142 d->timerId = 0;
00143 }
00144
00145 void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
00146 {
00147 Q_UNUSED(event)
00148 d->frames.clear();
00149 }
00150
00151 }
00152
00153 #include <busywidget.moc>
00154