Skip to content

Commit 5967bcf

Browse files
liushuyuPhysSongDomClark
authored
Support HTML markup for knob labels (LMMS#3134)
Co-authored-by: Hyunjin Song <[email protected]> Co-authored-by: Dominic Clark <[email protected]>
1 parent 45e8717 commit 5967bcf

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

include/Knob.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <QPixmap>
3131
#include <QWidget>
3232
#include <QtCore/QPoint>
33+
#include <QTextDocument>
3334

3435
#include "AutomatableModelView.h"
3536

@@ -90,6 +91,7 @@ class LMMS_EXPORT Knob : public QWidget, public FloatModelView
9091
setUnit( _txt_after );
9192
}
9293
void setLabel( const QString & txt );
94+
void setHtmlLabel( const QString &htmltxt );
9395

9496
void setTotalAngle( float angle );
9597

@@ -171,6 +173,8 @@ private slots:
171173
static TextFloat * s_textFloat;
172174

173175
QString m_label;
176+
bool m_isHtmlLabel;
177+
QTextDocument* m_tdRenderer;
174178

175179
std::unique_ptr<QPixmap> m_knobPixmap;
176180
BoolModel m_volumeKnob;

src/gui/widgets/Knob.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Knob::Knob( knobTypes _knob_num, QWidget * _parent, const QString & _name ) :
6060
QWidget( _parent ),
6161
FloatModelView( new FloatModel( 0, 0, 0, 1, NULL, _name, true ), this ),
6262
m_label( "" ),
63+
m_isHtmlLabel(false),
64+
m_tdRenderer(nullptr),
6365
m_volumeKnob( false ),
6466
m_volumeRatio( 100.0, 0.0, 1000000.0 ),
6567
m_buttonPressed( false ),
@@ -166,12 +168,36 @@ void Knob::onKnobNumUpdated()
166168
void Knob::setLabel( const QString & txt )
167169
{
168170
m_label = txt;
171+
m_isHtmlLabel = false;
169172
if( m_knobPixmap )
170173
{
171174
setFixedSize(qMax<int>( m_knobPixmap->width(),
172175
horizontalAdvance(QFontMetrics(pointSizeF(font(), 6.5)), m_label)),
173176
m_knobPixmap->height() + 10);
174177
}
178+
179+
update();
180+
}
181+
182+
183+
void Knob::setHtmlLabel(const QString &htmltxt)
184+
{
185+
m_label = htmltxt;
186+
m_isHtmlLabel = true;
187+
// Put the rendered HTML content into cache
188+
if (!m_tdRenderer)
189+
{
190+
m_tdRenderer = new QTextDocument(this);
191+
}
192+
193+
m_tdRenderer->setHtml(QString("<span style=\"color:%1;\">%2</span>").arg(textColor().name(), m_label));
194+
195+
if (m_knobPixmap)
196+
{
197+
setFixedSize(m_knobPixmap->width(),
198+
m_knobPixmap->height() + 15);
199+
}
200+
175201
update();
176202
}
177203

@@ -641,15 +667,20 @@ void Knob::paintEvent( QPaintEvent * _me )
641667
drawKnob( &p );
642668
if( !m_label.isEmpty() )
643669
{
644-
p.setFont( pointSizeF( p.font(), 6.5 ) );
645-
/* p.setPen( QColor( 64, 64, 64 ) );
646-
p.drawText( width() / 2 -
647-
p.fontMetrics().width( m_label ) / 2 + 1,
648-
height() - 1, m_label );*/
649-
p.setPen( textColor() );
650-
p.drawText(width() / 2 -
670+
if (!m_isHtmlLabel)
671+
{
672+
p.setFont(pointSizeF(p.font(), 6.5));
673+
p.setPen(textColor());
674+
p.drawText(width() / 2 -
651675
horizontalAdvance(p.fontMetrics(), m_label) / 2,
652676
height() - 2, m_label);
677+
}
678+
else
679+
{
680+
m_tdRenderer->setDefaultFont(pointSizeF(p.font(), 6.5));
681+
p.translate((width() - m_tdRenderer->idealWidth()) / 2, (height() - m_tdRenderer->pageSize().height()) / 2);
682+
m_tdRenderer->drawContents(&p);
683+
}
653684
}
654685
}
655686

0 commit comments

Comments
 (0)