hydrogen 1.2.6
ColorSelectionButton.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2008-2025 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
59#ifdef H2CORE_HAVE_QT6
60void ColorSelectionButton::enterEvent( QEnterEvent *ev ) {
61#else
63#endif
64 UNUSED( ev );
65 m_bMouseOver = true;
66 update();
67}
68
69
70void ColorSelectionButton::setColor( const QColor& color) {
71 m_sColor = color;
72 update();
73}
74
76 UNUSED( ev );
77 m_bMouseOver = false;
78 update();
79}
80
81void ColorSelectionButton::paintEvent( QPaintEvent* ev) {
82 QPainter painter(this);
83 QColor color( m_sColor );
84 QColor backgroundColor( "#333" );
85 if ( m_bMouseOver ) {
86 if ( isEnabled() ) {
87 backgroundColor = H2Core::Preferences::get_instance()->getColorTheme()->m_highlightColor;
88 } else {
89 backgroundColor = H2Core::Preferences::get_instance()->getColorTheme()->m_lightColor;
90 }
91 }
92
93 painter.setPen( backgroundColor );
94 painter.drawRect( 0, 0, width() - 1, height() -1 );
95 painter.setPen( color );
96 painter.fillRect( 3, 3, width() - 6, height() - 6, color );
97}
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