Graph.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 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();
  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. protected:
  67. virtual void paintEvent( QPaintEvent * _pe );
  68. virtual void dropEvent( QDropEvent * _de );
  69. virtual void dragEnterEvent( QDragEnterEvent * _dee );
  70. virtual void mousePressEvent( QMouseEvent * _me );
  71. virtual void mouseMoveEvent( QMouseEvent * _me );
  72. virtual void mouseReleaseEvent( QMouseEvent * _me );
  73. protected slots:
  74. void updateGraph( int _startPos, int _endPos );
  75. void updateGraph();
  76. private:
  77. virtual void modelChanged();
  78. void changeSampleAt( int _x, int _y );
  79. void drawLineAt( int _x, int _y, int _lastx );
  80. QPixmap m_foreground;
  81. QColor m_graphColor;
  82. graphStyle m_graphStyle;
  83. bool m_mouseDown;
  84. int m_lastCursorX;
  85. } ;
  86. class EXPORT graphModel : public Model
  87. {
  88. Q_OBJECT
  89. public:
  90. graphModel( float _min,
  91. float _max,
  92. int _size,
  93. :: Model * _parent,
  94. bool _default_constructed = false,
  95. float _step = 0.0 );
  96. virtual ~graphModel();
  97. // TODO: saveSettings, loadSettings?
  98. inline float minValue() const
  99. {
  100. return( m_minValue );
  101. }
  102. inline float maxValue() const
  103. {
  104. return( m_maxValue );
  105. }
  106. inline int length() const
  107. {
  108. return m_length;
  109. }
  110. inline const float * samples() const
  111. {
  112. return( m_samples.data() );
  113. }
  114. public slots:
  115. void setRange( float _min, float _max );
  116. void setLength( int _size );
  117. void setSampleAt( int x, float val );
  118. void setSamples( const float * _value );
  119. void setWaveToSine();
  120. void setWaveToTriangle();
  121. void setWaveToSaw();
  122. void setWaveToSquare();
  123. void setWaveToNoise();
  124. QString setWaveToUser( );
  125. void smooth();
  126. void smoothNonCyclic();
  127. void normalize();
  128. void invert();
  129. void shiftPhase( int _deg );
  130. void clearInvisible();
  131. signals:
  132. void lengthChanged();
  133. void samplesChanged( int startPos, int endPos );
  134. void rangeChanged();
  135. private:
  136. void drawSampleAt( int x, float val );
  137. QVector<float> m_samples;
  138. int m_length;
  139. float m_minValue;
  140. float m_maxValue;
  141. float m_step;
  142. friend class Graph;
  143. };
  144. #endif