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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "flash.h"
|
|
|
|
|
|
|
|
#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-07-01 20:56:43 +02:00
|
|
|
class Plasma::FlashPrivate
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum FlashType { Text, Pixmap };
|
2007-07-23 20:50:42 +02:00
|
|
|
enum State { Visible, Invisible };
|
2007-07-22 13:02:34 +02:00
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
FlashPrivate()
|
|
|
|
: defaultDuration(3000),
|
|
|
|
type(FlashPrivate::Text),
|
|
|
|
color(Qt::black),
|
|
|
|
animId(0),
|
|
|
|
state(FlashPrivate::Invisible)
|
|
|
|
{
|
|
|
|
//TODO: put this on a diet by using timerEvent instead?
|
|
|
|
fadeOutTimer.setInterval(defaultDuration);
|
|
|
|
fadeOutTimer.setSingleShot(true);
|
|
|
|
fadeInTimer.setInterval(0);
|
|
|
|
fadeInTimer.setSingleShot(true);
|
|
|
|
}
|
2008-07-01 20:56:43 +02:00
|
|
|
~FlashPrivate() { }
|
2007-07-22 13:02:34 +02:00
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void renderPixmap(const QSize &size);
|
|
|
|
void setupFlash(Flash *flash, int duration);
|
|
|
|
|
|
|
|
int defaultDuration;
|
|
|
|
FlashType type;
|
|
|
|
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;
|
2007-07-22 13:02:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Flash::Flash(QGraphicsItem *parent)
|
2008-05-08 13:45:49 +02:00
|
|
|
: QGraphicsWidget(parent),
|
2008-07-01 20:56:43 +02:00
|
|
|
d(new FlashPrivate)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Flash::~Flash()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void Flash::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-09-20 03:03:30 +02:00
|
|
|
void Flash::setColor(const QColor &color)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
d->color = color;
|
|
|
|
}
|
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void Flash::setFont(const QFont &font)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
d->font = font;
|
|
|
|
}
|
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void Flash::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-07-01 20:56:43 +02:00
|
|
|
d->type = FlashPrivate::Text;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->text = text;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->textOption = option;
|
2008-09-20 03:03:30 +02:00
|
|
|
d->setupFlash(this, duration);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void Flash::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-07-01 20:56:43 +02:00
|
|
|
d->type = FlashPrivate::Pixmap;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->pixmap = pixmap;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->alignment = align;
|
2008-09-20 03:03:30 +02:00
|
|
|
d->setupFlash(this, duration);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2007-07-23 20:50:42 +02:00
|
|
|
void Flash::kill()
|
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
d->fadeInTimer.stop();
|
|
|
|
if (d->state == FlashPrivate::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
|
|
|
}
|
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
void Flash::fadeIn()
|
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
//kDebug();
|
2008-07-01 20:56:43 +02:00
|
|
|
d->state = FlashPrivate::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
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::fadeOut()
|
|
|
|
{
|
2008-09-20 03:03:30 +02:00
|
|
|
if (d->state == FlashPrivate::Invisible) {
|
2007-07-23 20:50:42 +02:00
|
|
|
return; // Flash was already killed - do not animate again
|
2008-09-20 03:03:30 +02:00
|
|
|
}
|
2007-07-23 20:50:42 +02:00
|
|
|
|
2008-07-01 20:56:43 +02:00
|
|
|
d->state = FlashPrivate::Invisible;
|
2008-05-08 13:45:49 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (d->state == FlashPrivate::Visible) {
|
|
|
|
painter->drawPixmap(0, 0, d->renderedPixmap);
|
|
|
|
}
|
|
|
|
}
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 03:03:30 +02:00
|
|
|
void FlashPrivate::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);
|
|
|
|
if (type == FlashPrivate::Text) {
|
|
|
|
painter.setPen(color);
|
|
|
|
painter.setFont(font);
|
|
|
|
painter.drawText(QRect(QPoint(0, 0), size), text, textOption);
|
|
|
|
} else if (type == FlashPrivate::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 {
|
|
|
|
p.setX((size.width() - pixmap.width())/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alignment & Qt::AlignTop) {
|
|
|
|
p.setY(0);
|
|
|
|
} else if (alignment & Qt::AlignRight) {
|
|
|
|
p.setY(size.height() - pixmap.height());
|
|
|
|
} else {
|
|
|
|
p.setY((size.height() - pixmap.height())/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
void FlashPrivate::setupFlash(Flash *flash, 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);
|
|
|
|
|
|
|
|
renderPixmap(flash->size().toSize());
|
|
|
|
if (state != FlashPrivate::Visible) {
|
|
|
|
fadeInTimer.start();
|
|
|
|
} else {
|
|
|
|
flash->update();
|
|
|
|
}
|
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-09-20 03:03:30 +02:00
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
#include "flash.moc"
|