Knob.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Knob.h - powerful knob-widget
  3. *
  4. * Copyright (c) 2004-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef KNOB_H
  25. #define KNOB_H
  26. #include <QWidget>
  27. #include <QtCore/QPoint>
  28. #include "AutomatableModelView.h"
  29. #include "templates.h"
  30. class QPixmap;
  31. class TextFloat;
  32. enum knobTypes
  33. {
  34. knobDark_28, knobBright_26, knobSmall_17, knobVintage_32, knobStyled
  35. } ;
  36. class EXPORT Knob : public QWidget, public FloatModelView
  37. {
  38. Q_OBJECT
  39. Q_ENUMS( knobTypes )
  40. Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius)
  41. Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius)
  42. Q_PROPERTY(float centerPointX READ centerPointX WRITE setCenterPointX)
  43. Q_PROPERTY(float centerPointY READ centerPointY WRITE setCenterPointY)
  44. Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth)
  45. // Unfortunately, the gradient syntax doesn't create our gradient
  46. // correctly so we need to do this:
  47. Q_PROPERTY(QColor outerColor READ outerColor WRITE setOuterColor)
  48. Q_PROPERTY(QColor lineColor READ lineColor WRITE setlineColor)
  49. Q_PROPERTY(QColor arcColor READ arcColor WRITE setarcColor)
  50. mapPropertyFromModel(bool,isVolumeKnob,setVolumeKnob,m_volumeKnob);
  51. mapPropertyFromModel(float,volumeRatio,setVolumeRatio,m_volumeRatio);
  52. Q_PROPERTY(knobTypes knobNum READ knobNum WRITE setknobNum)
  53. Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
  54. void initUi( const QString & _name ); //!< to be called by ctors
  55. void onKnobNumUpdated(); //!< to be called when you updated @a m_knobNum
  56. public:
  57. Knob( knobTypes _knob_num, QWidget * _parent = NULL, const QString & _name = QString() );
  58. Knob( QWidget * _parent = NULL, const QString & _name = QString() ); //!< default ctor
  59. virtual ~Knob();
  60. // TODO: remove
  61. inline void setHintText( const QString & _txt_before,
  62. const QString & _txt_after )
  63. {
  64. setDescription( _txt_before );
  65. setUnit( _txt_after );
  66. }
  67. void setLabel( const QString & txt );
  68. void setTotalAngle( float angle );
  69. // Begin styled knob accessors
  70. float innerRadius() const;
  71. void setInnerRadius( float r );
  72. float outerRadius() const;
  73. void setOuterRadius( float r );
  74. knobTypes knobNum() const;
  75. void setknobNum( knobTypes k );
  76. QPointF centerPoint() const;
  77. float centerPointX() const;
  78. void setCenterPointX( float c );
  79. float centerPointY() const;
  80. void setCenterPointY( float c );
  81. float lineWidth() const;
  82. void setLineWidth( float w );
  83. QColor outerColor() const;
  84. void setOuterColor( const QColor & c );
  85. QColor lineColor() const;
  86. void setlineColor( const QColor & c );
  87. QColor arcColor() const;
  88. void setarcColor( const QColor & c );
  89. QColor textColor() const;
  90. void setTextColor( const QColor & c );
  91. signals:
  92. void sliderPressed();
  93. void sliderReleased();
  94. void sliderMoved( float value );
  95. protected:
  96. virtual void contextMenuEvent( QContextMenuEvent * _me );
  97. virtual void dragEnterEvent( QDragEnterEvent * _dee );
  98. virtual void dropEvent( QDropEvent * _de );
  99. virtual void focusOutEvent( QFocusEvent * _fe );
  100. virtual void mousePressEvent( QMouseEvent * _me );
  101. virtual void mouseReleaseEvent( QMouseEvent * _me );
  102. virtual void mouseMoveEvent( QMouseEvent * _me );
  103. virtual void mouseDoubleClickEvent( QMouseEvent * _me );
  104. virtual void paintEvent( QPaintEvent * _me );
  105. virtual void wheelEvent( QWheelEvent * _me );
  106. virtual float getValue( const QPoint & _p );
  107. private slots:
  108. virtual void enterValue();
  109. void displayHelp();
  110. void friendlyUpdate();
  111. void toggleScale();
  112. private:
  113. QString displayValue() const;
  114. virtual void doConnections();
  115. QLineF calculateLine( const QPointF & _mid, float _radius,
  116. float _innerRadius = 1) const;
  117. void drawKnob( QPainter * _p );
  118. void setPosition( const QPoint & _p );
  119. bool updateAngle();
  120. int angleFromValue( float value, float minValue, float maxValue, float totalAngle ) const
  121. {
  122. return static_cast<int>( ( value - 0.5 * ( minValue + maxValue ) ) / ( maxValue - minValue ) * m_totalAngle ) % 360;
  123. }
  124. inline float pageSize() const
  125. {
  126. return ( model()->maxValue() - model()->minValue() ) / 100.0f;
  127. }
  128. static TextFloat * s_textFloat;
  129. QString m_label;
  130. QPixmap * m_knobPixmap;
  131. BoolModel m_volumeKnob;
  132. FloatModel m_volumeRatio;
  133. QPoint m_mouseOffset;
  134. QPoint m_origMousePos;
  135. float m_leftOver;
  136. bool m_buttonPressed;
  137. float m_totalAngle;
  138. int m_angle;
  139. QImage m_cache;
  140. // Styled knob stuff, could break out
  141. QPointF m_centerPoint;
  142. float m_innerRadius;
  143. float m_outerRadius;
  144. float m_lineWidth;
  145. QColor m_outerColor;
  146. QColor m_lineColor; //!< unused yet
  147. QColor m_arcColor; //!< unused yet
  148. QColor m_textColor;
  149. knobTypes m_knobNum;
  150. } ;
  151. #endif