hydrogen 1.2.6
LED.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
23#include "LED.h"
24#include "../Skin.h"
25
26#include "../HydrogenApp.h"
27#include <core/Globals.h>
31
32LED::LED( QWidget *pParent, QSize size )
33 : QWidget( pParent )
34 , m_bActivated( false )
35{
36 setAttribute( Qt::WA_OpaquePaintEvent );
37 setFixedSize( size );
38
39 // Since the load function does not report success, we will check
40 // for the existence of the background image separately.
41 QString sPath;
42 float fAspectRatio = static_cast<float>( size.width() ) / static_cast<float>( size.height() );
43 if ( fAspectRatio < 1 ) {
44 sPath = QString( Skin::getSvgImagePath() + "/led_5_13.svg" );
45 } else {
46 sPath = QString( Skin::getSvgImagePath() + "/led_11_9.svg" );
47 }
48
49 QFile file( sPath );
50 if ( file.exists() ) {
51 m_background = new QSvgRenderer( sPath, this );
52 } else {
53 m_background = nullptr;
54 ERRORLOG( QString( "Unable to load background image [%1]" ).arg( sPath ) );
55 }
56
57 resize( size );
58}
59
61}
62
63void LED::setActivated( bool bActivated ) {
64 m_bActivated = bActivated;
65 update();
66}
67
68void LED::paintEvent( QPaintEvent* ev )
69{
70 QPainter painter( this );
71
72 if ( m_background != nullptr ) {
73
74 if ( m_bActivated ) {
75 m_background->render( &painter, "layer2" );
76 } else {
77 m_background->render( &painter, "layer1" );
78 }
79 }
80}
81
83
84MetronomeLED::MetronomeLED( QWidget *pParent, QSize size )
85 : LED( pParent, size )
86 , m_bFirstBar( false )
87 , m_activityTimeout( 250 )
88{
90
91 // Since the load function does not report success, we will check
92 // for the existence of the background image separately.
93 QString sPath( Skin::getSvgImagePath() + "/led_22_7.svg" );
94 QFile file( sPath );
95 if ( file.exists() ) {
96 m_background = new QSvgRenderer( sPath, this );
97 } else {
98 m_background = nullptr;
99 ERRORLOG( QString( "Unable to load background image [%1]" ).arg( sPath ) );
100 }
101
102 m_pTimer = new QTimer( this );
103 connect( m_pTimer, SIGNAL( timeout() ), this, SLOT( turnOff() ) );
104
105 resize( size );
106}
107
110
112
113 // Only trigger LED if the metronome button was pressed or it was
114 // activated via MIDI or OSC.
115 if ( ! H2Core::Preferences::get_instance()->m_bUseMetronome ) {
116 return;
117 }
118
119 m_bActivated = true;
120 m_bFirstBar = nValue == 0;
121
122 update();
123
124 m_pTimer->start( std::chrono::duration_cast<std::chrono::milliseconds>( m_activityTimeout ).count() );
125}
126
128 m_pTimer->stop();
129 m_bActivated = false;
130 update();
131}
132
133void MetronomeLED::paintEvent( QPaintEvent* ev )
134{
135 QPainter painter( this );
136
137 if ( m_background != nullptr ) {
138
139 if ( m_bActivated ) {
140 if ( m_bFirstBar ) {
141 m_background->render( &painter, "layer3" );
142 } else {
143 m_background->render( &painter, "layer2" );
144 }
145 } else {
146 m_background->render( &painter, "layer1" );
147 }
148 }
149}
#define ERRORLOG(x)
Definition Object.h:242
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
void addEventListener(EventListener *pListener)
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
virtual ~LED()
Definition LED.cpp:60
bool m_bActivated
Definition LED.h:60
LED(QWidget *pParent, QSize size)
Definition LED.cpp:32
void setActivated(bool bActivated)
Definition LED.cpp:63
QSvgRenderer * m_background
Definition LED.h:58
virtual void paintEvent(QPaintEvent *ev) override
Definition LED.cpp:68
std::chrono::milliseconds m_activityTimeout
Definition LED.h:89
QTimer * m_pTimer
Definition LED.h:88
void turnOff()
Definition LED.cpp:127
virtual ~MetronomeLED()
Definition LED.cpp:108
bool m_bFirstBar
Definition LED.h:87
virtual void metronomeEvent(int nValue) override
Definition LED.cpp:111
MetronomeLED(QWidget *pParent, QSize size)
Definition LED.cpp:84
virtual void paintEvent(QPaintEvent *ev) override
Definition LED.cpp:133
static QString getSvgImagePath()
Definition Skin.h:40