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> *
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. *
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. *
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"
29 #include "amarokconfig.h"
30 #include "overlayWidget.h"
31 #include "popupMessage.h"
40 #include <QMouseEvent>
43 #include <kactioncollection.h>
44 #include <kglobalsettings.h>
45 #include <kiconloader.h>
47 #include <kpassivepopup.h>
51 class SelectLabel
: public QLabel
55 Amarok::SelectAction
const*const m_action
;
58 void activated( int );
61 SelectLabel( Amarok::SelectAction
const*const action
, QWidget
*parent
)
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(); }
79 void mousePressEvent( QMouseEvent
* )
81 bool shown
= m_tooltipShowing
;
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
) );
94 m_tooltipHidden
= false;
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
* )
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
) );
128 if( testAttribute(Qt::WA_UnderMouse
) && !m_tooltipHidden
)
135 if( m_tooltipShowing
)
138 m_tooltipShowing
= true;
140 QString tip
= i18n("%1: %2", m_action
->text().remove( '&' ), m_action
->currentText().remove( '&' ) );
143 tip
+= i18n(" <br> <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()
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 ) ) );
168 m_tooltipHidden
= true;
169 if( m_tooltipShowing
)
172 m_tooltipShowing
= false;
176 KPassivePopup
*m_tooltip
;
177 bool m_tooltipShowing
;
178 bool m_tooltipHidden
;