hydrogen 1.2.3
LCDCombo.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include "LCDCombo.h"
24
25#include "../HydrogenApp.h"
26#include "../Skin.h"
27
28#include <core/Globals.h>
29
30
31LCDCombo::LCDCombo( QWidget *pParent, QSize size, bool bModifyOnChange )
32 : QComboBox( pParent )
33 , m_size( size )
34 , m_bEntered( false )
35 , m_bModifyOnChange( bModifyOnChange )
36 , m_nMaxWidth( 0 )
37 , m_bIsActive( true )
38{
39 setFocusPolicy( Qt::ClickFocus );
40
41 if ( ! size.isNull() ) {
42 adjustSize();
43 setFixedSize( size );
44 }
45
47
49 // Mark the current song modified if there was an user interaction
50 // with the widget.
51 connect( this, SIGNAL( activated(int) ), this, SLOT( handleIsModified(int) ) );
52}
53
56
62
63void LCDCombo::addItem( const QString& sText, const QVariant& userData ) {
64 int nWidth =
65 fontMetrics().size( Qt::TextSingleLine, sText ).width() *
66 1.1 + // custom factor to ensure the text does fit
67 view()->autoScrollMargin(); // width of the scrollbar
68
69 if ( nWidth > m_nMaxWidth ) {
70 m_nMaxWidth = nWidth;
71 }
72
73 QComboBox::addItem( sText, userData );
74}
75
76void LCDCombo::setIsActive( bool bIsActive ) {
77 m_bIsActive = bIsActive;
78
79 update();
80
81 setEnabled( bIsActive );
82}
83
85 if ( m_nMaxWidth > view()->sizeHint().width() ) {
86 view()->setMinimumWidth( m_nMaxWidth );
87 }
88
89 QComboBox::showPopup();
90}
91
93
95
96
97 QColor widgetColor = pPref->getColorTheme()->m_widgetColor;
98 QColor widgetTextColor = pPref->getColorTheme()->m_widgetTextColor;
99 QColor widgetInactiveColor =
100 Skin::makeWidgetColorInactive( widgetColor );
101 QColor widgetTextInactiveColor =
102 Skin::makeTextColorInactive( widgetTextColor );
103
104 setStyleSheet( QString( "\
105QComboBox:enabled { \
106 color: %1; \
107 background-color: %2; \
108 font-family: %3; \
109 font-size: %4; \
110} \
111QComboBox:disabled { \
112 color: %5; \
113 background-color: %6; \
114 font-family: %3; \
115 font-size: %4; \
116} \
117QComboBox QAbstractItemView { \
118 color: %1; \
119 background-color: #babfcf; \
120}")
121 .arg( widgetTextColor.name() )
122 .arg( widgetColor.name() )
123 .arg( pPref->getLevel3FontFamily() )
124 .arg( getPointSize( pPref->getFontSize() ) )
125 .arg( widgetTextInactiveColor.name() )
126 .arg( widgetInactiveColor.name() ) );
127}
128
136
137void LCDCombo::paintEvent( QPaintEvent *ev ) {
138
140
141 QComboBox::paintEvent( ev );
142
143 if ( m_bEntered || hasFocus() ) {
144 QPainter painter(this);
145
146 QColor colorHighlightActive;
147 if ( m_bIsActive ) {
148 colorHighlightActive = pPref->getColorTheme()->m_highlightColor;
149 } else {
150 colorHighlightActive = pPref->getColorTheme()->m_lightColor;
151 }
152
153 // If the mouse is placed on the widget but the user hasn't
154 // clicked it yet, the highlight will be done more transparent to
155 // indicate that keyboard inputs are not accepted yet.
156 if ( ! hasFocus() ) {
157 colorHighlightActive.setAlpha( 150 );
158 }
159
160 painter.fillRect( 0, m_size.height() - 2, m_size.width(), 2, colorHighlightActive );
161 }
162}
163
164void LCDCombo::enterEvent( QEvent* ev ) {
165 QComboBox::enterEvent( ev );
166 m_bEntered = true;
167}
168
169void LCDCombo::leaveEvent( QEvent* ev ) {
170 QComboBox::leaveEvent( ev );
171 m_bEntered = false;
172}
173
174void LCDCombo::setSize( QSize size ) {
175 m_size = size;
176
177 setFixedSize( size );
178 adjustSize();
179}
static Hydrogen * get_instance()
Returns the current Hydrogen instance __instance.
Definition Hydrogen.h:83
void setIsModified(bool bIsModified)
Wrapper around Song::setIsModified() that checks whether a song is set.
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
Changes
Bitwise or-able options showing which part of the Preferences were altered using the PreferencesDialo...
@ Font
Either the font size or font family have changed.
@ Colors
At least one of the colors has changed.
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
void preferencesChanged(H2Core::Preferences::Changes changes)
Propagates a change in the Preferences through the GUI.
virtual void showPopup() override
Definition LCDCombo.cpp:84
QSize m_size
Definition LCDCombo.h:59
LCDCombo(QWidget *pParent, QSize size=QSize(0, 0), bool bModifyOnChange=false)
Definition LCDCombo.cpp:31
int m_nMaxWidth
Keep track of the text width of the items added.
Definition LCDCombo.h:71
void onPreferencesChanged(H2Core::Preferences::Changes changes)
Definition LCDCombo.cpp:129
bool m_bModifyOnChange
Whether Hydrogen::setIsModified() is invoked with true as soon as the value of the widget does change...
Definition LCDCombo.h:66
virtual void leaveEvent(QEvent *ev) override
Definition LCDCombo.cpp:169
void updateStyleSheet()
Definition LCDCombo.cpp:92
void setSize(QSize size)
Definition LCDCombo.cpp:174
bool m_bIsActive
Definition LCDCombo.h:62
void handleIsModified(int)
Definition LCDCombo.cpp:57
virtual void paintEvent(QPaintEvent *ev) override
Definition LCDCombo.cpp:137
void setIsActive(bool bIsActive)
Definition LCDCombo.cpp:76
void addItem(const QString &text, const QVariant &userData=QVariant())
Definition LCDCombo.cpp:63
virtual void enterEvent(QEvent *ev) override
Definition LCDCombo.cpp:164
bool m_bEntered
Definition LCDCombo.h:61
static QColor makeTextColorInactive(QColor color)
If a widget is marked inactive the value of its text color are reduced by this factor.
Definition Skin.cpp:176
static QColor makeWidgetColorInactive(QColor color)
If a widget is marked inactive the value of its background color are reduced by this factor.
Definition Skin.cpp:167
constexpr int getPointSize(H2Core::FontTheme::FontSize fontSize) const