diff --git a/src/LabelTrack.cpp b/src/LabelTrack.cpp index 6166f3bec..cd0a293ad 100644 --- a/src/LabelTrack.cpp +++ b/src/LabelTrack.cpp @@ -148,6 +148,15 @@ LabelTrack::LabelTrack(const LabelTrack &orig) : ResetFlags(); } +void LabelTrack::SetLabel( size_t iLabel, const LabelStruct &newLabel ) +{ + if( iLabel >= mLabels.size() ) { + wxASSERT( false ); + mLabels.resize( iLabel + 1 ); + } + mLabels[ iLabel ] = newLabel; +} + LabelTrack::~LabelTrack() { } @@ -937,6 +946,14 @@ void LabelTrack::Draw } } +void LabelTrack::SetSelectedIndex( int index ) +{ + if ( index >= 0 && index < mLabels.size() ) + mSelIndex = index; + else + mSelIndex = -1; +} + /// uses GetTextExtent to find the character position /// corresponding to the x pixel position. int LabelTrack::FindCurrentCursorPosition(int xPos) diff --git a/src/LabelTrack.h b/src/LabelTrack.h index ff1aa95ac..3d16b02d2 100644 --- a/src/LabelTrack.h +++ b/src/LabelTrack.h @@ -38,6 +38,7 @@ struct TrackPanelDrawingContext; class LabelStruct { public: + LabelStruct() = default; // Copies region LabelStruct(const SelectedRegion& region, const wxString &aTitle); // Copies region but then overwrites other times @@ -84,15 +85,15 @@ public: public: SelectedRegion selectedRegion; wxString title; /// Text of the label. - mutable int width; /// width of the text in pixels. + mutable int width{}; /// width of the text in pixels. // Working storage for on-screen layout. - mutable int x; /// Pixel position of left hand glyph - mutable int x1; /// Pixel position of right hand glyph - mutable int xText; /// Pixel position of left hand side of text box - mutable int y; /// Pixel position of label. + mutable int x{}; /// Pixel position of left hand glyph + mutable int x1{}; /// Pixel position of right hand glyph + mutable int xText{}; /// Pixel position of left hand side of text box + mutable int y{}; /// Pixel position of label. - bool updated; /// flag to tell if the label times were updated + bool updated{}; /// flag to tell if the label times were updated }; using LabelArray = std::vector; @@ -121,6 +122,8 @@ class AUDACITY_DLL_API LabelTrack final : public Track virtual ~ LabelTrack(); + void SetLabel( size_t iLabel, const LabelStruct &newLabel ); + void SetOffset(double dOffset) override; static const int DefaultFontSize = 12; @@ -131,6 +134,7 @@ class AUDACITY_DLL_API LabelTrack final : public Track void Draw( TrackPanelDrawingContext &context, const wxRect & r ) const; int GetSelectedIndex() const; + void SetSelectedIndex( int index ); double GetOffset() const override; double GetStartTime() const override; @@ -297,9 +301,6 @@ private: protected: std::shared_ptr DoGetView() override; std::shared_ptr DoGetControls() override; - - friend class GetInfoCommand; // to get labels. - friend class SetLabelCommand; // to set labels. }; #endif diff --git a/src/commands/GetInfoCommand.cpp b/src/commands/GetInfoCommand.cpp index c05aad4ea..36eaa5513 100644 --- a/src/commands/GetInfoCommand.cpp +++ b/src/commands/GetInfoCommand.cpp @@ -610,8 +610,7 @@ bool GetInfoCommand::SendLabels(const CommandContext &context) context.StartArray(); context.AddItem( (double)i ); // Track number. context.StartArray(); - for (int nn = 0; nn< (int)labelTrack->mLabels.size(); nn++) { - const auto &label = labelTrack->mLabels[nn]; + for ( const auto &label : labelTrack->GetLabels() ) { context.StartArray(); context.AddItem( label.getT0() ); // start context.AddItem( label.getT1() ); // end diff --git a/src/commands/SetLabelCommand.cpp b/src/commands/SetLabelCommand.cpp index 9641f9eb4..9267d1a6e 100644 --- a/src/commands/SetLabelCommand.cpp +++ b/src/commands/SetLabelCommand.cpp @@ -68,49 +68,50 @@ bool SetLabelCommand::Apply(const CommandContext & context) AudacityProject * p = &context.project; auto &tracks = TrackList::Get( *p ); auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion; - LabelStruct * pLabel = NULL; - int i=0; - int nn=0; - - LabelTrack *labelTrack {}; - for (auto lt : tracks.Any()) { - if( i > mLabelIndex ) - break; - labelTrack = lt; - for (nn = 0; - (nn< (int)labelTrack->mLabels.size()) && i<=mLabelIndex; - nn++) { - i++; - pLabel = &labelTrack->mLabels[nn]; + const LabelStruct * pLabel = nullptr; + LabelTrack *labelTrack = nullptr; + auto ii = mLabelIndex; + if ( mLabelIndex >= 0 ) { + for (auto lt : tracks.Any()) { + const auto &labels = lt->GetLabels(); + const auto nLabels = labels.size(); + if( ii >= nLabels ) + ii -= nLabels; + else { + labelTrack = lt; + pLabel = &labels[ ii ]; + break; + } } } - if ( (i< mLabelIndex) || (pLabel == NULL)) + if ( !pLabel ) { context.Error(wxT("LabelIndex was invalid.")); return false; } + auto newLabel = *pLabel; if( bHasText ) - pLabel->title = mText; + newLabel.title = mText; if( bHasT0 ) - pLabel->selectedRegion.setT0(mT0, false); + newLabel.selectedRegion.setT0(mT0, false); if( bHasT1 ) - pLabel->selectedRegion.setT1(mT1, false); + newLabel.selectedRegion.setT1(mT1, false); if( bHasT0 || bHasT1 ) - pLabel->selectedRegion.ensureOrdering(); - pLabel->updated = true; + newLabel.selectedRegion.ensureOrdering(); + labelTrack->SetLabel( ii, newLabel ); // Only one label can be selected. - if( bHasSelected ){ + if( bHasSelected ) { if( mbSelected ) { - labelTrack->mSelIndex = nn-1; + labelTrack->SetSelectedIndex( ii ); double t0 = pLabel->selectedRegion.t0(); double t1 = pLabel->selectedRegion.t1(); selectedRegion.setTimes( t0, t1); } - else if( labelTrack->mSelIndex == (nn-1) ) - labelTrack->mSelIndex = -1; + else if( labelTrack->GetSelectedIndex() == ii ) + labelTrack->SetSelectedIndex( -1 ); } labelTrack->SortLabels(); diff --git a/src/commands/SetLabelCommand.h b/src/commands/SetLabelCommand.h index 8f9cebdab..4ade1b35f 100644 --- a/src/commands/SetLabelCommand.h +++ b/src/commands/SetLabelCommand.h @@ -37,6 +37,8 @@ public: bool Apply(const CommandContext & context) override; public: + // zero-based index of the desired label, within the concatenation of the + // arrays of labels of all label tracks int mLabelIndex; wxString mText; double mT0;