jamulus/src/levelmeter.cpp

316 lines
10 KiB
C++
Raw Normal View History

2013-01-23 11:41:13 +01:00
/******************************************************************************\
2020-01-01 15:41:43 +01:00
* Copyright (c) 2004-2020
2013-01-23 11:41:13 +01:00
*
* Author(s):
* Volker Fischer
*
* Description:
* Implements a multi color LED bar
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2013-01-23 11:41:13 +01:00
*
\******************************************************************************/
2020-06-21 19:55:12 +02:00
#include "levelmeter.h"
2013-01-23 11:41:13 +01:00
/* Implementation *************************************************************/
2020-06-21 20:09:13 +02:00
CLevelMeter::CLevelMeter ( QWidget* parent, Qt::WindowFlags f ) :
QWidget ( parent, f ),
eLevelMeterType ( MT_BAR )
2013-01-23 11:41:13 +01:00
{
// initialize LED meter
QWidget* pLEDMeter = new QWidget();
QVBoxLayout* pLEDLayout = new QVBoxLayout ( pLEDMeter );
pLEDLayout->setAlignment ( Qt::AlignHCenter );
pLEDLayout->setMargin ( 0 );
pLEDLayout->setSpacing ( 0 );
2013-01-23 11:41:13 +01:00
// create LEDs plus the clip LED
vecpLEDs.Init ( NUM_LEDS_INCL_CLIP_LED );
for ( int iLEDIdx = NUM_LEDS_INCL_CLIP_LED - 1; iLEDIdx >= 0; iLEDIdx-- )
2013-01-23 11:41:13 +01:00
{
// create LED object
vecpLEDs[iLEDIdx] = new cLED ( parent );
// add LED to layout with spacer (do not add spacer on the bottom of the first LED)
if ( iLEDIdx < NUM_LEDS_INCL_CLIP_LED - 1 )
2013-01-23 11:41:13 +01:00
{
pLEDLayout->addStretch();
2013-01-23 11:41:13 +01:00
}
2020-06-22 18:31:30 +02:00
pLEDLayout->addWidget ( vecpLEDs[iLEDIdx]->GetLabelPointer() );
2013-01-23 11:41:13 +01:00
}
// initialize bar meter
2020-06-21 22:39:15 +02:00
pBarMeter = new QProgressBar();
pBarMeter->setOrientation ( Qt::Vertical );
2020-06-23 17:28:19 +02:00
pBarMeter->setRange ( 0, 100 * NUM_STEPS_LED_BAR ); // use factor 100 to reduce quantization (bar is continuous)
2020-06-21 22:39:15 +02:00
pBarMeter->setFormat ( "" ); // suppress percent numbers
// setup stacked layout for meter type switching mechanism
pStackedLayout = new QStackedLayout ( this );
pStackedLayout->addWidget ( pLEDMeter );
2020-06-21 22:39:15 +02:00
pStackedLayout->addWidget ( pBarMeter );
// according to QScrollArea description: "When using a scroll area to display the
// contents of a custom widget, it is important to ensure that the size hint of
// the child widget is set to a suitable value."
2020-06-21 22:39:15 +02:00
pBarMeter->setMinimumSize ( QSize ( 1, 1 ) );
pLEDMeter->setMinimumSize ( QSize ( 1, 1 ) );
// update the meter type (using the default value of the meter type)
SetLevelMeterType ( eLevelMeterType );
// setup clip indicator timer
TimerClip.setSingleShot ( true );
TimerClip.setInterval ( CLIP_IND_TIME_OUT_MS );
// Connections -------------------------------------------------------------
QObject::connect ( &TimerClip, &QTimer::timeout,
this, &CLevelMeter::ClipReset );
2013-01-23 11:41:13 +01:00
}
2020-06-21 20:09:13 +02:00
CLevelMeter::~CLevelMeter()
2013-01-23 11:41:13 +01:00
{
// clean up the LED objects
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
2013-01-23 11:41:13 +01:00
{
delete vecpLEDs[iLEDIdx];
}
}
2020-06-21 20:09:13 +02:00
void CLevelMeter::SetLevelMeterType ( const ELevelMeterType eNType )
{
eLevelMeterType = eNType;
switch ( eNType )
{
case MT_LED:
2020-06-22 18:39:50 +02:00
// initialize all LEDs
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_BLACK );
}
pStackedLayout->setCurrentIndex ( 0 );
break;
case MT_BAR:
pStackedLayout->setCurrentIndex ( 1 );
break;
case MT_SLIM_BAR:
// set all LEDs to disabled, otherwise we would not get our desired small width
for ( int iLEDIdx = 0; iLEDIdx < NUM_LEDS_INCL_CLIP_LED; iLEDIdx++ )
{
2020-06-22 18:31:30 +02:00
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_DISABLED );
}
pStackedLayout->setCurrentIndex ( 1 );
break;
}
// update bar meter style and reset clip state
SetBarMeterStyleAndClipStatus ( eNType, false );
}
void CLevelMeter::SetBarMeterStyleAndClipStatus ( const ELevelMeterType eNType,
const bool bIsClip )
{
switch ( eNType )
{
case MT_SLIM_BAR:
if ( bIsClip )
{
pBarMeter->setStyleSheet (
"QProgressBar { border: 0px solid red;"
" margin: 0px;"
" padding: 0px;"
" width: 4px;"
" background: red; }"
"QProgressBar::chunk { background: green; }" );
}
else
{
pBarMeter->setStyleSheet (
"QProgressBar { border: 0px;"
" margin: 0px;"
" padding: 0px;"
" width: 4px; }"
"QProgressBar::chunk { background: green; }" );
}
break;
default: /* MT_BAR */
if ( bIsClip )
{
pBarMeter->setStyleSheet (
"QProgressBar { border: 2px solid red;"
" margin: 1px;"
" padding: 1px;"
" width: 15px;"
" background: transparent; }"
"QProgressBar::chunk { background: green; }" );
}
else
{
pBarMeter->setStyleSheet (
"QProgressBar { margin: 1px;"
" padding: 1px;"
" width: 15px; }"
"QProgressBar::chunk { background: green; }" );
}
break;
}
}
2020-06-21 22:39:15 +02:00
void CLevelMeter::SetValue ( const double dValue )
2013-01-23 11:41:13 +01:00
{
switch ( eLevelMeterType )
2013-01-23 11:41:13 +01:00
{
case MT_LED:
// update state of all LEDs for current level value (except of the clip LED)
for ( int iLEDIdx = 0; iLEDIdx < NUM_STEPS_LED_BAR; iLEDIdx++ )
2013-01-23 11:41:13 +01:00
{
// set active LED color if value is above current LED index
if ( iLEDIdx < dValue )
2013-01-23 11:41:13 +01:00
{
// check which color we should use (green, yellow or red)
if ( iLEDIdx < YELLOW_BOUND_LED_BAR )
{
// green region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_GREEN );
}
else
2013-01-23 11:41:13 +01:00
{
if ( iLEDIdx < RED_BOUND_LED_BAR )
2013-01-23 11:41:13 +01:00
{
// yellow region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_YELLOW );
2013-01-23 11:41:13 +01:00
}
else
{
// red region
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_RED );
2013-01-23 11:41:13 +01:00
}
}
}
else
2020-06-22 18:31:30 +02:00
{
// we use black LED for inactive state
vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_BLACK );
2020-06-22 18:31:30 +02:00
}
}
break;
case MT_BAR:
case MT_SLIM_BAR:
pBarMeter->setValue ( 100 * dValue );
break;
}
// clip indicator management (note that in case of clipping, i.e. full
// scale level, the value is above NUM_STEPS_LED_BAR since the minimum
// value of int16 is -32768 but we normalize with 32767 -> therefore
// we really only show the clipping indicator, if actually the largest
// value of int16 is used)
if ( dValue > NUM_STEPS_LED_BAR )
{
switch ( eLevelMeterType )
{
case MT_LED:
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_RED );
break;
case MT_BAR:
case MT_SLIM_BAR:
SetBarMeterStyleAndClipStatus ( eLevelMeterType, true );
break;
}
TimerClip.start();
}
2013-01-23 11:41:13 +01:00
}
void CLevelMeter::ClipReset()
{
// we manually want to reset the clipping indicator: stop timer and reset
// clipping indicator GUI element
TimerClip.stop();
switch ( eLevelMeterType )
{
case MT_LED:
2020-06-22 18:31:30 +02:00
vecpLEDs[NUM_STEPS_LED_BAR]->SetColor ( cLED::RL_BLACK );
break;
case MT_BAR:
case MT_SLIM_BAR:
SetBarMeterStyleAndClipStatus ( eLevelMeterType, false );
break;
}
}
2020-06-21 20:09:13 +02:00
CLevelMeter::cLED::cLED ( QWidget* parent ) :
BitmCubeRoundBlack ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDBlackSmall.png" ) ),
BitmCubeRoundGreen ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDGreenSmall.png" ) ),
BitmCubeRoundYellow ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDYellowSmall.png" ) ),
BitmCubeRoundRed ( QString::fromUtf8 ( ":/png/LEDs/res/HLEDRedSmall.png" ) )
2013-01-23 11:41:13 +01:00
{
// create LED label
pLEDLabel = new QLabel ( "", parent );
// set initial bitmap
2020-06-01 21:16:31 +02:00
pLEDLabel->setPixmap ( BitmCubeRoundBlack );
eCurLightColor = RL_BLACK;
2013-01-23 11:41:13 +01:00
}
2020-06-22 18:31:30 +02:00
void CLevelMeter::cLED::SetColor ( const ELightColor eNewColor )
2013-01-23 11:41:13 +01:00
{
// only update LED if color has changed
if ( eNewColor != eCurLightColor )
{
switch ( eNewColor )
{
case RL_DISABLED:
2020-06-21 22:39:15 +02:00
// note that this is required for the compact channel mode
pLEDLabel->setPixmap ( QPixmap() );
2013-01-23 11:41:13 +01:00
break;
2020-06-01 21:16:31 +02:00
case RL_BLACK:
pLEDLabel->setPixmap ( BitmCubeRoundBlack );
2013-01-23 11:41:13 +01:00
break;
case RL_GREEN:
pLEDLabel->setPixmap ( BitmCubeRoundGreen );
break;
case RL_YELLOW:
pLEDLabel->setPixmap ( BitmCubeRoundYellow );
break;
case RL_RED:
pLEDLabel->setPixmap ( BitmCubeRoundRed );
break;
}
eCurLightColor = eNewColor;
}
}