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

Plasma

  • 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
32namespace Plasma
33{
34
35class BusyWidgetPrivate
36{
37public:
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
72BusyWidget::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
84BusyWidget::~BusyWidget()
85{
86 delete d;
87}
88
89void 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
100bool BusyWidget::isRunning() const
101{
102 return d->running;
103}
104
105void BusyWidget::setLabel(const QString &label)
106{
107 d->label = label;
108 update();
109}
110
111QString BusyWidget::label() const
112{
113 return d->label;
114}
115
116void 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
132void 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
180void BusyWidget::showEvent(QShowEvent *event)
181{
182 Q_UNUSED(event)
183 if (d->running) {
184 d->timerId = startTimer(150);
185 }
186}
187
188void 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
198void BusyWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
199{
200 Q_UNUSED(event)
201 d->frames.clear();
202}
203
204void BusyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
205{
206 event->setAccepted(true);
207}
208
209void 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
busywidget.h
Plasma::BusyWidget::clicked
void clicked()
Plasma::BusyWidget::label
QString label
Definition: busywidget.h:45
Plasma::BusyWidget::running
bool running
Definition: busywidget.h:44
Plasma::Svg
A theme aware image-centric SVG class.
Definition: svg.h:57
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::Theme::TextColor
@ TextColor
the text color to be used by items resting on the background
Definition: theme.h:63
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
svg.h
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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