-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClipView.cpp
1515 lines (1294 loc) · 43.5 KB
/
ClipView.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
/*
* ClipView.cpp - implementation of ClipView class
*
* 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.
*
*/
#include "ClipView.h"
#include <set>
#include <cassert>
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include "AutomationClip.h"
#include "Clipboard.h"
#include "ColorChooser.h"
#include "ComboBoxModel.h"
#include "DataFile.h"
#include "Engine.h"
#include "embed.h"
#include "GuiApplication.h"
#include "InstrumentTrack.h"
#include "InstrumentTrackView.h"
#include "MidiClip.h"
#include "MidiClipView.h"
#include "Note.h"
#include "PatternClip.h"
#include "PatternStore.h"
#include "SampleClip.h"
#include "Song.h"
#include "SongEditor.h"
#include "StringPairDrag.h"
#include "TextFloat.h"
#include "TrackContainer.h"
#include "TrackContainerView.h"
#include "TrackView.h"
namespace lmms::gui
{
/*! 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 * ClipView::s_textFloat = nullptr;
/*! \brief Create a new ClipView
*
* Creates a new clip view for the given clip in the given track view.
*
* \param _clip The clip to be displayed
* \param _tv The track view that will contain the new object
*/
ClipView::ClipView( Clip * clip,
TrackView * tv ) :
selectableObject( tv->getTrackContentWidget() ),
ModelView( nullptr, this ),
m_trackView( tv ),
m_initialClipPos( TimePos(0) ),
m_initialClipEnd( TimePos(0) ),
m_clip( clip ),
m_action( Action::None ),
m_initialMousePos( QPoint( 0, 0 ) ),
m_initialMouseGlobalPos( QPoint( 0, 0 ) ),
m_initialOffsets( QVector<TimePos>() ),
m_hint( nullptr ),
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_patternClipBackground( 0, 0, 0 ),
m_gradient( true ),
m_mouseHotspotHand( 0, 0 ),
m_mouseHotspotKnife( 0, 0 ),
m_cursorHand( QCursor( embed::getIconPixmap( "hand" ) ) ),
m_cursorKnife( QCursor( embed::getIconPixmap( "cursor_knife" ) ) ),
m_cursorSetYet( false ),
m_needsUpdate( true )
{
if( s_textFloat == nullptr )
{
s_textFloat = new TextFloat;
s_textFloat->setPixmap( embed::getIconPixmap( "clock" ) );
}
setAttribute( Qt::WA_OpaquePaintEvent, true );
setAttribute( Qt::WA_DeleteOnClose, true );
setFocusPolicy( Qt::StrongFocus );
setCursor( m_cursorHand );
move( 0, 0 );
show();
setFixedHeight( tv->getTrackContentWidget()->height() - 1);
setAcceptDrops( true );
setMouseTracking( true );
connect( m_clip, SIGNAL(lengthChanged()),
this, SLOT(updateLength()));
connect(getGUI()->songEditor()->m_editor, &SongEditor::pixelsPerBarChanged, this, &ClipView::updateLength);
connect( m_clip, SIGNAL(positionChanged()),
this, SLOT(updatePosition()));
connect( m_clip, SIGNAL(destroyedClip()), this, SLOT(close()));
setModel( m_clip );
connect(m_clip, SIGNAL(colorChanged()), this, SLOT(update()));
connect(m_trackView->getTrack(), &Track::colorChanged, this, [this]
{
// redraw if clip uses track color
if (!m_clip->color().has_value()) { update(); }
});
m_trackView->getTrackContentWidget()->addClipView( this );
updateLength();
updatePosition();
}
/*! \brief Destroy a ClipView
*
* Destroys the given ClipView.
*
*/
ClipView::~ClipView()
{
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 ClipView
*
* Clip's get drawn only when needed,
* and when a Clip is updated,
* it needs to be redrawn.
*
*/
void ClipView::update()
{
if( !m_cursorSetYet )
{
m_cursorHand = QCursor( embed::getIconPixmap( "hand" ), m_mouseHotspotHand.width(), m_mouseHotspotHand.height() );
m_cursorKnife = QCursor( embed::getIconPixmap( "cursor_knife" ), m_mouseHotspotKnife.width(), m_mouseHotspotKnife.height() );
setCursor( m_cursorHand );
m_cursorSetYet = true;
}
if( fixedClips() )
{
updateLength();
}
m_needsUpdate = true;
selectableObject::update();
}
/*! \brief Does this ClipView have a fixed Clip?
*
* Returns whether the containing trackView has fixed
* Clips.
*
* \todo In what circumstance are they fixed?
*/
bool ClipView::fixedClips()
{
return m_trackView->trackContainerView()->fixedClips();
}
// qproperty access functions, to be inherited & used by Clipviews
//! \brief CSS theming qproperty access method
QColor ClipView::mutedColor() const
{ return m_mutedColor; }
QColor ClipView::mutedBackgroundColor() const
{ return m_mutedBackgroundColor; }
QColor ClipView::selectedColor() const
{ return m_selectedColor; }
QColor ClipView::textColor() const
{ return m_textColor; }
QColor ClipView::textBackgroundColor() const
{
return m_textBackgroundColor;
}
QColor ClipView::textShadowColor() const
{ return m_textShadowColor; }
QColor ClipView::patternClipBackground() const
{ return m_patternClipBackground; }
bool ClipView::gradient() const
{ return m_gradient; }
//! \brief CSS theming qproperty access method
void ClipView::setMutedColor( const QColor & c )
{ m_mutedColor = QColor( c ); }
void ClipView::setMutedBackgroundColor( const QColor & c )
{ m_mutedBackgroundColor = QColor( c ); }
void ClipView::setSelectedColor( const QColor & c )
{ m_selectedColor = QColor( c ); }
void ClipView::setTextColor( const QColor & c )
{ m_textColor = QColor( c ); }
void ClipView::setTextBackgroundColor( const QColor & c )
{
m_textBackgroundColor = c;
}
void ClipView::setTextShadowColor( const QColor & c )
{ m_textShadowColor = QColor( c ); }
void ClipView::setPatternClipBackground( const QColor & c )
{ m_patternClipBackground = QColor( c ); }
void ClipView::setGradient( const bool & b )
{ m_gradient = b; }
// access needsUpdate member variable
bool ClipView::needsUpdate()
{ return m_needsUpdate; }
void ClipView::setNeedsUpdate( bool b )
{ m_needsUpdate = b; }
/*! \brief Close a ClipView
*
* Closes a ClipView 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 ClipView::close()
{
m_trackView->getTrackContentWidget()->removeClipView( this );
return QWidget::close();
}
/*! \brief Removes a ClipView from its track view.
*
* Like the close() method, this asks the track view to remove this
* ClipView. However, the clip is
* scheduled for later deletion rather than closed immediately.
*
*/
void ClipView::remove()
{
m_trackView->getTrack()->addJournalCheckPoint();
// delete ourself
close();
if (m_clip->getTrack())
{
auto guard = Engine::audioEngine()->requestChangesGuard();
m_clip->getTrack()->removeClip(m_clip);
}
// TODO: Clip::~Clip should not be responsible for removing the Clip from the Track.
// One would expect that a call to Track::removeClip would already do that for you, as well
// as actually deleting the Clip with the deleteLater function. That being said, it shouldn't
// be possible to make a Clip without a Track (i.e., Clip::getTrack is never nullptr).
m_clip->deleteLater();
}
/*! \brief Updates a ClipView's length
*
* If this ClipView has a fixed Clip, then we must
* keep the width of our parent. Otherwise, calculate our width from
* the clip's length in pixels adding in the border.
*
*/
void ClipView::updateLength()
{
if( fixedClips() )
{
setFixedWidth( parentWidget()->width() );
}
else
{
// this std::max function is needed for clips that do not start or end on the beat, otherwise, they "disappear" when zooming to min
// 3 is the minimun width needed to make a clip visible
setFixedWidth(std::max(static_cast<int>(m_clip->length() * pixelsPerBar() / TimePos::ticksPerBar() + 1), 3));
}
m_trackView->trackContainerView()->update();
}
/*! \brief Updates a ClipView'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 ClipView::updatePosition()
{
m_trackView->getTrackContentWidget()->changePosition();
// moving a Clip can result in change of song-length etc.,
// therefore we update the track-container
m_trackView->trackContainerView()->update();
}
void ClipView::selectColor()
{
// Get a color from the user
const auto newColor = ColorChooser{this}
.withPalette(ColorChooser::Palette::Track)
->getColor(m_clip->color().value_or(palette().window().color()));
if (newColor.isValid()) { setColor(newColor); }
}
void ClipView::randomizeColor()
{
setColor(ColorChooser::getPalette(ColorChooser::Palette::Mixer)[std::rand() % 48]);
}
void ClipView::resetColor()
{
setColor(std::nullopt);
}
/*! \brief Change color of all selected clips
*
* \param color The new color.
*/
void ClipView::setColor(const std::optional<QColor>& color)
{
std::set<Track*> journaledTracks;
auto selectedClips = getClickedClips();
for (auto clipv : selectedClips)
{
auto clip = clipv->getClip();
auto track = clip->getTrack();
// TODO journal whole Song or group of clips instead of one journal entry for each track
// If only one clip changed, store that in the journal
if (selectedClips.length() == 1)
{
clip->addJournalCheckPoint();
}
// If multiple clips changed, store whole Track in the journal
// Check if track has been journaled already by trying to add it to the set
else if (journaledTracks.insert(track).second)
{
track->addJournalCheckPoint();
}
clip->setColor(color);
clipv->update();
}
Engine::getSong()->setModified();
}
/*! \brief Change the ClipView'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 ClipView::dragEnterEvent( QDragEnterEvent * dee )
{
TrackContentWidget * tcw = getTrackView()->getTrackContentWidget();
TimePos clipPos{m_clip->startPosition()};
if( tcw->canPasteSelection( clipPos, dee ) == false )
{
dee->ignore();
}
else
{
StringPairDrag::processDragEnterEvent( dee, "clip_" +
QString::number( static_cast<int>(m_clip->getTrack()->type()) ) );
}
}
/*! \brief Handle something being dropped on this ClipObjectView.
*
* When something has been dropped on this ClipView, and
* it's a clip, then use an instance of our dataFile reader
* to take the xml of the clip and turn it into something
* we can write over our current state.
*
* \param de The QDropEvent to handle.
*/
void ClipView::dropEvent( QDropEvent * de )
{
QString type = StringPairDrag::decodeKey( de );
QString value = StringPairDrag::decodeValue( de );
// Track must be the same type to paste into
if( type != ( "clip_" + QString::number( static_cast<int>(m_clip->getTrack()->type()) ) ) )
{
return;
}
// Defer to rubberband paste if we're in that mode
if( m_trackView->trackContainerView()->allowRubberband() == true )
{
TrackContentWidget * tcw = getTrackView()->getTrackContentWidget();
TimePos clipPos{m_clip->startPosition()};
if( tcw->pasteSelection( clipPos, de ) == true )
{
de->accept();
}
return;
}
// Don't allow pasting a clip into itself.
QObject* qwSource = de->source();
if( qwSource != nullptr &&
dynamic_cast<ClipView *>( qwSource ) == this )
{
return;
}
// Copy state into existing clip
DataFile dataFile( value.toUtf8() );
TimePos pos = m_clip->startPosition();
QDomElement clips = dataFile.content().firstChildElement("clips");
m_clip->restoreState( clips.firstChildElement().firstChildElement() );
m_clip->movePosition( pos );
AutomationClip::resolveAllIDs();
de->accept();
}
/* @brief Chooses the correct cursor to be displayed on the widget
*
* @param me The QMouseEvent that is triggering the cursor change
*/
void ClipView::updateCursor(QMouseEvent * me)
{
auto sClip = dynamic_cast<SampleClip*>(m_clip);
auto pClip = dynamic_cast<PatternClip*>(m_clip);
// If we are at the edges, use the resize cursor
if (!me->buttons() && !m_clip->getAutoResize() && !isSelected()
&& ((me->x() > width() - RESIZE_GRIP_WIDTH) || (me->x() < RESIZE_GRIP_WIDTH && (sClip || pClip))))
{
setCursor(Qt::SizeHorCursor);
}
// If we are in the middle on knife mode, use the knife cursor
else if (sClip && m_trackView->trackContainerView()->knifeMode() && !isSelected())
{
setCursor(m_cursorKnife);
}
// If we are in the middle in any other mode, use the hand cursor
else { setCursor(m_cursorHand); }
}
/*! \brief Create a DataFile suitable for copying multiple clips.
*
* Clips in the vector are written to the "clips" node in the
* DataFile. The ClipView'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 Clips.
*
* \param clips The trackContectObjects to save in a DataFile
*/
DataFile ClipView::createClipDataFiles(
const QVector<ClipView *> & clipViews) const
{
Track * t = m_trackView->getTrack();
TrackContainer * tc = t->trackContainer();
DataFile dataFile( DataFile::Type::DragNDropData );
QDomElement clipParent = dataFile.createElement("clips");
for (const auto& clipView : clipViews)
{
// Insert into the dom under the "clips" element
Track* clipTrack = clipView->m_trackView->getTrack();
int trackIndex = std::distance(tc->tracks().begin(), std::find(tc->tracks().begin(), tc->tracks().end(), clipTrack));
assert(trackIndex != tc->tracks().size());
QDomElement clipElement = dataFile.createElement("clip");
clipElement.setAttribute( "trackIndex", trackIndex );
clipElement.setAttribute( "trackType", static_cast<int>(clipTrack->type()) );
clipElement.setAttribute( "trackName", clipTrack->name() );
clipView->m_clip->saveState(dataFile, clipElement);
clipParent.appendChild( clipElement );
}
dataFile.content().appendChild( clipParent );
// Add extra metadata needed for calculations later
const auto initialTrackIt = std::find(tc->tracks().begin(), tc->tracks().end(), t);
if (initialTrackIt == tc->tracks().end())
{
printf("Failed to find selected track in the TrackContainer.\n");
return dataFile;
}
const int initialTrackIndex = std::distance(tc->tracks().begin(), initialTrackIt);
QDomElement metadata = dataFile.createElement( "copyMetadata" );
// initialTrackIndex is the index of the track that was touched
metadata.setAttribute( "initialTrackIndex", initialTrackIndex );
metadata.setAttribute( "trackContainerId", tc->id() );
// grabbedClipPos is the pos of the bar containing the Clip we grabbed
metadata.setAttribute( "grabbedClipPos", m_clip->startPosition() );
dataFile.content().appendChild( metadata );
return dataFile;
}
void ClipView::paintTextLabel(QString const & text, QPainter & painter)
{
if (text.trimmed() == "")
{
return;
}
painter.setRenderHint( QPainter::TextAntialiasing );
QFont labelFont = this->font();
labelFont.setHintingPreference( QFont::PreferFullHinting );
painter.setFont( labelFont );
const int textTop = BORDER_WIDTH + 1;
const int textLeft = BORDER_WIDTH + 3;
QFontMetrics fontMetrics(labelFont);
QString elidedClipName = fontMetrics.elidedText(text, Qt::ElideMiddle, width() - 2 * textLeft);
if (elidedClipName.length() < 2)
{
elidedClipName = text.trimmed();
}
painter.fillRect(QRect(0, 0, width(), fontMetrics.height() + 2 * textTop), textBackgroundColor());
int const finalTextTop = textTop + fontMetrics.ascent();
painter.setPen(textShadowColor());
painter.drawText( textLeft + 1, finalTextTop + 1, elidedClipName );
painter.setPen( textColor() );
painter.drawText( textLeft, finalTextTop, elidedClipName );
}
/*! \brief Handle a mouse press on this ClipView.
*
* Handles the various ways in which a ClipView 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 clip
* * or if middle button, maybe delete the clip.
*
* \param me The QMouseEvent to handle.
*/
void ClipView::mousePressEvent( QMouseEvent * me )
{
// Right now, active is only used on right/mid clicks actions, so we use a ternary operator
// to avoid the overhead of calling getClickedClips when it's not used
auto active = me->button() == Qt::LeftButton
? QVector<ClipView *>()
: getClickedClips();
setInitialPos( me->pos() );
setInitialOffsets();
if( !fixedClips() && me->button() == Qt::LeftButton )
{
auto sClip = dynamic_cast<SampleClip*>(m_clip);
auto pClip = dynamic_cast<PatternClip*>(m_clip);
const bool knifeMode = m_trackView->trackContainerView()->knifeMode();
if ( me->modifiers() & Qt::ControlModifier && !(sClip && knifeMode) )
{
if( isSelected() )
{
m_action = Action::CopySelection;
}
else
{
m_action = Action::ToggleSelected;
}
}
else
{
if( isSelected() )
{
m_action = Action::MoveSelection;
}
else
{
getGUI()->songEditor()->m_editor->selectAllClips( false );
m_clip->addJournalCheckPoint();
// Action::Move, Action::Resize and Action::ResizeLeft
// Action::Split action doesn't disable Clip journalling
if (m_action == Action::Move || m_action == Action::Resize || m_action == Action::ResizeLeft)
{
m_clip->setJournalling(false);
}
setInitialPos( me->pos() );
setInitialOffsets();
if( m_clip->getAutoResize() )
{ // Always move clips that can't be manually resized
m_action = Action::Move;
setCursor( Qt::SizeAllCursor );
}
else if( me->x() >= width() - RESIZE_GRIP_WIDTH )
{
m_action = Action::Resize;
setCursor( Qt::SizeHorCursor );
}
else if( me->x() < RESIZE_GRIP_WIDTH && (sClip || pClip) )
{
m_action = Action::ResizeLeft;
setCursor( Qt::SizeHorCursor );
}
else if( sClip && knifeMode )
{
m_action = Action::Split;
setCursor( m_cursorKnife );
setMarkerPos( knifeMarkerPos( me ) );
setMarkerEnabled( true );
update();
}
else
{
m_action = Action::Move;
setCursor( Qt::SizeAllCursor );
}
if( m_action == Action::Move )
{
s_textFloat->setTitle( tr( "Current position" ) );
s_textFloat->setText( QString( "%1:%2" ).
arg( m_clip->startPosition().getBar() + 1 ).
arg( m_clip->startPosition().getTicks() %
TimePos::ticksPerBar() ) );
}
else if( m_action == Action::Resize || m_action == Action::ResizeLeft )
{
s_textFloat->setTitle( tr( "Current length" ) );
s_textFloat->setText( tr( "%1:%2 (%3:%4 to %5:%6)" ).
arg( m_clip->length().getBar() ).
arg( m_clip->length().getTicks() %
TimePos::ticksPerBar() ).
arg( m_clip->startPosition().getBar() + 1 ).
arg( m_clip->startPosition().getTicks() %
TimePos::ticksPerBar() ).
arg( m_clip->endPosition().getBar() + 1 ).
arg( m_clip->endPosition().getTicks() %
TimePos::ticksPerBar() ) );
}
// s_textFloat->reparent( this );
// setup text-float as if Clip was already moved/resized
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2) );
if ( m_action != Action::Split) { s_textFloat->show(); }
}
delete m_hint;
QString hint = m_action == Action::Move || m_action == Action::MoveSelection
? tr( "Press <%1> and drag to make a copy." )
: tr( "Press <%1> for free resizing." );
m_hint = TextFloat::displayMessage( tr( "Hint" ), hint.arg(UI_CTRL_KEY),
embed::getIconPixmap( "hint" ), 0 );
}
}
else if( me->button() == Qt::RightButton )
{
if( me->modifiers() & Qt::ControlModifier )
{
toggleMute( active );
}
else if( me->modifiers() & Qt::ShiftModifier && !fixedClips() )
{
remove( active );
}
if (m_action == Action::Split)
{
m_action = Action::None;
auto sClip = dynamic_cast<SampleClip*>(m_clip);
if (sClip)
{
setMarkerEnabled( false );
update();
}
}
}
else if( me->button() == Qt::MiddleButton )
{
if( me->modifiers() & Qt::ControlModifier )
{
toggleMute( active );
}
else if( !fixedClips() )
{
remove( active );
}
}
}
/*! \brief Handle a mouse movement (drag) on this ClipView.
*
* Handles the various ways in which a ClipView 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 ClipView::mouseMoveEvent( QMouseEvent * me )
{
if( m_action == Action::CopySelection || m_action == Action::ToggleSelected )
{
if( mouseMovedDistance( me, 2 ) == true )
{
QVector<ClipView *> clipViews;
if( m_action == Action::CopySelection )
{
// Collect all selected Clips
QVector<selectableObject *> so =
m_trackView->trackContainerView()->selectedObjects();
for (const auto& selectedClip : so)
{
auto clipv = dynamic_cast<ClipView*>(selectedClip);
if( clipv != nullptr )
{
clipViews.push_back( clipv );
}
}
}
else
{
getGUI()->songEditor()->m_editor->selectAllClips( false );
clipViews.push_back( this );
}
// Clear the action here because mouseReleaseEvent will not get
// triggered once we go into drag.
m_action = Action::None;
// Write the Clips to the DataFile for copying
DataFile dataFile = createClipDataFiles( clipViews );
// TODO -- thumbnail for all selected
QPixmap thumbnail = grab().scaled(
128, 128,
Qt::KeepAspectRatio,
Qt::SmoothTransformation );
new StringPairDrag( QString( "clip_%1" ).arg(
static_cast<int>(m_clip->getTrack()->type()) ),
dataFile.toString(), thumbnail, this );
}
}
if( me->modifiers() & Qt::ControlModifier )
{
delete m_hint;
m_hint = nullptr;
}
const float ppb = m_trackView->trackContainerView()->pixelsPerBar();
if( m_action == Action::Move )
{
TimePos newPos = draggedClipPos( me );
m_clip->movePosition(newPos);
newPos = m_clip->startPosition(); // Get the real position the Clip was dragged to for the label
m_trackView->getTrackContentWidget()->changePosition();
s_textFloat->setText( QString( "%1:%2" ).
arg( newPos.getBar() + 1 ).
arg( newPos.getTicks() %
TimePos::ticksPerBar() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2 ) );
}
else if( m_action == Action::MoveSelection )
{
// 1: Find the position we want to move the grabbed Clip to
TimePos newPos = draggedClipPos( me );
// 2: Handle moving the other selected Clips the same distance
QVector<selectableObject *> so =
m_trackView->trackContainerView()->selectedObjects();
QVector<Clip *> clips; // List of selected clips
int leftmost = 0; // Leftmost clip's offset from grabbed clip
// Populate clips, find leftmost
for( QVector<selectableObject *>::iterator it = so.begin();
it != so.end(); ++it )
{
auto clipv = dynamic_cast<ClipView*>(*it);
if( clipv == nullptr ) { continue; }
clips.push_back( clipv->m_clip );
int index = std::distance( so.begin(), it );
leftmost = std::min(leftmost, m_initialOffsets[index].getTicks());
}
// Make sure the leftmost clip doesn't get moved to a negative position
if ( newPos.getTicks() + leftmost < 0 ) { newPos = -leftmost; }
for( QVector<Clip *>::iterator it = clips.begin();
it != clips.end(); ++it )
{
int index = std::distance( clips.begin(), it );
( *it )->movePosition( newPos + m_initialOffsets[index] );
}
}
else if( m_action == Action::Resize || m_action == Action::ResizeLeft )
{
const float snapSize = getGUI()->songEditor()->m_editor->getSnapSize();
// Length in ticks of one snap increment
const TimePos snapLength = TimePos( (int)(snapSize * TimePos::ticksPerBar()) );
if( m_action == Action::Resize )
{
// The clip's new length
TimePos l = static_cast<int>( me->x() * TimePos::ticksPerBar() / ppb );
// If the user is holding alt, or pressed ctrl after beginning the drag, don't quantize
if ( unquantizedModHeld(me) )
{ // We want to preserve this adjusted offset,
// even if the user switches to snapping later
setInitialPos( m_initialMousePos );
// Don't resize to less than 1 tick
m_clip->changeLength( qMax<int>( 1, l ) );
}
else if ( me->modifiers() & Qt::ShiftModifier )
{ // If shift is held, quantize clip's end position
TimePos end = TimePos( m_initialClipPos + l ).quantize( snapSize );
// The end position has to be after the clip's start
TimePos min = m_initialClipPos.quantize( snapSize );
if ( min <= m_initialClipPos ) min += snapLength;
m_clip->changeLength( qMax<int>(min - m_initialClipPos, end - m_initialClipPos) );
}
else
{ // Otherwise, resize in fixed increments
TimePos initialLength = m_initialClipEnd - m_initialClipPos;
TimePos offset = TimePos( l - initialLength ).quantize( snapSize );
// Don't resize to less than 1 tick
auto min = TimePos(initialLength % snapLength);
if (min < 1) min += snapLength;
m_clip->changeLength( qMax<int>( min, initialLength + offset) );
}
}
else
{
auto sClip = dynamic_cast<SampleClip*>(m_clip);
auto pClip = dynamic_cast<PatternClip*>(m_clip);
if( sClip || pClip )
{
const int x = mapToParent( me->pos() ).x() - m_initialMousePos.x();
TimePos t = qMax( 0, (int)
m_trackView->trackContainerView()->currentPosition() +
static_cast<int>( x * TimePos::ticksPerBar() / ppb ) );
if( unquantizedModHeld(me) )
{ // We want to preserve this adjusted offset,
// even if the user switches to snapping later
setInitialPos( m_initialMousePos );
//Don't resize to less than 1 tick
t = qMin<int>( m_initialClipEnd - 1, t);
}
else if( me->modifiers() & Qt::ShiftModifier )
{ // If shift is held, quantize clip's start position
// Don't let the start position move past the end position
TimePos max = m_initialClipEnd.quantize( snapSize );
if ( max >= m_initialClipEnd ) max -= snapLength;
t = qMin<int>( max, t.quantize( snapSize ) );
}
else
{ // Otherwise, resize in fixed increments
// Don't resize to less than 1 tick
TimePos initialLength = m_initialClipEnd - m_initialClipPos;
auto minLength = TimePos(initialLength % snapLength);
if (minLength < 1) minLength += snapLength;
TimePos offset = TimePos(t - m_initialClipPos).quantize( snapSize );
t = qMin<int>( m_initialClipEnd - minLength, m_initialClipPos + offset );
}
TimePos positionOffset = m_clip->startPosition() - t;
if (m_clip->length() + positionOffset >= 1)
{
m_clip->movePosition(t);
m_clip->changeLength(m_clip->length() + positionOffset);
if (sClip)
{
sClip->setStartTimeOffset(sClip->startTimeOffset() + positionOffset);
}
else if (pClip)
{
// Modulus the start time offset as we need it only for offsets
// inside the pattern length. This is done to prevent a value overflow.
// The start time offset may still become larger than the pattern length
// whenever the pattern length decreases without a clip resize following.
// To deal safely with it, always modulus before use.
tick_t patternLength = Engine::patternStore()->lengthOfPattern(pClip->patternIndex())
* TimePos::ticksPerBar();
TimePos position = (pClip->startTimeOffset() + positionOffset) % patternLength;
pClip->setStartTimeOffset(position);
}
}
}
}
s_textFloat->setText( tr( "%1:%2 (%3:%4 to %5:%6)" ).
arg( m_clip->length().getBar() ).
arg( m_clip->length().getTicks() %
TimePos::ticksPerBar() ).
arg( m_clip->startPosition().getBar() + 1 ).
arg( m_clip->startPosition().getTicks() %
TimePos::ticksPerBar() ).
arg( m_clip->endPosition().getBar() + 1 ).
arg( m_clip->endPosition().getTicks() %
TimePos::ticksPerBar() ) );
s_textFloat->moveGlobal( this, QPoint( width() + 2, height() + 2) );
}
else if( m_action == Action::Split )
{
auto sClip = dynamic_cast<SampleClip*>(m_clip);
if (sClip) {
setCursor( m_cursorKnife );
setMarkerPos( knifeMarkerPos( me ) );
}
update();
}
// None of the actions above, we will just handle the cursor
else { updateCursor(me); }
}
/*! \brief Handle a mouse release on this ClipView.