Audacity  3.0.3
SetLabelCommand.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity - A Digital Audio Editor
4  Copyright 1999-2018 Audacity Team
5  License: wxwidgets
6 
7  James Crook
8 
9 ******************************************************************//*******************************************************************/
18 
19 
20 #include "SetLabelCommand.h"
21 
22 #include "LoadCommands.h"
23 #include "../ViewInfo.h"
24 #include "../WaveTrack.h"
25 #include "../LabelTrack.h"
26 #include "../ProjectHistory.h"
27 #include "../Shuttle.h"
28 #include "../ShuttleGui.h"
29 #include "CommandContext.h"
30 #include "../tracks/labeltrack/ui/LabelTrackView.h"
31 
33 { XO("Set Label") };
34 
36 
38 {
39 }
40 
41 
43  S.Define( mLabelIndex, wxT("Label"), 0, 0, 100 );
44  S.OptionalY( bHasText ).Define( mText, wxT("Text"), wxT("empty") );
45  S.OptionalY( bHasT0 ).Define( mT0, wxT("Start"), 0.0, 0.0, 100000.0);
46  S.OptionalY( bHasT1 ).Define( mT1, wxT("End"), 0.0, 0.0, 100000.0);
47  S.OptionalN( bHasSelected ).Define( mbSelected, wxT("Selected"), false );
48  return true;
49 };
50 
52 {
53  S.AddSpace(0, 5);
54 
55  S.StartMultiColumn(2, wxALIGN_CENTER);
56  {
57  S.TieNumericTextBox( XXO("Label Index"), mLabelIndex );
58  }
59  S.EndMultiColumn();
60  S.StartMultiColumn(3, wxALIGN_CENTER);
61  {
62  S.Optional( bHasText ).TieTextBox( XXO("Text:"), mText );
63  S.Optional( bHasT0 ).TieNumericTextBox( XXO("Start:"), mT0 );
64  S.Optional( bHasT1 ).TieNumericTextBox( XXO("End:"), mT1 );
65  S.Optional( bHasSelected ).TieCheckBox( XXO("Selected"), mbSelected );
66  }
67  S.EndMultiColumn();
68 }
69 
71 {
72  // \todo we have similar code for finding the nth Label, Clip, Track etc.
73  // this code could be put in subroutines/reduced.
74 
75  //wxString mode = GetString(wxT("Type"));
76  AudacityProject * p = &context.project;
77  auto &tracks = TrackList::Get( *p );
78  auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion;
79  const LabelStruct * pLabel = nullptr;
80  LabelTrack *labelTrack = nullptr;
81  auto ii = mLabelIndex;
82  if ( mLabelIndex >= 0 ) {
83  for (auto lt : tracks.Any<LabelTrack>()) {
84  const auto &labels = lt->GetLabels();
85  const auto nLabels = labels.size();
86  if( ii >= (int)nLabels )
87  ii -= nLabels;
88  else {
89  labelTrack = lt;
90  pLabel = &labels[ ii ];
91  break;
92  }
93  }
94  }
95 
96  if ( !pLabel )
97  {
98  context.Error(wxT("LabelIndex was invalid."));
99  return false;
100  }
101  auto newLabel = *pLabel;
102  if( bHasText )
103  newLabel.title = mText;
104  if( bHasT0 )
105  newLabel.selectedRegion.setT0(mT0, false);
106  if( bHasT1 )
107  newLabel.selectedRegion.setT1(mT1, false);
108  if( bHasT0 || bHasT1 )
109  newLabel.selectedRegion.ensureOrdering();
110  labelTrack->SetLabel( ii, newLabel );
111 
112  // Only one label can be selected.
113  if( bHasSelected ) {
114  auto &view = LabelTrackView::Get( *labelTrack );
115  if( mbSelected )
116  {
117  view.SetNavigationIndex( ii );
118  double t0 = pLabel->selectedRegion.t0();
119  double t1 = pLabel->selectedRegion.t1();
120  selectedRegion.setTimes( t0, t1);
121  }
122  else if( view.GetNavigationIndex( context.project ) == ii )
123  view.SetNavigationIndex( -1 );
124  }
125 
126  labelTrack->SortLabels();
127 
129  XO("Edited Label"), XO("Label"));
130 
131  return true;
132 }
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:156
SetLabelCommand::Symbol
static const ComponentInterfaceSymbol Symbol
Definition: SetLabelCommand.h:25
LoadCommands.h
SetLabelCommand.h
Declarations of SetLabelCommand and SetLabelCommandType classes.
SetLabelCommand::DefineParams
bool DefineParams(ShuttleParams &S) override
Definition: SetLabelCommand.cpp:42
SetLabelCommand::Apply
bool Apply(const CommandContext &context) override
Definition: SetLabelCommand.cpp:70
ShuttleGuiBase::TieCheckBox
wxCheckBox * TieCheckBox(const TranslatableString &Prompt, bool &Var)
Definition: ShuttleGui.cpp:1629
ShuttleGui::AddSpace
wxSizerItem * AddSpace(int width, int height, int prop=0)
Definition: ShuttleGui.cpp:2447
XO
#define XO(s)
Definition: Internat.h:31
ShuttleParams
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:62
LabelTrack
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:88
anonymous_namespace{SetLabelCommand.cpp}::reg
BuiltinCommandsModule::Registration< SetLabelCommand > reg
Definition: SetLabelCommand.cpp:35
ShuttleGuiBase::EndMultiColumn
void EndMultiColumn()
Definition: ShuttleGui.cpp:1238
LabelStruct
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:30
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
SetLabelCommand::bHasSelected
bool bHasSelected
Definition: SetLabelCommand.h:52
ShuttleParams::OptionalN
virtual ShuttleParams & OptionalN(bool &var)
Definition: Shuttle.h:72
ShuttleGuiBase::TieTextBox
wxTextCtrl * TieTextBox(const TranslatableString &Caption, wxString &Value, const int nChars=0)
Definition: ShuttleGui.cpp:1652
BuiltinCommandsModule::Registration
Definition: LoadCommands.h:41
CommandContext.h
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:200
XXO
#define XXO(s)
Definition: Internat.h:44
CommandContext
CommandContext provides additional information to an 'Apply()' command. It provides the project,...
Definition: CommandContext.h:22
ShuttleGuiBase::StartMultiColumn
void StartMultiColumn(int nCols, int PositionFlags=wxALIGN_LEFT)
Definition: ShuttleGui.cpp:1229
SetLabelCommand::mLabelIndex
int mLabelIndex
Definition: SetLabelCommand.h:42
SetLabelCommand::bHasText
bool bHasText
Definition: SetLabelCommand.h:49
LabelTrackView::Get
static LabelTrackView & Get(LabelTrack &)
Definition: LabelTrackView.cpp:158
ShuttleParams::OptionalY
virtual ShuttleParams & OptionalY(bool &var)
Definition: Shuttle.h:71
CommandContext::Error
virtual void Error(const wxString &message) const
Definition: CommandContext.cpp:73
ShuttleGuiBase::TieNumericTextBox
wxTextCtrl * TieNumericTextBox(const TranslatableString &Prompt, int &Value, const int nChars=0)
Definition: ShuttleGui.cpp:1673
SetLabelCommand::bHasT1
bool bHasT1
Definition: SetLabelCommand.h:51
SetLabelCommand::bHasT0
bool bHasT0
Definition: SetLabelCommand.h:50
SetLabelCommand::mT0
double mT0
Definition: SetLabelCommand.h:44
SetLabelCommand::mT1
double mT1
Definition: SetLabelCommand.h:45
ProjectHistory::PushState
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
Definition: ProjectHistory.cpp:90
TrackList::Get
static TrackList & Get(AudacityProject &project)
Definition: Track.cpp:495
AudacityProject
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:113
SetLabelCommand::mText
wxString mText
Definition: SetLabelCommand.h:43
ShuttleParams::Define
virtual void Define(bool &var, const wxChar *key, const bool vdefault, const bool vmin=false, const bool vmax=false, const bool vscl=false)
Definition: Shuttle.cpp:332
ShuttleGui::Optional
ShuttleGui & Optional(bool &bVar)
Definition: ShuttleGui.cpp:2280
CommandContext::project
AudacityProject & project
Definition: CommandContext.h:52
SetLabelCommand::PopulateOrExchange
void PopulateOrExchange(ShuttleGui &S) override
Definition: SetLabelCommand.cpp:51
ProjectHistory::Get
static ProjectHistory & Get(AudacityProject &project)
Definition: ProjectHistory.cpp:26
SetLabelCommand::SetLabelCommand
SetLabelCommand()
Definition: SetLabelCommand.cpp:37
ShuttleGui
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:631
SetLabelCommand::mbSelected
bool mbSelected
Definition: SetLabelCommand.h:46