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.,
19 * et, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <QtCore/QString>
25 #include <QtCore/QTimeLine>
26 #include <QtCore/QTimer>
27 #include <QtGui/QPainter>
28 #include <QtGui/QPixmap>
29 #include <QtGui/QColor>
33 #include <plasma/phase.h>
35 using namespace Plasma
;
40 enum FlashType
{ Text
, Pixmap
};
41 enum State
{ Visible
, Invisible
};
54 Plasma::Phase::AnimId animId
;
55 QPixmap renderedPixmap
;
57 QTextOption textOption
;
58 Qt::Alignment alignment
;
64 Flash::Flash(QGraphicsItem
*parent
)
65 : Plasma::Widget(parent
),
68 d
->defaultDuration
= 3000;
69 d
->type
= Private::Text
;
72 d
->state
= Private::Invisible
;
73 resize(QSizeF(40, 100));
75 setCacheMode(NoCache
);
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
)
98 void Flash::setFont( const QFont
&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
;
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
;
118 d
->alignment
= align
;
119 QTimer::singleShot( 0, this, SLOT(fadeIn()) );
124 if( d
->state
== Private::Visible
)
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
) {
160 if( d
->alignment
& Qt::AlignLeft
)
162 else if( d
->alignment
& Qt::AlignRight
)
163 p
.setX( pm
.width() - d
->pixmap
.width() );
165 p
.setX( (pm
.width() - d
->pixmap
.width())/2 );
167 if( d
->alignment
& Qt::AlignTop
)
169 else if( d
->alignment
& Qt::AlignRight
)
170 p
.setY( pm
.height() - d
->pixmap
.height() );
172 p
.setY( (pm
.height() - d
->pixmap
.height())/2 );
174 painter
.drawPixmap( p
, d
->pixmap
);
178 void Flash::paintWidget(QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*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
);