hydrogen 1.2.6
ClickableLabel.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-2025 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 "ClickableLabel.h"
24#include "../HydrogenApp.h"
25
26#include <QtGui>
27#include <QtWidgets>
28
29#include <core/Globals.h>
30
31ClickableLabel::ClickableLabel( QWidget *pParent, QSize size, QString sText, Color color, bool bIsEditable )
32 : QLabel( pParent )
33 , m_size( size )
34 , m_color( color )
35 , m_bIsEditable( bIsEditable )
36 , m_bEntered( false )
37{
38 if ( ! size.isNull() ) {
39 setFixedSize( size );
40 resize( size );
41 }
42
44
45 updateFont( pPref->getLevel3FontFamily(), pPref->getFontSize() );
47
48 setAlignment( Qt::AlignCenter );
49 setText( sText );
50
52
53}
54
56
58
59 QColor text;
60 if ( m_color == Color::Bright ) {
61 text = pPref->getColorTheme()->m_windowTextColor;
62 } else {
63 text = pPref->getColorTheme()->m_widgetTextColor;
64 }
65
66 setStyleSheet( QString( "QLabel { color: %1; }" ).arg( text.name() ) );
67}
68
69void ClickableLabel::mousePressEvent( QMouseEvent * e )
70{
71 UNUSED( e );
72 emit labelClicked( this );
73}
74
75void ClickableLabel::paintEvent( QPaintEvent *ev ) {
76
77 QLabel::paintEvent( ev );
78
79 if ( ! m_bIsEditable ) {
80 return;
81 }
82
84
85 if ( m_bEntered || hasFocus() ) {
86 QPainter painter(this);
87
88 QColor colorHighlightActive = pPref->getColorTheme()->m_highlightColor;
89
90 // If the mouse is placed on the widget but the user hasn't
91 // clicked it yet, the highlight will be done more transparent to
92 // indicate that keyboard inputs are not accepted yet.
93 if ( ! hasFocus() ) {
94 colorHighlightActive.setAlpha( 150 );
95 }
96
97 QPen pen;
98 pen.setColor( colorHighlightActive );
99 pen.setWidth( 2 );
100 painter.setPen( pen );
101 painter.drawRoundedRect( QRect( 1, 1, m_size.width() - 2, m_size.height() - 2 ), 3, 3 );
102 }
103}
104
105#ifdef H2CORE_HAVE_QT6
106void ClickableLabel::enterEvent( QEnterEvent *ev ) {
107#else
108void ClickableLabel::enterEvent( QEvent *ev ) {
109#endif
110 QLabel::enterEvent( ev );
111 if ( m_bIsEditable ) {
112 m_bEntered = true;
113 update();
114 }
115}
116
117void ClickableLabel::leaveEvent( QEvent* ev ) {
118 QLabel::leaveEvent( ev );
119 if ( m_bIsEditable ) {
120 m_bEntered = false;
121 update();
122 }
123}
124
125void ClickableLabel::updateFont( QString sFontFamily, H2Core::FontTheme::FontSize fontSize ) {
126
127 int nPixelSize = 0;
128
129 if ( ! m_size.isNull() ) {
130
131 float fScalingFactor = 1.0;
132 switch ( fontSize ) {
134 fScalingFactor = 1.0;
135 break;
137 fScalingFactor = 0.75;
138 break;
140 fScalingFactor = 0.5;
141 break;
142 }
143
144 int nMargin;
145 if ( m_size.height() <= 9 ) {
146 nMargin = 1;
147 } else if ( m_size.height() <= 16 ) {
148 nMargin = 2;
149 } else {
150 nMargin = 8;
151 }
152
153 nPixelSize = m_size.height() - std::round( fScalingFactor * nMargin );
154 }
155
156 QFont font( sFontFamily );
157
158 if ( ! m_size.isNull() ) {
159 font.setPixelSize( nPixelSize );
160 }
161 font.setBold( true );
162
163 if ( ! m_size.isNull() || width() > height() ) {
164 // Check whether the width of the text fits the available frame
165 // width of the label
166 while ( QFontMetrics( font ).size( Qt::TextSingleLine, text() ).width() >
167 width() && nPixelSize > 1 ) {
168 nPixelSize--;
169 font.setPixelSize( nPixelSize );
170 }
171 }
172
173 // This method must not be called more than once in this routine. Otherwise,
174 // a repaint of the widget is triggered, which calls `updateFont()` again
175 // and we are trapped in an infinite loop.
176 setFont( font );
177}
178
181
182 if ( changes & ( H2Core::Preferences::Changes::Colors |
184 updateFont( pPref->getLevel3FontFamily(), pPref->getFontSize() );
186 }
187}
188
189void ClickableLabel::setText( const QString& sNewText ) {
190 if ( text() == sNewText ) {
191 return;
192 }
193
195
196 QLabel::setText( sNewText );
197 updateFont( pPref->getLevel3FontFamily(), pPref->getFontSize() );
198}
void labelClicked(ClickableLabel *pLabel)
virtual void mousePressEvent(QMouseEvent *e) override
bool m_bIsEditable
If set to true a highlight will be painted when hovered.
virtual void leaveEvent(QEvent *e) override
void onPreferencesChanged(H2Core::Preferences::Changes changes)
void setText(const QString &sNewText)
ClickableLabel(QWidget *pParent, QSize size=QSize(0, 0), QString sText="", Color color=Color::Bright, bool bIsEditable=false)
Color
The individual colors of the text won't be exposed but are up to the palette/application-wide setting...
void updateFont(QString sFontFamily, H2Core::FontTheme::FontSize fontSize)
virtual void enterEvent(QEvent *ev) override
virtual void paintEvent(QPaintEvent *e) override
FontSize
Enables custom scaling of the font size in the GUI.
Definition Theme.h:184
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.
#define UNUSED(v)
Definition Globals.h:42