From e2bb606341ca14c3c77b1970e0dca68dab3cce1d Mon Sep 17 00:00:00 2001 From: IanCaio Date: Sat, 7 Nov 2020 09:54:04 -0300 Subject: [PATCH] Fixes createTCO method on some classes (#5699) * Fixes createTCO method on some classes Classes that inherit from "Track", also inherit the createTCO method. That method takes a MidiTime position as an argument, but except on the class SampleTrack that argument was ignored. That lead to unnecessary calls to TCO->movePosition after creating a TCO in many parts of the codebase (making the argument completely redundant) and even to a bug on the BBEditor, caused by a call to createTCO that expected the position to be set on the constructor (see issue #5673). That PR adds code to move the TCO to the appropriate position inside the constructor of the classes that didn't have it, fixes the code style on the SampleTrack createTCO method and removes the now unneeded calls to movePosition from source files on src/ and plugins/. On Track::loadSettings there was a call to saveJournallingState(false) followed immediately by restoreJournallingState() which was deleted because it's redundant (probably a left over from copying the code from pasteSelection?). * Fix code style issues Fixes code style issues on some files (except for ones where the current statements already had a different code style. In those the used code style was kept for consistency). * Fixes more code style issues * Fixes code style issues on the parameter name Fixes code style issue on the parameter name of createTCO, where _pos was supposed to be just pos. The existing code had the old style and I ended up replicating it on the other methods. * Code style fixes Fixes code style in the changed lines. * Fixes bug with dragging to negative positions There was a bug (already present before this PR) where dragging a selection before MidiTime 0 would result in some TCOs being placed on negative positions. This PR fixes this bug by applying the following changes: 1) TrackContentObject::movePosition now moves the TCO to positions equal or above 0 only. 2) Because of the previous change, I removed the line that calculated the max value between 0 and the new position on TrackContentObjectView::mouseMoveEvent when dragging a single TCO (and added a line updating the value to the real new position of the TCO so the label displays the position correctly). 3) Unrelated to this bug, but removed an unnecessary call to TrackContentWidget::changePosition on the left resize of sample TCOs because it will already be called when movePosition triggers the positionChanged signal. 4) Added some logic to the TrackContentWidget::pasteSelection method to find the left most TCO being pasted and make sure that the offset is corrected so it doesn't end up on a negative position (similar to the logic for the MoveSelection action). 5) Removed another line that calculated the max between 0 and the new position on Track::removeBar since it's now safe to call movePosition with negative values. * Uses std::max instead of a conditional statement As suggested by Spekular, we use std::max instead of a conditional statement to correct the value of offset if it positions a TCO on a negative position. --- include/AutomationTrack.h | 2 +- include/BBTrack.h | 2 +- include/InstrumentTrack.h | 2 +- include/SampleTrack.h | 2 +- plugins/HydrogenImport/HydrogenImport.cpp | 4 +-- plugins/MidiImport/MidiImport.cpp | 6 ++-- src/core/Track.cpp | 39 +++++++++++++---------- src/tracks/AutomationTrack.cpp | 7 ++-- src/tracks/BBTrack.cpp | 5 +-- src/tracks/InstrumentTrack.cpp | 6 ++-- 10 files changed, 40 insertions(+), 35 deletions(-) diff --git a/include/AutomationTrack.h b/include/AutomationTrack.h index 92a50dd04..c8a0009e9 100644 --- a/include/AutomationTrack.h +++ b/include/AutomationTrack.h @@ -46,7 +46,7 @@ public: } TrackView * createView( TrackContainerView* ) override; - TrackContentObject * createTCO( const MidiTime & _pos ) override; + TrackContentObject* createTCO(const MidiTime & pos) override; virtual void saveTrackSpecificSettings( QDomDocument & _doc, QDomElement & _parent ) override; diff --git a/include/BBTrack.h b/include/BBTrack.h index 36a108845..5f9cfe408 100644 --- a/include/BBTrack.h +++ b/include/BBTrack.h @@ -106,7 +106,7 @@ public: virtual bool play( const MidiTime & _start, const fpp_t _frames, const f_cnt_t _frame_base, int _tco_num = -1 ) override; TrackView * createView( TrackContainerView* tcv ) override; - TrackContentObject * createTCO( const MidiTime & _pos ) override; + TrackContentObject* createTCO(const MidiTime & pos) override; virtual void saveTrackSpecificSettings( QDomDocument & _doc, QDomElement & _parent ) override; diff --git a/include/InstrumentTrack.h b/include/InstrumentTrack.h index 29348b98e..fa1d1692e 100644 --- a/include/InstrumentTrack.h +++ b/include/InstrumentTrack.h @@ -136,7 +136,7 @@ public: TrackView * createView( TrackContainerView* tcv ) override; // create new track-content-object = pattern - TrackContentObject * createTCO( const MidiTime & _pos ) override; + TrackContentObject* createTCO(const MidiTime & pos) override; // called by track diff --git a/include/SampleTrack.h b/include/SampleTrack.h index 47cc0df39..eda1299a5 100644 --- a/include/SampleTrack.h +++ b/include/SampleTrack.h @@ -140,7 +140,7 @@ public: virtual bool play( const MidiTime & _start, const fpp_t _frames, const f_cnt_t _frame_base, int _tco_num = -1 ) override; TrackView * createView( TrackContainerView* tcv ) override; - TrackContentObject * createTCO( const MidiTime & _pos ) override; + TrackContentObject* createTCO(const MidiTime & pos) override; virtual void saveTrackSpecificSettings( QDomDocument & _doc, diff --git a/plugins/HydrogenImport/HydrogenImport.cpp b/plugins/HydrogenImport/HydrogenImport.cpp index 4a69bc451..c39c52416 100644 --- a/plugins/HydrogenImport/HydrogenImport.cpp +++ b/plugins/HydrogenImport/HydrogenImport.cpp @@ -314,10 +314,8 @@ bool HydrogenImport::readSong() int i = pattern_id[patId]+song_num_tracks; Track *t = ( BBTrack * ) s->tracks().at( i ); - TrackContentObject *tco = t->createTCO( pos ); - tco->movePosition( pos ); + t->createTCO(pos); - if ( pattern_length[patId] > best_length ) { best_length = pattern_length[patId]; diff --git a/plugins/MidiImport/MidiImport.cpp b/plugins/MidiImport/MidiImport.cpp index 8b2aa6117..a93e1e41e 100644 --- a/plugins/MidiImport/MidiImport.cpp +++ b/plugins/MidiImport/MidiImport.cpp @@ -193,8 +193,7 @@ public: { MidiTime pPos = MidiTime( time.getBar(), 0 ); ap = dynamic_cast( - at->createTCO(0) ); - ap->movePosition( pPos ); + at->createTCO(pPos)); ap->addObject( objModel ); } @@ -287,8 +286,7 @@ public: if (!newPattern || n->pos() > lastEnd + DefaultTicksPerBar) { MidiTime pPos = MidiTime(n->pos().getBar(), 0); - newPattern = dynamic_cast(it->createTCO(0)); - newPattern->movePosition(pPos); + newPattern = dynamic_cast(it->createTCO(pPos)); } lastEnd = n->pos() + n->length(); diff --git a/src/core/Track.cpp b/src/core/Track.cpp index 70da1e1d7..d73bdbed7 100644 --- a/src/core/Track.cpp +++ b/src/core/Track.cpp @@ -151,10 +151,11 @@ TrackContentObject::~TrackContentObject() */ void TrackContentObject::movePosition( const MidiTime & pos ) { - if( m_startPosition != pos ) + MidiTime newPos = qMax(0, pos.getTicks()); + if (m_startPosition != newPos) { Engine::mixer()->requestChangeInModel(); - m_startPosition = pos; + m_startPosition = newPos; Engine::mixer()->doneChangeInModel(); Engine::getSong()->updateLength(); emit positionChanged(); @@ -955,9 +956,8 @@ void TrackContentObjectView::mouseMoveEvent( QMouseEvent * me ) { MidiTime newPos = draggedTCOPos( me ); - // Don't go left of bar zero - newPos = max( 0, newPos.getTicks() ); - m_tco->movePosition( newPos ); + m_tco->movePosition(newPos); + newPos = m_tco->startPosition(); // Get the real position the TCO was dragged to for the label m_trackView->getTrackContentWidget()->changePosition(); s_textFloat->setText( QString( "%1:%2" ). arg( newPos.getBar() + 1 ). @@ -1073,7 +1073,6 @@ void TrackContentObjectView::mouseMoveEvent( QMouseEvent * me ) if( m_tco->length() + ( oldPos - t ) >= 1 ) { m_tco->movePosition( t ); - m_trackView->getTrackContentWidget()->changePosition(); m_tco->changeLength( m_tco->length() + ( oldPos - t ) ); sTco->setStartTimeOffset( sTco->startTimeOffset() + ( oldPos - t ) ); } @@ -1896,6 +1895,20 @@ bool TrackContentWidget::pasteSelection( MidiTime tcoPos, const QMimeData * md, // The offset is quantized (rather than the positions) to preserve fine adjustments offset = offset.quantize(snapSize); + // Get the leftmost TCO and fix the offset if it reaches below bar 0 + MidiTime leftmostPos = grabbedTCOPos; + for(int i = 0; i < tcoNodes.length(); ++i) + { + QDomElement outerTCOElement = tcoNodes.item(i).toElement(); + QDomElement tcoElement = outerTCOElement.firstChildElement(); + + MidiTime pos = tcoElement.attributeNode("pos").value().toInt(); + + if(pos < leftmostPos) { leftmostPos = pos; } + } + // Fix offset if it sets the left most TCO to a negative position + offset = std::max(offset.getTicks(), -leftmostPos.getTicks()); + for( int i = 0; icreateTCO( pos ); tco->restoreState( tcoElement ); - tco->movePosition( pos ); + tco->movePosition(pos); // Because we restored the state, we need to move the TCO again. if( wasSelection ) { tco->selectViewOnCreate( true ); @@ -1967,11 +1980,7 @@ void TrackContentWidget::mousePressEvent( QMouseEvent * me ) getTrack()->addJournalCheckPoint(); const MidiTime pos = getPosition( me->x() ).getBar() * MidiTime::ticksPerBar(); - TrackContentObject * tco = getTrack()->createTCO( pos ); - - tco->saveJournallingState( false ); - tco->movePosition( pos ); - tco->restoreJournallingState(); + getTrack()->createTCO(pos); } } @@ -2697,8 +2706,6 @@ void Track::loadSettings( const QDomElement & element ) TrackContentObject * tco = createTCO( MidiTime( 0 ) ); tco->restoreState( node.toElement() ); - saveJournallingState( false ); - restoreJournallingState(); } } node = node.nextSibling(); @@ -2886,7 +2893,6 @@ void Track::createTCOsForBB( int bb ) { MidiTime position = MidiTime( numOfTCOs(), 0 ); TrackContentObject * tco = createTCO( position ); - tco->movePosition( position ); tco->changeLength( MidiTime( 1, 0 ) ); } } @@ -2932,8 +2938,7 @@ void Track::removeBar( const MidiTime & pos ) { if( ( *it )->startPosition() >= pos ) { - ( *it )->movePosition( qMax( ( *it )->startPosition() - - MidiTime::ticksPerBar(), 0 ) ); + (*it)->movePosition((*it)->startPosition() - MidiTime::ticksPerBar()); } } } diff --git a/src/tracks/AutomationTrack.cpp b/src/tracks/AutomationTrack.cpp index 430f54a56..fe3622921 100644 --- a/src/tracks/AutomationTrack.cpp +++ b/src/tracks/AutomationTrack.cpp @@ -57,9 +57,11 @@ TrackView * AutomationTrack::createView( TrackContainerView* tcv ) -TrackContentObject * AutomationTrack::createTCO( const MidiTime & ) +TrackContentObject* AutomationTrack::createTCO(const MidiTime & pos) { - return new AutomationPattern( this ); + AutomationPattern* p = new AutomationPattern(this); + p->movePosition(pos); + return p; } @@ -133,7 +135,6 @@ void AutomationTrackView::dropEvent( QDropEvent * _de ) TrackContentObject * tco = getTrack()->createTCO( pos ); AutomationPattern * pat = dynamic_cast( tco ); pat->addObject( mod ); - pat->movePosition( pos ); } } diff --git a/src/tracks/BBTrack.cpp b/src/tracks/BBTrack.cpp index 2bd0fdaf5..5ec03db30 100644 --- a/src/tracks/BBTrack.cpp +++ b/src/tracks/BBTrack.cpp @@ -382,9 +382,10 @@ TrackView * BBTrack::createView( TrackContainerView* tcv ) -TrackContentObject * BBTrack::createTCO( const MidiTime & _pos ) +TrackContentObject* BBTrack::createTCO(const MidiTime & pos) { - BBTCO * bbtco = new BBTCO( this ); + BBTCO* bbtco = new BBTCO(this); + bbtco->movePosition(pos); return bbtco; } diff --git a/src/tracks/InstrumentTrack.cpp b/src/tracks/InstrumentTrack.cpp index 5a51bb8ac..0f3f7b31e 100644 --- a/src/tracks/InstrumentTrack.cpp +++ b/src/tracks/InstrumentTrack.cpp @@ -713,9 +713,11 @@ bool InstrumentTrack::play( const MidiTime & _start, const fpp_t _frames, -TrackContentObject * InstrumentTrack::createTCO( const MidiTime & ) +TrackContentObject* InstrumentTrack::createTCO(const MidiTime & pos) { - return new Pattern( this ); + Pattern* p = new Pattern(this); + p->movePosition(pos); + return p; }