Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / plasma / widgets / flash.cpp
blob6ba99fe2964593ce0d1be9f230afe70312b48683
1 /*
2 * Copyright 2007 by André Duffeck <duffeck@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Stre
19 * et, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "flash.h"
24 #include <QtCore/QString>
25 #include <QtCore/QTimeLine>
26 #include <QtCore/QTimer>
27 #include <QtGui/QPainter>
28 #include <QtGui/QPixmap>
29 #include <QtGui/QColor>
31 #include <KDebug>
33 #include <plasma/phase.h>
35 using namespace Plasma;
37 class Flash::Private
39 public:
40 enum FlashType { Text, Pixmap };
41 enum State { Visible, Invisible };
43 Private() { }
44 ~Private() { }
46 QString text;
47 QColor color;
48 QFont font;
49 QPixmap pixmap;
50 int duration;
51 int defaultDuration;
52 FlashType type;
54 Plasma::Phase::AnimId animId;
55 QPixmap renderedPixmap;
57 QTextOption textOption;
58 Qt::Alignment alignment;
60 State state;
64 Flash::Flash(QGraphicsItem *parent)
65 : Plasma::Widget(parent),
66 d(new Private)
68 d->defaultDuration = 3000;
69 d->type = Private::Text;
70 d->color = Qt::black;
71 d->animId = 0;
72 d->state = Private::Invisible;
73 resize(QSizeF(40, 100));
75 setCacheMode(NoCache);
78 Flash::~Flash()
80 delete d;
83 QRectF Flash::boundingRect() const
85 return QRectF(0, 0, size().width(), size().height());
88 void Flash::setDuration( int duration )
90 d->defaultDuration = duration;
93 void Flash::setColor( const QColor &color )
95 d->color = color;
98 void Flash::setFont( const QFont &font )
100 d->font = font;
103 void Flash::flash( const QString &text, int duration, const QTextOption &option)
105 kDebug() << duration;
106 d->type = Private::Text;
107 d->duration = (duration == 0) ? d->defaultDuration : duration;
108 d->text = text;
109 d->textOption = option;
110 QTimer::singleShot( 0, this, SLOT(fadeIn()) );
113 void Flash::flash( const QPixmap &pixmap, int duration, Qt::Alignment align )
115 d->type = Private::Pixmap;
116 d->duration = (duration == 0) ? d->defaultDuration : duration;
117 d->pixmap = pixmap;
118 d->alignment = align;
119 QTimer::singleShot( 0, this, SLOT(fadeIn()) );
122 void Flash::kill()
124 if( d->state == Private::Visible )
125 fadeOut();
128 void Flash::fadeIn()
130 d->state = Private::Visible;
131 d->renderedPixmap = renderPixmap();
132 d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementAppear);
133 Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap );
134 if( d->duration > 0 )
135 QTimer::singleShot( d->duration, this, SLOT(fadeOut()) );
138 void Flash::fadeOut()
140 if( d->state == Private::Invisible )
141 return; // Flash was already killed - do not animate again
143 d->state = Private::Invisible;
144 d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementDisappear);
145 Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap );
148 QPixmap Flash::renderPixmap()
150 QPixmap pm( size().toSize() );
151 pm.fill(Qt::transparent);
153 QPainter painter( &pm );
154 if( d->type == Private::Text ) {
155 painter.setPen( d->color );
156 painter.setFont( d->font );
157 painter.drawText( QRect( QPoint(0, 0), size().toSize() ), d->text, d->textOption);
158 } else if( d->type == Private::Pixmap ) {
159 QPoint p;
160 if( d->alignment & Qt::AlignLeft )
161 p.setX( 0 );
162 else if( d->alignment & Qt::AlignRight )
163 p.setX( pm.width() - d->pixmap.width() );
164 else
165 p.setX( (pm.width() - d->pixmap.width())/2 );
167 if( d->alignment & Qt::AlignTop )
168 p.setY( 0 );
169 else if( d->alignment & Qt::AlignRight )
170 p.setY( pm.height() - d->pixmap.height() );
171 else
172 p.setY( (pm.height() - d->pixmap.height())/2 );
174 painter.drawPixmap( p, d->pixmap );
176 return pm;
178 void Flash::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
180 Q_UNUSED(option)
181 Q_UNUSED(widget)
183 if( d->animId && !Plasma::Phase::self()->animationResult(d->animId).isNull() ) {
184 painter->drawPixmap( 0, 0, Plasma::Phase::self()->animationResult(d->animId) );
185 } else if( d->state == Private::Visible ) {
186 painter->drawPixmap( 0, 0, d->renderedPixmap );
190 #include "flash.moc"