hydrogen 1.2.3
MainSampleWaveDisplay.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#include <core/Basics/Sample.h>
24#include <core/Basics/Song.h>
26#include "HydrogenApp.h"
27#include "SampleEditor.h"
28using namespace H2Core;
29
31#include "../Skin.h"
32
34 : QWidget( pParent )
35 {
36// setAttribute(Qt::WA_OpaquePaintEvent);
37
38 //
39 int w = 624;
40 int h = 265;
41 resize( w, h );
42
43 bool ok = m_background.load( Skin::getImagePath() + "/waveDisplay/mainsamplewavedisplay.png" );
44 if( ok == false ){
45 ERRORLOG( "Error loading pixmap" );
46 }
47
48 m_pPeakDatal = new int[ w ];
49 m_pPeakDatar = new int[ w ];
50
53 m_nEndFramePosition = width() -25;
54 m_nLocator = -1;
55 m_bUpdatePosition = false;
57
60 m_bEndSliderIsmoved = false;
61
63 setMouseTracking(true);
64}
65
66
67
68
70{
71 //INFOLOG( "DESTROY" );
72
73 delete[] m_pPeakDatal;
74 delete[] m_pPeakDatar;
75}
76
77void MainSampleWaveDisplay::paintLocatorEvent( int pos, bool updateposi)
78{
79 m_bUpdatePosition = updateposi;
80 if ( !updateposi ){
81 m_nLocator = -1;
82 }else
83 {
84 m_nLocator = pos;
85 }
86 update();
87}
88
89static void set_paint_color(QPainter & painter, const QColor & color, bool selected, MainSampleWaveDisplay::Slider which)
90{
91 if (!selected) {
92 painter.setPen( color );
93 } else {
94 QColor highlight = QColor(std::min(255, color.red() + 20 + 20 * (which == MainSampleWaveDisplay::END)),
95 std::min(255, color.green() + 20 + 20 * (which == MainSampleWaveDisplay::START)),
96 std::min(255, color.blue() + 20 + 20 * (which == MainSampleWaveDisplay::LOOP)));
97
98 painter.setPen ( highlight );
99 }
100}
101
103{
104 QPainter painter( this );
105 painter.setRenderHint( QPainter::Antialiasing );
106
107 bool issmaller = false;
108
109 painter.drawPixmap( ev->rect(), m_background, ev->rect() );
110 painter.setPen( QColor( 230, 230, 230 ) );
111 int VCenterl = height() / 4;
112 int VCenterr = height() / 4 + height() / 2;
113
114 if ( width() >= m_nSampleLength ) issmaller = true;
115
116 for ( int x = 25; x < width() -25; x++ ) {
117 if ( !issmaller || x <= m_nSampleLength){
118 painter.drawLine( x, -m_pPeakDatal[x -25] +VCenterl, x, -m_pPeakDatal[x -24] +VCenterl );
119 painter.drawLine( x, -m_pPeakDatar[x -25] +VCenterr, x, -m_pPeakDatar[x -24] +VCenterr );
120 }else
121 {
122 painter.drawLine( x, 0 +VCenterl, x, 0 +VCenterl );
123 painter.drawLine( x, 0 +VCenterr, x, 0 +VCenterr );
124 }
125
126 }
127
128
129 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
130
131 painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::DotLine ) );
132 painter.drawLine( 23, 4, 23, height() -4 );
133 painter.drawLine( width() -23, 4,width() -23, height() -4 );
134 painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::SolidLine ) );
135 painter.drawLine( m_nLocator, 4, m_nLocator, height() -4);
136 painter.drawLine( 0, VCenterl, width(),VCenterl );
137 painter.drawLine( 0, VCenterr, width(),VCenterr );
138
139 QFont font;
140
141 QColor startColor = QColor( 32, 173, 0, 200 );
142 QColor endColor = QColor( 217, 68, 0, 200 );
143 QColor loopColor = QColor( 93, 170, 254, 200 );
144 font.setWeight( 63 );
145 painter.setFont( font );
146//start frame pointer
147 set_paint_color(painter, startColor, m_SelectedSlider == START, m_SelectedSlider);
148 painter.drawLine( m_nStartFramePosition, 4, m_nStartFramePosition, height() -4 );
149 painter.drawText( m_nStartFramePosition -10, 250, 10,20, Qt::AlignRight, "S" );
150//endframe pointer
152 painter.drawLine( m_nEndFramePosition, 4, m_nEndFramePosition, height() -4 );
153 painter.drawText( m_nEndFramePosition -10, 123, 10, 20, Qt::AlignRight, "E" );
154//loopframe pointer
155 set_paint_color(painter, loopColor, m_SelectedSlider == LOOP, m_SelectedSlider);
156 painter.drawLine( m_nLoopFramePosition, 4, m_nLoopFramePosition, height() -4 );
157 painter.drawText( m_nLoopFramePosition , 0, 10, 20, Qt::AlignLeft, "L" );
158
159
160}
161
162
163
165{
166 update();
167}
168
169
170
171void MainSampleWaveDisplay::updateDisplay( const QString& filename )
172{
173
174 auto pNewSample = Sample::load( filename );
175
176 if ( pNewSample ) {
177
178 int nSampleLength = pNewSample->get_frames();
179 m_nSampleLength = nSampleLength;
180 float nScaleFactor = nSampleLength / (width() -50);
181 if ( nScaleFactor < 1 ){
182 nScaleFactor = 1;
183 }
184
185 float fGain = height() / 4.0 * 1.0;
186
187 auto pSampleDatal = pNewSample->get_data_l();
188 auto pSampleDatar = pNewSample->get_data_r();
189
190 unsigned nSamplePos = 0;
191 int nVall = 0;
192 int nValr = 0;
193 int newVall = 0;
194 int newValr = 0;
195 for ( int i = 0; i < width(); ++i ){
196 for ( int j = 0; j < nScaleFactor; ++j ) {
197 if ( j < nSampleLength && nSamplePos < nSampleLength) {
198 if ( pSampleDatal[ nSamplePos ] && pSampleDatar[ nSamplePos ] ){
199 newVall = static_cast<int>( pSampleDatal[ nSamplePos ] * fGain );
200 newValr = static_cast<int>( pSampleDatar[ nSamplePos ] * fGain );
201 nVall = newVall;
202 nValr = newValr;
203 }else
204 {
205 nVall = 0;
206 nValr = 0;
207 }
208 }
209 ++nSamplePos;
210 }
211 m_pPeakDatal[ i ] = nVall;
212 m_pPeakDatar[ i ] = nValr;
213 }
214 }
215 update();
216
217}
224
225
227{
228 if (ev->buttons() & Qt::LeftButton) {
229 testPosition( ev );
230 } else {
231 chooseSlider( ev );
232 }
233 update();
235}
236
238{
239 chooseSlider( ev );
240 testPosition( ev );
241 update();
243}
244
246{
247 assert(ev);
248//startframepointer
249 int x = std::min(width() - 25, std::max(25, ev->x()));
250
251 if ( m_SelectedSlider == START ) {
257 }
260 m_bEndSliderIsmoved = true;
261 }
262 }
263
264//loopframeposition
265 else if ( m_SelectedSlider == LOOP ) {
266 if (x >= m_nStartFramePosition && x <= m_nEndFramePosition ) {
269 }
270 }
271//endframeposition
272 else if ( m_SelectedSlider == END) {
273 if (x >= m_nStartFramePosition) {
275 m_bEndSliderIsmoved = true;
276 }
280 }
281 }
282}
283
284
286{
288 update();
290}
291
292
293
294
296{
297 assert(ev);
298
299 QPoint start = QPoint(m_nStartFramePosition, height());
300 QPoint end = QPoint(m_nEndFramePosition, height() / 2);
301 QPoint loop = QPoint(m_nLoopFramePosition, 0);
302
303 int ds = (ev->pos() - start).manhattanLength();
304 int de = (ev->pos() - end).manhattanLength();
305 int dl = (ev->pos() - loop).manhattanLength();
307
308 if (ds <= de && ds <= dl) {
310 } else if (de < ds && de <= dl) {
312 } else if (dl < ds && dl < de) {
314 }
315}
316
static void set_paint_color(QPainter &painter, const QColor &color, bool selected, MainSampleWaveDisplay::Slider which)
#define ERRORLOG(x)
Definition Object.h:239
static std::shared_ptr< Sample > load(const QString &filepath, const License &license=License())
Definition Sample.cpp:136
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
SampleEditor * getSampleEditor()
virtual void mouseMoveEvent(QMouseEvent *ev) override
MainSampleWaveDisplay(QWidget *pParent)
void updateDisplay(const QString &filename)
virtual void mousePressEvent(QMouseEvent *ev) override
void chooseSlider(QMouseEvent *ev)
virtual void mouseReleaseEvent(QMouseEvent *ev) override
void testPosition(QMouseEvent *ev)
void paintLocatorEvent(int pos, bool last_event)
virtual void paintEvent(QPaintEvent *ev) override
bool returnAllMainWaveDisplayValues()
static QString getImagePath()
Definition Skin.h:36