hydrogen 1.2.6
CpuLoadWidget.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 "CpuLoadWidget.h"
24
25#include <core/Hydrogen.h>
27
28#include "../HydrogenApp.h"
29
31 : QWidget( pParent )
32 , m_fValue( 0 )
33 , m_nXRunValue( 0 )
34 , m_size( QSize( 96, 10 ) )
35{
36 setAttribute(Qt::WA_OpaquePaintEvent);
37
38 adjustSize();
39 setFixedSize( m_size.width(), m_size.height() );
40
41 m_recentValues.resize( 5 );
42 for ( auto& ii : m_recentValues ) {
43 ii = 0;
44 }
45
46 QTimer* pTimer = new QTimer(this);
47 connect( pTimer, SIGNAL( timeout() ), this, SLOT( updateCpuLoadWidget() ) );
48 pTimer->start( 100 ); // update player control at 10 fps
49
51
52 resize( m_size.width(), m_size.height() );
53}
54
55
56
59
60void CpuLoadWidget::paintEvent( QPaintEvent*)
61{
62 if ( !isVisible() ) {
63 return;
64 }
65
66 QPainter painter(this);
67
68 float fSum = 0;
69 for ( auto ii : m_recentValues ) {
70 fSum += ii;
71 }
72 float fPeak = static_cast<float>( m_size.width() ) * fSum / static_cast<float>( m_recentValues.size() );
73 float fBorderWidth = 2;
74
75 QColor colorGradientGreen( Qt::green );
76 QColor colorGradientLightGreen( 175, 255, 0 );
77 QColor colorGradientYellow( Qt::yellow );
78 QColor colorGradientOrange( 255, 125, 0 );
79 QColor colorGradientRed( Qt::red );
80 QColor colorBorder( QColor( 0, 0, 0 ) );
81
82 QLinearGradient gradient = QLinearGradient( 0, 0, m_size.width(), m_size.height() );
83 gradient.setColorAt( 0.0, colorGradientGreen );
84 gradient.setColorAt( 0.5, colorGradientLightGreen );
85 gradient.setColorAt( 0.7, colorGradientYellow );
86 gradient.setColorAt( 0.8, colorGradientOrange );
87 gradient.setColorAt( 0.92, colorGradientRed );
88
89 painter.fillRect( QRect( 0, 0, m_size.width(), m_size.height() ),
90 H2Core::Preferences::get_instance()->getColorTheme()->m_midLightColor );
91 painter.fillRect( QRectF( fBorderWidth / 2, fBorderWidth / 2, fPeak, m_size.height() - fBorderWidth ), QBrush( gradient ) );
92
93 QPen pen;
94 if ( m_nXRunValue > 0 ) {
95 pen.setColor( colorGradientRed );
96 } else {
97 pen.setColor( colorBorder );
98 }
99 pen.setWidth( fBorderWidth );
100 painter.setPen( pen );
101
102 // Border
103 painter.drawRoundedRect( QRect( fBorderWidth / 2, fBorderWidth / 2, m_size.width() - fBorderWidth,
104 m_size.height() - fBorderWidth ), 1, 1 );
105
106 // Grid lines
107 float fDistance = 5;
108
109 pen.setWidth( 1 );
110 painter.setPen( pen );
111 float fXX = fDistance;
112 while ( fXX < m_size.width() - fBorderWidth ) {
113 painter.drawLine( fXX, fBorderWidth, fXX, m_size.height() - fBorderWidth );
114 fXX += fDistance;
115 }
116}
117
119{
120 // Process time
122 float fPercentage = 0;
123 if ( pAudioEngine->getMaxProcessTime() != 0.0 ) {
124 fPercentage = ( pAudioEngine->getProcessTime() / pAudioEngine->getMaxProcessTime() );
125 }
126
127 if ( fPercentage > 1.0 ) {
128 fPercentage = 1.0;
129 } else if ( fPercentage < 0.0 ) {
130 fPercentage = 0.0;
131 }
132
133 for ( int ii = ( m_recentValues.size() - 1 ) ; ii > 0; ii-- ) {
134 m_recentValues[ ii ] = m_recentValues[ ii - 1 ];
135 }
136 m_recentValues[ 0 ] = fPercentage;
137
138 if ( m_nXRunValue > 0 ){
139 m_nXRunValue--;
140 }
141
142 update();
143}
144
145
146
148{
149 m_nXRunValue = 31;
150
151 update();
152}
153
154
155
CpuLoadWidget(QWidget *pParent)
void updateCpuLoadWidget()
virtual void XRunEvent() override
virtual void paintEvent(QPaintEvent *ev) override
std::vector< float > m_recentValues
The audio engine deals with two distinct TransportPosition.
Definition AudioEngine.h:99
float getProcessTime() const
float getMaxProcessTime() const
static Hydrogen * get_instance()
Returns the current Hydrogen instance __instance.
Definition Hydrogen.h:84
AudioEngine * getAudioEngine() const
Definition Hydrogen.h:663
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.