From 5967bcf1be6985f802c4c23fab0e2b29f6e02b29 Mon Sep 17 00:00:00 2001 From: liushuyu Date: Wed, 27 Jan 2021 23:27:13 -0700 Subject: [PATCH] Support HTML markup for knob labels (#3134) Co-authored-by: Hyunjin Song Co-authored-by: Dominic Clark --- include/Knob.h | 4 ++++ src/gui/widgets/Knob.cpp | 45 +++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/Knob.h b/include/Knob.h index 3c730e3dd60..5b3ca902253 100644 --- a/include/Knob.h +++ b/include/Knob.h @@ -30,6 +30,7 @@ #include #include #include +#include #include "AutomatableModelView.h" @@ -90,6 +91,7 @@ class LMMS_EXPORT Knob : public QWidget, public FloatModelView setUnit( _txt_after ); } void setLabel( const QString & txt ); + void setHtmlLabel( const QString &htmltxt ); void setTotalAngle( float angle ); @@ -171,6 +173,8 @@ private slots: static TextFloat * s_textFloat; QString m_label; + bool m_isHtmlLabel; + QTextDocument* m_tdRenderer; std::unique_ptr m_knobPixmap; BoolModel m_volumeKnob; diff --git a/src/gui/widgets/Knob.cpp b/src/gui/widgets/Knob.cpp index 0f2a819152e..98a2683b198 100644 --- a/src/gui/widgets/Knob.cpp +++ b/src/gui/widgets/Knob.cpp @@ -60,6 +60,8 @@ Knob::Knob( knobTypes _knob_num, QWidget * _parent, const QString & _name ) : QWidget( _parent ), FloatModelView( new FloatModel( 0, 0, 0, 1, NULL, _name, true ), this ), m_label( "" ), + m_isHtmlLabel(false), + m_tdRenderer(nullptr), m_volumeKnob( false ), m_volumeRatio( 100.0, 0.0, 1000000.0 ), m_buttonPressed( false ), @@ -166,12 +168,36 @@ void Knob::onKnobNumUpdated() void Knob::setLabel( const QString & txt ) { m_label = txt; + m_isHtmlLabel = false; if( m_knobPixmap ) { setFixedSize(qMax( m_knobPixmap->width(), horizontalAdvance(QFontMetrics(pointSizeF(font(), 6.5)), m_label)), m_knobPixmap->height() + 10); } + + update(); +} + + +void Knob::setHtmlLabel(const QString &htmltxt) +{ + m_label = htmltxt; + m_isHtmlLabel = true; + // Put the rendered HTML content into cache + if (!m_tdRenderer) + { + m_tdRenderer = new QTextDocument(this); + } + + m_tdRenderer->setHtml(QString("%2").arg(textColor().name(), m_label)); + + if (m_knobPixmap) + { + setFixedSize(m_knobPixmap->width(), + m_knobPixmap->height() + 15); + } + update(); } @@ -641,15 +667,20 @@ void Knob::paintEvent( QPaintEvent * _me ) drawKnob( &p ); if( !m_label.isEmpty() ) { - p.setFont( pointSizeF( p.font(), 6.5 ) ); -/* p.setPen( QColor( 64, 64, 64 ) ); - p.drawText( width() / 2 - - p.fontMetrics().width( m_label ) / 2 + 1, - height() - 1, m_label );*/ - p.setPen( textColor() ); - p.drawText(width() / 2 - + if (!m_isHtmlLabel) + { + p.setFont(pointSizeF(p.font(), 6.5)); + p.setPen(textColor()); + p.drawText(width() / 2 - horizontalAdvance(p.fontMetrics(), m_label) / 2, height() - 2, m_label); + } + else + { + m_tdRenderer->setDefaultFont(pointSizeF(p.font(), 6.5)); + p.translate((width() - m_tdRenderer->idealWidth()) / 2, (height() - m_tdRenderer->pageSize().height()) / 2); + m_tdRenderer->drawContents(&p); + } } }