From 048ffd690f21377e6fc5471c817f0a140b352a4b Mon Sep 17 00:00:00 2001 From: Paul Giblock Date: Sun, 18 May 2008 14:23:41 +0000 Subject: [PATCH] stylable knobs git-svn-id: https://lmms.svn.sf.net/svnroot/lmms/trunk/lmms@986 0778d3d1-df1d-0410-868b-ea421aaaa00d --- ChangeLog | 8 ++ include/knob.h | 39 ++++++++- src/gui/widgets/knob.cpp | 178 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 210 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6a9286786..27cc25bf1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-05-18 Paul Giblock + + * src/gui/widgets/knob.cpp: + * include/knob.h: + First version of stylable knobs. Perhaps can be overlayed with current + knobs to add line coloring/width in other places around the GUI. + 2008-05-18 Tobias Doerffel * plugins/stk/mallets/mallets.cpp: @@ -58,6 +65,7 @@ protect individual buffers of FX-channels from being processed by more than one thread +>>>>>>> .r985 2008-05-17 Paul Giblock * plugins/stereo_matrix/stereomatrix_controls.cpp: diff --git a/include/knob.h b/include/knob.h index f1553355a..230ebca91 100644 --- a/include/knob.h +++ b/include/knob.h @@ -42,7 +42,7 @@ class textFloat; enum knobTypes { - knobDark_28, knobBright_26, knobSmall_17, knobGreen_17 + knobDark_28, knobBright_26, knobSmall_17, knobGreen_17, knobStyled } ; @@ -50,6 +50,18 @@ enum knobTypes class knob : public QWidget, public floatModelView { Q_OBJECT + Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius) + Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius) + + Q_PROPERTY(float centerPointX READ centerPointX WRITE setCenterPointX) + Q_PROPERTY(float centerPointY READ centerPointY WRITE setCenterPointY) + + Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth) + + // Unfortunately, the gradient syntax doesn't create our gradient correctly + // so we need to do this: + Q_PROPERTY(QColor outerColor READ outerColor WRITE setOuterColor) + public: knob( int _knob_num, QWidget * _parent, const QString & _name ); virtual ~knob(); @@ -61,6 +73,24 @@ public: void setTotalAngle( float _angle ); + // Begin styled knob accessors + float innerRadius( void ) const; + void setInnerRadius( float _r ); + + float outerRadius( void ) const; + void setOuterRadius( float _r ); + + QPointF centerPoint( void ) const; + float centerPointX( void ) const; + void setCenterPointX( float _c ); + float centerPointY( void ) const; + void setCenterPointY( float _c ); + + float lineWidth( void ) const; + void setLineWidth( float _w ); + + QColor outerColor( void ) const; + void setOuterColor( const QColor & _c ); public slots: void reset( void ); @@ -91,6 +121,13 @@ protected: QString m_hintTextBeforeValue; QString m_hintTextAfterValue; + // Styled knob stuff, could break out + QPointF m_centerPoint; + float m_innerRadius; + float m_outerRadius; + float m_lineWidth; + QColor * m_outerColor; + virtual void contextMenuEvent( QContextMenuEvent * _me ); virtual void dragEnterEvent( QDragEnterEvent * _dee ); virtual void dropEvent( QDropEvent * _de ); diff --git a/src/gui/widgets/knob.cpp b/src/gui/widgets/knob.cpp index d98a87aab..42a82cb33 100644 --- a/src/gui/widgets/knob.cpp +++ b/src/gui/widgets/knob.cpp @@ -38,12 +38,12 @@ #include #include - #ifndef __USE_XOPEN #define __USE_XOPEN #endif #include + #include "automatable_model_templates.h" #include "caption_menu.h" #include "config_mgr.h" @@ -71,6 +71,8 @@ knob::knob( int _knob_num, QWidget * _parent, const QString & _name ) : m_hintTextBeforeValue( "" ), m_hintTextAfterValue( "" ), m_knobNum( _knob_num ), + m_knobPixmap( NULL ), + m_outerColor( NULL ), m_label( "" ) { if( s_textFloat == NULL ) @@ -81,11 +83,16 @@ knob::knob( int _knob_num, QWidget * _parent, const QString & _name ) : setAcceptDrops( TRUE ); setAccessibleName( _name ); - m_knobPixmap = new QPixmap( embed::getIconPixmap( QString( "knob0" + - QString::number( m_knobNum + 1 ) ).toAscii().constData() ) ); + + if( m_knobNum != knobStyled ) { + m_knobPixmap = new QPixmap( embed::getIconPixmap( QString( "knob0" + + QString::number( m_knobNum + 1 ) ).toAscii().constData() ) ); - setFixedSize( m_knobPixmap->width(), m_knobPixmap->height() ); + setFixedSize( m_knobPixmap->width(), m_knobPixmap->height() ); + } setTotalAngle( 270.0f ); + setInnerRadius( 1.0f ); + setOuterRadius( 10.0f ); doConnections(); } @@ -94,7 +101,10 @@ knob::knob( int _knob_num, QWidget * _parent, const QString & _name ) : knob::~knob() { - delete m_knobPixmap; + if( m_knobPixmap ) + { + delete m_knobPixmap; + } } @@ -113,10 +123,13 @@ void knob::setHintText( const QString & _txt_before, void knob::setLabel( const QString & _txt ) { m_label = _txt; - setFixedSize( tMax( m_knobPixmap->width(), - QFontMetrics( pointSize<6>( font() - ) ).width( m_label ) ), - m_knobPixmap->height() + 10 ); + if( m_knobPixmap ) + { + setFixedSize( tMax( m_knobPixmap->width(), + QFontMetrics( pointSize<6>( font() + ) ).width( m_label ) ), + m_knobPixmap->height() + 10 ); + } update(); } @@ -138,6 +151,114 @@ void knob::setTotalAngle( float _angle ) } + + +float knob::innerRadius( void ) const +{ + return m_innerRadius; +} + + + +void knob::setInnerRadius( float _r ) +{ + m_innerRadius = _r; +} + + + +float knob::outerRadius( void ) const +{ + return m_outerRadius; +} + + + +void knob::setOuterRadius( float _r ) +{ + m_outerRadius = _r; +} + + + +QPointF knob::centerPoint( void ) const +{ + return m_centerPoint; +} + + + +float knob::centerPointX( void ) const +{ + return m_centerPoint.x(); +} + + + +void knob::setCenterPointX( float _c ) +{ + m_centerPoint.setX( _c ); +} + + + +float knob::centerPointY( void ) const +{ + return m_centerPoint.y(); +} + + + +void knob::setCenterPointY( float _c ) +{ + m_centerPoint.setY( _c ); +} + + + +float knob::lineWidth( void ) const +{ + return m_lineWidth; +} + + + +void knob::setLineWidth( float _w ) +{ + m_lineWidth = _w; +} + + + +QColor knob::outerColor( void ) const +{ + if( m_outerColor ) + { + return *m_outerColor; + } + else + { + return QColor(); + } +} + + + +void knob::setOuterColor( const QColor & _c ) +{ + if( m_outerColor ) + { + *m_outerColor = _c; + } + else + { + m_outerColor = new QColor( _c ); + } +} + + + + QLineF knob::calculateLine( const QPointF & _mid, float _radius, float _innerRadius ) const { float angle = 0.0f; @@ -162,8 +283,37 @@ QLineF knob::calculateLine( const QPointF & _mid, float _radius, float _innerRad void knob::drawKnob( QPainter * _p ) { + QPoint mid; + + if( m_knobNum == knobStyled ) + { + _p->setRenderHint( QPainter::Antialiasing ); + + // Perhaps this can move to setOuterRadius() + if( m_outerColor ) + { + QRadialGradient gradient( centerPoint(), outerRadius() ); + gradient.setColorAt(0.33, _p->pen().brush().color() ); + gradient.setColorAt(1, *m_outerColor ); + + _p->setPen( QPen( gradient, lineWidth(), Qt::SolidLine, Qt::RoundCap ) ); + } + else { + QPen pen = _p->pen(); + pen.setWidth( lineWidth() ); + pen.setCapStyle( Qt::RoundCap ); + + _p->setPen( pen ); + } + + _p->drawLine( calculateLine( centerPoint(), outerRadius(), innerRadius() ) ); + return; + } + + + // Old-skool knobs const float radius = m_knobPixmap->width() / 2.0f - 1; - QPoint mid = QPoint( width() / 2.0, + mid = QPoint( width() / 2.0, m_knobPixmap->height() / 2.0f ); _p->drawPixmap( static_cast( @@ -357,7 +507,7 @@ void knob::mousePressEvent( QMouseEvent * _me ) model()->value() ) + m_hintTextAfterValue ); s_textFloat->moveGlobal( this, - QPoint( m_knobPixmap->width() + 2, 0 ) ); + QPoint( width() + 2, 0 ) ); s_textFloat->show(); m_buttonPressed = TRUE; } @@ -475,7 +625,7 @@ void knob::wheelEvent( QWheelEvent * _we ) s_textFloat->setText( m_hintTextBeforeValue + QString::number( model()->value() ) + m_hintTextAfterValue ); - s_textFloat->moveGlobal( this, QPoint( m_knobPixmap->width() + 2, 0 ) ); + s_textFloat->moveGlobal( this, QPoint( width() + 2, 0 ) ); s_textFloat->setVisibilityTimeOut( 1000 ); emit sliderMoved( model()->value() ); @@ -519,7 +669,7 @@ void knob::reset( void ) s_textFloat->setText( m_hintTextBeforeValue + QString::number( model()->value() ) + m_hintTextAfterValue ); - s_textFloat->moveGlobal( this, QPoint( m_knobPixmap->width() + 2, 0 ) ); + s_textFloat->moveGlobal( this, QPoint( width() + 2, 0 ) ); s_textFloat->setVisibilityTimeOut( 1000 ); } @@ -540,7 +690,7 @@ void knob::pasteValue( void ) s_textFloat->setText( m_hintTextBeforeValue + QString::number( model()->value() ) + m_hintTextAfterValue ); - s_textFloat->moveGlobal( this, QPoint( m_knobPixmap->width() + 2, 0 ) ); + s_textFloat->moveGlobal( this, QPoint( width() + 2, 0 ) ); s_textFloat->setVisibilityTimeOut( 1000 ); }