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

Plasma

  • plasma
  • widgets
flashinglabel.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2007 by André Duffeck <duffeck@kde.org>
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 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Stre
19 * et, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "flashinglabel.h"
23
24#include <QtCore/QString>
25#include <QtCore/QTimeLine>
26#include <QtCore/QTimer>
27#include <QtCore/QWeakPointer>
28#include <QtGui/QPainter>
29#include <QtGui/QPixmap>
30#include <QtGui/QColor>
31
32#include <kdebug.h>
33
34#include <plasma/animator.h>
35#include <plasma/animations/animation.h>
36#include <plasma/theme.h>
37
38using namespace Plasma;
39
40class Plasma::FlashingLabelPrivate
41{
42 public:
43 enum FlashingLabelType {
44 Text,
45 Pixmap
46 };
47 enum State {
48 Visible,
49 Invisible
50 };
51
52 FlashingLabelPrivate(FlashingLabel *flash)
53 : q(flash),
54 defaultDuration(3000),
55 type(FlashingLabelPrivate::Text),
56 color(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor)),
57 state(FlashingLabelPrivate::Invisible),
58 autohide(false)
59 {
60 fadeOutTimer.setInterval(defaultDuration);
61 fadeOutTimer.setSingleShot(true);
62 fadeInTimer.setInterval(0);
63 fadeInTimer.setSingleShot(true);
64 QObject::connect(Theme::defaultTheme(), SIGNAL(themeChanged()), q, SLOT(setPalette()));
65 }
66
67 ~FlashingLabelPrivate() { }
68
69 void renderPixmap(const QSize &size);
70 void setupFlash(int duration);
71 void elementAnimationFinished();
72 void setPalette();
73
74 FlashingLabel *q;
75 int defaultDuration;
76 FlashingLabelType type;
77 QTimer fadeInTimer;
78 QTimer fadeOutTimer;
79 QString text;
80 QColor color;
81 QFont font;
82 QPixmap pixmap;
83
84 QWeakPointer<Plasma::Animation> anim;
85 QPixmap renderedPixmap;
86
87 QTextOption textOption;
88 Qt::Alignment alignment;
89
90 State state;
91 bool autohide;
92};
93
94FlashingLabel::FlashingLabel(QGraphicsItem *parent)
95 : QGraphicsWidget(parent),
96 d(new FlashingLabelPrivate(this))
97{
98 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
99 setCacheMode(NoCache);
100 connect(&d->fadeOutTimer, SIGNAL(timeout()), this, SLOT(fadeOut()));
101 connect(&d->fadeInTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
102}
103
104FlashingLabel::~FlashingLabel()
105{
106 delete d;
107}
108
109int FlashingLabel::duration() const
110{
111 return d->defaultDuration;
112}
113
114void FlashingLabel::setDuration(int duration)
115{
116 if (duration < 1) {
117 return;
118 }
119
120 d->defaultDuration = duration;
121}
122
123QColor FlashingLabel::color() const
124{
125 return d->color;
126}
127
128void FlashingLabel::setColor(const QColor &color)
129{
130 d->color = color;
131}
132
133QFont FlashingLabel::font() const
134{
135 return d->font;
136}
137
138void FlashingLabel::setFont(const QFont &font)
139{
140 d->font = font;
141}
142
143void FlashingLabel::flash(const QString &text, int duration, const QTextOption &option)
144{
145 if (text.isEmpty()) {
146 return;
147 }
148
149 //kDebug() << duration << text;
150 d->type = FlashingLabelPrivate::Text;
151 d->text = text;
152 d->textOption = option;
153 d->setupFlash(duration);
154}
155
156void FlashingLabel::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
157{
158 if (pixmap.isNull()) {
159 return;
160 }
161
162 d->type = FlashingLabelPrivate::Pixmap;
163 d->pixmap = pixmap;
164 d->alignment = align;
165 d->setupFlash(duration);
166}
167
168void FlashingLabel::setAutohide(bool autohide)
169{
170 d->autohide = autohide;
171
172 if (autohide) {
173 if (d->anim.data()) {
174 connect(d->anim.data(), SIGNAL(finished()), this, SLOT(elementAnimationFinished()));
175 }
176 } else if (d->anim.data()) {
177 disconnect(d->anim.data(), SIGNAL(finished()), this, SLOT(elementAnimationFinished()));
178 }
179}
180
181bool FlashingLabel::autohide() const
182{
183 return d->autohide;
184}
185
186void FlashingLabel::kill()
187{
188 d->fadeInTimer.stop();
189 if (d->state == FlashingLabelPrivate::Visible) {
190 fadeOut();
191 }
192}
193
194void FlashingLabel::fadeIn()
195{
196 //kDebug();
197 if (d->autohide) {
198 show();
199 }
200
201 d->state = FlashingLabelPrivate::Visible;
202 if (!d->anim.data()) {
203 d->anim = Plasma::Animator::create(Plasma::Animator::PixmapTransitionAnimation);
204 Plasma::Animation *animation = d->anim.data();
205 animation->setProperty("startPixmap", d->renderedPixmap);
206 animation->setTargetWidget(this);
207 animation->start();
208 } else {
209 Plasma::Animation *animation = d->anim.data();
210 if (animation->state() == QAbstractAnimation::Running) {
211 animation->stop();
212 animation->start();
213 }
214 }
215}
216
217void FlashingLabel::fadeOut()
218{
219 if (d->state == FlashingLabelPrivate::Invisible) {
220 return; // FlashingLabel was already killed - do not animate again
221 }
222
223 d->state = FlashingLabelPrivate::Invisible;
224 if (d->anim.data()) {
225 Plasma::Animation *animation = d->anim.data();
226 animation->setProperty("direction", QAbstractAnimation::Backward);
227 animation->start(QAbstractAnimation::DeleteWhenStopped);
228 } else {
229 d->anim = Plasma::Animator::create(Plasma::Animator::PixmapTransitionAnimation);
230 Plasma::Animation *animation = d->anim.data();
231 animation->setProperty("direction", QAbstractAnimation::Backward);
232 animation->setProperty("startPixmap", d->renderedPixmap);
233 animation->setTargetWidget(this);
234 animation->start(QAbstractAnimation::DeleteWhenStopped);
235 }
236}
237
238void FlashingLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
239{
240 Q_UNUSED(option)
241 Q_UNUSED(widget)
242
243 if (d->anim.data() && d->anim.data()->state() == QAbstractAnimation::Running) {
244 Plasma::Animation *animation = d->anim.data();
245 painter->drawPixmap(0, 0, qvariant_cast<QPixmap>(animation->property("currentPixmap")));
246 } else if (d->state == FlashingLabelPrivate::Visible) {
247 painter->drawPixmap(0, 0, d->renderedPixmap);
248 }
249}
250
251QSizeF FlashingLabel::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
252{
253 if (which == Qt::PreferredSize) {
254 QFontMetrics fm(d->font);
255 return fm.boundingRect(d->text).size();
256 }
257 return QGraphicsWidget::sizeHint(which, constraint);
258}
259
260void FlashingLabelPrivate::renderPixmap(const QSize &size)
261{
262 if (renderedPixmap.size() != size) {
263 renderedPixmap = QPixmap(size);
264 }
265 renderedPixmap.fill(Qt::transparent);
266
267 QPainter painter(&renderedPixmap);
268 if (type == FlashingLabelPrivate::Text) {
269 painter.setPen(color);
270 painter.setFont(font);
271 painter.drawText(QRect(QPoint(0, 0), size), text, textOption);
272 } else if (type == FlashingLabelPrivate::Pixmap) {
273 QPoint p;
274
275 if(alignment & Qt::AlignLeft) {
276 p.setX(0);
277 } else if (alignment & Qt::AlignRight) {
278 p.setX(size.width() - pixmap.width());
279 } else {
280 p.setX((size.width() - pixmap.width()) / 2);
281 }
282
283 if (alignment & Qt::AlignTop) {
284 p.setY(0);
285 } else if (alignment & Qt::AlignRight) {
286 p.setY(size.height() - pixmap.height());
287 } else {
288 p.setY((size.height() - pixmap.height()) / 2);
289 }
290
291 painter.drawPixmap(p, pixmap);
292 }
293 painter.end();
294
295 if (anim.data()) {
296 Plasma::Animation *animation = anim.data();
297 animation->setProperty("startPixmap", renderedPixmap);
298 }
299}
300
301void FlashingLabelPrivate::setupFlash(int duration)
302{
303 fadeOutTimer.stop();
304 fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
305
306 renderPixmap(q->size().toSize());
307 if (state != FlashingLabelPrivate::Visible) {
308 fadeInTimer.start();
309 } else {
310 q->update();
311 }
312
313 if (fadeOutTimer.interval() > 0) {
314 fadeOutTimer.start();
315 }
316}
317
318void FlashingLabelPrivate::elementAnimationFinished()
319{
320 if (autohide && state == FlashingLabelPrivate::Invisible && anim.data()) {
321 q->hide();
322 }
323}
324
325void FlashingLabelPrivate::setPalette()
326{
327 color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
328 q->update();
329}
330
331#include "flashinglabel.moc"
animation.h
animator.h
Plasma::Animation
Abstract representation of a single animation.
Definition: animation.h:47
Plasma::Animator::create
static Plasma::Animation * create(Animator::Animation type, QObject *parent=0)
Factory to build new animation objects.
Definition: animator.cpp:61
Plasma::Animator::PixmapTransitionAnimation
@ PixmapTransitionAnimation
Definition: animator.h:68
Plasma::FlashingLabel
Provides flashing text or icons inside Plasma.
Definition: flashinglabel.h:40
Plasma::FlashingLabel::setAutohide
void setAutohide(bool autohide)
Definition: flashinglabel.cpp:168
Plasma::FlashingLabel::setColor
void setColor(const QColor &)
Definition: flashinglabel.cpp:128
Plasma::FlashingLabel::color
QColor color
Definition: flashinglabel.h:44
Plasma::FlashingLabel::kill
void kill()
Definition: flashinglabel.cpp:186
Plasma::FlashingLabel::FlashingLabel
FlashingLabel(QGraphicsItem *parent=0)
Definition: flashinglabel.cpp:94
Plasma::FlashingLabel::setDuration
void setDuration(int duration)
Definition: flashinglabel.cpp:114
Plasma::FlashingLabel::autohide
bool autohide
Definition: flashinglabel.h:42
Plasma::FlashingLabel::duration
int duration
Definition: flashinglabel.h:45
Plasma::FlashingLabel::fadeOut
void fadeOut()
Definition: flashinglabel.cpp:217
Plasma::FlashingLabel::setFont
void setFont(const QFont &)
Definition: flashinglabel.cpp:138
Plasma::FlashingLabel::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: flashinglabel.cpp:238
Plasma::FlashingLabel::flash
Q_INVOKABLE void flash(const QString &text, int duration=0, const QTextOption &option=QTextOption(Qt::AlignCenter))
Definition: flashinglabel.cpp:143
Plasma::FlashingLabel::fadeIn
void fadeIn()
Definition: flashinglabel.cpp:194
Plasma::FlashingLabel::font
QFont font
Definition: flashinglabel.h:43
Plasma::FlashingLabel::sizeHint
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
Definition: flashinglabel.cpp:251
Plasma::FlashingLabel::~FlashingLabel
virtual ~FlashingLabel()
Definition: flashinglabel.cpp:104
Plasma::Theme
Interface to the Plasma theme.
Definition: theme.h:57
Plasma::Theme::color
Q_INVOKABLE QColor color(ColorRole role) const
Returns the text color to be used by items resting on the background.
Definition: theme.cpp:918
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
flashinglabel.h
Plasma::AnimationScriptEngine::animation
QScriptValue animation(const QString &anim)
Definition: animationscriptengine.cpp:55
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
Plasma::type
static QScriptValue type(QScriptContext *ctx, QScriptEngine *eng)
Definition: easingcurve.cpp:63
Plasma::NoCache
@ NoCache
Definition: theme.cpp:68
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