shadow element is optional

cache frame pixmaps since qpainter rtation is expensive

svn path=/trunk/KDE/kdelibs/; revision=879936
This commit is contained in:
Marco Martin 2008-11-04 12:04:54 +00:00
parent 2592544047
commit 27238f17b7
2 changed files with 38 additions and 15 deletions

View File

@ -46,12 +46,18 @@ public:
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;
};
@ -64,7 +70,7 @@ BusyWidget::BusyWidget(QGraphicsWidget *parent)
d->svg = new Plasma::Svg(this);
d->svg->setImagePath("widgets/busywidget");
d->svg->setContainsMultipleImages(true);
d->rotationAngle = d->svg->elementSize("hint-rotation-angle").width();
d->themeChanged();
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
}
@ -94,21 +100,32 @@ void BusyWidget::paint(QPainter *painter,
Q_UNUSED(option)
Q_UNUSED(widget)
int intRotation = int(d->rotation);
if (!d->frames[intRotation]) {
QPointF translatedPos(size().width()/2.0, size().height()/2.0);
painter->save();
painter->setRenderHints(QPainter::SmoothPixmapTransform);
painter->translate(translatedPos);
d->frames[intRotation] = QPixmap(size().toSize());
d->frames[intRotation].fill(Qt::transparent);
painter->save();
painter->translate(2,2);
painter->rotate(d->rotation);
d->svg->paint(painter, QRect(-translatedPos.toPoint(), size().toSize()), "busywidget-shadow");
painter->restore();
QPainter buffPainter(&d->frames[intRotation]);
painter->rotate(d->rotation);
d->svg->paint(painter, QRect(-translatedPos.toPoint(), size().toSize()), "busywidget");
painter->restore();
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(), size().toSize()), "busywidget-shadow");
buffPainter.restore();
}
buffPainter.rotate(intRotation);
d->svg->paint(&buffPainter, QRect(-translatedPos.toPoint(), size().toSize()), "busywidget");
}
painter->drawPixmap(QPoint(0,0), d->frames[intRotation]);
}
void BusyWidget::showEvent(QShowEvent *event)
@ -124,6 +141,11 @@ void BusyWidget::hideEvent(QHideEvent *event)
d->timerId = 0;
}
void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
d->frames.clear();
}
} // namespace Plasma
#include <busywidget.moc>

View File

@ -58,6 +58,7 @@ protected:
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void resizeEvent(QGraphicsSceneResizeEvent *event);
protected Q_SLOTS:
void timerEvent(QTimerEvent *event);