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