EqParameterWidget.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * eqparameterwidget.cpp - defination of EqParameterWidget class.
  3. *
  4. * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
  5. * Copyright (c) 2015 Steffen Baranowsky <BaraMGB/at/freenet/dot/de>
  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. #include "EqParameterWidget.h"
  26. #include <QGraphicsScene>
  27. #include <QGraphicsView>
  28. #include <QMouseEvent>
  29. #include <QWidget>
  30. #include "EqControls.h"
  31. #include "lmms_math.h"
  32. EqParameterWidget::EqParameterWidget( QWidget *parent, EqControls * controls ) :
  33. QWidget( parent ),
  34. m_displayWidth ( 450 ),
  35. m_displayHeigth ( 200 ),
  36. m_controls ( controls )
  37. {
  38. m_bands = new EqBand[8];
  39. resize( m_displayWidth, m_displayHeigth );
  40. float totalHeight = 36; // gain range from -18 to +18
  41. m_pixelsPerUnitHeight = m_displayHeigth / totalHeight;
  42. m_pixelsPerOctave = EqHandle::freqToXPixel( 10000, m_displayWidth ) - EqHandle::freqToXPixel( 5000, m_displayWidth );
  43. //GraphicsScene and GraphicsView stuff
  44. QGraphicsScene *scene = new QGraphicsScene();
  45. scene->setSceneRect( 0, 0, m_displayWidth, m_displayHeigth );
  46. QGraphicsView *view = new QGraphicsView( this );
  47. view->setStyleSheet( "border-style: none; background: transparent;" );
  48. view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  49. view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  50. view->setScene( scene );
  51. //adds the handles
  52. m_handleList = new QList<EqHandle*>;
  53. for ( int i = 0; i < bandCount(); i++ )
  54. {
  55. m_handle = new EqHandle ( i, m_displayWidth, m_displayHeigth );
  56. m_handleList->append( m_handle );
  57. m_handle->setZValue( 1 );
  58. scene->addItem( m_handle );
  59. }
  60. //adds the curve widget
  61. m_eqcurve = new EqCurve( m_handleList, m_displayWidth, m_displayHeigth );
  62. scene->addItem( m_eqcurve );
  63. for ( int i = 0; i < bandCount(); i++ )
  64. {
  65. // if the data of handle position has changed update the models
  66. QObject::connect( m_handleList->at( i ) ,SIGNAL( positionChanged() ), this ,SLOT( updateModels() ) );
  67. }
  68. }
  69. EqParameterWidget::~EqParameterWidget()
  70. {
  71. if( m_bands )
  72. {
  73. delete[] m_bands;
  74. m_bands = 0;
  75. }
  76. }
  77. EqBand *EqParameterWidget::getBandModels( int i )
  78. {
  79. return &m_bands[i];
  80. }
  81. void EqParameterWidget::updateHandle()
  82. {
  83. m_eqcurve->setModelChanged( true );
  84. for( int i = 0 ; i < bandCount(); i++ )
  85. {
  86. if ( !m_handleList->at( i )->mousePressed() ) //prevents a short circuit between handle and data model
  87. {
  88. //sets the band on active if a fader or a knob is moved
  89. bool hover = false; // prevents an action if handle is moved
  90. for ( int j = 0; j < bandCount(); j++ )
  91. {
  92. if ( m_handleList->at(j)->isMouseHover() )
  93. {
  94. hover = true;
  95. }
  96. }
  97. if ( !hover )
  98. {
  99. if ( sender() == m_bands[i].gain ) m_bands[i].active->setValue( true );
  100. if ( sender() == m_bands[i].freq ) m_bands[i].active->setValue( true );
  101. if ( sender() == m_bands[i].res ) m_bands[i].active->setValue( true );
  102. }
  103. changeHandle( i );
  104. }
  105. else
  106. {
  107. m_handleList->at( i )->setHandleActive( m_bands[i].active->value() );
  108. }
  109. }
  110. if ( m_bands[0].hp12->value() ) m_handleList->at( 0 )->sethp12();
  111. if ( m_bands[0].hp24->value() ) m_handleList->at( 0 )->sethp24();
  112. if ( m_bands[0].hp48->value() ) m_handleList->at( 0 )->sethp48();
  113. if ( m_bands[7].lp12->value() ) m_handleList->at( 7 )->setlp12();
  114. if ( m_bands[7].lp24->value() ) m_handleList->at( 7 )->setlp24();
  115. if ( m_bands[7].lp48->value() ) m_handleList->at( 7 )->setlp48();
  116. }
  117. void EqParameterWidget::changeHandle( int i )
  118. {
  119. //fill x, y, and bw with data from model
  120. float x = EqHandle::freqToXPixel( m_bands[i].freq->value(), m_displayWidth );
  121. float y = m_handleList->at( i )->y();
  122. //for pass filters there is no gain model
  123. if( m_bands[i].gain )
  124. {
  125. float gain = m_bands[i].gain->value();
  126. y = EqHandle::gainToYPixel( gain, m_displayHeigth, m_pixelsPerUnitHeight );
  127. }
  128. float bw = m_bands[i].res->value();
  129. // set the handle position, filter type for each handle
  130. switch ( i )
  131. {
  132. case 0 :
  133. m_handleList->at( i )->setType( highpass );
  134. m_handleList->at( i )->setPos( x, m_displayHeigth / 2 );
  135. break;
  136. case 1:
  137. m_handleList->at( i )->setType( lowshelf );
  138. m_handleList->at( i )->setPos( x, y );
  139. break;
  140. case 2:
  141. m_handleList->at( i )->setType( para );
  142. m_handleList->at( i )->setPos( x, y );
  143. break;
  144. case 3:
  145. m_handleList->at( i )->setType( para );
  146. m_handleList->at( i )->setPos( x, y );
  147. break;
  148. case 4:
  149. m_handleList->at( i )->setType( para );
  150. m_handleList->at( i )->setPos( x, y );
  151. break;
  152. case 5:
  153. m_handleList->at( i )->setType( para );
  154. m_handleList->at( i )->setPos( x, y );
  155. break;
  156. case 6:
  157. m_handleList->at( i )->setType( highshelf );
  158. m_handleList->at( i )->setPos( x, y );
  159. break;
  160. case 7:
  161. m_handleList->at( i )->setType( lowpass );
  162. m_handleList->at( i )->setPos( QPointF( x, m_displayHeigth / 2 ) );
  163. break;
  164. }
  165. // set resonance/bandwidth for each handle
  166. if ( m_handleList->at( i )->getResonance() != bw )
  167. {
  168. m_handleList->at( i )->setResonance( bw );
  169. }
  170. // and the active status
  171. m_handleList->at( i )->setHandleActive( m_bands[i].active->value() );
  172. m_handleList->at( i )->update();
  173. m_eqcurve->update();
  174. }
  175. //this is called if a handle is moved
  176. void EqParameterWidget::updateModels()
  177. {
  178. for ( int i=0 ; i < bandCount(); i++ )
  179. {
  180. m_bands[i].freq->setValue( EqHandle::xPixelToFreq( m_handleList->at( i )->x(), m_displayWidth ) );
  181. if( m_bands[i].gain )
  182. {
  183. m_bands[i].gain->setValue( EqHandle::yPixelToGain( m_handleList->at(i)->y(), m_displayHeigth, m_pixelsPerUnitHeight ) );
  184. }
  185. m_bands[i].res->setValue( m_handleList->at( i )->getResonance() );
  186. //identifies the handle which is moved and set the band active
  187. if ( sender() == m_handleList->at( i ) )
  188. {
  189. m_bands[i].active->setValue( true );
  190. }
  191. }
  192. m_eqcurve->update();
  193. }
  194. EqBand::EqBand() :
  195. gain ( 0 ),
  196. res ( 0 ),
  197. freq ( 0 ),
  198. color ( QColor( 255, 255, 255 ) ),
  199. x( 0 ),
  200. y( 0 ),
  201. name ( QString( "" ) ),
  202. peakL( 0 ),
  203. peakR( 0 )
  204. {
  205. }