Graph.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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,
  41. LinearStyle,
  42. LinearNonCyclicStyle,
  43. BarStyle,
  44. NumGraphStyles
  45. };
  46. Graph( QWidget * _parent, graphStyle _style = Graph::LinearStyle,
  47. int _width = 132,
  48. int _height = 104
  49. );
  50. virtual ~Graph() = default;
  51. void setForeground( const QPixmap & _pixmap );
  52. void setGraphColor( const QColor );
  53. inline graphModel * model()
  54. {
  55. return castModel<graphModel>();
  56. }
  57. inline graphStyle getGraphStyle()
  58. {
  59. return m_graphStyle;
  60. }
  61. inline void setGraphStyle( graphStyle _s )
  62. {
  63. m_graphStyle = _s;
  64. update();
  65. }
  66. signals:
  67. void drawn();
  68. protected:
  69. virtual void paintEvent( QPaintEvent * _pe );
  70. virtual void dropEvent( QDropEvent * _de );
  71. virtual void dragEnterEvent( QDragEnterEvent * _dee );
  72. virtual void mousePressEvent( QMouseEvent * _me );
  73. virtual void mouseMoveEvent( QMouseEvent * _me );
  74. virtual void mouseReleaseEvent( QMouseEvent * _me );
  75. protected slots:
  76. void updateGraph( int _startPos, int _endPos );
  77. void updateGraph();
  78. private:
  79. virtual void modelChanged();
  80. void changeSampleAt( int _x, int _y );
  81. void drawLineAt( int _x, int _y, int _lastx );
  82. QPixmap m_foreground;
  83. QColor m_graphColor;
  84. graphStyle m_graphStyle;
  85. bool m_mouseDown;
  86. int m_lastCursorX;
  87. } ;
  88. class LMMS_EXPORT graphModel : public Model
  89. {
  90. Q_OBJECT
  91. public:
  92. graphModel( float _min,
  93. float _max,
  94. int _size,
  95. :: Model * _parent,
  96. bool _default_constructed = false,
  97. float _step = 0.0 );
  98. virtual ~graphModel() = default;
  99. // TODO: saveSettings, loadSettings?
  100. inline float minValue() const
  101. {
  102. return( m_minValue );
  103. }
  104. inline float maxValue() const
  105. {
  106. return( m_maxValue );
  107. }
  108. inline int length() const
  109. {
  110. return m_length;
  111. }
  112. inline const float * samples() const
  113. {
  114. return( m_samples.data() );
  115. }
  116. void convolve(const float *convolution, const int convolutionLength, const int centerOffset);
  117. public slots:
  118. void setRange( float _min, float _max );
  119. void setLength( int _size );
  120. void setSampleAt( int x, float val );
  121. void setSamples( const float * _value );
  122. void setWaveToSine();
  123. void setWaveToTriangle();
  124. void setWaveToSaw();
  125. void setWaveToSquare();
  126. void setWaveToNoise();
  127. QString setWaveToUser( );
  128. void smooth();
  129. void smoothNonCyclic();
  130. void normalize();
  131. void invert();
  132. void shiftPhase( int _deg );
  133. void clear();
  134. signals:
  135. void lengthChanged();
  136. void samplesChanged( int startPos, int endPos );
  137. void rangeChanged();
  138. private:
  139. void drawSampleAt( int x, float val );
  140. QVector<float> m_samples;
  141. int m_length;
  142. float m_minValue;
  143. float m_maxValue;
  144. float m_step;
  145. friend class Graph;
  146. };
  147. #endif