* make flash not reset the flash every time the text/pixmap is set if it already in the process of showing.
* clean up a lot of the code * don't use single shots, as they may need to be interupted svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=862833
This commit is contained in:
parent
88ac98fdb0
commit
afb72ab0c7
@ -40,16 +40,32 @@ class Plasma::FlashPrivate
|
|||||||
enum FlashType { Text, Pixmap };
|
enum FlashType { Text, Pixmap };
|
||||||
enum State { Visible, Invisible };
|
enum State { Visible, Invisible };
|
||||||
|
|
||||||
FlashPrivate() { }
|
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);
|
||||||
|
}
|
||||||
~FlashPrivate() { }
|
~FlashPrivate() { }
|
||||||
|
|
||||||
|
void renderPixmap(const QSize &size);
|
||||||
|
void setupFlash(Flash *flash, int duration);
|
||||||
|
|
||||||
|
int defaultDuration;
|
||||||
|
FlashType type;
|
||||||
|
QTimer fadeInTimer;
|
||||||
|
QTimer fadeOutTimer;
|
||||||
QString text;
|
QString text;
|
||||||
QColor color;
|
QColor color;
|
||||||
QFont font;
|
QFont font;
|
||||||
QPixmap pixmap;
|
QPixmap pixmap;
|
||||||
int duration;
|
|
||||||
int defaultDuration;
|
|
||||||
FlashType type;
|
|
||||||
|
|
||||||
int animId;
|
int animId;
|
||||||
QPixmap renderedPixmap;
|
QPixmap renderedPixmap;
|
||||||
@ -65,15 +81,10 @@ Flash::Flash(QGraphicsItem *parent)
|
|||||||
: QGraphicsWidget(parent),
|
: QGraphicsWidget(parent),
|
||||||
d(new FlashPrivate)
|
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);
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
|
||||||
|
|
||||||
setCacheMode(NoCache);
|
setCacheMode(NoCache);
|
||||||
|
connect(&d->fadeOutTimer, SIGNAL(timeout()), this, SLOT(fadeOut()));
|
||||||
|
connect(&d->fadeInTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Flash::~Flash()
|
Flash::~Flash()
|
||||||
@ -83,6 +94,10 @@ Flash::~Flash()
|
|||||||
|
|
||||||
void Flash::setDuration(int duration)
|
void Flash::setDuration(int duration)
|
||||||
{
|
{
|
||||||
|
if (duration < 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
d->defaultDuration = duration;
|
d->defaultDuration = duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,79 +113,56 @@ void Flash::setFont( const QFont &font )
|
|||||||
|
|
||||||
void Flash::flash(const QString &text, int duration, const QTextOption &option)
|
void Flash::flash(const QString &text, int duration, const QTextOption &option)
|
||||||
{
|
{
|
||||||
kDebug() << duration;
|
if (text.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//kDebug() << duration << text;
|
||||||
d->type = FlashPrivate::Text;
|
d->type = FlashPrivate::Text;
|
||||||
d->duration = (duration == 0) ? d->defaultDuration : duration;
|
|
||||||
d->text = text;
|
d->text = text;
|
||||||
d->textOption = option;
|
d->textOption = option;
|
||||||
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
|
d->setupFlash(this, duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Flash::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
|
void Flash::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
|
||||||
{
|
{
|
||||||
|
if (pixmap.isNull()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
d->type = FlashPrivate::Pixmap;
|
d->type = FlashPrivate::Pixmap;
|
||||||
d->duration = (duration == 0) ? d->defaultDuration : duration;
|
|
||||||
d->pixmap = pixmap;
|
d->pixmap = pixmap;
|
||||||
d->alignment = align;
|
d->alignment = align;
|
||||||
QTimer::singleShot( 0, this, SLOT(fadeIn()) );
|
d->setupFlash(this, duration);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Flash::kill()
|
void Flash::kill()
|
||||||
{
|
{
|
||||||
if( d->state == FlashPrivate::Visible )
|
d->fadeInTimer.stop();
|
||||||
|
if (d->state == FlashPrivate::Visible) {
|
||||||
fadeOut();
|
fadeOut();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Flash::fadeIn()
|
void Flash::fadeIn()
|
||||||
{
|
{
|
||||||
|
//kDebug();
|
||||||
d->state = FlashPrivate::Visible;
|
d->state = FlashPrivate::Visible;
|
||||||
d->renderedPixmap = renderPixmap();
|
|
||||||
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
|
||||||
Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
|
Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
|
||||||
if( d->duration > 0 )
|
|
||||||
QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Flash::fadeOut()
|
void Flash::fadeOut()
|
||||||
{
|
{
|
||||||
if( d->state == FlashPrivate::Invisible )
|
if (d->state == FlashPrivate::Invisible) {
|
||||||
return; // Flash was already killed - do not animate again
|
return; // Flash was already killed - do not animate again
|
||||||
|
}
|
||||||
|
|
||||||
d->state = FlashPrivate::Invisible;
|
d->state = FlashPrivate::Invisible;
|
||||||
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
|
d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
|
||||||
Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
|
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)
|
void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
{
|
{
|
||||||
Q_UNUSED(option)
|
Q_UNUSED(option)
|
||||||
@ -178,9 +170,71 @@ void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi
|
|||||||
|
|
||||||
if (d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull()) {
|
if (d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull()) {
|
||||||
painter->drawPixmap(0, 0, Plasma::Animator::self()->currentPixmap(d->animId));
|
painter->drawPixmap(0, 0, Plasma::Animator::self()->currentPixmap(d->animId));
|
||||||
} else if( d->state == FlashPrivate::Visible ) {
|
} else {
|
||||||
|
d->animId = 0;
|
||||||
|
|
||||||
|
if (d->state == FlashPrivate::Visible) {
|
||||||
painter->drawPixmap(0, 0, d->renderedPixmap);
|
painter->drawPixmap(0, 0, d->renderedPixmap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FlashPrivate::renderPixmap(const QSize &size)
|
||||||
|
{
|
||||||
|
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) {
|
||||||
|
QPoint p;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FlashPrivate::setupFlash(Flash *flash, int duration)
|
||||||
|
{
|
||||||
|
fadeOutTimer.stop();
|
||||||
|
fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
|
||||||
|
|
||||||
|
renderPixmap(flash->size().toSize());
|
||||||
|
if (state != FlashPrivate::Visible) {
|
||||||
|
fadeInTimer.start();
|
||||||
|
} else {
|
||||||
|
flash->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fadeOutTimer.interval() > 0) {
|
||||||
|
fadeOutTimer.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#include "flash.moc"
|
#include "flash.moc"
|
||||||
|
@ -59,9 +59,6 @@ class PLASMA_EXPORT Flash : public QGraphicsWidget
|
|||||||
void fadeIn();
|
void fadeIn();
|
||||||
void fadeOut();
|
void fadeOut();
|
||||||
|
|
||||||
protected:
|
|
||||||
QPixmap renderPixmap();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FlashPrivate * const d;
|
FlashPrivate * const d;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user