Graph.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Graph.h - a QT widget for displaying and manipulating waveforms
  3. *
  4. * Copyright (c) 2006-2007 Andreas Brandmaier <andy/at/brandmaier/dot/de>
  5. * 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef GRAPH_H
  26. #define GRAPH_H
  27. #include <QWidget>
  28. #include <QPixmap>
  29. #include <QCursor>
  30. #include "Model.h"
  31. #include "ModelView.h"
  32. #include "lmms_basics.h"
  33. class graphModel;
  34. class LMMS_EXPORT Graph : public QWidget, public ModelView
  35. {
  36. Q_OBJECT
  37. public:
  38. enum graphStyle
  39. {
  40. NearestStyle, //!< draw as stairs
  41. LinearStyle, //!< connect each 2 samples with a line, with wrapping
  42. LinearNonCyclicStyle, //!< LinearStyle without wrapping
  43. BarStyle, //!< draw thick bars
  44. NumGraphStyles
  45. };
  46. /**
  47. * @brief Constructor
  48. * @param _width Pixel width of widget
  49. * @param _height Pixel height of widget
  50. */
  51. Graph( QWidget * _parent, graphStyle _style = Graph::LinearStyle,
  52. int _width = 132,
  53. int _height = 104
  54. );
  55. virtual ~Graph() = default;
  56. void setForeground( const QPixmap & _pixmap );
  57. void setGraphColor( const QColor );
  58. inline graphModel * model()
  59. {
  60. return castModel<graphModel>();
  61. }
  62. inline graphStyle getGraphStyle()
  63. {
  64. return m_graphStyle;
  65. }
  66. inline void setGraphStyle( graphStyle _s )
  67. {
  68. m_graphStyle = _s;
  69. update();
  70. }
  71. signals:
  72. void drawn();
  73. protected:
  74. void paintEvent( QPaintEvent * _pe ) override;
  75. void dropEvent( QDropEvent * _de ) override;
  76. void dragEnterEvent( QDragEnterEvent * _dee ) override;
  77. void mousePressEvent( QMouseEvent * _me ) override;
  78. void mouseMoveEvent( QMouseEvent * _me ) override;
  79. void mouseReleaseEvent( QMouseEvent * _me ) override;
  80. protected slots:
  81. void updateGraph( int _startPos, int _endPos );
  82. void updateGraph();
  83. private:
  84. void modelChanged() override;
  85. void changeSampleAt( int _x, int _y );
  86. void drawLineAt( int _x, int _y, int _lastx );
  87. QPixmap m_foreground;
  88. QColor m_graphColor;
  89. graphStyle m_graphStyle;
  90. bool m_mouseDown;
  91. int m_lastCursorX;
  92. } ;
  93. /**
  94. @brief 2 dimensional function plot
  95. Function plot graph with discrete x scale and continous y scale
  96. This makes it possible to display "#x" samples
  97. */
  98. class LMMS_EXPORT graphModel : public Model
  99. {
  100. Q_OBJECT
  101. public:
  102. /**
  103. * @brief Constructor
  104. * @param _min Minimum y value to display
  105. * @param _max Maximum y value to display
  106. * @param _size Number of samples (e.g. x value)
  107. * @param _step Step size on y axis where values snap to, or 0.0f
  108. * for "no snapping"
  109. */
  110. graphModel( float _min,
  111. float _max,
  112. int _size,
  113. :: Model * _parent,
  114. bool _default_constructed = false,
  115. float _step = 0.0 );
  116. virtual ~graphModel() = default;
  117. // TODO: saveSettings, loadSettings?
  118. inline float minValue() const
  119. {
  120. return( m_minValue );
  121. }
  122. inline float maxValue() const
  123. {
  124. return( m_maxValue );
  125. }
  126. inline int length() const
  127. {
  128. return m_length;
  129. }
  130. inline const float * samples() const
  131. {
  132. return( m_samples.data() );
  133. }
  134. //! Make cyclic convolution
  135. //! @param convolution Samples to convolve with
  136. //! @param convolutionLength Number of samples to take for each sum
  137. //! @param centerOffset Offset for resulting values
  138. void convolve(const float *convolution,
  139. const int convolutionLength, const int centerOffset);
  140. public slots:
  141. //! Set range of y values
  142. void setRange( float _min, float _max );
  143. void setLength( int _size );
  144. //! Update one sample
  145. void setSampleAt( int x, float val );
  146. //! Update samples array
  147. void setSamples( const float * _value );
  148. void setWaveToSine();
  149. void setWaveToTriangle();
  150. void setWaveToSaw();
  151. void setWaveToSquare();
  152. void setWaveToNoise();
  153. QString setWaveToUser( );
  154. void smooth();
  155. void smoothNonCyclic();
  156. void normalize();
  157. void invert();
  158. void shiftPhase( int _deg );
  159. void clear();
  160. void clearInvisible();
  161. signals:
  162. void lengthChanged();
  163. void samplesChanged( int startPos, int endPos );
  164. void rangeChanged();
  165. private:
  166. void drawSampleAt( int x, float val );
  167. QVector<float> m_samples;
  168. int m_length;
  169. float m_minValue;
  170. float m_maxValue;
  171. float m_step;
  172. friend class Graph;
  173. };
  174. #endif