use a qlabel instead of a k3activelabel for mouseover.. it doesn't help much yet...
[amarok.git] / src / statusbar / popupMessage.cpp
blobd086217adcbb1dedf19e502e27ed9602db02bafc
1 /***************************************************************************
2 * Copyright (C) 2005 by Max Howell <max.howell@methylblue.com> *
3 * 2005 by Seb Ruiz <me@sebruiz.net> *
4 * *
5 * Dissolve Mask (c) Kicker Authors kickertip.cpp, 2005/08/17 *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
23 #include "popupMessage.h"
24 #include "debug.h"
26 #include <kpushbutton.h>
27 #include <kstandardguiitem.h>
29 #include <qfont.h>
30 #include <q3frame.h>
31 #include <QLabel>
32 #include <QMessageBox>
33 #include <QPainter>
34 #include <QTimer>
35 #include <QToolTip>
36 #include <kguiitem.h>
37 //Added by qt3to4:
38 #include <QTimerEvent>
39 #include <QPixmap>
41 namespace KDE
44 PopupMessage::PopupMessage( QWidget *parent, QWidget *anchor, int timeout, const char *name )
45 : OverlayWidget( parent, anchor, name )
46 , m_anchor( anchor )
47 , m_parent( parent )
48 , m_maskEffect( Slide )
49 , m_dissolveSize( 0 )
50 , m_dissolveDelta( -1 )
51 , m_offset( 0 )
52 , m_counter( 0 )
53 , m_stage( 1 )
54 , m_timeout( timeout )
55 , m_showCounter( true )
57 setFrameStyle( QFrame::Panel | QFrame::Raised );
58 setFrameShape( QFrame::StyledPanel );
59 setWindowFlags( Qt::WX11BypassWM );
61 QPalette p = QToolTip::palette();
62 setPalette( p );
64 QHBoxLayout *hbox;
65 QLabel *label;
66 QLabel *alabel;
68 m_layout = new QVBoxLayout( this );
69 m_layout->setMargin( 9 );
70 m_layout->setSpacing( 6 );
72 hbox = new QHBoxLayout;
73 hbox->setParent( m_layout );
74 hbox->setSpacing( 12 );
76 m_countdownFrame = new QFrame( this );
77 m_countdownFrame->setObjectName( "counterVisual" );
78 hbox->addWidget( m_countdownFrame );
79 m_countdownFrame->setFixedWidth( fontMetrics().width( "X" ) );
80 m_countdownFrame->setFrameStyle( QFrame::Plain | QFrame::Box );
81 m_countdownFrame->setPaletteForegroundColor( paletteBackgroundColor().dark() );
83 label = new QLabel( this );
84 label->setObjectName( "image" );
85 hbox->addWidget( label );
87 alabel = new QLabel;
88 alabel->setParent( this );
89 alabel->setObjectName( "label" );
90 alabel->setTextFormat( Qt::RichText );
91 alabel->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
92 alabel->setPalette( p );
94 hbox->addWidget( alabel );
96 hbox = new QHBoxLayout;
97 hbox->setParent( m_layout );
99 hbox->addItem( new QSpacerItem( 4, 4, QSizePolicy::Expanding, QSizePolicy::Preferred ) );
100 KPushButton *button = new KPushButton( KStandardGuiItem::close(), this );
101 button->setObjectName( "closeButton" );
102 hbox->addWidget( button );
104 connect( button, SIGNAL(clicked()), SLOT(close()) );
107 void PopupMessage::addWidget( QWidget *widget )
109 m_layout->addWidget( widget );
110 adjustSize();
113 void PopupMessage::setShowCloseButton( const bool show )
115 findChild<KPushButton*>( "closeButton" )->setVisible( show );
116 adjustSize();
119 void PopupMessage::setShowCounter( const bool show )
121 m_showCounter = show;
122 findChild<QFrame*>( "counterVisual" )->setVisible( show );
123 adjustSize();
126 void PopupMessage::setText( const QString &text )
128 findChild<QLabel*>( "label" )->setText( text );
129 adjustSize();
132 void PopupMessage::setImage( const QString &location )
134 findChild<QLabel*>( "image" )->setPixmap( QPixmap( location ) );
135 adjustSize();
138 void PopupMessage::setImage( const QPixmap &pix )
140 findChild<QLabel*>( "image" )->setPixmap( pix );
141 adjustSize();
145 ////////////////////////////////////////////////////////////////////////
146 // Public Slots
147 ////////////////////////////////////////////////////////////////////////
149 void PopupMessage::close() //SLOT
151 m_stage = 3;
152 killTimer( m_timerId );
153 m_timerId = startTimer( 6 );
156 void PopupMessage::display() //SLOT
158 m_dissolveSize = 24;
159 m_dissolveDelta = -1;
161 if( m_maskEffect == Dissolve )
163 // necessary to create the mask
164 m_mask.resize( width(), height() );
165 // make the mask empty and hence will not show widget with show() called below
166 dissolveMask();
167 m_timerId = startTimer( 1000 / 30 );
169 else
171 m_timerId = startTimer( 6 );
173 show();
176 ////////////////////////////////////////////////////////////////////////
177 // Protected
178 ////////////////////////////////////////////////////////////////////////
180 void PopupMessage::timerEvent( QTimerEvent* )
182 switch( m_maskEffect )
184 case Plain:
185 plainMask();
186 break;
188 case Slide:
189 slideMask();
190 break;
192 case Dissolve:
193 dissolveMask();
194 break;
198 void PopupMessage::countDown()
200 if( !m_timeout )
202 killTimer( m_timerId );
203 return;
206 QFrame *&h = m_countdownFrame;
208 if( m_counter < h->height() - 3 )
209 QPainter( h ).fillRect( 2, 2, h->width() - 4, m_counter, palette().active().highlight() );
211 if( !testAttribute(Qt::WA_UnderMouse))
212 m_counter++;
214 if( m_counter > h->height() )
216 m_stage = 3;
217 killTimer( m_timerId );
218 m_timerId = startTimer( 6 );
220 else
222 killTimer( m_timerId );
223 m_timerId = startTimer( m_timeout / h->height() );
227 void PopupMessage::dissolveMask()
229 if( m_stage == 1 )
231 repaint();
232 QPainter maskPainter(&m_mask);
234 m_mask.fill(Qt::black);
236 maskPainter.setBrush(Qt::white);
237 maskPainter.setPen(Qt::white);
238 maskPainter.drawRect( m_mask.rect() );
240 m_dissolveSize += m_dissolveDelta;
242 if( m_dissolveSize > 0 )
244 maskPainter.setCompositionMode( QPainter::CompositionMode_SourceOut );
246 int x, y, s;
247 const int size = 16;
249 for (y = 0; y < height() + size; y += size)
251 x = width();
252 s = m_dissolveSize * x / 128;
254 for ( ; x > size; x -= size, s -= 2 )
256 if (s < 0)
257 break;
259 maskPainter.drawEllipse(x - s / 2, y - s / 2, s, s);
263 else if( m_dissolveSize < 0 )
265 m_dissolveDelta = 1;
266 killTimer( m_timerId );
268 if( m_timeout )
270 m_timerId = startTimer( 40 );
271 m_stage = 2;
275 setMask(m_mask);
277 else if ( m_stage == 2 )
279 countDown();
281 else
283 deleteLater();
288 void PopupMessage::plainMask()
290 switch( m_stage )
292 case 1: // Raise
293 killTimer( m_timerId );
294 if( m_timeout )
296 m_timerId = startTimer( 40 );
297 m_stage = 2;
300 break;
302 case 2: // Counter
303 countDown();
304 break;
306 case 3: // Lower/Remove
307 deleteLater();
312 void PopupMessage::slideMask()
314 switch( m_stage )
316 case 1: //raise
317 move( 0, m_parent->y() - m_offset );
319 m_offset++;
320 if( m_offset > height() )
322 killTimer( m_timerId );
324 if( m_timeout )
326 m_timerId = startTimer( 40 );
327 m_stage = 2;
331 break;
333 case 2: //fill in pause timer bar
334 countDown();
335 break;
337 case 3: //lower
338 m_offset--;
339 move( 0, m_parent->y() - m_offset );
341 if( m_offset < 0 )
342 deleteLater();
349 #include "popupMessage.moc"