1 /*
2  * NStateButton.cpp - implementation of n-state-button
3  *
4  * Copyright (c) 2005-2006 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5  *
6  * This file is part of LMMS - https://lmms.io
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program (see COPYING); if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  *
23  */
24 
25 
26 #include <QMouseEvent>
27 
28 #include "NStateButton.h"
29 #include "ToolTip.h"
30 
31 
32 
NStateButton(QWidget * _parent)33 NStateButton::NStateButton( QWidget * _parent ) :
34 	ToolButton( _parent ),
35 	m_generalToolTip( "" ),
36 	m_curState( -1 )
37 {
38 }
39 
40 
41 
42 
~NStateButton()43 NStateButton::~NStateButton()
44 {
45 	while( m_states.size() )
46 	{
47 		m_states.erase( m_states.begin() );
48 	}
49 }
50 
51 
52 
53 
addState(const QPixmap & _pm,const QString & _tooltip)54 void NStateButton::addState( const QPixmap & _pm, const QString & _tooltip )
55 {
56 	m_states.push_back( qMakePair( _pm, _tooltip ) );
57 	// first inserted pixmap?
58 	if( m_states.size() == 1 )
59 	{
60 		// and set state to first pixmap
61 		changeState( 0 );
62 	}
63 }
64 
65 
66 
67 
changeState(int _n)68 void NStateButton::changeState( int _n )
69 {
70 	if( _n >= 0 && _n < (int) m_states.size() )
71 	{
72 		m_curState = _n;
73 
74 		const QString & _tooltip =
75 			( m_states[m_curState].second != "" ) ?
76 				m_states[m_curState].second :
77 					m_generalToolTip;
78 		ToolTip::add( this, _tooltip );
79 
80 		setIcon( m_states[m_curState].first );
81 
82 		emit changedState( m_curState );
83 	}
84 }
85 
86 
87 
mousePressEvent(QMouseEvent * _me)88 void NStateButton::mousePressEvent( QMouseEvent * _me )
89 {
90 	if( _me->button() == Qt::LeftButton && m_states.size() )
91 	{
92 		changeState( ( ++m_curState ) % m_states.size() );
93 	}
94 	ToolButton::mousePressEvent( _me );
95 }
96