File: EqParameterWidget.cpp

package info (click to toggle)
lmms 1.2.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 55,144 kB
  • sloc: cpp: 159,861; ansic: 98,570; python: 2,555; sh: 551; makefile: 27; xml: 18
file content (242 lines) | stat: -rw-r--r-- 6,652 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * eqparameterwidget.cpp - defination of EqParameterWidget class.
 *
 * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
 * Copyright (c) 2015 Steffen Baranowsky <BaraMGB/at/freenet/dot/de>
 *
 * This file is part of LMMS - https://lmms.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 */

#include "EqParameterWidget.h"

#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QWidget>

#include "EqControls.h"
#include "lmms_math.h"


EqParameterWidget::EqParameterWidget( QWidget *parent, EqControls * controls ) :
	QWidget( parent ),
	m_displayWidth ( 450 ),
	m_displayHeigth ( 200 ),
	m_controls ( controls )

{
	m_bands = new EqBand[8];
	resize( m_displayWidth, m_displayHeigth );
	float totalHeight = 36; // gain range from -18 to +18
	m_pixelsPerUnitHeight = m_displayHeigth / totalHeight;
	m_pixelsPerOctave = EqHandle::freqToXPixel( 10000, m_displayWidth ) - EqHandle::freqToXPixel( 5000, m_displayWidth );

	//GraphicsScene and GraphicsView stuff
	QGraphicsScene *scene = new QGraphicsScene();
	scene->setSceneRect( 0, 0, m_displayWidth, m_displayHeigth );
	QGraphicsView *view = new QGraphicsView( this );
	view->setStyleSheet( "border-style: none; background: transparent;" );
	view->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
	view->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
	view->setScene( scene );

	//adds the handles
	m_handleList = new QList<EqHandle*>;
	for ( int i = 0; i < bandCount(); i++ )
	{
		m_handle = new EqHandle ( i, m_displayWidth, m_displayHeigth );
		m_handleList->append( m_handle );
		m_handle->setZValue( 1 );
		scene->addItem( m_handle );
	}

	//adds the curve widget
	m_eqcurve = new EqCurve( m_handleList, m_displayWidth, m_displayHeigth );
	scene->addItem( m_eqcurve );
	for ( int i = 0; i < bandCount(); i++ )
	{
		// if the data of handle position has changed update the models
		QObject::connect( m_handleList->at( i ) ,SIGNAL( positionChanged() ), this ,SLOT( updateModels() ) );
	}
}




EqParameterWidget::~EqParameterWidget()
{
	if( m_bands )
	{
		delete[] m_bands;
		m_bands = 0;
	}
}




EqBand *EqParameterWidget::getBandModels( int i )
{
	return &m_bands[i];
}




void EqParameterWidget::updateHandle()
{
	m_eqcurve->setModelChanged( true );
	for( int i = 0 ; i < bandCount(); i++ )
	{
		if ( !m_handleList->at( i )->mousePressed() ) //prevents a short circuit between handle and data model
		{
			//sets the band on active if a fader or a knob is moved
			bool hover = false; // prevents an action if handle is moved
			for ( int j = 0; j < bandCount(); j++ )
			{
				if ( m_handleList->at(j)->isMouseHover() )
				{
					hover = true;
				}
			}
			if ( !hover )
			{
				if ( sender() == m_bands[i].gain ) m_bands[i].active->setValue( true );
				if ( sender() == m_bands[i].freq ) m_bands[i].active->setValue( true );
				if ( sender() == m_bands[i].res ) m_bands[i].active->setValue( true );
			}
			changeHandle( i );
		}
		else
		{
			m_handleList->at( i )->setHandleActive( m_bands[i].active->value() );
		}
	}
	if ( m_bands[0].hp12->value() ) m_handleList->at( 0 )->sethp12();
	if ( m_bands[0].hp24->value() ) m_handleList->at( 0 )->sethp24();
	if ( m_bands[0].hp48->value() ) m_handleList->at( 0 )->sethp48();
	if ( m_bands[7].lp12->value() ) m_handleList->at( 7 )->setlp12();
	if ( m_bands[7].lp24->value() ) m_handleList->at( 7 )->setlp24();
	if ( m_bands[7].lp48->value() ) m_handleList->at( 7 )->setlp48();
}




void EqParameterWidget::changeHandle( int i )
{
	//fill x, y, and bw with data from model
	float x = EqHandle::freqToXPixel( m_bands[i].freq->value(), m_displayWidth );
	float y = m_handleList->at( i )->y();
	//for pass filters there is no gain model
	if( m_bands[i].gain )
	{
		float gain = m_bands[i].gain->value();
		y = EqHandle::gainToYPixel( gain, m_displayHeigth, m_pixelsPerUnitHeight );
	}
	float bw = m_bands[i].res->value();

	// set the handle position, filter type for each handle
	switch ( i )
	{
	case 0 :
		m_handleList->at( i )->setType( highpass );
		m_handleList->at( i )->setPos( x, m_displayHeigth / 2 );
		break;
	case 1:
		m_handleList->at( i )->setType( lowshelf );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 2:
		m_handleList->at( i )->setType( para );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 3:
		m_handleList->at( i )->setType( para );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 4:
		m_handleList->at( i )->setType( para );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 5:
		m_handleList->at( i )->setType( para );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 6:
		m_handleList->at( i )->setType( highshelf );
		m_handleList->at( i )->setPos( x, y );
		break;
	case 7:
		m_handleList->at( i )->setType( lowpass );
		m_handleList->at( i )->setPos( QPointF( x, m_displayHeigth / 2 ) );
		break;
	}

	// set resonance/bandwidth for each handle
	if ( m_handleList->at( i )->getResonance() != bw )
	{
		m_handleList->at( i )->setResonance( bw );
	}

	// and the active status
	m_handleList->at( i )->setHandleActive( m_bands[i].active->value() );
	m_handleList->at( i )->update();
	m_eqcurve->update();
}



//this is called if a handle is moved
void EqParameterWidget::updateModels()
{
	for ( int i=0 ; i < bandCount(); i++ )
	{
		m_bands[i].freq->setValue( EqHandle::xPixelToFreq( m_handleList->at( i )->x(), m_displayWidth ) );

		if( m_bands[i].gain )
		{
			m_bands[i].gain->setValue( EqHandle::yPixelToGain( m_handleList->at(i)->y(), m_displayHeigth, m_pixelsPerUnitHeight ) );
		}

		m_bands[i].res->setValue( m_handleList->at( i )->getResonance() );
		//identifies the handle which is moved and set the band active
		if ( sender() == m_handleList->at( i ) )
		{
			m_bands[i].active->setValue( true );
		}
	}
	m_eqcurve->update();
}




EqBand::EqBand() :
	gain ( 0 ),
	res ( 0 ),
	freq ( 0 ),
	color ( QColor( 255, 255, 255 ) ),
	x( 0 ),
	y( 0 ),
	name ( QString( "" ) ),
	peakL( 0 ),
	peakR( 0 )
{
}