hydrogen 1.2.3
ColorSelectionButton.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
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 Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
24
25#include <QColorDialog>
26#include <core/Globals.h>
27
28ColorSelectionButton::ColorSelectionButton( QWidget* pParent, QColor sInitialColor, int nSize )
29 : QPushButton( pParent )
30 , m_sColor( sInitialColor )
31 , m_bMouseOver( false )
32{
33 setFlat( true );
34
35 if ( nSize != 0 ) {
36 QSize size( nSize, nSize );
37 setFixedSize( size );
38 resize( size );
39 }
40}
41
44
46
47 if ( isEnabled() ) {
48
49 QColor newColor = QColorDialog::getColor( m_sColor, this, tr( "Pick a pattern color" ) );
50
51 if ( m_sColor != newColor && newColor.isValid() ) {
52 m_sColor = newColor;
53 update();
54 emit colorChanged();
55 }
56 }
57}
58
60 UNUSED( ev );
61 m_bMouseOver = true;
62 update();
63}
64
65
66void ColorSelectionButton::setColor( const QColor& color) {
67 m_sColor = color;
68 update();
69}
70
72 UNUSED( ev );
73 m_bMouseOver = false;
74 update();
75}
76
77void ColorSelectionButton::paintEvent( QPaintEvent* ev) {
78 QPainter painter(this);
79 QColor color( m_sColor );
80 QColor backgroundColor( "#333" );
81 if ( m_bMouseOver ) {
82 if ( isEnabled() ) {
83 backgroundColor = H2Core::Preferences::get_instance()->getColorTheme()->m_highlightColor;
84 } else {
85 backgroundColor = H2Core::Preferences::get_instance()->getColorTheme()->m_lightColor;
86 }
87 }
88
89 painter.setPen( backgroundColor );
90 painter.drawRect( 0, 0, width() - 1, height() -1 );
91 painter.setPen( color );
92 painter.fillRect( 3, 3, width() - 6, height() - 6, color );
93}
virtual void mousePressEvent(QMouseEvent *ev) override
ColorSelectionButton(QWidget *pParent, QColor sInitialColor=Qt::black, int nSize=0)
virtual void leaveEvent(QEvent *ev) override
void setColor(const QColor &color)
virtual void paintEvent(QPaintEvent *ev) override
virtual void enterEvent(QEvent *ev) override
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
const std::shared_ptr< ColorTheme > getColorTheme() const
#define UNUSED(v)
Definition Globals.h:42