/
TimeLineWidget.cpp
426 lines (342 loc) · 11.1 KB
/
TimeLineWidget.cpp
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* TimeLineWidget.cpp - class timeLine, representing a time-line with position marker
*
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - http://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 <QDomElement>
#include <QTimer>
#include <QApplication>
#include <QLayout>
#include <QMouseEvent>
#include <QPainter>
#include <QToolBar>
#include "TimeLineWidget.h"
#include "embed.h"
#include "Engine.h"
#include "templates.h"
#include "NStateButton.h"
#include "GuiApplication.h"
#include "TextFloat.h"
#include "SongEditor.h"
#if QT_VERSION < 0x040800
#define MiddleButton MidButton
#endif
QPixmap * TimeLineWidget::s_posMarkerPixmap = NULL;
TimeLineWidget::TimeLineWidget( const int xoff, const int yoff, const float ppt,
Song::PlayPos & pos, const MidiTime & begin,
QWidget * parent ) :
QWidget( parent ),
m_inactiveLoopColor( 52, 63, 53, 64 ),
m_inactiveLoopBrush( QColor( 255, 255, 255, 32 ) ),
m_inactiveLoopInnerColor( 255, 255, 255, 32 ),
m_activeLoopColor( 52, 63, 53, 255 ),
m_activeLoopBrush( QColor( 55, 141, 89 ) ),
m_activeLoopInnerColor( 74, 155, 100, 255 ),
m_loopRectangleVerticalPadding( 1 ),
m_barLineColor( 192, 192, 192 ),
m_barNumberColor( m_barLineColor.darker( 120 ) ),
m_autoScroll( AutoScrollEnabled ),
m_loopPoints( LoopPointsDisabled ),
m_behaviourAtStop( BackToZero ),
m_changedPosition( true ),
m_xOffset( xoff ),
m_posMarkerX( 0 ),
m_ppt( ppt ),
m_pos( pos ),
m_begin( begin ),
m_savedPos( -1 ),
m_hint( NULL ),
m_action( NoAction ),
m_moveXOff( 0 )
{
m_loopPos[0] = 0;
m_loopPos[1] = DefaultTicksPerTact;
if( s_posMarkerPixmap == NULL )
{
s_posMarkerPixmap = new QPixmap( embed::getIconPixmap(
"playpos_marker" ) );
}
setAttribute( Qt::WA_OpaquePaintEvent, true );
move( 0, yoff );
m_xOffset -= s_posMarkerPixmap->width() / 2;
m_pos.m_timeLine = this;
QTimer * updateTimer = new QTimer( this );
connect( updateTimer, SIGNAL( timeout() ),
this, SLOT( updatePosition() ) );
updateTimer->start( 50 );
}
TimeLineWidget::~TimeLineWidget()
{
if( gui->songEditor() )
{
m_pos.m_timeLine = NULL;
}
delete m_hint;
}
void TimeLineWidget::addToolButtons( QToolBar * _tool_bar )
{
NStateButton * autoScroll = new NStateButton( _tool_bar );
autoScroll->setGeneralToolTip( tr( "Enable/disable auto-scrolling" ) );
autoScroll->addState( embed::getIconPixmap( "autoscroll_on" ) );
autoScroll->addState( embed::getIconPixmap( "autoscroll_off" ) );
connect( autoScroll, SIGNAL( changedState( int ) ), this,
SLOT( toggleAutoScroll( int ) ) );
NStateButton * loopPoints = new NStateButton( _tool_bar );
loopPoints->setGeneralToolTip( tr( "Enable/disable loop-points" ) );
loopPoints->addState( embed::getIconPixmap( "loop_points_off" ) );
loopPoints->addState( embed::getIconPixmap( "loop_points_on" ) );
connect( loopPoints, SIGNAL( changedState( int ) ), this,
SLOT( toggleLoopPoints( int ) ) );
connect( this, SIGNAL( loopPointStateLoaded( int ) ), loopPoints,
SLOT( changeState( int ) ) );
NStateButton * behaviourAtStop = new NStateButton( _tool_bar );
behaviourAtStop->addState( embed::getIconPixmap( "back_to_zero" ),
tr( "After stopping go back to begin" )
);
behaviourAtStop->addState( embed::getIconPixmap( "back_to_start" ),
tr( "After stopping go back to "
"position at which playing was "
"started" ) );
behaviourAtStop->addState( embed::getIconPixmap( "keep_stop_position" ),
tr( "After stopping keep position" ) );
connect( behaviourAtStop, SIGNAL( changedState( int ) ), this,
SLOT( toggleBehaviourAtStop( int ) ) );
_tool_bar->addWidget( autoScroll );
_tool_bar->addWidget( loopPoints );
_tool_bar->addWidget( behaviourAtStop );
}
void TimeLineWidget::saveSettings( QDomDocument & _doc, QDomElement & _this )
{
_this.setAttribute( "lp0pos", (int) loopBegin() );
_this.setAttribute( "lp1pos", (int) loopEnd() );
_this.setAttribute( "lpstate", m_loopPoints );
}
void TimeLineWidget::loadSettings( const QDomElement & _this )
{
m_loopPos[0] = _this.attribute( "lp0pos" ).toInt();
m_loopPos[1] = _this.attribute( "lp1pos" ).toInt();
m_loopPoints = static_cast<LoopPointStates>(
_this.attribute( "lpstate" ).toInt() );
update();
emit loopPointStateLoaded( m_loopPoints );
}
void TimeLineWidget::updatePosition( const MidiTime & )
{
const int new_x = markerX( m_pos );
if( new_x != m_posMarkerX )
{
m_posMarkerX = new_x;
m_changedPosition = true;
emit positionChanged( m_pos );
update();
}
}
void TimeLineWidget::toggleAutoScroll( int _n )
{
m_autoScroll = static_cast<AutoScrollStates>( _n );
}
void TimeLineWidget::toggleLoopPoints( int _n )
{
m_loopPoints = static_cast<LoopPointStates>( _n );
update();
}
void TimeLineWidget::toggleBehaviourAtStop( int _n )
{
m_behaviourAtStop = static_cast<BehaviourAtStopStates>( _n );
}
void TimeLineWidget::paintEvent( QPaintEvent * )
{
QPainter p( this );
// Draw background
p.fillRect( 0, 0, width(), height(), p.background() );
// Clip so that we only draw everything starting from the offset
p.setClipRect( m_xOffset, 0, width() - m_xOffset, height() );
// Draw the loop rectangle
int const & loopRectMargin = getLoopRectangleVerticalPadding();
int const loopRectHeight = this->height() - 2 * loopRectMargin;
int const loopStart = markerX( loopBegin() ) + 8;
int const loopEndR = markerX( loopEnd() ) + 9;
int const loopRectWidth = loopEndR - loopStart;
bool const loopPointsActive = loopPointsEnabled();
// Draw the main rectangle (inner fill only)
QRect outerRectangle( loopStart, loopRectMargin, loopRectWidth - 1, loopRectHeight - 1 );
p.fillRect( outerRectangle, loopPointsActive ? getActiveLoopBrush() : getInactiveLoopBrush());
// Draw the bar lines and numbers
// Activate hinting on the font
QFont font = p.font();
font.setHintingPreference( QFont::PreferFullHinting );
p.setFont(font);
int const fontAscent = p.fontMetrics().ascent();
int const fontHeight = p.fontMetrics().height();
QColor const & barLineColor = getBarLineColor();
QColor const & barNumberColor = getBarNumberColor();
tact_t barNumber = m_begin.getTact();
int const x = m_xOffset + s_posMarkerPixmap->width() / 2 -
( ( static_cast<int>( m_begin * m_ppt ) / MidiTime::ticksPerTact() ) % static_cast<int>( m_ppt ) );
for( int i = 0; x + i * m_ppt < width(); ++i )
{
++barNumber;
if( ( barNumber - 1 ) %
qMax( 1, qRound( 1.0f / 3.0f *
MidiTime::ticksPerTact() / m_ppt ) ) == 0 )
{
const int cx = x + qRound( i * m_ppt );
p.setPen( barLineColor );
p.drawLine( cx, 5, cx, height() - 6 );
const QString s = QString::number( barNumber );
p.setPen( barNumberColor );
p.drawText( cx + 5, ((height() - fontHeight) / 2) + fontAscent, s );
}
}
// Draw the main rectangle (outer border)
p.setPen( loopPointsActive ? getActiveLoopColor() : getInactiveLoopColor() );
p.setBrush( Qt::NoBrush );
p.drawRect( outerRectangle );
// Draw the inner border outline (no fill)
QRect innerRectangle = outerRectangle.adjusted( 1, 1, -1, -1 );
p.setPen( loopPointsActive ? getActiveLoopInnerColor() : getInactiveLoopInnerColor() );
p.setBrush( Qt::NoBrush );
p.drawRect( innerRectangle );
// Draw the position marker
p.setOpacity( 0.6 );
p.drawPixmap( m_posMarkerX, height() - s_posMarkerPixmap->height(), *s_posMarkerPixmap );
}
void TimeLineWidget::mousePressEvent( QMouseEvent* event )
{
if( event->x() < m_xOffset )
{
return;
}
if( event->button() == Qt::LeftButton && !(event->modifiers() & Qt::ShiftModifier) )
{
m_action = MovePositionMarker;
if( event->x() - m_xOffset < s_posMarkerPixmap->width() )
{
m_moveXOff = event->x() - m_xOffset;
}
else
{
m_moveXOff = s_posMarkerPixmap->width() / 2;
}
}
else if( event->button() == Qt::LeftButton && (event->modifiers() & Qt::ShiftModifier) )
{
m_action = SelectSongTCO;
m_initalXSelect = event->x();
}
else if( event->button() == Qt::RightButton || event->button() == Qt::MiddleButton )
{
m_moveXOff = s_posMarkerPixmap->width() / 2;
const MidiTime t = m_begin + static_cast<int>( event->x() * MidiTime::ticksPerTact() / m_ppt );
if( m_loopPos[0] > m_loopPos[1] )
{
qSwap( m_loopPos[0], m_loopPos[1] );
}
if( ( event->modifiers() & Qt::ShiftModifier ) || event->button() == Qt::MiddleButton )
{
m_action = MoveLoopBegin;
}
else
{
m_action = MoveLoopEnd;
}
m_loopPos[( m_action == MoveLoopBegin ) ? 0 : 1] = t;
}
if( m_action == MoveLoopBegin )
{
delete m_hint;
m_hint = TextFloat::displayMessage( tr( "Hint" ),
tr( "Press <%1> to disable magnetic loop points." ).arg(
#ifdef LMMS_BUILD_APPLE
"⌘"),
#else
"Ctrl"),
#endif
embed::getIconPixmap( "hint" ), 0 );
}
else if( m_action == MoveLoopEnd )
{
delete m_hint;
m_hint = TextFloat::displayMessage( tr( "Hint" ),
tr( "Hold <Shift> to move the begin loop point; Press <%1> to disable magnetic loop points." ).arg(
#ifdef LMMS_BUILD_APPLE
"⌘"),
#else
"Ctrl"),
#endif
embed::getIconPixmap( "hint" ), 0 );
}
mouseMoveEvent( event );
}
void TimeLineWidget::mouseMoveEvent( QMouseEvent* event )
{
const MidiTime t = m_begin + static_cast<int>( qMax( event->x() - m_xOffset - m_moveXOff, 0 ) * MidiTime::ticksPerTact() / m_ppt );
switch( m_action )
{
case MovePositionMarker:
m_pos.setTicks( t.getTicks() );
Engine::getSong()->setMilliSeconds(((((t.getTicks()))*60*1000/48)/Engine::getSong()->getTempo()));
m_pos.setCurrentFrame( 0 );
updatePosition();
positionMarkerMoved();
break;
case MoveLoopBegin:
case MoveLoopEnd:
{
const int i = m_action - MoveLoopBegin;
if( event->modifiers() & Qt::ControlModifier )
{
// no ctrl-press-hint when having ctrl pressed
delete m_hint;
m_hint = NULL;
m_loopPos[i] = t;
}
else
{
m_loopPos[i] = t.toNearestTact();
}
// Catch begin == end
if( m_loopPos[0] == m_loopPos[1] )
{
// Note, swap 1 and 0 below and the behavior "skips" the other
// marking instead of pushing it.
if( m_action == MoveLoopBegin )
m_loopPos[0] -= MidiTime::ticksPerTact();
else
m_loopPos[1] += MidiTime::ticksPerTact();
}
update();
break;
}
case SelectSongTCO:
emit regionSelectedFromPixels( m_initalXSelect , event->x() );
break;
default:
break;
}
}
void TimeLineWidget::mouseReleaseEvent( QMouseEvent* event )
{
delete m_hint;
m_hint = NULL;
if ( m_action == SelectSongTCO ) { emit selectionFinished(); }
m_action = NoAction;
}