1 /*
2 * PixmapButton.cpp - implementation of pixmap-button (often used as "themed"
3 * checkboxes/radiobuttons etc)
4 *
5 * Copyright (c) 2004-2013 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26
27 #include <QMouseEvent>
28 #include <QPainter>
29
30 #include "PixmapButton.h"
31 #include "MainWindow.h"
32 #include "embed.h"
33
34
35
PixmapButton(QWidget * _parent,const QString & _name)36 PixmapButton::PixmapButton( QWidget * _parent, const QString & _name ) :
37 AutomatableButton( _parent, _name ),
38 m_activePixmap(),
39 m_inactivePixmap(),
40 m_pressed( false )
41 {
42 setActiveGraphic( embed::getIconPixmap( "led_yellow" ) );
43 setInactiveGraphic( embed::getIconPixmap( "led_off" ), false );
44 }
45
46
47
48
~PixmapButton()49 PixmapButton::~PixmapButton()
50 {
51 }
52
53
54
55
paintEvent(QPaintEvent *)56 void PixmapButton::paintEvent( QPaintEvent * )
57 {
58 QPainter p( this );
59
60 if( ( model() != NULL && model()->value() ) || m_pressed )
61 {
62 if( !m_activePixmap.isNull() )
63 {
64 p.drawPixmap( 0, 0, m_activePixmap );
65 }
66 }
67 else if( !m_inactivePixmap.isNull() )
68 {
69 p.drawPixmap( 0, 0, m_inactivePixmap );
70 }
71 }
72
73
74
75
76
mousePressEvent(QMouseEvent * _me)77 void PixmapButton::mousePressEvent( QMouseEvent * _me )
78 {
79 // Show pressing graphics if this isn't checkable
80 if( !isCheckable() )
81 {
82 m_pressed = true;
83 update();
84 }
85
86 AutomatableButton::mousePressEvent( _me );
87 }
88
89
90
91
mouseReleaseEvent(QMouseEvent * _me)92 void PixmapButton::mouseReleaseEvent( QMouseEvent * _me )
93 {
94 AutomatableButton::mouseReleaseEvent( _me );
95
96 if( !isCheckable() )
97 {
98 m_pressed = false;
99 update();
100 }
101 }
102
103
104
105
mouseDoubleClickEvent(QMouseEvent * _me)106 void PixmapButton::mouseDoubleClickEvent( QMouseEvent * _me )
107 {
108 emit doubleClicked();
109 _me->accept();
110 }
111
112
113
114
setActiveGraphic(const QPixmap & _pm)115 void PixmapButton::setActiveGraphic( const QPixmap & _pm )
116 {
117 m_activePixmap = _pm;
118 resize( m_activePixmap.width(), m_activePixmap.height() );
119 }
120
121
122
123
setInactiveGraphic(const QPixmap & _pm,bool _update)124 void PixmapButton::setInactiveGraphic( const QPixmap & _pm, bool _update )
125 {
126 m_inactivePixmap = _pm;
127 if( _update )
128 {
129 update();
130 }
131 }
132
sizeHint() const133 QSize PixmapButton::sizeHint() const
134 {
135 if( ( model() != NULL && model()->value() ) || m_pressed )
136 {
137 return m_activePixmap.size();
138 }
139 else
140 {
141 return m_inactivePixmap.size();
142 }
143 }
144
145
146
147
148
149