1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-09 16:41:14 +02:00

Do without some friend declarations in LabelTrack...

... and leave "updated" alone, it's only for use in interactive dragging
This commit is contained in:
Paul Licameli 2018-11-08 16:53:45 -05:00
parent 0750f62e88
commit 8ff5a4b345
5 changed files with 55 additions and 35 deletions

View File

@ -148,6 +148,15 @@ LabelTrack::LabelTrack(const LabelTrack &orig) :
ResetFlags(); 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() 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 /// uses GetTextExtent to find the character position
/// corresponding to the x pixel position. /// corresponding to the x pixel position.
int LabelTrack::FindCurrentCursorPosition(int xPos) int LabelTrack::FindCurrentCursorPosition(int xPos)

View File

@ -38,6 +38,7 @@ struct TrackPanelDrawingContext;
class LabelStruct class LabelStruct
{ {
public: public:
LabelStruct() = default;
// Copies region // Copies region
LabelStruct(const SelectedRegion& region, const wxString &aTitle); LabelStruct(const SelectedRegion& region, const wxString &aTitle);
// Copies region but then overwrites other times // Copies region but then overwrites other times
@ -84,15 +85,15 @@ public:
public: public:
SelectedRegion selectedRegion; SelectedRegion selectedRegion;
wxString title; /// Text of the label. 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. // Working storage for on-screen layout.
mutable int x; /// Pixel position of left hand glyph mutable int x{}; /// Pixel position of left hand glyph
mutable int x1; /// Pixel position of right 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 xText{}; /// Pixel position of left hand side of text box
mutable int y; /// Pixel position of label. 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<LabelStruct>; using LabelArray = std::vector<LabelStruct>;
@ -121,6 +122,8 @@ class AUDACITY_DLL_API LabelTrack final : public Track
virtual ~ LabelTrack(); virtual ~ LabelTrack();
void SetLabel( size_t iLabel, const LabelStruct &newLabel );
void SetOffset(double dOffset) override; void SetOffset(double dOffset) override;
static const int DefaultFontSize = 12; 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; void Draw( TrackPanelDrawingContext &context, const wxRect & r ) const;
int GetSelectedIndex() const; int GetSelectedIndex() const;
void SetSelectedIndex( int index );
double GetOffset() const override; double GetOffset() const override;
double GetStartTime() const override; double GetStartTime() const override;
@ -297,9 +301,6 @@ private:
protected: protected:
std::shared_ptr<TrackView> DoGetView() override; std::shared_ptr<TrackView> DoGetView() override;
std::shared_ptr<TrackControls> DoGetControls() override; std::shared_ptr<TrackControls> DoGetControls() override;
friend class GetInfoCommand; // to get labels.
friend class SetLabelCommand; // to set labels.
}; };
#endif #endif

View File

@ -610,8 +610,7 @@ bool GetInfoCommand::SendLabels(const CommandContext &context)
context.StartArray(); context.StartArray();
context.AddItem( (double)i ); // Track number. context.AddItem( (double)i ); // Track number.
context.StartArray(); context.StartArray();
for (int nn = 0; nn< (int)labelTrack->mLabels.size(); nn++) { for ( const auto &label : labelTrack->GetLabels() ) {
const auto &label = labelTrack->mLabels[nn];
context.StartArray(); context.StartArray();
context.AddItem( label.getT0() ); // start context.AddItem( label.getT0() ); // start
context.AddItem( label.getT1() ); // end context.AddItem( label.getT1() ); // end

View File

@ -68,49 +68,50 @@ bool SetLabelCommand::Apply(const CommandContext & context)
AudacityProject * p = &context.project; AudacityProject * p = &context.project;
auto &tracks = TrackList::Get( *p ); auto &tracks = TrackList::Get( *p );
auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion; auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion;
LabelStruct * pLabel = NULL; const LabelStruct * pLabel = nullptr;
int i=0; LabelTrack *labelTrack = nullptr;
int nn=0; auto ii = mLabelIndex;
if ( mLabelIndex >= 0 ) {
LabelTrack *labelTrack {};
for (auto lt : tracks.Any<LabelTrack>()) { for (auto lt : tracks.Any<LabelTrack>()) {
if( i > mLabelIndex ) const auto &labels = lt->GetLabels();
break; const auto nLabels = labels.size();
if( ii >= nLabels )
ii -= nLabels;
else {
labelTrack = lt; labelTrack = lt;
for (nn = 0; pLabel = &labels[ ii ];
(nn< (int)labelTrack->mLabels.size()) && i<=mLabelIndex; break;
nn++) { }
i++;
pLabel = &labelTrack->mLabels[nn];
} }
} }
if ( (i< mLabelIndex) || (pLabel == NULL)) if ( !pLabel )
{ {
context.Error(wxT("LabelIndex was invalid.")); context.Error(wxT("LabelIndex was invalid."));
return false; return false;
} }
auto newLabel = *pLabel;
if( bHasText ) if( bHasText )
pLabel->title = mText; newLabel.title = mText;
if( bHasT0 ) if( bHasT0 )
pLabel->selectedRegion.setT0(mT0, false); newLabel.selectedRegion.setT0(mT0, false);
if( bHasT1 ) if( bHasT1 )
pLabel->selectedRegion.setT1(mT1, false); newLabel.selectedRegion.setT1(mT1, false);
if( bHasT0 || bHasT1 ) if( bHasT0 || bHasT1 )
pLabel->selectedRegion.ensureOrdering(); newLabel.selectedRegion.ensureOrdering();
pLabel->updated = true; labelTrack->SetLabel( ii, newLabel );
// Only one label can be selected. // Only one label can be selected.
if( bHasSelected ) { if( bHasSelected ) {
if( mbSelected ) if( mbSelected )
{ {
labelTrack->mSelIndex = nn-1; labelTrack->SetSelectedIndex( ii );
double t0 = pLabel->selectedRegion.t0(); double t0 = pLabel->selectedRegion.t0();
double t1 = pLabel->selectedRegion.t1(); double t1 = pLabel->selectedRegion.t1();
selectedRegion.setTimes( t0, t1); selectedRegion.setTimes( t0, t1);
} }
else if( labelTrack->mSelIndex == (nn-1) ) else if( labelTrack->GetSelectedIndex() == ii )
labelTrack->mSelIndex = -1; labelTrack->SetSelectedIndex( -1 );
} }
labelTrack->SortLabels(); labelTrack->SortLabels();

View File

@ -37,6 +37,8 @@ public:
bool Apply(const CommandContext & context) override; bool Apply(const CommandContext & context) override;
public: public:
// zero-based index of the desired label, within the concatenation of the
// arrays of labels of all label tracks
int mLabelIndex; int mLabelIndex;
wxString mText; wxString mText;
double mT0; double mT0;