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

libplasma

flash.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 "flash.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>
00032 
00033 #include <plasma/animator.h>
00034 
00035 using namespace Plasma;
00036 
00037 class Plasma::FlashPrivate
00038 {
00039     public:
00040         enum FlashType { Text, Pixmap };
00041         enum State { Visible, Invisible };
00042 
00043         FlashPrivate() { }
00044         ~FlashPrivate() { }
00045 
00046         QString text;
00047         QColor color;
00048         QFont font;
00049         QPixmap pixmap;
00050         int duration;
00051         int defaultDuration;
00052         FlashType type;
00053 
00054         int animId;
00055         QPixmap renderedPixmap;
00056 
00057         QTextOption textOption;
00058         Qt::Alignment alignment;
00059 
00060         State state;
00061 };
00062 
00063 
00064 Flash::Flash(QGraphicsItem *parent)
00065     : QGraphicsWidget(parent),
00066       d(new FlashPrivate)
00067 {
00068     d->defaultDuration = 3000;
00069     d->type = FlashPrivate::Text;
00070     d->color = Qt::black;
00071     d->animId = 0;
00072     d->state = FlashPrivate::Invisible;
00073 
00074     setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
00075 
00076     setCacheMode(NoCache);
00077 }
00078 
00079 Flash::~Flash()
00080 {
00081     delete d;
00082 }
00083 
00084 void Flash::setDuration( int duration )
00085 {
00086     d->defaultDuration = duration;
00087 }
00088 
00089 void Flash::setColor( const QColor &color )
00090 {
00091     d->color = color;
00092 }
00093 
00094 void Flash::setFont( const QFont &font )
00095 {
00096     d->font = font;
00097 }
00098 
00099 void Flash::flash( const QString &text, int duration, const QTextOption &option)
00100 {
00101     kDebug() << duration;
00102     d->type = FlashPrivate::Text;
00103     d->duration = (duration == 0) ? d->defaultDuration : duration;
00104     d->text = text;
00105     d->textOption = option;
00106     QTimer::singleShot( 0, this, SLOT(fadeIn()) );
00107 }
00108 
00109 void Flash::flash( const QPixmap &pixmap, int duration, Qt::Alignment align )
00110 {
00111     d->type = FlashPrivate::Pixmap;
00112     d->duration = (duration == 0) ? d->defaultDuration : duration;
00113     d->pixmap = pixmap;
00114     d->alignment = align;
00115     QTimer::singleShot( 0, this, SLOT(fadeIn()) );
00116 }
00117 
00118 void Flash::kill()
00119 {
00120     if( d->state == FlashPrivate::Visible )
00121         fadeOut();
00122 }
00123 
00124 void Flash::fadeIn()
00125 {
00126     d->state = FlashPrivate::Visible;
00127     d->renderedPixmap = renderPixmap();
00128     d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::AppearAnimation);
00129     Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
00130     if( d->duration > 0 )
00131         QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
00132 }
00133 
00134 void Flash::fadeOut()
00135 {
00136     if( d->state == FlashPrivate::Invisible )
00137         return;    // Flash was already killed - do not animate again
00138 
00139     d->state = FlashPrivate::Invisible;
00140     d->animId = Plasma::Animator::self()->animateElement(this, Plasma::Animator::DisappearAnimation);
00141     Plasma::Animator::self()->setInitialPixmap( d->animId, d->renderedPixmap );
00142 }
00143 
00144 QPixmap Flash::renderPixmap()
00145 {
00146     QPixmap pm( size().toSize() );
00147     pm.fill(Qt::transparent);
00148 
00149     QPainter painter( &pm );
00150     if( d->type == FlashPrivate::Text ) {
00151         painter.setPen( d->color );
00152         painter.setFont( d->font );
00153         painter.drawText( QRect( QPoint(0, 0), size().toSize() ), d->text, d->textOption);
00154     } else if( d->type == FlashPrivate::Pixmap ) {
00155         QPoint p;
00156         if( d->alignment & Qt::AlignLeft )
00157             p.setX( 0 );
00158         else if( d->alignment & Qt::AlignRight )
00159             p.setX( pm.width() - d->pixmap.width() );
00160         else
00161             p.setX( (pm.width() - d->pixmap.width())/2 );
00162 
00163         if( d->alignment & Qt::AlignTop )
00164             p.setY( 0 );
00165         else if( d->alignment & Qt::AlignRight )
00166             p.setY( pm.height() - d->pixmap.height() );
00167         else
00168             p.setY( (pm.height() - d->pixmap.height())/2 );
00169 
00170         painter.drawPixmap( p, d->pixmap );
00171     }
00172     return pm;
00173 }
00174 void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00175 {
00176     Q_UNUSED(option)
00177     Q_UNUSED(widget)
00178 
00179     if( d->animId && !Plasma::Animator::self()->currentPixmap(d->animId).isNull() ) {
00180         painter->drawPixmap( 0, 0, Plasma::Animator::self()->currentPixmap(d->animId) );
00181     } else if( d->state == FlashPrivate::Visible ) {
00182         painter->drawPixmap( 0, 0, d->renderedPixmap );
00183     }
00184 }
00185 
00186 #include "flash.moc"

libplasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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