Plasma
flashinglabel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "flashinglabel.h"
00023
00024 #include <QtCore/QString>
00025 #include <QtCore/QTimeLine>
00026 #include <QtCore/QTimer>
00027 #include <QtGui/QPainter>
00028 #include <QtGui/QPixmap>
00029 #include <QtGui/QColor>
00030
00031 #include <kdebug.h>
00032
00033 #include <plasma/animator.h>
00034
00035 using namespace Plasma;
00036
00037 class Plasma::FlashingLabelPrivate
00038 {
00039 public:
00040 enum FlashingLabelType {
00041 Text,
00042 Pixmap
00043 };
00044 enum State {
00045 Visible,
00046 Invisible
00047 };
00048
00049 FlashingLabelPrivate(FlashingLabel *flash)
00050 : q(flash),
00051 defaultDuration(3000),
00052 type(FlashingLabelPrivate::Text),
00053 color(Qt::black),
00054 animId(0),
00055 state(FlashingLabelPrivate::Invisible),
00056 autohide(false)
00057 {
00058
00059 fadeOutTimer.setInterval(defaultDuration);
00060 fadeOutTimer.setSingleShot(true);
00061 fadeInTimer.setInterval(0);
00062 fadeInTimer.setSingleShot(true);
00063 }
00064
00065 ~FlashingLabelPrivate() { }
00066
00067 void renderPixmap(const QSize &size);
00068 void setupFlash(int duration);
00069 void elementAnimationFinished(int);
00070
00071 FlashingLabel *q;
00072 int defaultDuration;
00073 FlashingLabelType type;
00074 QTimer fadeInTimer;
00075 QTimer fadeOutTimer;
00076 QString text;
00077 QColor color;
00078 QFont font;
00079 QPixmap pixmap;
00080
00081 int animId;
00082 QPixmap renderedPixmap;
00083
00084 QTextOption textOption;
00085 Qt::Alignment alignment;
00086
00087 State state;
00088 bool autohide;
00089 };
00090
00091 FlashingLabel::FlashingLabel(QGraphicsItem *parent)
00092 : QGraphicsWidget(parent),
00093 d(new FlashingLabelPrivate(this))
00094 {
00095 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
00096 setCacheMode(NoCache);
00097 connect(&d->fadeOutTimer, SIGNAL(timeout()), this, SLOT(fadeOut()));
00098 connect(&d->fadeInTimer, SIGNAL(timeout()), this, SLOT(fadeIn()));
00099 }
00100
00101 FlashingLabel::~FlashingLabel()
00102 {
00103 delete d;
00104 }
00105
00106 void FlashingLabel::setDuration(int duration)
00107 {
00108 if (duration < 1) {
00109 return;
00110 }
00111
00112 d->defaultDuration = duration;
00113 }
00114
00115 void FlashingLabel::setColor(const QColor &color)
00116 {
00117 d->color = color;
00118 }
00119
00120 void FlashingLabel::setFont(const QFont &font)
00121 {
00122 d->font = font;
00123 }
00124
00125 void FlashingLabel::flash(const QString &text, int duration, const QTextOption &option)
00126 {
00127 if (text.isEmpty()) {
00128 return;
00129 }
00130
00131
00132 d->type = FlashingLabelPrivate::Text;
00133 d->text = text;
00134 d->textOption = option;
00135 d->setupFlash(duration);
00136 }
00137
00138 void FlashingLabel::flash(const QPixmap &pixmap, int duration, Qt::Alignment align)
00139 {
00140 if (pixmap.isNull()) {
00141 return;
00142 }
00143
00144 d->type = FlashingLabelPrivate::Pixmap;
00145 d->pixmap = pixmap;
00146 d->alignment = align;
00147 d->setupFlash(duration);
00148 }
00149
00150 void FlashingLabel::setAutohide(bool autohide)
00151 {
00152 d->autohide = autohide;
00153
00154 if (autohide) {
00155 connect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
00156 this, SLOT(elementAnimationFinished(int)));
00157 } else {
00158 disconnect(Plasma::Animator::self(), SIGNAL(elementAnimationFinished(int)),
00159 this, SLOT(elementAnimationFinished(int)));
00160 }
00161 }
00162
00163 bool FlashingLabel::autohide() const
00164 {
00165 return d->autohide;
00166 }
00167
00168 void FlashingLabel::kill()
00169 {
00170 d->fadeInTimer.stop();
00171 if (d->state == FlashingLabelPrivate::Visible) {
00172 fadeOut();
00173 }
00174 }
00175
00176 void FlashingLabel::fadeIn()
00177 {
00178
00179 if (d->autohide) {
00180 show();
00181 }
00182
00183 d->state = FlashingLabelPrivate::Visible;
00184 d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
00185 Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
00186 }
00187
00188 void FlashingLabel::fadeOut()
00189 {
00190 if (d->state == FlashingLabelPrivate::Invisible) {
00191 return;
00192 }
00193
00194 d->state = FlashingLabelPrivate::Invisible;
00195 d->animId = Plasma::Animator::self()->animateElement(
00196 this, Plasma::Animator::DisappearAnimation);
00197 Plasma::Animator::self()->setInitialPixmap(d->animId, d->renderedPixmap);
00198 }
00199
00200 void FlashingLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00201 {
00202 Q_UNUSED(option)
00203 Q_UNUSED(widget)
00204
00205 if (d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull()) {
00206 painter->drawPixmap(0, 0, Plasma::Animator::self()->currentPixmap(d->animId));
00207 } else {
00208 d->animId = 0;
00209
00210 if (d->state == FlashingLabelPrivate::Visible) {
00211 painter->drawPixmap(0, 0, d->renderedPixmap);
00212 }
00213 }
00214 }
00215
00216 void FlashingLabelPrivate::renderPixmap(const QSize &size)
00217 {
00218 if (renderedPixmap.size() != size) {
00219 renderedPixmap = QPixmap(size);
00220 }
00221 renderedPixmap.fill(Qt::transparent);
00222
00223 QPainter painter(&renderedPixmap);
00224 if (type == FlashingLabelPrivate::Text) {
00225 painter.setPen(color);
00226 painter.setFont(font);
00227 painter.drawText(QRect(QPoint(0, 0), size), text, textOption);
00228 } else if (type == FlashingLabelPrivate::Pixmap) {
00229 QPoint p;
00230
00231 if(alignment & Qt::AlignLeft) {
00232 p.setX(0);
00233 } else if (alignment & Qt::AlignRight) {
00234 p.setX(size.width() - pixmap.width());
00235 } else {
00236 p.setX((size.width() - pixmap.width()) / 2);
00237 }
00238
00239 if (alignment & Qt::AlignTop) {
00240 p.setY(0);
00241 } else if (alignment & Qt::AlignRight) {
00242 p.setY(size.height() - pixmap.height());
00243 } else {
00244 p.setY((size.height() - pixmap.height()) / 2);
00245 }
00246
00247 painter.drawPixmap(p, pixmap);
00248 }
00249 painter.end();
00250
00251 if (animId) {
00252 Plasma::Animator::self()->setInitialPixmap(animId, renderedPixmap);
00253 }
00254 }
00255
00256 void FlashingLabelPrivate::setupFlash(int duration)
00257 {
00258 fadeOutTimer.stop();
00259 fadeOutTimer.setInterval(duration > 0 ? duration : defaultDuration);
00260
00261 renderPixmap(q->size().toSize());
00262 if (state != FlashingLabelPrivate::Visible) {
00263 fadeInTimer.start();
00264 } else {
00265 q->update();
00266 }
00267
00268 if (fadeOutTimer.interval() > 0) {
00269 fadeOutTimer.start();
00270 }
00271 }
00272
00273 void FlashingLabelPrivate::elementAnimationFinished(int id)
00274 {
00275 if (autohide && state == FlashingLabelPrivate::Invisible && id == animId) {
00276 q->hide();
00277 }
00278 }
00279
00280 #include "flashinglabel.moc"