plasma-framework/widgets/flash.cpp
2008-07-01 18:56:43 +00:00

187 lines
4.9 KiB
C++

/*
* Copyright 2007 by André Duffeck <duffeck@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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/animator.h>
using namespace Plasma;
class Plasma::FlashPrivate
{
public:
enum FlashType { Text, Pixmap };
enum State { Visible, Invisible };
FlashPrivate() { }
~FlashPrivate() { }
QString text;
QColor color;
QFont font;
QPixmap pixmap;
int duration;
int defaultDuration;
FlashType type;
int animId;
QPixmap renderedPixmap;
QTextOption textOption;
Qt::Alignment alignment;
State state;
};
Flash::Flash(QGraphicsItem *parent)
: QGraphicsWidget(parent),
d(new FlashPrivate)
{
d->defaultDuration = 3000;
d->type = FlashPrivate::Text;
d->color = Qt::black;
d->animId = 0;
d->state = FlashPrivate::Invisible;
setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
setCacheMode(NoCache);
}
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;
}
void Flash::flash( const QString &text, int duration, const QTextOption &option)
{
kDebug() << duration;
d->type = FlashPrivate::Text;
d->duration = (duration == 0) ? d->defaultDuration : duration;
d->text = text;
d->textOption = option;
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
}
void Flash::flash( const QPixmap &pixmap, int duration, Qt::Alignment align )
{
d->type = FlashPrivate::Pixmap;
d->duration = (duration == 0) ? d->defaultDuration : duration;
d->pixmap = pixmap;
d->alignment = align;
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
}
void Flash::kill()
{
if( d->state == FlashPrivate::Visible )
fadeOut();
}
void Flash::fadeIn()
{
d->state = FlashPrivate::Visible;
d->renderedPixmap = renderPixmap();
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
if( d->duration > 0 )
QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
}
void Flash::fadeOut()
{
if( d->state == FlashPrivate::Invisible )
return; // Flash was already killed - do not animate again
d->state = FlashPrivate::Invisible;
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
}
QPixmap Flash::renderPixmap()
{
QPixmap pm( size().toSize() );
pm.fill(Qt::transparent);
QPainter painter( &pm );
if( d->type == FlashPrivate::Text ) {
painter.setPen( d->color );
painter.setFont( d->font );
painter.drawText( QRect( QPoint(0, 0), size().toSize() ), d->text, d->textOption);
} else if( d->type == FlashPrivate::Pixmap ) {
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 );
}
return pm;
}
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 if( d->state == FlashPrivate::Visible ) {
painter->drawPixmap( 0, 0, d->renderedPixmap );
}
}
#include "flash.moc"