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