From 01b0c3aea7dbc30c244982a03ffb879c3446ff88 Mon Sep 17 00:00:00 2001 From: Christian Hoffmann Date: Sun, 6 Feb 2022 11:35:57 +0100 Subject: [PATCH] Audiomixerboard: Adjust size when switching meter styles There are two different base meter styles (LED vs. bars) with multiple variants each (regular, slim, round). The base meter styles are implmeneted as widgets and are always part of a QStackedLayout. The selected meter style is shown via setCurrentWidget(). This hides the inactive widget, but the inactive widget is still there and is part of QStackedLayout's size calculations which always uses the maximum size over all widgets, not just the currently visible one. This leads to problems when the currently visible meter widget is narrower than the hidden counterpart. In that case, the QStackedLayout is wider than expected. Technically, the following transitions show this issue: - Select Bar, then Small LEDs, or - Select LEDs, then Narrow Bar However, the latter case already had a workaround (choosing Narrow Bar silently adjusted details of the LED meter as well). The proper fix is using a subclass of QStackedLayout which calculates its sizeHint solely based on the currently visible widget. This fixes the Bar->Small LEDs transition and makes the LEDs->Narrow Bar workaround redundant, which has therefore been removed. Fixes #2351 --- ChangeLog | 4 ++-- src/levelmeter.cpp | 23 +++++++---------------- src/levelmeter.h | 9 ++++----- src/util.cpp | 10 ++++++++++ src/util.h | 10 ++++++++++ 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/ChangeLog b/ChangeLog index d13e262b71..1eabc3c6e9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,8 @@ ### 3.8.2beta1dev <- NOTE: the release version number will be 3.8.2 ### -- Client: Added selection option for level meter style (#1688). - (contributed by @henkdegroot) +- Client: Added selection option for level meter style (#1688, #2352). + (contributed by @henkdegroot, @hoffie, @pgScorpio) - Client: On Windows, if no driver found while installing, the "Run Jamulus" option will not be checked (#2103). diff --git a/src/levelmeter.cpp b/src/levelmeter.cpp index 24a55ee8e1..8bce2b9066 100644 --- a/src/levelmeter.cpp +++ b/src/levelmeter.cpp @@ -61,9 +61,9 @@ CLevelMeter::CLevelMeter ( QWidget* parent ) : QWidget ( parent ), eLevelMeterTy pBarMeter->setFormat ( "" ); // suppress percent numbers // setup stacked layout for meter type switching mechanism - pStackedLayout = new QStackedLayout ( this ); - pStackedLayout->addWidget ( pLEDMeter ); - pStackedLayout->addWidget ( pBarMeter ); + pMinStackedLayout = new CMinimumStackedLayout ( this ); + pMinStackedLayout->addWidget ( pLEDMeter ); + pMinStackedLayout->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 @@ -103,7 +103,7 @@ void CLevelMeter::SetLevelMeterType ( const ELevelMeterType eNType ) { vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_BLACK ); } - pStackedLayout->setCurrentIndex ( 0 ); + pMinStackedLayout->setCurrentIndex ( 0 ); break; case MT_SLIM_LED: @@ -112,7 +112,7 @@ void CLevelMeter::SetLevelMeterType ( const ELevelMeterType eNType ) { vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_SLIM_BLACK ); } - pStackedLayout->setCurrentIndex ( 0 ); + pMinStackedLayout->setCurrentIndex ( 0 ); break; case MT_SMALL_LED: @@ -121,21 +121,12 @@ void CLevelMeter::SetLevelMeterType ( const ELevelMeterType eNType ) { vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_SMALL_BLACK ); } - pStackedLayout->setCurrentIndex ( 0 ); + pMinStackedLayout->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++ ) - { - vecpLEDs[iLEDIdx]->SetColor ( cLED::RL_DISABLED ); - } - - pStackedLayout->setCurrentIndex ( 1 ); + pMinStackedLayout->setCurrentIndex ( 1 ); break; } diff --git a/src/levelmeter.h b/src/levelmeter.h index ba1208eff9..545f60adba 100644 --- a/src/levelmeter.h +++ b/src/levelmeter.h @@ -29,7 +29,6 @@ #include #include #include -#include #include "util.h" #include "global.h" @@ -107,10 +106,10 @@ class CLevelMeter : public QWidget void SetBarMeterStyleAndClipStatus ( const ELevelMeterType eNType, const bool bIsClip ); - QStackedLayout* pStackedLayout; - ELevelMeterType eLevelMeterType; - CVector vecpLEDs; - QProgressBar* pBarMeter; + CMinimumStackedLayout* pMinStackedLayout; + ELevelMeterType eLevelMeterType; + CVector vecpLEDs; + QProgressBar* pBarMeter; QTimer TimerClip; diff --git a/src/util.cpp b/src/util.cpp index d1e39d73ca..ee814ca747 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -680,6 +680,16 @@ QString TruncateString ( QString str, int position ) } return str.left ( position ); } + +QSize CMinimumStackedLayout::sizeHint() const +{ + // always use the size of the currently visible widget: + if ( currentWidget() ) + { + return currentWidget()->sizeHint(); + } + return QStackedLayout::sizeHint(); +} #endif /******************************************************************************\ diff --git a/src/util.h b/src/util.h index 3337d30180..09feef17ab 100644 --- a/src/util.h +++ b/src/util.h @@ -41,6 +41,7 @@ # include # include # include +# include # include "ui_aboutdlgbase.h" #endif #include @@ -445,6 +446,15 @@ public slots: signals: void LanguageChanged ( QString strLanguage ); }; + +// StackedLayout which auto-reduces to the size of the currently visible widget +class CMinimumStackedLayout : public QStackedLayout +{ + Q_OBJECT +public: + CMinimumStackedLayout ( QWidget* parent = nullptr ) : QStackedLayout ( parent ) {} + virtual QSize sizeHint() const override; +}; #endif /******************************************************************************\