Knob.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <memory>
  27. #include <QPixmap>
  28. #include <QWidget>
  29. #include <QtCore/QPoint>
  30. #include <QTextDocument>
  31. #include "AutomatableModelView.h"
  32. class QPixmap;
  33. class TextFloat;
  34. enum knobTypes
  35. {
  36. knobDark_28, knobBright_26, knobSmall_17, knobVintage_32, knobStyled
  37. } ;
  38. void convertPixmapToGrayScale(QPixmap &pixMap);
  39. class LMMS_EXPORT Knob : public QWidget, public FloatModelView
  40. {
  41. Q_OBJECT
  42. Q_ENUMS( knobTypes )
  43. Q_PROPERTY(float innerRadius READ innerRadius WRITE setInnerRadius)
  44. Q_PROPERTY(float outerRadius READ outerRadius WRITE setOuterRadius)
  45. Q_PROPERTY(float centerPointX READ centerPointX WRITE setCenterPointX)
  46. Q_PROPERTY(float centerPointY READ centerPointY WRITE setCenterPointY)
  47. Q_PROPERTY(float lineWidth READ lineWidth WRITE setLineWidth)
  48. // Unfortunately, the gradient syntax doesn't create our gradient
  49. // correctly so we need to do this:
  50. Q_PROPERTY(QColor outerColor READ outerColor WRITE setOuterColor)
  51. Q_PROPERTY(QColor lineActiveColor MEMBER m_lineActiveColor)
  52. Q_PROPERTY(QColor lineInactiveColor MEMBER m_lineInactiveColor)
  53. Q_PROPERTY(QColor arcActiveColor MEMBER m_arcActiveColor)
  54. Q_PROPERTY(QColor arcInactiveColor MEMBER m_arcInactiveColor)
  55. mapPropertyFromModel(bool,isVolumeKnob,setVolumeKnob,m_volumeKnob);
  56. mapPropertyFromModel(float,volumeRatio,setVolumeRatio,m_volumeRatio);
  57. Q_PROPERTY(knobTypes knobNum READ knobNum WRITE setknobNum)
  58. Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
  59. void initUi( const QString & _name ); //!< to be called by ctors
  60. void onKnobNumUpdated(); //!< to be called when you updated @a m_knobNum
  61. public:
  62. Knob( knobTypes _knob_num, QWidget * _parent = nullptr, const QString & _name = QString() );
  63. Knob( QWidget * _parent = nullptr, const QString & _name = QString() ); //!< default ctor
  64. Knob( const Knob& other ) = delete;
  65. // TODO: remove
  66. inline void setHintText( const QString & _txt_before,
  67. const QString & _txt_after )
  68. {
  69. setDescription( _txt_before );
  70. setUnit( _txt_after );
  71. }
  72. void setLabel( const QString & txt );
  73. void setHtmlLabel( const QString &htmltxt );
  74. void setTotalAngle( float angle );
  75. // Begin styled knob accessors
  76. float innerRadius() const;
  77. void setInnerRadius( float r );
  78. float outerRadius() const;
  79. void setOuterRadius( float r );
  80. knobTypes knobNum() const;
  81. void setknobNum( knobTypes k );
  82. QPointF centerPoint() const;
  83. float centerPointX() const;
  84. void setCenterPointX( float c );
  85. float centerPointY() const;
  86. void setCenterPointY( float c );
  87. float lineWidth() const;
  88. void setLineWidth( float w );
  89. QColor outerColor() const;
  90. void setOuterColor( const QColor & c );
  91. QColor textColor() const;
  92. void setTextColor( const QColor & c );
  93. signals:
  94. void sliderPressed();
  95. void sliderReleased();
  96. void sliderMoved( float value );
  97. protected:
  98. void contextMenuEvent( QContextMenuEvent * _me ) override;
  99. void dragEnterEvent( QDragEnterEvent * _dee ) override;
  100. void dropEvent( QDropEvent * _de ) override;
  101. void focusOutEvent( QFocusEvent * _fe ) override;
  102. void mousePressEvent( QMouseEvent * _me ) override;
  103. void mouseReleaseEvent( QMouseEvent * _me ) override;
  104. void mouseMoveEvent( QMouseEvent * _me ) override;
  105. void mouseDoubleClickEvent( QMouseEvent * _me ) override;
  106. void paintEvent( QPaintEvent * _me ) override;
  107. void wheelEvent( QWheelEvent * _me ) override;
  108. void changeEvent(QEvent * ev) override;
  109. virtual float getValue( const QPoint & _p );
  110. private slots:
  111. virtual void enterValue();
  112. void friendlyUpdate();
  113. void toggleScale();
  114. private:
  115. virtual QString displayValue() const;
  116. void doConnections() override;
  117. QLineF calculateLine( const QPointF & _mid, float _radius,
  118. float _innerRadius = 1) const;
  119. void drawKnob( QPainter * _p );
  120. void setPosition( const QPoint & _p );
  121. bool updateAngle();
  122. int angleFromValue( float value, float minValue, float maxValue, float totalAngle ) const
  123. {
  124. return static_cast<int>( ( value - 0.5 * ( minValue + maxValue ) ) / ( maxValue - minValue ) * m_totalAngle ) % 360;
  125. }
  126. inline float pageSize() const
  127. {
  128. return ( model()->maxValue() - model()->minValue() ) / 100.0f;
  129. }
  130. static TextFloat * s_textFloat;
  131. QString m_label;
  132. bool m_isHtmlLabel;
  133. QTextDocument* m_tdRenderer;
  134. std::unique_ptr<QPixmap> m_knobPixmap;
  135. BoolModel m_volumeKnob;
  136. FloatModel m_volumeRatio;
  137. QPoint m_lastMousePos; //!< mouse position in last mouseMoveEvent
  138. float m_leftOver;
  139. bool m_buttonPressed;
  140. float m_totalAngle;
  141. int m_angle;
  142. QImage m_cache;
  143. // Styled knob stuff, could break out
  144. QPointF m_centerPoint;
  145. float m_innerRadius;
  146. float m_outerRadius;
  147. float m_lineWidth;
  148. QColor m_outerColor;
  149. QColor m_lineActiveColor;
  150. QColor m_lineInactiveColor;
  151. QColor m_arcActiveColor;
  152. QColor m_arcInactiveColor;
  153. QColor m_textColor;
  154. knobTypes m_knobNum;
  155. } ;
  156. #endif