Allow persistant flashs that disappear only when they are killed.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=691487
This commit is contained in:
Andre Duffeck 2007-07-23 18:50:42 +00:00
parent 4d8628230f
commit 6a4841b19a
2 changed files with 23 additions and 4 deletions

View File

@ -36,6 +36,7 @@ class Flash::Private
{ {
public: public:
enum FlashType { Text, Pixmap }; enum FlashType { Text, Pixmap };
enum State { Visible, Invisible };
Private() { } Private() { }
~Private() { } ~Private() { }
@ -55,6 +56,8 @@ class Flash::Private
QTextOption textOption; QTextOption textOption;
Qt::Alignment alignment; Qt::Alignment alignment;
State state;
}; };
@ -69,6 +72,7 @@ Flash::Flash(QGraphicsItem *parent)
d->height = 40; d->height = 40;
d->width = 100 ; d->width = 100 ;
d->animId = 0; d->animId = 0;
d->state = Private::Invisible;
} }
Flash::~Flash() Flash::~Flash()
@ -136,7 +140,7 @@ void Flash::setFont( const QFont &font )
void Flash::flash( const QString &text, const QTextOption &option, int duration) void Flash::flash( const QString &text, const QTextOption &option, int duration)
{ {
d->type = Private::Text; d->type = Private::Text;
d->duration = duration > 0 ? duration : d->defaultDuration; d->duration = (duration == 0) ? duration : d->defaultDuration;
d->text = text; d->text = text;
d->textOption = option; d->textOption = option;
QTimer::singleShot( 0, this, SLOT(fadeIn()) ); QTimer::singleShot( 0, this, SLOT(fadeIn()) );
@ -145,22 +149,34 @@ void Flash::flash( const QString &text, const QTextOption &option, int duration)
void Flash::flash( const QPixmap &pixmap, Qt::Alignment align, int duration ) void Flash::flash( const QPixmap &pixmap, Qt::Alignment align, int duration )
{ {
d->type = Private::Pixmap; d->type = Private::Pixmap;
d->duration = duration > 0 ? duration : d->defaultDuration; d->duration = (duration == 0) ? duration : d->defaultDuration;
d->pixmap = pixmap; d->pixmap = pixmap;
d->alignment = align; d->alignment = align;
QTimer::singleShot( 0, this, SLOT(fadeIn()) ); QTimer::singleShot( 0, this, SLOT(fadeIn()) );
} }
void Flash::kill()
{
if( d->state == Private::Visible )
fadeOut();
}
void Flash::fadeIn() void Flash::fadeIn()
{ {
d->state = Private::Visible;
d->renderedPixmap = renderPixmap(); d->renderedPixmap = renderPixmap();
d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementAppear); d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementAppear);
Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap ); Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap );
QTimer::singleShot( d->duration, this, SLOT(fadeOut()) ); if( d->duration > 0 )
QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
} }
void Flash::fadeOut() void Flash::fadeOut()
{ {
if( d->state == Private::Invisible )
return; // Flash was already killed - do not animate again
d->state = Private::Invisible;
d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementDisappear); d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementDisappear);
Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap ); Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap );
} }
@ -174,7 +190,7 @@ QPixmap Flash::renderPixmap()
if( d->type == Private::Text ) { if( d->type == Private::Text ) {
painter.setPen( d->color ); painter.setPen( d->color );
painter.setFont( d->font ); painter.setFont( d->font );
painter.drawText( QRect( 0, 0, pm.width(), pm.height() ), d->text, d->textOption); painter.drawText( QRect( 0, 0, width(), height() ), d->text, d->textOption);
} else if( d->type == Private::Pixmap ) { } else if( d->type == Private::Pixmap ) {
QPoint p; QPoint p;
if( d->alignment & Qt::AlignLeft ) if( d->alignment & Qt::AlignLeft )

View File

@ -54,6 +54,9 @@ class PLASMA_EXPORT Flash : public QObject, public QGraphicsItem
void flash( const QString &text, const QTextOption &option = QTextOption(Qt::AlignCenter), int duration = 0 ); void flash( const QString &text, const QTextOption &option = QTextOption(Qt::AlignCenter), int duration = 0 );
void flash( const QPixmap &pixmap, Qt::Alignment align = Qt::AlignCenter, int duration = 0 ); void flash( const QPixmap &pixmap, Qt::Alignment align = Qt::AlignCenter, int duration = 0 );
public Q_SLOTS:
void kill();
protected Q_SLOTS: protected Q_SLOTS:
void fadeIn(); void fadeIn();
void fadeOut(); void fadeOut();