Skip to content

Commit

Permalink
Support HTML markup for knob labels (LMMS#3134)
Browse files Browse the repository at this point in the history
Co-authored-by: Hyunjin Song <tteu.ingog@gmail.com>
Co-authored-by: Dominic Clark <mrdomclark@gmail.com>
  • Loading branch information
3 people authored Jan 28, 2021
1 parent 45e8717 commit 5967bcf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/Knob.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QPixmap>
#include <QWidget>
#include <QtCore/QPoint>
#include <QTextDocument>

#include "AutomatableModelView.h"

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -171,6 +173,8 @@ private slots:
static TextFloat * s_textFloat;

QString m_label;
bool m_isHtmlLabel;
QTextDocument* m_tdRenderer;

std::unique_ptr<QPixmap> m_knobPixmap;
BoolModel m_volumeKnob;
Expand Down
45 changes: 38 additions & 7 deletions src/gui/widgets/Knob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -166,12 +168,36 @@ void Knob::onKnobNumUpdated()
void Knob::setLabel( const QString & txt )
{
m_label = txt;
m_isHtmlLabel = false;
if( m_knobPixmap )
{
setFixedSize(qMax<int>( 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("<span style=\"color:%1;\">%2</span>").arg(textColor().name(), m_label));

if (m_knobPixmap)
{
setFixedSize(m_knobPixmap->width(),
m_knobPixmap->height() + 15);
}

update();
}

Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 5967bcf

Please sign in to comment.