-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathTrack.cpp
2864 lines (2361 loc) · 74.1 KB
/
Track.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Track.cpp - implementation of classes concerning tracks -> necessary for
* all track-like objects (beat/bassline, sample-track...)
*
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* 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.
*
*/
/** \file Track.cpp
* \brief All classes concerning tracks and track-like objects
*/
/*
* \mainpage Track classes
*
* \section introduction Introduction
*
* \todo fill this out
*/
#include "Track.h"
#include <assert.h>
#include <QLayout>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QStyleOption>
#include "AutomationPattern.h"
#include "AutomationTrack.h"
#include "AutomationEditor.h"
#include "BBEditor.h"
#include "BBTrack.h"
#include "BBTrackContainer.h"
#include "ConfigManager.h"
#include "Clipboard.h"
#include "embed.h"
#include "Engine.h"
#include "GuiApplication.h"
#include "FxMixerView.h"
#include "gui_templates.h"
#include "MainWindow.h"
#include "Mixer.h"
#include "ProjectJournal.h"
#include "SampleTrack.h"
#include "Song.h"
#include "SongEditor.h"
#include "StringPairDrag.h"
#include "TextFloat.h"
/*! The width of the resize grip in pixels
*/
const int RESIZE_GRIP_WIDTH = 4;
/*! A pointer for that text bubble used when moving segments, etc.
*
* In a number of situations, LMMS displays a floating text bubble
* beside the cursor as you move or resize elements of a track about.
* This pointer keeps track of it, as you only ever need one at a time.
*/
TextFloat * TrackContentObjectView::s_textFloat = NULL;
// ===========================================================================
// TrackContentObject
// ===========================================================================
/*! \brief Create a new TrackContentObject
*
* Creates a new track content object for the given track.
*
* \param _track The track that will contain the new object
*/
TrackContentObject::TrackContentObject( Track * track ) :
Model( track ),
m_track( track ),
m_name( QString::null ),
m_startPosition(),
m_length(),
m_mutedModel( false, this, tr( "Mute" ) ),
m_selectViewOnCreate( false )
{
if( getTrack() )
{
getTrack()->addTCO( this );
}
setJournalling( false );
movePosition( 0 );
changeLength( 0 );
setJournalling( true );
}
/*! \brief Destroy a TrackContentObject
*
* Destroys the given track content object.
*
*/
TrackContentObject::~TrackContentObject()
{
emit destroyedTCO();
if( getTrack() )
{
getTrack()->removeTCO( this );
}
}
/*! \brief Move this TrackContentObject's position in time
*
* If the track content object has moved, update its position. We
* also add a journal entry for undo and update the display.
*
* \param _pos The new position of the track content object.
*/
void TrackContentObject::movePosition( const MidiTime & pos )
{
if( m_startPosition != pos )
{
Engine::mixer()->requestChangeInModel();
m_startPosition = pos;
Engine::mixer()->doneChangeInModel();
Engine::getSong()->updateLength();
emit positionChanged();
}
}
/*! \brief Change the length of this TrackContentObject
*
* If the track content object's length has chaanged, update it. We
* also add a journal entry for undo and update the display.
*
* \param _length The new length of the track content object.
*/
void TrackContentObject::changeLength( const MidiTime & length )
{
m_length = length;
Engine::getSong()->updateLength();
emit lengthChanged();
}
bool TrackContentObject::comparePosition(const TrackContentObject *a, const TrackContentObject *b)
{
return a->startPosition() < b->startPosition();
}
/*! \brief Copy this TrackContentObject to the clipboard.
*
* Copies this track content object to the clipboard.
*/
void TrackContentObject::copy()
{
Clipboard::copy( this );
}
/*! \brief Pastes this TrackContentObject into a track.
*
* Pastes this track content object into a track.
*
* \param _je The journal entry to undo
*/
void TrackContentObject::paste()
{
if( Clipboard::getContent( nodeName() ) != NULL )
{
const MidiTime pos = startPosition();
restoreState( *( Clipboard::getContent( nodeName() ) ) );
movePosition( pos );
}
AutomationPattern::resolveAllIDs();
GuiApplication::instance()->automationEditor()->m_editor->updateAfterPatternChange();
}
/*! \brief Mutes this TrackContentObject
*
* Restore the previous state of this track content object. This will
* restore the position or the length of the track content object
* depending on what was changed.
*
* \param _je The journal entry to undo
*/
void TrackContentObject::toggleMute()
{
m_mutedModel.setValue( !m_mutedModel.value() );
emit dataChanged();
}
// ===========================================================================
// trackContentObjectView
// ===========================================================================
/*! \brief Create a new trackContentObjectView
*
* Creates a new track content object view for the given
* track content object in the given track view.
*
* \param _tco The track content object to be displayed
* \param _tv The track view that will contain the new object
*/
TrackContentObjectView::TrackContentObjectView( TrackContentObject * tco,
TrackView * tv ) :
selectableObject( tv->getTrackContentWidget() ),
ModelView( NULL, this ),
m_tco( tco ),
m_trackView( tv ),
m_action( NoAction ),
m_initialMousePos( QPoint( 0, 0 ) ),
m_initialMouseGlobalPos( QPoint( 0, 0 ) ),
m_hint( NULL ),
m_mutedColor( 0, 0, 0 ),
m_mutedBackgroundColor( 0, 0, 0 ),
m_selectedColor( 0, 0, 0 ),
m_textColor( 0, 0, 0 ),
m_textShadowColor( 0, 0, 0 ),
m_BBPatternBackground( 0, 0, 0 ),
m_gradient( true ),
m_needsUpdate( true )
{
if( s_textFloat == NULL )
{
s_textFloat = new TextFloat;
s_textFloat->setPixmap( embed::getIconPixmap( "clock" ) );
}
setAttribute( Qt::WA_OpaquePaintEvent, true );
setAttribute( Qt::WA_DeleteOnClose, true );
setFocusPolicy( Qt::StrongFocus );
setCursor( QCursor( embed::getIconPixmap( "hand" ), 3, 3 ) );
move( 0, 0 );
show();
setFixedHeight( tv->getTrackContentWidget()->height() - 1);
setAcceptDrops( true );
setMouseTracking( true );
connect( m_tco, SIGNAL( lengthChanged() ),
this, SLOT( updateLength() ) );
connect( gui->songEditor()->m_editor->zoomingModel(), SIGNAL( dataChanged() ), this, SLOT( updateLength() ) );
connect( m_tco, SIGNAL( positionChanged() ),
this, SLOT( updatePosition() ) );
connect( m_tco, SIGNAL( destroyedTCO() ), this, SLOT( close() ) );
setModel( m_tco );
m_trackView->getTrackContentWidget()->addTCOView( this );
updateLength();
updatePosition();
}
/*! \brief Destroy a trackContentObjectView
*
* Destroys the given track content object view.
*
*/
TrackContentObjectView::~TrackContentObjectView()
{
delete m_hint;
// we have to give our track-container the focus because otherwise the
// op-buttons of our track-widgets could become focus and when the user
// presses space for playing song, just one of these buttons is pressed
// which results in unwanted effects
m_trackView->trackContainerView()->setFocus();
}
/*! \brief Update a TrackContentObjectView
*
* TCO's get drawn only when needed,
* and when a TCO is updated,
* it needs to be redrawn.
*
*/
void TrackContentObjectView::update()
{
if( fixedTCOs() )
{
updateLength();
}
m_needsUpdate = true;
selectableObject::update();
}
/*! \brief Does this trackContentObjectView have a fixed TCO?
*
* Returns whether the containing trackView has fixed
* TCOs.
*
* \todo What the hell is a TCO here - track content object? And in
* what circumstance are they fixed?
*/
bool TrackContentObjectView::fixedTCOs()
{
return m_trackView->trackContainerView()->fixedTCOs();
}
// qproperty access functions, to be inherited & used by TCOviews
//! \brief CSS theming qproperty access method
QColor TrackContentObjectView::mutedColor() const
{ return m_mutedColor; }
QColor TrackContentObjectView::mutedBackgroundColor() const
{ return m_mutedBackgroundColor; }
QColor TrackContentObjectView::selectedColor() const
{ return m_selectedColor; }
QColor TrackContentObjectView::textColor() const
{ return m_textColor; }
QColor TrackContentObjectView::textShadowColor() const
{ return m_textShadowColor; }
QColor TrackContentObjectView::BBPatternBackground() const
{ return m_BBPatternBackground; }
bool TrackContentObjectView::gradient() const
{ return m_gradient; }
//! \brief CSS theming qproperty access method
void TrackContentObjectView::setMutedColor( const QColor & c )
{ m_mutedColor = QColor( c ); }
void TrackContentObjectView::setMutedBackgroundColor( const QColor & c )
{ m_mutedBackgroundColor = QColor( c ); }
void TrackContentObjectView::setSelectedColor( const QColor & c )
{ m_selectedColor = QColor( c ); }
void TrackContentObjectView::setTextColor( const QColor & c )
{ m_textColor = QColor( c ); }
void TrackContentObjectView::setTextShadowColor( const QColor & c )
{ m_textShadowColor = QColor( c ); }
void TrackContentObjectView::setBBPatternBackground( const QColor & c )
{ m_BBPatternBackground = QColor( c ); }
void TrackContentObjectView::setGradient( const bool & b )
{ m_gradient = b; }
// access needsUpdate member variable
bool TrackContentObjectView::needsUpdate()
{ return m_needsUpdate; }
void TrackContentObjectView::setNeedsUpdate( bool b )
{ m_needsUpdate = b; }
/*! \brief Close a trackContentObjectView
*
* Closes a track content object view by asking the track
* view to remove us and then asking the QWidget to close us.
*
* \return Boolean state of whether the QWidget was able to close.
*/
bool TrackContentObjectView::close()
{
m_trackView->getTrackContentWidget()->removeTCOView( this );
return QWidget::close();
}
/*! \brief Removes a trackContentObjectView from its track view.
*
* Like the close() method, this asks the track view to remove this
* track content object view. However, the track content object is
* scheduled for later deletion rather than closed immediately.
*
*/
void TrackContentObjectView::remove()
{
m_trackView->getTrack()->addJournalCheckPoint();
// delete ourself
close();
m_tco->deleteLater();
}
/*! \brief Cut this trackContentObjectView from its track to the clipboard.
*
* Perform the 'cut' action of the clipboard - copies the track content
* object to the clipboard and then removes it from the track.
*/
void TrackContentObjectView::cut()
{
m_tco->copy();
remove();
}
/*! \brief Updates a trackContentObjectView's length
*
* If this track content object view has a fixed TCO, then we must
* keep the width of our parent. Otherwise, calculate our width from
* the track content object's length in pixels adding in the border.
*
*/
void TrackContentObjectView::updateLength()
{
if( fixedTCOs() )
{
setFixedWidth( parentWidget()->width() );
}
else
{
setFixedWidth(
static_cast<int>( m_tco->length() * pixelsPerTact() /
MidiTime::ticksPerTact() ) + 1 /*+
TCO_BORDER_WIDTH * 2-1*/ );
}
m_trackView->trackContainerView()->update();
}
/*! \brief Updates a trackContentObjectView's position.
*
* Ask our track view to change our position. Then make sure that the
* track view is updated in case this position has changed the track
* view's length.
*
*/
void TrackContentObjectView::updatePosition()
{
m_trackView->getTrackContentWidget()->changePosition();
// moving a TCO can result in change of song-length etc.,
// therefore we update the track-container
m_trackView->trackContainerView()->update();
}
/*! \brief Change the trackContentObjectView's display when something
* being dragged enters it.
*
* We need to notify Qt to change our display if something being
* dragged has entered our 'airspace'.
*
* \param dee The QDragEnterEvent to watch.
*/
void TrackContentObjectView::dragEnterEvent( QDragEnterEvent * dee )
{
TrackContentWidget * tcw = getTrackView()->getTrackContentWidget();
MidiTime tcoPos = MidiTime( m_tco->startPosition().getTact(), 0 );
if( tcw->canPasteSelection( tcoPos, dee->mimeData() ) == false )
{
dee->ignore();
}
else
{
StringPairDrag::processDragEnterEvent( dee, "tco_" +
QString::number( m_tco->getTrack()->type() ) );
}
}
/*! \brief Handle something being dropped on this trackContentObjectView.
*
* When something has been dropped on this trackContentObjectView, and
* it's a track content object, then use an instance of our dataFile reader
* to take the xml of the track content object and turn it into something
* we can write over our current state.
*
* \param de The QDropEvent to handle.
*/
void TrackContentObjectView::dropEvent( QDropEvent * de )
{
QString type = StringPairDrag::decodeKey( de );
QString value = StringPairDrag::decodeValue( de );
// Track must be the same type to paste into
if( type != ( "tco_" + QString::number( m_tco->getTrack()->type() ) ) )
{
return;
}
// Defer to rubberband paste if we're in that mode
if( m_trackView->trackContainerView()->allowRubberband() == true )
{
TrackContentWidget * tcw = getTrackView()->getTrackContentWidget();
MidiTime tcoPos = MidiTime( m_tco->startPosition().getTact(), 0 );
if( tcw->pasteSelection( tcoPos, de ) == true )
{
de->accept();
}
return;
}
// Don't allow pasting a tco into itself.
QObject* qwSource = de->source();
if( qwSource != NULL &&
dynamic_cast<TrackContentObjectView *>( qwSource ) == this )
{
return;
}
// Copy state into existing tco
DataFile dataFile( value.toUtf8() );
MidiTime pos = m_tco->startPosition();
QDomElement tcos = dataFile.content().firstChildElement( "tcos" );
m_tco->restoreState( tcos.firstChildElement().firstChildElement() );
m_tco->movePosition( pos );
AutomationPattern::resolveAllIDs();
de->accept();
}
/*! \brief Handle a dragged selection leaving our 'airspace'.
*
* \param e The QEvent to watch.
*/
void TrackContentObjectView::leaveEvent( QEvent * e )
{
while( QApplication::overrideCursor() != NULL )
{
QApplication::restoreOverrideCursor();
}
if( e != NULL )
{
QWidget::leaveEvent( e );
}
}
/*! \brief Create a DataFile suitable for copying multiple trackContentObjects.
*
* trackContentObjects in the vector are written to the "tcos" node in the
* DataFile. The trackContentObjectView's initial mouse position is written
* to the "initialMouseX" node in the DataFile. When dropped on a track,
* this is used to create copies of the TCOs.
*
* \param tcos The trackContectObjects to save in a DataFile
*/
DataFile TrackContentObjectView::createTCODataFiles(
const QVector<TrackContentObjectView *> & tcoViews) const
{
Track * t = m_trackView->getTrack();
TrackContainer * tc = t->trackContainer();
DataFile dataFile( DataFile::DragNDropData );
QDomElement tcoParent = dataFile.createElement( "tcos" );
typedef QVector<TrackContentObjectView *> tcoViewVector;
for( tcoViewVector::const_iterator it = tcoViews.begin();
it != tcoViews.end(); ++it )
{
// Insert into the dom under the "tcos" element
int trackIndex = tc->tracks().indexOf( ( *it )->m_trackView->getTrack() );
QDomElement tcoElement = dataFile.createElement( "tco" );
tcoElement.setAttribute( "trackIndex", trackIndex );
( *it )->m_tco->saveState( dataFile, tcoElement );
tcoParent.appendChild( tcoElement );
}
dataFile.content().appendChild( tcoParent );
// Add extra metadata needed for calculations later
int initialTrackIndex = tc->tracks().indexOf( t );
if( initialTrackIndex < 0 )
{
printf("Failed to find selected track in the TrackContainer.\n");
return dataFile;
}
QDomElement metadata = dataFile.createElement( "copyMetadata" );
// initialTrackIndex is the index of the track that was touched
metadata.setAttribute( "initialTrackIndex", initialTrackIndex );
// grabbedTCOPos is the pos of the tact containing the TCO we grabbed
metadata.setAttribute( "grabbedTCOPos", m_tco->startPosition() );
dataFile.content().appendChild( metadata );
return dataFile;
}
/*! \brief Handle a mouse press on this trackContentObjectView.
*
* Handles the various ways in which a trackContentObjectView can be
* used with a click of a mouse button.
*
* * If our container supports rubber band selection then handle
* selection events.
* * or if shift-left button, add this object to the selection
* * or if ctrl-left button, start a drag-copy event
* * or if just plain left button, resize if we're resizeable
* * or if ctrl-middle button, mute the track content object
* * or if middle button, maybe delete the track content object.
*
* \param me The QMouseEvent to handle.
*/
void TrackContentObjectView::mousePressEvent( QMouseEvent * me )
{
setInitialMousePos( me->pos() );
if( m_trackView->trackContainerView()->allowRubberband() == true &&
me->button() == Qt::LeftButton )
{
if( m_trackView->trackContainerView()->rubberBandActive() == true )
{
// Propagate to trackView for rubberbanding
selectableObject::mousePressEvent( me );
}
else if ( me->modifiers() & Qt::ControlModifier )
{
if( isSelected() == true )
{
m_action = CopySelection;
}
else
{
m_action = ToggleSelected;
}
}
else if( !me->modifiers() )
{
if( isSelected() == true )
{
m_action = MoveSelection;
}
}
}
else if( me->button() == Qt::LeftButton &&
me->modifiers() & Qt::ControlModifier )
{
// start drag-action
QVector<TrackContentObjectView *> tcoViews;
tcoViews.push_back( this );
DataFile dataFile = createTCODataFiles( tcoViews );
QPixmap thumbnail = QPixmap::grabWidget( this ).scaled(
128, 128,
Qt::KeepAspectRatio,
Qt::SmoothTransformation );
new StringPairDrag( QString( "tco_%1" ).arg(
m_tco->getTrack()->type() ),
dataFile.toString(), thumbnail, this );
}
else if( me->button() == Qt::LeftButton &&
/* engine::mainWindow()->isShiftPressed() == false &&*/
fixedTCOs() == false )
{
m_tco->addJournalCheckPoint();
// move or resize
m_tco->setJournalling( false );
setInitialMousePos( me->pos() );
if( me->x() < width() - RESIZE_GRIP_WIDTH )
{
m_action = Move;
QCursor c( Qt::SizeAllCursor );
QApplication::setOverrideCursor( c );
delete m_hint;
m_hint = TextFloat::displayMessage( tr( "Hint" ),
tr( "Press <%1> and drag to make "
"a copy." ).arg(
#ifdef LMMS_BUILD_APPLE
"⌘"),
#else
"Ctrl"),
#endif
embed::getIconPixmap( "hint" ), 0 );
s_textFloat->setTitle( tr( "Current position" ) );
s_textFloat->setText( QString( "%1:%2" ).
arg( m_tco->startPosition().getTact() + 1 ).
arg( m_tco->startPosition().getTicks() %
MidiTime::ticksPerTact() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2 ) );
}
else if( !m_tco->getAutoResize() )
{
m_action = Resize;
QCursor c( Qt::SizeHorCursor );
QApplication::setOverrideCursor( c );
delete m_hint;
m_hint = TextFloat::displayMessage( tr( "Hint" ),
tr( "Press <%1> for free "
"resizing." ).arg(
#ifdef LMMS_BUILD_APPLE
"⌘"),
#else
"Ctrl"),
#endif
embed::getIconPixmap( "hint" ), 0 );
s_textFloat->setTitle( tr( "Current length" ) );
s_textFloat->setText( tr( "%1:%2 (%3:%4 to %5:%6)" ).
arg( m_tco->length().getTact() ).
arg( m_tco->length().getTicks() %
MidiTime::ticksPerTact() ).
arg( m_tco->startPosition().getTact() + 1 ).
arg( m_tco->startPosition().getTicks() %
MidiTime::ticksPerTact() ).
arg( m_tco->endPosition().getTact() + 1 ).
arg( m_tco->endPosition().getTicks() %
MidiTime::ticksPerTact() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2) );
}
// s_textFloat->reparent( this );
s_textFloat->show();
}
else if( me->button() == Qt::RightButton )
{
if( me->modifiers() & Qt::ControlModifier )
{
m_tco->toggleMute();
}
else if( me->modifiers() & Qt::ShiftModifier && fixedTCOs() == false )
{
remove();
}
}
else if( me->button() == Qt::MidButton )
{
if( me->modifiers() & Qt::ControlModifier )
{
m_tco->toggleMute();
}
else if( fixedTCOs() == false )
{
remove();
}
}
}
/*! \brief Handle a mouse movement (drag) on this trackContentObjectView.
*
* Handles the various ways in which a trackContentObjectView can be
* used with a mouse drag.
*
* * If in move mode, move ourselves in the track,
* * or if in move-selection mode, move the entire selection,
* * or if in resize mode, resize ourselves,
* * otherwise ???
*
* \param me The QMouseEvent to handle.
* \todo what does the final else case do here?
*/
void TrackContentObjectView::mouseMoveEvent( QMouseEvent * me )
{
if( m_action == CopySelection )
{
if( mouseMovedDistance( me, 2 ) == true &&
m_trackView->trackContainerView()->allowRubberband() == true &&
m_trackView->trackContainerView()->rubberBandActive() == false &&
( me->modifiers() & Qt::ControlModifier ) )
{
// Clear the action here because mouseReleaseEvent will not get
// triggered once we go into drag.
m_action = NoAction;
// Collect all selected TCOs
QVector<TrackContentObjectView *> tcoViews;
QVector<selectableObject *> so =
m_trackView->trackContainerView()->selectedObjects();
for( QVector<selectableObject *>::iterator it = so.begin();
it != so.end(); ++it )
{
TrackContentObjectView * tcov =
dynamic_cast<TrackContentObjectView *>( *it );
if( tcov != NULL )
{
tcoViews.push_back( tcov );
}
}
// Write the TCOs to the DataFile for copying
DataFile dataFile = createTCODataFiles( tcoViews );
// TODO -- thumbnail for all selected
QPixmap thumbnail = QPixmap::grabWidget( this ).scaled(
128, 128,
Qt::KeepAspectRatio,
Qt::SmoothTransformation );
new StringPairDrag( QString( "tco_%1" ).arg(
m_tco->getTrack()->type() ),
dataFile.toString(), thumbnail, this );
}
}
if( me->modifiers() & Qt::ControlModifier )
{
delete m_hint;
m_hint = NULL;
}
const float ppt = m_trackView->trackContainerView()->pixelsPerTact();
if( m_action == Move )
{
const int x = mapToParent( me->pos() ).x() - m_initialMousePos.x();
MidiTime t = qMax( 0, (int)
m_trackView->trackContainerView()->currentPosition()+
static_cast<int>( x * MidiTime::ticksPerTact() /
ppt ) );
if( ! ( me->modifiers() & Qt::ControlModifier )
&& me->button() == Qt::NoButton )
{
t = t.toNearestTact();
}
m_tco->movePosition( t );
m_trackView->getTrackContentWidget()->changePosition();
s_textFloat->setText( QString( "%1:%2" ).
arg( m_tco->startPosition().getTact() + 1 ).
arg( m_tco->startPosition().getTicks() %
MidiTime::ticksPerTact() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2 ) );
}
else if( m_action == MoveSelection )
{
const int dx = me->x() - m_initialMousePos.x();
const bool snap = !(me->modifiers() & Qt::AltModifier) &&
me->button() == Qt::NoButton;
QVector<selectableObject *> so =
m_trackView->trackContainerView()->selectedObjects();
QVector<TrackContentObject *> tcos;
int smallestPos = 0;
MidiTime dtick = MidiTime( static_cast<int>( dx *
MidiTime::ticksPerTact() / ppt ) );
if( snap )
{
dtick = dtick.toNearestTact();
}
// find out smallest position of all selected objects for not
// moving an object before zero
for( QVector<selectableObject *>::iterator it = so.begin();
it != so.end(); ++it )
{
TrackContentObjectView * tcov =
dynamic_cast<TrackContentObjectView *>( *it );
if( tcov == NULL )
{
continue;
}
TrackContentObject * tco = tcov->m_tco;
tcos.push_back( tco );
smallestPos = qMin<int>( smallestPos,
(int)tco->startPosition() + dtick );
}
dtick -= smallestPos;
if( snap )
{
dtick = dtick.toAbsoluteTact(); // round toward 0
}
for( QVector<TrackContentObject *>::iterator it = tcos.begin();
it != tcos.end(); ++it )
{
( *it )->movePosition( ( *it )->startPosition() + dtick );
}
}
else if( m_action == Resize )
{
MidiTime t = qMax( MidiTime::ticksPerTact() / 16, static_cast<int>( me->x() * MidiTime::ticksPerTact() / ppt ) );
if( ! ( me->modifiers() & Qt::ControlModifier ) && me->button() == Qt::NoButton )
{
t = qMax<int>( MidiTime::ticksPerTact(), t.toNearestTact() );
}
m_tco->changeLength( t );
s_textFloat->setText( tr( "%1:%2 (%3:%4 to %5:%6)" ).
arg( m_tco->length().getTact() ).
arg( m_tco->length().getTicks() %
MidiTime::ticksPerTact() ).
arg( m_tco->startPosition().getTact() + 1 ).
arg( m_tco->startPosition().getTicks() %
MidiTime::ticksPerTact() ).
arg( m_tco->endPosition().getTact() + 1 ).
arg( m_tco->endPosition().getTicks() %
MidiTime::ticksPerTact() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2) );
}
else
{
if( me->x() > width() - RESIZE_GRIP_WIDTH && !me->buttons() && !m_tco->getAutoResize() )
{
if( QApplication::overrideCursor() != NULL &&
QApplication::overrideCursor()->shape() !=
Qt::SizeHorCursor )
{
while( QApplication::overrideCursor() != NULL )
{
QApplication::restoreOverrideCursor();
}
}
QCursor c( Qt::SizeHorCursor );
QApplication::setOverrideCursor( c );
}
else
{
leaveEvent( NULL );
}
}
}
/*! \brief Handle a mouse release on this trackContentObjectView.
*
* If we're in move or resize mode, journal the change as appropriate.
* Then tidy up.
*
* \param me The QMouseEvent to handle.
*/
void TrackContentObjectView::mouseReleaseEvent( QMouseEvent * me )
{
// If the CopySelection was chosen as the action due to mouse movement,
// it will have been cleared. At this point Toggle is the desired action.
// An active StringPairDrag will prevent this method from being called,
// so a real CopySelection would not have occurred.
if( m_action == CopySelection ||
( m_action == ToggleSelected && mouseMovedDistance( me, 2 ) == false ) )
{
setSelected( !isSelected() );
}
if( m_action == Move || m_action == Resize )
{
m_tco->setJournalling( true );
}
m_action = NoAction;
delete m_hint;
m_hint = NULL;
s_textFloat->hide();
leaveEvent( NULL );
selectableObject::mouseReleaseEvent( me );
}
/*! \brief Set up the context menu for this trackContentObjectView.
*
* Set up the various context menu events that can apply to a
* track content object view.
*
* \param cme The QContextMenuEvent to add the actions to.
*/
void TrackContentObjectView::contextMenuEvent( QContextMenuEvent * cme )
{
if( cme->modifiers() )
{
return;
}