2007-07-22 13:02:34 +02:00
|
|
|
/*
|
2007-10-15 10:54:06 +02:00
|
|
|
* Copyright 2007 by André Duffeck <duffeck@kde.org>
|
2007-07-22 13:02:34 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Stre
|
|
|
|
* et, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "flashinglabel.h"
|
2007-07-22 13:02:34 +02:00
|
|
|
|
|
|
|
#include <QtCore/QString>
|
|
|
|
#include <QtCore/QTimeLine>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QPainter>
|
|
|
|
#include <QtGui/QPixmap>
|
|
|
|
#include <QtGui/QColor>
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
|
2008-04-25 05:23:31 +02:00
|
|
|
#include <plasma/animator.h>
|
2007-07-22 13:02:34 +02:00
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
class Plasma::FlashingLabelPrivate
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
public:
|
2008-10-29 18:46:33 +01:00
|
|
|
enum FlashingLabelType {
|
2008-10-12 03:31:54 +02:00
|
|
|
Text,
|
|
|
|
Pixmap
|
|
|
|
};
|
|
|
|
enum State {
|
|
|
|
Visible,
|
|
|
|
Invisible
|
|
|
|
};
|
2007-07-22 13:02:34 +02:00
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
FlashingLabelPrivate(FlashingLabel *flash)
|
2008-10-28 10:25:57 +01:00
|
|
|
: q(flash),
|
|
|
|
defaultDuration(3000),
|
2008-10-29 18:46:33 +01:00
|
|
|
type(FlashingLabelPrivate::Text),
|
2008-09-20 03:03:30 +02:00
|
|
|
color(Qt::black),
|
|
|
|
animId(0),
|
2008-10-29 18:46:33 +01:00
|
|
|
state(FlashingLabelPrivate::Invisible),
|
2008-10-28 10:25:57 +01:00
|
|
|
autohide(false)
|
2008-09-20 03:03:30 +02:00
|
|
|
{
|
|
|
|
//TODO: put this on a diet by using timerEvent instead?
|
|
|
|
fadeOutTimer.setInterval(defaultDuration);
|
|
|
|
fadeOutTimer.setSingleShot(true);
|
|
|
|
fadeInTimer.setInterval(0);
|
|
|
|
fadeInTimer.setSingleShot(true);
|
|
|
|
}
|
2008-10-28 10:25:57 +01:00
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
~FlashingLabelPrivate() { }
|
2007-07-22 13:02:34 +02:00
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void renderPixmap(const QSize &size);
|
2008-10-28 10:25:57 +01:00
|
|
|
void setupFlash(int duration);
|
|
|
|
void elementAnimationFinished(int);
|
2008-09-20 03:03:30 +02:00
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
FlashingLabel *q;
|
2008-09-20 03:03:30 +02:00
|
|
|
int defaultDuration;
|
2008-10-29 18:46:33 +01:00
|
|
|
FlashingLabelType type;
|
2008-09-20 03:03:30 +02:00
|
|
|
QTimer fadeInTimer;
|
|
|
|
QTimer fadeOutTimer;
|
2007-07-22 13:02:34 +02:00
|
|
|
QString text;
|
|
|
|
QColor color;
|
|
|
|
QFont font;
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
2008-05-08 13:45:49 +02:00
|
|
|
int animId;
|
2007-07-22 13:02:34 +02:00
|
|
|
QPixmap renderedPixmap;
|
2007-07-22 14:01:01 +02:00
|
|
|
|
|
|
|
QTextOption textOption;
|
|
|
|
Qt::Alignment alignment;
|
2007-07-23 20:50:42 +02:00
|
|
|
|
|
|
|
State state;
|
2008-10-28 10:25:57 +01:00
|
|
|
bool autohide;
|
2007-07-22 13:02:34 +02:00
|
|
|
};
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
FlashingLabel::FlashingLabel(QGraphicsItem *parent)
|
2008-05-08 13:45:49 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-10-29 18:46:33 +01:00
|
|
|
d(new FlashingLabelPrivate(this))
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
|
2008-02-15 07:51:37 +01:00
|
|
|
setCacheMode(NoCache);
|
2008-09-20 03:03:30 +02:00
|
|
|
connect(&d->fadeOutTimer, SIGNAL(timeout()), this, SLOT(fadeOut()));
|
|
|
|
connect(&d->fadeInTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
FlashingLabel::~FlashingLabel()
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::setDuration(int duration)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
if (duration < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
d->defaultDuration = duration;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::setColor(const QColor &color)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
d->color = color;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::setFont(const QFont &font)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
d->font = font;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::flash(const QString &text, int duration, const QTextOption &option)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
if (text.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << duration << text;
|
2008-10-29 18:46:33 +01:00
|
|
|
d->type = FlashingLabelPrivate::Text;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->text = text;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->textOption = option;
|
2008-10-28 10:25:57 +01:00
|
|
|
d->setupFlash(duration);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
if (pixmap.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->type = FlashingLabelPrivate::Pixmap;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->pixmap = pixmap;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->alignment = align;
|
2008-10-28 10:25:57 +01:00
|
|
|
d->setupFlash(duration);
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::setAutohide(bool autohide)
|
2008-10-28 10:25:57 +01:00
|
|
|
{
|
|
|
|
d->autohide = autohide;
|
|
|
|
|
|
|
|
if (autohide) {
|
|
|
|
connect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
|
|
|
|
this, SLOT(elementAnimationFinished(int)));
|
|
|
|
} else {
|
|
|
|
disconnect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
|
|
|
|
this, SLOT(elementAnimationFinished(int)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
bool FlashingLabel::autohide() const
|
2008-10-28 10:25:57 +01:00
|
|
|
{
|
|
|
|
return d->autohide;
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::kill()
|
2007-07-23 20:50:42 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
d->fadeInTimer.stop();
|
2008-10-29 18:46:33 +01:00
|
|
|
if (d->state == FlashingLabelPrivate::Visible) {
|
2007-07-23 20:50:42 +02:00
|
|
|
fadeOut();
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
2007-07-23 20:50:42 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::fadeIn()
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
//kDebug();
|
2008-10-28 10:25:57 +01:00
|
|
|
if (d->autohide) {
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->state = FlashingLabelPrivate::Visible;
|
2008-05-08 13:45:49 +02:00
|
|
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
|
2008-09-20 03:03:30 +02:00
|
|
|
Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::fadeOut()
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-10-29 18:46:33 +01:00
|
|
|
if (d->state == FlashingLabelPrivate::Invisible) {
|
|
|
|
return; // FlashingLabel was already killed - do not animate again
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
2007-07-23 20:50:42 +02:00
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->state = FlashingLabelPrivate::Invisible;
|
2008-10-12 03:31:54 +02:00
|
|
|
d->animId = Plasma::Animator::self()->animateElement(
|
|
|
|
this, Plasma::Animator::DisappearAnimation);
|
2008-09-20 03:03:30 +02:00
|
|
|
Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2008-09-20 03:03:30 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
|
|
|
if (d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull()) {
|
|
|
|
painter->drawPixmap(0, 0, Plasma::Animator::self()->currentPixmap(d->animId));
|
|
|
|
} else {
|
|
|
|
d->animId = 0;
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
if (d->state == FlashingLabelPrivate::Visible) {
|
2008-09-20 03:03:30 +02:00
|
|
|
painter->drawPixmap(0, 0, d->renderedPixmap);
|
|
|
|
}
|
|
|
|
}
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabelPrivate::renderPixmap(const QSize &size)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
if (renderedPixmap.size() != size) {
|
|
|
|
renderedPixmap = QPixmap(size);
|
|
|
|
}
|
|
|
|
renderedPixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter painter(&renderedPixmap);
|
2008-10-29 18:46:33 +01:00
|
|
|
if (type == FlashingLabelPrivate::Text) {
|
2008-09-20 03:03:30 +02:00
|
|
|
painter.setPen(color);
|
|
|
|
painter.setFont(font);
|
|
|
|
painter.drawText(QRect(QPoint(0, 0), size), text, textOption);
|
2008-10-29 18:46:33 +01:00
|
|
|
} else if (type == FlashingLabelPrivate::Pixmap) {
|
2007-07-22 14:01:01 +02:00
|
|
|
QPoint p;
|
2008-09-20 03:03:30 +02:00
|
|
|
|
|
|
|
if(alignment & Qt::AlignLeft) {
|
|
|
|
p.setX(0);
|
|
|
|
} else if (alignment & Qt::AlignRight) {
|
|
|
|
p.setX(size.width() - pixmap.width());
|
|
|
|
} else {
|
2008-10-12 03:31:54 +02:00
|
|
|
p.setX((size.width() - pixmap.width()) / 2);
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alignment & Qt::AlignTop) {
|
|
|
|
p.setY(0);
|
|
|
|
} else if (alignment & Qt::AlignRight) {
|
|
|
|
p.setY(size.height() - pixmap.height());
|
|
|
|
} else {
|
2008-10-12 03:31:54 +02:00
|
|
|
p.setY((size.height() - pixmap.height()) / 2);
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
painter.drawPixmap(p, pixmap);
|
|
|
|
}
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
if (animId) {
|
|
|
|
Plasma::Animator::self()->setInitialPixmap(animId, renderedPixmap);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-20 03:03:30 +02:00
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabelPrivate::setupFlash(int duration)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
fadeOutTimer.stop();
|
|
|
|
fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
|
|
|
|
|
2008-10-28 10:25:57 +01:00
|
|
|
renderPixmap(q->size().toSize());
|
2008-10-29 18:46:33 +01:00
|
|
|
if (state != FlashingLabelPrivate::Visible) {
|
2008-09-20 03:03:30 +02:00
|
|
|
fadeInTimer.start();
|
|
|
|
} else {
|
2008-10-28 10:25:57 +01:00
|
|
|
q->update();
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
2007-07-22 13:02:34 +02:00
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
if (fadeOutTimer.interval() > 0) {
|
|
|
|
fadeOutTimer.start();
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
void FlashingLabelPrivate::elementAnimationFinished(int id)
|
2008-10-28 10:25:57 +01:00
|
|
|
{
|
2008-10-29 18:46:33 +01:00
|
|
|
if (autohide && state == FlashingLabelPrivate::Invisible && id == animId) {
|
2008-10-28 10:25:57 +01:00
|
|
|
q->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "flashinglabel.moc"
|