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-07-01 20:56:43 +02:00
|
|
|
FlashPrivate() { }
|
|
|
|
~FlashPrivate() { }
|
2007-07-22 13:02:34 +02:00
|
|
|
|
|
|
|
QString text;
|
|
|
|
QColor color;
|
|
|
|
QFont font;
|
|
|
|
QPixmap pixmap;
|
|
|
|
int duration;
|
|
|
|
int defaultDuration;
|
|
|
|
FlashType type;
|
|
|
|
|
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
|
|
|
{
|
2007-07-22 14:01:01 +02:00
|
|
|
d->defaultDuration = 3000;
|
2008-07-01 20:56:43 +02:00
|
|
|
d->type = FlashPrivate::Text;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->color = Qt::black;
|
|
|
|
d->animId = 0;
|
2008-07-01 20:56:43 +02:00
|
|
|
d->state = FlashPrivate::Invisible;
|
2008-05-08 13:45:49 +02:00
|
|
|
|
|
|
|
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
|
2007-10-15 11:41:39 +02:00
|
|
|
|
2008-02-15 07:51:37 +01:00
|
|
|
setCacheMode(NoCache);
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Flash::~Flash()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::setDuration( int duration )
|
|
|
|
{
|
|
|
|
d->defaultDuration = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::setColor( const QColor &color )
|
|
|
|
{
|
|
|
|
d->color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::setFont( const QFont &font )
|
|
|
|
{
|
|
|
|
d->font = font;
|
|
|
|
}
|
|
|
|
|
2007-07-23 22:01:20 +02:00
|
|
|
void Flash::flash( const QString &text, int duration, const QTextOption &option)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-01-08 02:25:09 +01:00
|
|
|
kDebug() << duration;
|
2008-07-01 20:56:43 +02:00
|
|
|
d->type = FlashPrivate::Text;
|
2007-10-15 11:41:39 +02:00
|
|
|
d->duration = (duration == 0) ? d->defaultDuration : duration;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->text = text;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->textOption = option;
|
2007-07-22 13:02:34 +02:00
|
|
|
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
|
|
|
|
}
|
|
|
|
|
2007-07-23 22:01:20 +02:00
|
|
|
void Flash::flash( const QPixmap &pixmap, int duration, Qt::Alignment align )
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
d->type = FlashPrivate::Pixmap;
|
2007-10-15 11:41:39 +02:00
|
|
|
d->duration = (duration == 0) ? d->defaultDuration : duration;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->pixmap = pixmap;
|
2007-07-22 14:01:01 +02:00
|
|
|
d->alignment = align;
|
2007-07-22 13:02:34 +02:00
|
|
|
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
|
|
|
|
}
|
|
|
|
|
2007-07-23 20:50:42 +02:00
|
|
|
void Flash::kill()
|
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
if( d->state == FlashPrivate::Visible )
|
2007-07-23 20:50:42 +02:00
|
|
|
fadeOut();
|
|
|
|
}
|
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
void Flash::fadeIn()
|
|
|
|
{
|
2008-07-01 20:56:43 +02:00
|
|
|
d->state = FlashPrivate::Visible;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->renderedPixmap = renderPixmap();
|
2008-05-08 13:45:49 +02:00
|
|
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
|
|
|
|
Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
|
2007-07-23 20:50:42 +02:00
|
|
|
if( d->duration > 0 )
|
|
|
|
QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Flash::fadeOut()
|
|
|
|
{
|
2008-07-01 20:56:43 +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-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);
|
|
|
|
Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap Flash::renderPixmap()
|
|
|
|
{
|
2007-10-15 10:54:06 +02:00
|
|
|
QPixmap pm( size().toSize() );
|
2007-07-22 13:02:34 +02:00
|
|
|
pm.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter painter( &pm );
|
2008-07-01 20:56:43 +02:00
|
|
|
if( d->type == FlashPrivate::Text ) {
|
2007-07-22 13:02:34 +02:00
|
|
|
painter.setPen( d->color );
|
|
|
|
painter.setFont( d->font );
|
2007-10-15 10:54:06 +02:00
|
|
|
painter.drawText( QRect( QPoint(0, 0), size().toSize() ), d->text, d->textOption);
|
2008-07-01 20:56:43 +02:00
|
|
|
} else if( d->type == FlashPrivate::Pixmap ) {
|
2007-07-22 14:01:01 +02:00
|
|
|
QPoint p;
|
|
|
|
if( d->alignment & Qt::AlignLeft )
|
|
|
|
p.setX( 0 );
|
|
|
|
else if( d->alignment & Qt::AlignRight )
|
|
|
|
p.setX( pm.width() - d->pixmap.width() );
|
|
|
|
else
|
|
|
|
p.setX( (pm.width() - d->pixmap.width())/2 );
|
|
|
|
|
|
|
|
if( d->alignment & Qt::AlignTop )
|
|
|
|
p.setY( 0 );
|
|
|
|
else if( d->alignment & Qt::AlignRight )
|
|
|
|
p.setY( pm.height() - d->pixmap.height() );
|
|
|
|
else
|
|
|
|
p.setY( (pm.height() - d->pixmap.height())/2 );
|
|
|
|
|
|
|
|
painter.drawPixmap( p, d->pixmap );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
return pm;
|
|
|
|
}
|
2008-05-08 13:45:49 +02:00
|
|
|
void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-05-08 13:45:49 +02:00
|
|
|
if( d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull() ) {
|
|
|
|
painter->drawPixmap( 0, 0, Plasma::Animator::self()->currentPixmap(d->animId) );
|
2008-07-01 20:56:43 +02:00
|
|
|
} else if( d->state == FlashPrivate::Visible ) {
|
2008-02-12 06:52:49 +01:00
|
|
|
painter->drawPixmap( 0, 0, d->renderedPixmap );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "flash.moc"
|