hydrogen 1.2.3
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-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 "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
105void ClickableLabel::enterEvent( QEvent* ev ) {
106 QLabel::enterEvent( ev );
107 if ( m_bIsEditable ) {
108 m_bEntered = true;
109 update();
110 }
111}
112
113void ClickableLabel::leaveEvent( QEvent* ev ) {
114 QLabel::leaveEvent( ev );
115 if ( m_bIsEditable ) {
116 m_bEntered = false;
117 update();
118 }
119}
120
121void ClickableLabel::updateFont( QString sFontFamily, H2Core::FontTheme::FontSize fontSize ) {
122
123 int nPixelSize = 0;
124
125 if ( ! m_size.isNull() ) {
126
127 float fScalingFactor = 1.0;
128 switch ( fontSize ) {
130 fScalingFactor = 1.0;
131 break;
133 fScalingFactor = 0.75;
134 break;
136 fScalingFactor = 0.5;
137 break;
138 }
139
140 int nMargin;
141 if ( m_size.height() <= 9 ) {
142 nMargin = 1;
143 } else if ( m_size.height() <= 16 ) {
144 nMargin = 2;
145 } else {
146 nMargin = 8;
147 }
148
149 nPixelSize = m_size.height() - std::round( fScalingFactor * nMargin );
150 }
151
152 QFont font( sFontFamily );
153
154 if ( ! m_size.isNull() ) {
155 font.setPixelSize( nPixelSize );
156 }
157 font.setBold( true );
158
159 if ( ! m_size.isNull() || width() > height() ) {
160 // Check whether the width of the text fits the available frame
161 // width of the label
162 while ( QFontMetrics( font ).size( Qt::TextSingleLine, text() ).width() >
163 width() && nPixelSize > 1 ) {
164 nPixelSize--;
165 font.setPixelSize( nPixelSize );
166 }
167 }
168
169 // This method must not be called more than once in this routine. Otherwise,
170 // a repaint of the widget is triggered, which calls `updateFont()` again
171 // and we are trapped in an infinite loop.
172 setFont( font );
173}
174
177
178 if ( changes & ( H2Core::Preferences::Changes::Colors |
180 updateFont( pPref->getLevel3FontFamily(), pPref->getFontSize() );
182 }
183}
184
185void ClickableLabel::setText( const QString& sNewText ) {
186 if ( text() == sNewText ) {
187 return;
188 }
189
191
192 QLabel::setText( sNewText );
193 updateFont( pPref->getLevel3FontFamily(), pPref->getFontSize() );
194}
void labelClicked(ClickableLabel *pLabel)
virtual void enterEvent(QEvent *e) override
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 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