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>
38 using namespace Plasma;
40 class Plasma::FlashingLabelPrivate
43 enum FlashingLabelType {
54 defaultDuration(3000),
55 type(FlashingLabelPrivate::Text),
56 color(Plasma::
Theme::defaultTheme()->color(Plasma::
Theme::TextColor)),
57 state(FlashingLabelPrivate::Invisible),
60 fadeOutTimer.setInterval(defaultDuration);
61 fadeOutTimer.setSingleShot(
true);
62 fadeInTimer.setInterval(0);
63 fadeInTimer.setSingleShot(
true);
67 ~FlashingLabelPrivate() { }
69 void renderPixmap(
const QSize &size);
70 void setupFlash(
int duration);
71 void elementAnimationFinished();
76 FlashingLabelType
type;
84 QWeakPointer<Plasma::Animation> anim;
85 QPixmap renderedPixmap;
87 QTextOption textOption;
88 Qt::Alignment alignment;
96 d(new FlashingLabelPrivate(this))
98 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
100 connect(&d->fadeOutTimer, SIGNAL(timeout()),
this, SLOT(
fadeOut()));
101 connect(&d->fadeInTimer, SIGNAL(timeout()),
this, SLOT(
fadeIn()));
111 return d->defaultDuration;
145 if (text.isEmpty()) {
150 d->type = FlashingLabelPrivate::Text;
152 d->textOption = option;
153 d->setupFlash(duration);
158 if (pixmap.isNull()) {
162 d->type = FlashingLabelPrivate::Pixmap;
164 d->alignment = align;
165 d->setupFlash(duration);
173 if (d->anim.data()) {
174 connect(d->anim.data(), SIGNAL(finished()),
this, SLOT(elementAnimationFinished()));
176 }
else if (d->anim.data()) {
177 disconnect(d->anim.data(), SIGNAL(finished()),
this, SLOT(elementAnimationFinished()));
188 d->fadeInTimer.stop();
189 if (d->state == FlashingLabelPrivate::Visible) {
201 d->state = FlashingLabelPrivate::Visible;
202 if (!d->anim.data()) {
205 animation->setProperty(
"startPixmap", d->renderedPixmap);
210 if (animation->state() == QAbstractAnimation::Running) {
219 if (d->state == FlashingLabelPrivate::Invisible) {
223 d->state = FlashingLabelPrivate::Invisible;
224 if (d->anim.data()) {
226 animation->setProperty(
"direction", QAbstractAnimation::Backward);
227 animation->start(QAbstractAnimation::DeleteWhenStopped);
231 animation->setProperty(
"direction", QAbstractAnimation::Backward);
232 animation->setProperty(
"startPixmap", d->renderedPixmap);
234 animation->start(QAbstractAnimation::DeleteWhenStopped);
243 if (d->anim.data() && d->anim.data()->state() == QAbstractAnimation::Running) {
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);
253 if (which == Qt::PreferredSize) {
254 QFontMetrics fm(d->font);
255 return fm.boundingRect(d->text).size();
257 return QGraphicsWidget::sizeHint(which, constraint);
260 void FlashingLabelPrivate::renderPixmap(
const QSize &size)
262 if (renderedPixmap.size() != size) {
263 renderedPixmap = QPixmap(size);
265 renderedPixmap.fill(Qt::transparent);
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) {
275 if(alignment & Qt::AlignLeft) {
277 }
else if (alignment & Qt::AlignRight) {
278 p.setX(size.width() - pixmap.width());
280 p.setX((size.width() - pixmap.width()) / 2);
283 if (alignment & Qt::AlignTop) {
285 }
else if (alignment & Qt::AlignRight) {
286 p.setY(size.height() - pixmap.height());
288 p.setY((size.height() - pixmap.height()) / 2);
291 painter.drawPixmap(p, pixmap);
297 animation->setProperty(
"startPixmap", renderedPixmap);
301 void FlashingLabelPrivate::setupFlash(
int duration)
304 fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
306 renderPixmap(q->size().toSize());
307 if (state != FlashingLabelPrivate::Visible) {
313 if (fadeOutTimer.interval() > 0) {
314 fadeOutTimer.start();
318 void FlashingLabelPrivate::elementAnimationFinished()
320 if (autohide && state == FlashingLabelPrivate::Invisible && anim.data()) {
325 void FlashingLabelPrivate::setPalette()
331 #include "flashinglabel.moc"
the text color to be used by items resting on the background
Abstract representation of a single animation.
QScriptValue animation(const QString &anim)
void setFont(const QFont &)
void setAutohide(bool autohide)
void setColor(const QColor &)
Provides flashing text or icons inside Plasma.
FlashingLabel(QGraphicsItem *parent=0)
Interface to the Plasma theme.
void setDuration(int duration)
static QScriptValue type(QScriptContext *ctx, QScriptEngine *eng)
static Theme * defaultTheme()
Singleton pattern accessor.
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
static Plasma::Animation * create(Animator::Animation type, QObject *parent=0)
Factory to build new animation objects.
Q_INVOKABLE QColor color(ColorRole role) const
Returns the text color to be used by items resting on the background.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Q_INVOKABLE void flash(const QString &text, int duration=0, const QTextOption &option=QTextOption(Qt::AlignCenter))
void setTargetWidget(QGraphicsWidget *widget)
Set the widget on which the animation is to be performed.