From 660f2e96b39da04431fd3ac40989aafacebdfc90 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sun, 9 Jul 2023 19:10:56 +0200 Subject: [PATCH] Render text of LedCheckBox with default font Extend LedCheckBox so that it adds support to render the text with the default font in the default size. The class now supports two modes: * Legacy mode * Non-legacy mode Legacy mode is the default and renders the LedCheckBox as before. The font is set to 7 pixels and all the text is rendered with a shadow. Non-legacy mode uses the current font to render the text. The text is rendered without a shadow and the pixmap is centered vertically to the left side of the text. The non-legacy mode is currently only used in the context of the matrix LADSPA dialog. Toggle options are rendered using it as well as the "Link Channels" button. --- include/LedCheckBox.h | 10 ++- .../LadspaMatrixControlDialog.cpp | 2 +- plugins/LadspaEffect/LadspaWidgetFactory.cpp | 2 +- src/gui/widgets/LedCheckBox.cpp | 84 ++++++++++++++----- 4 files changed, 73 insertions(+), 25 deletions(-) diff --git a/include/LedCheckBox.h b/include/LedCheckBox.h index 95016b87f..ba1b196fa 100644 --- a/include/LedCheckBox.h +++ b/include/LedCheckBox.h @@ -48,10 +48,12 @@ public: LedCheckBox( const QString & _txt, QWidget * _parent, const QString & _name = QString(), - LedColors _color = Yellow ); + LedColors _color = Yellow, + bool legacyMode = true); LedCheckBox( QWidget * _parent, const QString & _name = QString(), - LedColors _color = Yellow ); + LedColors _color = Yellow, + bool legacyMode = true); ~LedCheckBox() override; @@ -75,8 +77,12 @@ private: QString m_text; + bool m_legacyMode; + void initUi( LedColors _color ); //!< to be called by ctors void onTextUpdated(); //!< to be called when you updated @a m_text + void paintLegacy(QPaintEvent * p); + void paintNonLegacy(QPaintEvent * p); } ; diff --git a/plugins/LadspaEffect/LadspaMatrixControlDialog.cpp b/plugins/LadspaEffect/LadspaMatrixControlDialog.cpp index e492638ba..d3015634a 100644 --- a/plugins/LadspaEffect/LadspaMatrixControlDialog.cpp +++ b/plugins/LadspaEffect/LadspaMatrixControlDialog.cpp @@ -67,7 +67,7 @@ LadspaMatrixControlDialog::LadspaMatrixControlDialog(LadspaControls * ladspaCont { mainLayout->addSpacing(3); - m_stereoLink = new LedCheckBox(tr("Link Channels"), this); + m_stereoLink = new LedCheckBox(tr("Link Channels"), this, QString(), LedCheckBox::Yellow, false); m_stereoLink->setModel(&ladspaControls->m_stereoLinkModel); mainLayout->addWidget(m_stereoLink, 0, Qt::AlignCenter); } diff --git a/plugins/LadspaEffect/LadspaWidgetFactory.cpp b/plugins/LadspaEffect/LadspaWidgetFactory.cpp index 8f09a333f..22785fa19 100644 --- a/plugins/LadspaEffect/LadspaWidgetFactory.cpp +++ b/plugins/LadspaEffect/LadspaWidgetFactory.cpp @@ -55,7 +55,7 @@ QWidget * LadspaWidgetFactory::createWidget(LadspaControl * ladspaControl, QWidg case TOGGLED: { LedCheckBox * toggle = new LedCheckBox( - name, parent, QString(), LedCheckBox::Green); + name, parent, QString(), LedCheckBox::Green, false); toggle->setModel(ladspaControl->toggledModel()); return toggle; } diff --git a/src/gui/widgets/LedCheckBox.cpp b/src/gui/widgets/LedCheckBox.cpp index 1be072815..f607171f4 100644 --- a/src/gui/widgets/LedCheckBox.cpp +++ b/src/gui/widgets/LedCheckBox.cpp @@ -44,9 +44,10 @@ static const auto names = std::array LedCheckBox::LedCheckBox( const QString & _text, QWidget * _parent, - const QString & _name, LedColors _color ) : + const QString & _name, LedColors _color, bool legacyMode ) : AutomatableButton( _parent, _name ), - m_text( _text ) + m_text( _text ), + m_legacyMode(legacyMode) { initUi( _color ); } @@ -55,8 +56,8 @@ LedCheckBox::LedCheckBox( const QString & _text, QWidget * _parent, LedCheckBox::LedCheckBox( QWidget * _parent, - const QString & _name, LedColors _color ) : - LedCheckBox( QString(), _parent, _name, _color ) + const QString & _name, LedColors _color, bool legacyMode ) : + LedCheckBox( QString(), _parent, _name, _color, legacyMode ) { } @@ -80,24 +81,16 @@ void LedCheckBox::setText( const QString &s ) -void LedCheckBox::paintEvent( QPaintEvent * ) +void LedCheckBox::paintEvent( QPaintEvent * pe ) { - QPainter p( this ); - p.setFont( pointSize<7>( font() ) ); - - if( model()->value() == true ) - { - p.drawPixmap( 0, 0, *m_ledOnPixmap ); + if (!m_legacyMode) + { + paintNonLegacy(pe); } else { - p.drawPixmap( 0, 0, *m_ledOffPixmap ); + paintLegacy(pe); } - - p.setPen( QColor( 64, 64, 64 ) ); - p.drawText( m_ledOffPixmap->width() + 4, 11, text() ); - p.setPen( QColor( 255, 255, 255 ) ); - p.drawText( m_ledOffPixmap->width() + 3, 10, text() ); } @@ -115,7 +108,11 @@ void LedCheckBox::initUi( LedColors _color ) names[_color].toUtf8().constData() ) ); m_ledOffPixmap = new QPixmap( embed::getIconPixmap( "led_off" ) ); - setFont( pointSize<7>( font() ) ); + if (m_legacyMode) + { + setFont( pointSize<7>( font() ) ); + } + setText( m_text ); } @@ -124,9 +121,54 @@ void LedCheckBox::initUi( LedColors _color ) void LedCheckBox::onTextUpdated() { - setFixedSize(m_ledOffPixmap->width() + 5 + horizontalAdvance(QFontMetrics(font()), - text()), - m_ledOffPixmap->height()); + if (m_legacyMode) + { + setFixedSize(m_ledOffPixmap->width() + 5 + horizontalAdvance(QFontMetrics(font()), + text()), + m_ledOffPixmap->height()); + } + else + { + QFontMetrics fm(font()); + + int const width = m_ledOffPixmap->width() + 5 + fm.horizontalAdvance(text()); + int const height = qMax(m_ledOffPixmap->height(), fm.height()); + + setFixedSize(width, height); + } +} + +void LedCheckBox::paintLegacy(QPaintEvent * pe) +{ + QPainter p( this ); + p.setFont( pointSize<7>( font() ) ); + + if( model()->value() == true ) + { + p.drawPixmap( 0, 0, *m_ledOnPixmap ); + } + else + { + p.drawPixmap( 0, 0, *m_ledOffPixmap ); + } + + p.setPen( QColor( 64, 64, 64 ) ); + p.drawText( m_ledOffPixmap->width() + 4, 11, text() ); + p.setPen( QColor( 255, 255, 255 ) ); + p.drawText( m_ledOffPixmap->width() + 3, 10, text() ); +} + +void LedCheckBox::paintNonLegacy(QPaintEvent * pe) +{ + QPainter p(this); + + QPixmap * drawnPixmap = model()->value() ? m_ledOnPixmap : m_ledOffPixmap; + + p.drawPixmap( 0, rect().height() / 2 - drawnPixmap->height() / 2, *drawnPixmap); + + QRect r = rect(); + r -= QMargins(m_ledOffPixmap->width() + 5, 0, 0, 0); + p.drawText(r, text()); }