something i've wanted for a bit: autohide on fade. perfect kind of "i don't have to think to add this" kind of feature to fill in my boredom during meetings full of powerpoint presentations ;)

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=876852
This commit is contained in:
Aaron J. Seigo 2008-10-28 09:25:57 +00:00
parent 8560092760
commit b4e741c3f0
2 changed files with 49 additions and 10 deletions

View File

@ -46,12 +46,14 @@ class Plasma::FlashPrivate
Invisible Invisible
}; };
FlashPrivate() FlashPrivate(Flash *flash)
: defaultDuration(3000), : q(flash),
defaultDuration(3000),
type(FlashPrivate::Text), type(FlashPrivate::Text),
color(Qt::black), color(Qt::black),
animId(0), animId(0),
state(FlashPrivate::Invisible) state(FlashPrivate::Invisible),
autohide(false)
{ {
//TODO: put this on a diet by using timerEvent instead? //TODO: put this on a diet by using timerEvent instead?
fadeOutTimer.setInterval(defaultDuration); fadeOutTimer.setInterval(defaultDuration);
@ -59,11 +61,14 @@ class Plasma::FlashPrivate
fadeInTimer.setInterval(0); fadeInTimer.setInterval(0);
fadeInTimer.setSingleShot(true); fadeInTimer.setSingleShot(true);
} }
~FlashPrivate() { } ~FlashPrivate() { }
void renderPixmap(const QSize &size); void renderPixmap(const QSize &size);
void setupFlash(Flash *flash, int duration); void setupFlash(int duration);
void elementAnimationFinished(int);
Flash *q;
int defaultDuration; int defaultDuration;
FlashType type; FlashType type;
QTimer fadeInTimer; QTimer fadeInTimer;
@ -80,11 +85,12 @@ class Plasma::FlashPrivate
Qt::Alignment alignment; Qt::Alignment alignment;
State state; State state;
bool autohide;
}; };
Flash::Flash(QGraphicsItem *parent) Flash::Flash(QGraphicsItem *parent)
: QGraphicsWidget(parent), : QGraphicsWidget(parent),
d(new FlashPrivate) d(new FlashPrivate(this))
{ {
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum); setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
setCacheMode(NoCache); setCacheMode(NoCache);
@ -126,7 +132,7 @@ void Flash::flash(const QString &text, int duration, const QTextOption &option)
d->type = FlashPrivate::Text; d->type = FlashPrivate::Text;
d->text = text; d->text = text;
d->textOption = option; d->textOption = option;
d->setupFlash(this, duration); d->setupFlash(duration);
} }
void Flash::flash(const QPixmap &pixmap, int duration, Qt::Alignment align) void Flash::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
@ -138,7 +144,25 @@ void Flash::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
d->type = FlashPrivate::Pixmap; d->type = FlashPrivate::Pixmap;
d->pixmap = pixmap; d->pixmap = pixmap;
d->alignment = align; d->alignment = align;
d->setupFlash(this, duration); d->setupFlash(duration);
}
void Flash::setAutohide(bool autohide)
{
d->autohide = autohide;
if (autohide) {
connect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
this, SLOT(elementAnimationFinished(int)));
} else {
disconnect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
this, SLOT(elementAnimationFinished(int)));
}
}
bool Flash::autohide() const
{
return d->autohide;
} }
void Flash::kill() void Flash::kill()
@ -152,6 +176,10 @@ void Flash::kill()
void Flash::fadeIn() void Flash::fadeIn()
{ {
//kDebug(); //kDebug();
if (d->autohide) {
show();
}
d->state = FlashPrivate::Visible; d->state = FlashPrivate::Visible;
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);
@ -225,16 +253,16 @@ void FlashPrivate::renderPixmap(const QSize &size)
} }
} }
void FlashPrivate::setupFlash(Flash *flash, int duration) void FlashPrivate::setupFlash(int duration)
{ {
fadeOutTimer.stop(); fadeOutTimer.stop();
fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration); fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
renderPixmap(flash->size().toSize()); renderPixmap(q->size().toSize());
if (state != FlashPrivate::Visible) { if (state != FlashPrivate::Visible) {
fadeInTimer.start(); fadeInTimer.start();
} else { } else {
flash->update(); q->update();
} }
if (fadeOutTimer.interval() > 0) { if (fadeOutTimer.interval() > 0) {
@ -242,4 +270,11 @@ void FlashPrivate::setupFlash(Flash *flash, int duration)
} }
} }
void FlashPrivate::elementAnimationFinished(int id)
{
if (autohide && state == FlashPrivate::Invisible && id == animId) {
q->hide();
}
}
#include "flash.moc" #include "flash.moc"

View File

@ -54,6 +54,9 @@ class PLASMA_EXPORT Flash : public QGraphicsWidget
void flash(const QPixmap &pixmap, int duration = 0, void flash(const QPixmap &pixmap, int duration = 0,
Qt::Alignment align = Qt::AlignCenter); Qt::Alignment align = Qt::AlignCenter);
void setAutohide(bool autohide);
bool autohide() const;
public Q_SLOTS: public Q_SLOTS:
void kill(); void kill();
@ -62,6 +65,7 @@ class PLASMA_EXPORT Flash : public QGraphicsWidget
void fadeOut(); void fadeOut();
private: private:
Q_PRIVATE_SLOT(d, void elementAnimationFinished(int))
FlashPrivate *const d; FlashPrivate *const d;
}; };