some more work on collabsible albums. I think I will need to optimize the playlist...
[amarok.git] / src / statusbar / selectLabel.h
blob3dad4fdf200991e190c9b5285bbcda8235a33d04
1 /***************************************************************************
2 * Copyright (C) 2005 by Max Howell <max.howell@methylblue.com> *
3 * (C) 2004 Frederik Holljen <fh@ez.no> *
4 * (C) 2005 Gábor Lehel <illissius@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
22 /** WARNING this is not meant for use outside this unit! */
24 #ifndef AMAROK_SELECTLABEL_H
25 #define AMAROK_SELECTLABEL_H
27 #include "actionclasses.h"
28 #include "amarok.h"
29 #include "amarokconfig.h"
30 #include "overlayWidget.h"
31 #include "popupMessage.h"
33 #include <QIcon>
34 #include <QLabel>
35 #include <QTimer>
36 #include <QToolTip>
37 //Added by qt3to4:
38 #include <QPixmap>
39 #include <QEvent>
40 #include <QMouseEvent>
41 #include <QStyle>
43 #include <kactioncollection.h>
44 #include <kglobalsettings.h>
45 #include <kiconloader.h>
46 #include <klocale.h>
47 #include <kpassivepopup.h>
51 class SelectLabel : public QLabel
53 Q_OBJECT
55 Amarok::SelectAction const*const m_action;
57 signals:
58 void activated( int );
60 public:
61 SelectLabel( Amarok::SelectAction const*const action, QWidget *parent )
62 : QLabel( parent )
63 , m_action( action )
64 , m_tooltip( 0 )
65 , m_tooltipShowing( false )
66 , m_tooltipHidden( false )
68 connect( this, SIGNAL( activated( int ) ), action, SLOT( setCurrentItem( int ) ) );
69 connect( action, SIGNAL( activated( int ) ), this, SLOT( setCurrentItem( int ) ) );
70 connect( action, SIGNAL( enabled( bool ) ), this, SLOT( setEnabled( bool ) ) );
72 setCurrentItem( currentItem() );
75 inline int currentItem() const { return m_action->currentItem(); }
76 inline bool isEnabled() const { return m_action->isEnabled(); }
78 protected:
79 void mousePressEvent( QMouseEvent* )
81 bool shown = m_tooltipShowing;
82 hideToolTip();
83 int n = currentItem();
84 do //TODO doesn't handle all of them being disabled, but we don't do that anyways.
86 n = ( int( n ) == m_action->items().count() - 1 ) ? 0 : n + 1;
87 } while ( !m_action->menu()->isItemEnabled( n ) );
88 if( isEnabled() )
90 setCurrentItem( n );
91 emit activated( n );
92 if( shown )
94 m_tooltipHidden = false;
95 showToolTip();
100 void enterEvent( QEvent* )
102 //Show the tooltip after 1/2 second
103 m_tooltipHidden = false;
104 QTimer::singleShot( 500, this, SLOT(aboutToShow()) );
107 void leaveEvent( QEvent* )
109 hideToolTip();
112 public slots:
113 void setCurrentItem( int )
115 if( isEnabled() && !m_action->currentIcon().isNull() )
116 setPixmap( SmallIcon( m_action->currentIcon() ) );
119 void setEnabled( bool /*on*/ )
121 if( !m_action->currentIcon().isNull() )
122 setPixmap( KIcon( m_action->currentIcon() ).pixmap( style()->pixelMetric(QStyle::PM_SmallIconSize), QIcon::Disabled ) );
125 private slots:
126 void aboutToShow()
128 if( testAttribute(Qt::WA_UnderMouse) && !m_tooltipHidden )
129 showToolTip();
132 private:
133 void showToolTip()
135 if( m_tooltipShowing )
136 return;
138 m_tooltipShowing = true;
140 QString tip = i18n("%1: %2", m_action->text().remove( '&' ), m_action->currentText().remove( '&' ) );
142 if( !isEnabled() )
143 tip += i18n("&nbsp;<br>&nbsp;<i>Disabled</i>");
144 else if( AmarokConfig::favorTracks() &&
145 m_action == Amarok::actionCollection()->action( "random_mode" ) ) //hack?
147 KSelectAction *a = static_cast<KSelectAction*>( Amarok::actionCollection()->action( "favor_tracks" ) );
148 tip += QString("<br><br>") + i18n("%1: %2", a->text().remove( '&' ), a->currentText().remove( '&' ) );
151 const QPixmap pix = KIcon( m_action->currentIcon() )
152 .pixmap( style()->pixelMetric(QStyle::PM_SmallIconSize), m_action->isEnabled()
153 ? QIcon::Normal
154 : QIcon::Disabled );
155 m_tooltip = new KPassivePopup( parentWidget()->parentWidget() );
156 KHBox *labBox = new KHBox( m_tooltip );
157 QLabel *lab = new QLabel( labBox );
158 lab->setPixmap( pix );
159 QLabel *lab1 = new QLabel( labBox );
160 lab1->setText( tip );
161 m_tooltip->setView( labBox );
162 m_tooltip->show( mapToGlobal( QPoint(parentWidget()->contentsRect().topLeft().x() - 70,
163 parentWidget()->contentsRect().topLeft().y() - 80 ) ) );
166 void hideToolTip()
168 m_tooltipHidden = true;
169 if( m_tooltipShowing )
171 m_tooltip->hide();
172 m_tooltipShowing = false;
176 KPassivePopup *m_tooltip;
177 bool m_tooltipShowing;
178 bool m_tooltipHidden;
182 #endif