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>
|
|
|
|
|
|
|
|
#include <plasma/phase.h>
|
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
|
|
|
class Flash::Private
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
Private() { }
|
|
|
|
~Private() { }
|
|
|
|
|
|
|
|
QString text;
|
|
|
|
QColor color;
|
|
|
|
QFont font;
|
|
|
|
QPixmap pixmap;
|
|
|
|
int duration;
|
|
|
|
int defaultDuration;
|
|
|
|
FlashType type;
|
|
|
|
|
|
|
|
Plasma::Phase::AnimId animId;
|
|
|
|
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)
|
2007-08-03 18:00:10 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-07-22 13:02:34 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
2007-07-22 14:01:01 +02:00
|
|
|
d->defaultDuration = 3000;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->type = Private::Text;
|
|
|
|
d->color = Qt::black;
|
|
|
|
d->animId = 0;
|
2007-07-23 20:50:42 +02:00
|
|
|
d->state = Private::Invisible;
|
2008-02-11 09:24:09 +01:00
|
|
|
resize(QSizeF(40, 100));
|
2007-10-15 11:41:39 +02:00
|
|
|
|
|
|
|
setCachePaintMode( NoCacheMode );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Flash::~Flash()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Flash::boundingRect() const
|
|
|
|
{
|
2007-11-19 08:57:04 +01:00
|
|
|
return QRectF(0, 0, size().width(), size().height());
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->type = Private::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
|
|
|
{
|
|
|
|
d->type = Private::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()
|
|
|
|
{
|
|
|
|
if( d->state == Private::Visible )
|
|
|
|
fadeOut();
|
|
|
|
}
|
|
|
|
|
2007-07-22 13:02:34 +02:00
|
|
|
void Flash::fadeIn()
|
|
|
|
{
|
2007-07-23 20:50:42 +02:00
|
|
|
d->state = Private::Visible;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->renderedPixmap = renderPixmap();
|
|
|
|
d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementAppear);
|
|
|
|
Plasma::Phase::self()->setAnimationPixmap( 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()
|
|
|
|
{
|
2007-07-23 20:50:42 +02:00
|
|
|
if( d->state == Private::Invisible )
|
|
|
|
return; // Flash was already killed - do not animate again
|
|
|
|
|
|
|
|
d->state = Private::Invisible;
|
2007-07-22 13:02:34 +02:00
|
|
|
d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementDisappear);
|
|
|
|
Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
if( d->type == Private::Text ) {
|
|
|
|
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);
|
2007-07-22 13:02:34 +02:00
|
|
|
} else if( d->type == Private::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;
|
|
|
|
}
|
2007-08-03 18:00:10 +02:00
|
|
|
void Flash::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-07-22 13:02:34 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-02-12 06:52:49 +01:00
|
|
|
if( d->animId && !Plasma::Phase::self()->animationResult(d->animId).isNull() ) {
|
|
|
|
painter->drawPixmap( 0, 0, Plasma::Phase::self()->animationResult(d->animId) );
|
|
|
|
} else if( d->state == Private::Visible ) {
|
|
|
|
painter->drawPixmap( 0, 0, d->renderedPixmap );
|
2007-07-22 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "flash.moc"
|