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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • plasma
  • widgets
busywidget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Marco Martin <notmart@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "busywidget.h"
21 
22 //Qt
23 #include <QPainter>
24 #include <QTimer>
25 #include <QGraphicsSceneResizeEvent>
26 #include <QTextOption>
27 
28 //Plasma
29 #include "plasma/theme.h"
30 #include "plasma/svg.h"
31 
32 namespace Plasma
33 {
34 
35 class BusyWidgetPrivate
36 {
37 public:
38  BusyWidgetPrivate()
39  : svg(0),
40  timerId(0),
41  rotationAngle(0),
42  rotation(0),
43  running(true)
44  {
45  }
46 
47  ~BusyWidgetPrivate()
48  {
49  }
50 
51  void themeChanged()
52  {
53  frames.clear();
54  rotationAngle = svg->elementSize("hint-rotation-angle").width();
55 
56  //use an angle near to rotationAngle but that it fits an integer number of times in 360
57  int nFrames = 360/rotationAngle;
58  rotationAngle = 360/nFrames;
59  }
60 
61  Svg *svg;
62  QString styleSheet;
63  int timerId;
64  QHash<int, QPixmap> frames;
65  qreal rotationAngle;
66  qreal rotation;
67  bool running;
68  QString label;
69 };
70 
71 
72 BusyWidget::BusyWidget(QGraphicsWidget *parent)
73  : QGraphicsWidget(parent),
74  d(new BusyWidgetPrivate)
75 {
76  d->svg = new Plasma::Svg(this);
77  d->svg->setImagePath("widgets/busywidget");
78  d->svg->setContainsMultipleImages(true);
79  d->themeChanged();
80 
81  connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(themeChanged()));
82 }
83 
84 BusyWidget::~BusyWidget()
85 {
86  delete d;
87 }
88 
89 void BusyWidget::setRunning(bool running)
90 {
91  if (running && !d->timerId && isVisible()) {
92  d->timerId = startTimer(150);
93  } else if (!running && d->timerId) {
94  killTimer(d->timerId);
95  d->timerId = 0;
96  }
97  d->running = running;
98 }
99 
100 bool BusyWidget::isRunning() const
101 {
102  return d->running;
103 }
104 
105 void BusyWidget::setLabel(const QString &label)
106 {
107  d->label = label;
108  update();
109 }
110 
111 QString BusyWidget::label() const
112 {
113  return d->label;
114 }
115 
116 void BusyWidget::timerEvent(QTimerEvent *event)
117 {
118  if (event->timerId() != d->timerId) {
119  QObject::timerEvent(event);
120  return;
121  }
122 
123  d->rotation += d->rotationAngle;
124 
125  qreal overflow = d->rotation - 360;
126  if ( overflow > 0) {
127  d->rotation = overflow;
128  }
129  update();
130 }
131 
132 void BusyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
133 {
134  Q_UNUSED(option)
135  Q_UNUSED(widget)
136 
137  int intRotation = int(d->rotation);
138 
139  QRectF spinnerRect(QPoint(0, 0), QSize(qMin(size().width(), size().height()), qMin(size().width(), size().height())));
140  spinnerRect.moveCenter(boundingRect().center());
141 
142  if (!isEnabled()) {
143  painter->setOpacity(painter->opacity() / 2);
144  }
145 
146  if (!d->running && d->svg->hasElement("paused")) {
147  d->svg->paint(painter, spinnerRect, "paused");
148  } else {
149  if (!d->frames[intRotation]) {
150  QPointF translatedPos(spinnerRect.width()/2, spinnerRect.height()/2);
151 
152  d->frames[intRotation] = QPixmap(spinnerRect.size().toSize());
153  d->frames[intRotation].fill(Qt::transparent);
154 
155  QPainter buffPainter(&d->frames[intRotation]);
156 
157  buffPainter.setRenderHints(QPainter::SmoothPixmapTransform);
158  buffPainter.translate(translatedPos);
159 
160  if (d->svg->hasElement("busywidget-shadow")) {
161  buffPainter.save();
162  buffPainter.translate(1,1);
163  buffPainter.rotate(intRotation);
164  d->svg->paint(&buffPainter, QRectF(-translatedPos.toPoint(), spinnerRect.size()), "busywidget-shadow");
165  buffPainter.restore();
166  }
167 
168  buffPainter.rotate(intRotation);
169  d->svg->paint(&buffPainter, QRectF(-translatedPos.toPoint(), spinnerRect.size()), "busywidget");
170  }
171 
172  painter->drawPixmap(spinnerRect.topLeft().toPoint(), d->frames[intRotation]);
173  }
174 
175  painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor));
176  Qt::Alignment align(Qt::AlignVCenter | Qt::AlignHCenter);
177  painter->drawText(boundingRect(), d->label, QTextOption(align));
178 }
179 
180 void BusyWidget::showEvent(QShowEvent *event)
181 {
182  Q_UNUSED(event)
183  if (d->running) {
184  d->timerId = startTimer(150);
185  }
186 }
187 
188 void BusyWidget::hideEvent(QHideEvent *event)
189 {
190  Q_UNUSED(event)
191  if (d->timerId) {
192  killTimer(d->timerId);
193  }
194 
195  d->timerId = 0;
196 }
197 
198 void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
199 {
200  Q_UNUSED(event)
201  d->frames.clear();
202 }
203 
204 void BusyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
205 {
206  event->setAccepted(true);
207 }
208 
209 void BusyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
210 {
211  if ((event->button() & Qt::LeftButton) ||
212  (event->buttons() & Qt::LeftButton)) {
213  emit clicked();
214  }
215 }
216 
217 } // namespace Plasma
218 
219 #include <busywidget.moc>
220 
Plasma::BusyWidget::timerEvent
void timerEvent(QTimerEvent *event)
Definition: busywidget.cpp:116
busywidget.h
Plasma::BusyWidget::running
bool running
Definition: busywidget.h:44
Plasma::BusyWidget::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: busywidget.cpp:198
Plasma::BusyWidget::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: busywidget.cpp:132
Plasma::BusyWidget::label
QString label() const
QWidget
Plasma::BusyWidget::isRunning
bool isRunning() const
Definition: busywidget.cpp:100
Plasma::Theme::TextColor
the text color to be used by items resting on the background
Definition: theme.h:63
theme.h
Plasma::BusyWidget::hideEvent
void hideEvent(QHideEvent *event)
Definition: busywidget.cpp:188
Plasma::BusyWidget::clicked
void clicked()
Plasma::BusyWidget::setLabel
void setLabel(const QString &label)
Definition: busywidget.cpp:105
Plasma::BusyWidget::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: busywidget.cpp:204
Plasma::BusyWidget::showEvent
void showEvent(QShowEvent *event)
Definition: busywidget.cpp:180
Plasma::BusyWidget::~BusyWidget
~BusyWidget()
Definition: busywidget.cpp:84
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::BusyWidget::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: busywidget.cpp:209
Plasma::BusyWidget::setRunning
void setRunning(bool running)
Definition: busywidget.cpp:89
QStyleOptionGraphicsItem
Plasma::Svg
A theme aware image-centric SVG class.
Definition: svg.h:56
svg.h
QGraphicsWidget
Plasma::BusyWidget::BusyWidget
BusyWidget(QGraphicsWidget *parent=0)
Constructs a new BusyWidget.
Definition: busywidget.cpp:72
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal