• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

flashinglabel.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 by André Duffeck <duffeck@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008 
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Stre
00019  *   et, Fifth Floor, Boston, MA  02110-1301, USA.
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     //kDebug() << duration << text;
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     //kDebug();
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;    // FlashingLabel was already killed - do not animate again
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"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal