Knob.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. class QPixmap;
  30. class TextFloat;
  31. enum knobTypes
  32. {
  33. knobDark_28, knobBright_26, knobSmall_17, knobVintage_32, knobStyled
  34. } ;
  35. class LMMS_EXPORT Knob : public QWidget, public FloatModelView
  36. {
  37. Q_OBJECT
  38. Q_ENUMS( knobTypes )
  39. Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius)
  40. Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius)
  41. Q_PROPERTY(float centerPointX READ centerPointX WRITE setCenterPointX)
  42. Q_PROPERTY(float centerPointY READ centerPointY WRITE setCenterPointY)
  43. Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth)
  44. // Unfortunately, the gradient syntax doesn't create our gradient
  45. // correctly so we need to do this:
  46. Q_PROPERTY(QColor outerColor READ outerColor WRITE setOuterColor)
  47. Q_PROPERTY(QColor lineColor READ lineColor WRITE setlineColor)
  48. Q_PROPERTY(QColor arcColor READ arcColor WRITE setarcColor)
  49. mapPropertyFromModel(bool,isVolumeKnob,setVolumeKnob,m_volumeKnob);
  50. mapPropertyFromModel(float,volumeRatio,setVolumeRatio,m_volumeRatio);
  51. Q_PROPERTY(knobTypes knobNum READ knobNum WRITE setknobNum)
  52. Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
  53. void initUi( const QString & _name ); //!< to be called by ctors
  54. void onKnobNumUpdated(); //!< to be called when you updated @a m_knobNum
  55. public:
  56. Knob( knobTypes _knob_num, QWidget * _parent = NULL, const QString & _name = QString() );
  57. Knob( QWidget * _parent = NULL, const QString & _name = QString() ); //!< default ctor
  58. Knob( const Knob& other ) = delete;
  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. void contextMenuEvent( QContextMenuEvent * _me ) override;
  97. void dragEnterEvent( QDragEnterEvent * _dee ) override;
  98. void dropEvent( QDropEvent * _de ) override;
  99. void focusOutEvent( QFocusEvent * _fe ) override;
  100. void mousePressEvent( QMouseEvent * _me ) override;
  101. void mouseReleaseEvent( QMouseEvent * _me ) override;
  102. void mouseMoveEvent( QMouseEvent * _me ) override;
  103. void mouseDoubleClickEvent( QMouseEvent * _me ) override;
  104. void paintEvent( QPaintEvent * _me ) override;
  105. void wheelEvent( QWheelEvent * _me ) override;
  106. virtual float getValue( const QPoint & _p );
  107. private slots:
  108. virtual void enterValue();
  109. void friendlyUpdate();
  110. void toggleScale();
  111. private:
  112. QString displayValue() const;
  113. void doConnections() override;
  114. QLineF calculateLine( const QPointF & _mid, float _radius,
  115. float _innerRadius = 1) const;
  116. void drawKnob( QPainter * _p );
  117. void setPosition( const QPoint & _p );
  118. bool updateAngle();
  119. int angleFromValue( float value, float minValue, float maxValue, float totalAngle ) const
  120. {
  121. return static_cast<int>( ( value - 0.5 * ( minValue + maxValue ) ) / ( maxValue - minValue ) * m_totalAngle ) % 360;
  122. }
  123. inline float pageSize() const
  124. {
  125. return ( model()->maxValue() - model()->minValue() ) / 100.0f;
  126. }
  127. static TextFloat * s_textFloat;
  128. QString m_label;
  129. QPixmap * m_knobPixmap;
  130. BoolModel m_volumeKnob;
  131. FloatModel m_volumeRatio;
  132. QPoint m_mouseOffset;
  133. QPoint m_origMousePos;
  134. float m_leftOver;
  135. bool m_buttonPressed;
  136. float m_totalAngle;
  137. int m_angle;
  138. QImage m_cache;
  139. // Styled knob stuff, could break out
  140. QPointF m_centerPoint;
  141. float m_innerRadius;
  142. float m_outerRadius;
  143. float m_lineWidth;
  144. QColor m_outerColor;
  145. QColor m_lineColor; //!< unused yet
  146. QColor m_arcColor; //!< unused yet
  147. QColor m_textColor;
  148. knobTypes m_knobNum;
  149. } ;
  150. #endif