• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

busywidget.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Marco Martin <notmart@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "busywidget.h"
00021 
00022 //Qt
00023 #include <QPainter>
00024 #include <QTimer>
00025 #include <QGraphicsSceneResizeEvent>
00026 
00027 //Plasma
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         //use an angle near to rotationAngle but that it fits an integer number of times in 360
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 } // namespace Plasma
00152 
00153 #include <busywidget.moc>
00154 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal