hydrogen 1.2.6
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-2025 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
24
25#include <core/Basics/Sample.h>
26#include <core/Basics/Song.h>
29#include "HydrogenApp.h"
30#include "SampleEditor.h"
31using namespace H2Core;
32
33#include "../Skin.h"
34
36 : QWidget( pParent )
37 {
38// setAttribute(Qt::WA_OpaquePaintEvent);
39
40 //
41 int w = 624;
42 int h = 265;
43 resize( w, h );
44
45 bool ok = m_background.load( Skin::getImagePath() + "/waveDisplay/mainsamplewavedisplay.png" );
46 if( ok == false ){
47 ERRORLOG( "Error loading pixmap" );
48 }
49
50 m_pPeakDatal = new int[ w ];
51 m_pPeakDatar = new int[ w ];
52
55 m_nEndFramePosition = width() -25;
56 m_nLocator = -1;
57 m_bUpdatePosition = false;
59
62 m_bEndSliderIsmoved = false;
63
65 setMouseTracking(true);
66}
67
68
69
70
72{
73 //INFOLOG( "DESTROY" );
74
75 delete[] m_pPeakDatal;
76 delete[] m_pPeakDatar;
77}
78
79void MainSampleWaveDisplay::paintLocatorEvent( int pos, bool updateposi)
80{
81 m_bUpdatePosition = updateposi;
82 if ( !updateposi ){
83 m_nLocator = -1;
84 }else
85 {
86 m_nLocator = pos;
87 }
88 update();
89}
90
91static void set_paint_color(QPainter & painter, const QColor & color, bool selected, MainSampleWaveDisplay::Slider which)
92{
93 if (!selected) {
94 painter.setPen( color );
95 } else {
96 QColor highlight = QColor(std::min(255, color.red() + 20 + 20 * (which == MainSampleWaveDisplay::END)),
97 std::min(255, color.green() + 20 + 20 * (which == MainSampleWaveDisplay::START)),
98 std::min(255, color.blue() + 20 + 20 * (which == MainSampleWaveDisplay::LOOP)));
99
100 painter.setPen ( highlight );
101 }
102}
103
105{
106 QPainter painter( this );
107 painter.setRenderHint( QPainter::Antialiasing );
108
109 bool issmaller = false;
110
111 painter.drawPixmap( ev->rect(), m_background, ev->rect() );
112 painter.setPen( QColor( 230, 230, 230 ) );
113 int VCenterl = height() / 4;
114 int VCenterr = height() / 4 + height() / 2;
115
116 if ( width() >= m_nSampleLength ) issmaller = true;
117
118 for ( int x = 25; x < width() -25; x++ ) {
119 if ( !issmaller || x <= m_nSampleLength){
120 painter.drawLine( x, -m_pPeakDatal[x -25] +VCenterl, x, -m_pPeakDatal[x -24] +VCenterl );
121 painter.drawLine( x, -m_pPeakDatar[x -25] +VCenterr, x, -m_pPeakDatar[x -24] +VCenterr );
122 }else
123 {
124 painter.drawLine( x, 0 +VCenterl, x, 0 +VCenterl );
125 painter.drawLine( x, 0 +VCenterr, x, 0 +VCenterr );
126 }
127
128 }
129
130
131 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
132
133 painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::DotLine ) );
134 painter.drawLine( 23, 4, 23, height() -4 );
135 painter.drawLine( width() -23, 4,width() -23, height() -4 );
136 painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::SolidLine ) );
137 painter.drawLine( m_nLocator, 4, m_nLocator, height() -4);
138 painter.drawLine( 0, VCenterl, width(),VCenterl );
139 painter.drawLine( 0, VCenterr, width(),VCenterr );
140
141 QFont font;
142
143 QColor startColor = QColor( 32, 173, 0, 200 );
144 QColor endColor = QColor( 217, 68, 0, 200 );
145 QColor loopColor = QColor( 93, 170, 254, 200 );
146 font.setWeight( QFont::Bold );
147 painter.setFont( font );
148//start frame pointer
149 set_paint_color(painter, startColor, m_SelectedSlider == START, m_SelectedSlider);
150 painter.drawLine( m_nStartFramePosition, 4, m_nStartFramePosition, height() -4 );
151 painter.drawText( m_nStartFramePosition -10, 250, 10,20, Qt::AlignRight, "S" );
152//endframe pointer
154 painter.drawLine( m_nEndFramePosition, 4, m_nEndFramePosition, height() -4 );
155 painter.drawText( m_nEndFramePosition -10, 123, 10, 20, Qt::AlignRight, "E" );
156//loopframe pointer
157 set_paint_color(painter, loopColor, m_SelectedSlider == LOOP, m_SelectedSlider);
158 painter.drawLine( m_nLoopFramePosition, 4, m_nLoopFramePosition, height() -4 );
159 painter.drawText( m_nLoopFramePosition , 0, 10, 20, Qt::AlignLeft, "L" );
160
161
162}
163
164
165
167{
168 update();
169}
170
171
172
173void MainSampleWaveDisplay::updateDisplay( const QString& filename )
174{
175
176 auto pNewSample = Sample::load( filename );
177
178 if ( pNewSample ) {
179
180 int nSampleLength = pNewSample->get_frames();
181 m_nSampleLength = nSampleLength;
182 float nScaleFactor = nSampleLength / (width() -50);
183 if ( nScaleFactor < 1 ){
184 nScaleFactor = 1;
185 }
186
187 float fGain = height() / 4.0 * 1.0;
188
189 auto pSampleDatal = pNewSample->get_data_l();
190 auto pSampleDatar = pNewSample->get_data_r();
191
192 unsigned nSamplePos = 0;
193 int nVall = 0;
194 int nValr = 0;
195 int newVall = 0;
196 int newValr = 0;
197 for ( int i = 0; i < width(); ++i ){
198 for ( int j = 0; j < nScaleFactor; ++j ) {
199 if ( j < nSampleLength && nSamplePos < nSampleLength) {
200 if ( pSampleDatal[ nSamplePos ] && pSampleDatar[ nSamplePos ] ){
201 newVall = static_cast<int>( pSampleDatal[ nSamplePos ] * fGain );
202 newValr = static_cast<int>( pSampleDatar[ nSamplePos ] * fGain );
203 nVall = newVall;
204 nValr = newValr;
205 }else
206 {
207 nVall = 0;
208 nValr = 0;
209 }
210 }
211 ++nSamplePos;
212 }
213 m_pPeakDatal[ i ] = nVall;
214 m_pPeakDatar[ i ] = nValr;
215 }
216 }
217 update();
218
219}
226
227
229{
230 if (ev->buttons() & Qt::LeftButton) {
231 testPosition( ev );
232 } else {
233 chooseSlider( ev );
234 }
235 update();
237}
238
240{
241 chooseSlider( ev );
242 testPosition( ev );
243 update();
245}
246
248{
249 assert(ev);
250
251 auto pEv = static_cast<MouseEvent*>( ev );
252
253//startframepointer
254 int x = std::min(width() - 25,
255 std::max(25, static_cast<int>(pEv->position().x())));
256
257 if ( m_SelectedSlider == START ) {
263 }
266 m_bEndSliderIsmoved = true;
267 }
268 }
269
270//loopframeposition
271 else if ( m_SelectedSlider == LOOP ) {
272 if (x >= m_nStartFramePosition && x <= m_nEndFramePosition ) {
275 }
276 }
277//endframeposition
278 else if ( m_SelectedSlider == END) {
279 if (x >= m_nStartFramePosition) {
281 m_bEndSliderIsmoved = true;
282 }
286 }
287 }
288}
289
290
292{
294 update();
296}
297
298
299
300
302{
303 assert(ev);
304
305 auto pEv = static_cast<MouseEvent*>( ev );
306
307 QPoint start = QPoint(m_nStartFramePosition, height());
308 QPoint end = QPoint(m_nEndFramePosition, height() / 2);
309 QPoint loop = QPoint(m_nLoopFramePosition, 0);
310
311 int ds = (pEv->position() - start).manhattanLength();
312 int de = (pEv->position() - end).manhattanLength();
313 int dl = (pEv->position() - loop).manhattanLength();
315
316 if (ds <= de && ds <= dl) {
318 } else if (de < ds && de <= dl) {
320 } else if (dl < ds && dl < de) {
322 }
323}
324
static void set_paint_color(QPainter &painter, const QColor &color, bool selected, MainSampleWaveDisplay::Slider which)
#define ERRORLOG(x)
Definition Object.h:242
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
Compatibility class to support QMouseEvent more esily in Qt5 and Qt6.
Definition MouseEvent.h:35
bool returnAllMainWaveDisplayValues()
static QString getImagePath()
Definition Skin.h:36