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 "../Shuttle.h"
27 #include "../ShuttleGui.h"
28 #include "CommandContext.h"
29 #include "../tracks/labeltrack/ui/LabelTrackView.h"
30 
32 { XO("Set Label") };
33 
35 
37 {
38 }
39 
40 
42  S.Define( mLabelIndex, wxT("Label"), 0, 0, 100 );
43  S.OptionalY( bHasText ).Define( mText, wxT("Text"), wxT("empty") );
44  S.OptionalY( bHasT0 ).Define( mT0, wxT("Start"), 0.0, 0.0, 100000.0);
45  S.OptionalY( bHasT1 ).Define( mT1, wxT("End"), 0.0, 0.0, 100000.0);
46  S.OptionalN( bHasSelected ).Define( mbSelected, wxT("Selected"), false );
47  return true;
48 };
49 
51 {
52  S.AddSpace(0, 5);
53 
54  S.StartMultiColumn(2, wxALIGN_CENTER);
55  {
56  S.TieNumericTextBox( XXO("Label Index"), mLabelIndex );
57  }
58  S.EndMultiColumn();
59  S.StartMultiColumn(3, wxALIGN_CENTER);
60  {
61  S.Optional( bHasText ).TieTextBox( XXO("Text:"), mText );
62  S.Optional( bHasT0 ).TieNumericTextBox( XXO("Start:"), mT0 );
63  S.Optional( bHasT1 ).TieNumericTextBox( XXO("End:"), mT1 );
64  S.Optional( bHasSelected ).TieCheckBox( XXO("Selected"), mbSelected );
65  }
66  S.EndMultiColumn();
67 }
68 
70 {
71  // \todo we have similar code for finding the nth Label, Clip, Track etc.
72  // this code could be put in subroutines/reduced.
73 
74  //wxString mode = GetString(wxT("Type"));
75  AudacityProject * p = &context.project;
76  auto &tracks = TrackList::Get( *p );
77  auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion;
78  const LabelStruct * pLabel = nullptr;
79  LabelTrack *labelTrack = nullptr;
80  auto ii = mLabelIndex;
81  if ( mLabelIndex >= 0 ) {
82  for (auto lt : tracks.Any<LabelTrack>()) {
83  const auto &labels = lt->GetLabels();
84  const auto nLabels = labels.size();
85  if( ii >= (int)nLabels )
86  ii -= nLabels;
87  else {
88  labelTrack = lt;
89  pLabel = &labels[ ii ];
90  break;
91  }
92  }
93  }
94 
95  if ( !pLabel )
96  {
97  context.Error(wxT("LabelIndex was invalid."));
98  return false;
99  }
100  auto newLabel = *pLabel;
101  if( bHasText )
102  newLabel.title = mText;
103  if( bHasT0 )
104  newLabel.selectedRegion.setT0(mT0, false);
105  if( bHasT1 )
106  newLabel.selectedRegion.setT1(mT1, false);
107  if( bHasT0 || bHasT1 )
108  newLabel.selectedRegion.ensureOrdering();
109  labelTrack->SetLabel( ii, newLabel );
110 
111  // Only one label can be selected.
112  if( bHasSelected ) {
113  auto &view = LabelTrackView::Get( *labelTrack );
114  if( mbSelected )
115  {
116  view.SetSelectedIndex( ii );
117  double t0 = pLabel->selectedRegion.t0();
118  double t1 = pLabel->selectedRegion.t1();
119  selectedRegion.setTimes( t0, t1);
120  }
121  else if( view.GetSelectedIndex( context.project ) == ii )
122  view.SetSelectedIndex( -1 );
123  }
124 
125  labelTrack->SortLabels();
126  return true;
127 }
ViewInfo::Get
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:162
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:41
SetLabelCommand::Apply
bool Apply(const CommandContext &context) override
Definition: SetLabelCommand.cpp:69
ShuttleGuiBase::TieCheckBox
wxCheckBox * TieCheckBox(const TranslatableString &Prompt, bool &Var)
Definition: ShuttleGui.cpp:1603
ShuttleGui::AddSpace
wxSizerItem * AddSpace(int width, int height, int prop=0)
Definition: ShuttleGui.cpp:2421
XO
#define XO(s)
Definition: Internat.h:32
ShuttleParams
Shuttle that deals with parameters. This is a base class with lots of virtual functions that do nothi...
Definition: Shuttle.h:61
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:34
ShuttleGuiBase::EndMultiColumn
void EndMultiColumn()
Definition: ShuttleGui.cpp:1212
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: ComponentInterface.h:60
SetLabelCommand::bHasSelected
bool bHasSelected
Definition: SetLabelCommand.h:52
ShuttleParams::OptionalN
virtual ShuttleParams & OptionalN(bool &var)
Definition: Shuttle.h:71
ShuttleGuiBase::TieTextBox
wxTextCtrl * TieTextBox(const TranslatableString &Caption, wxString &Value, const int nChars=0)
Definition: ShuttleGui.cpp:1626
BuiltinCommandsModule::Registration
Definition: LoadCommands.h:41
CommandContext.h
ViewInfo::selectedRegion
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:199
XXO
#define XXO(s)
Definition: Internat.h:45
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:1203
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:70
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:1647
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
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:112
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:2254
CommandContext::project
AudacityProject & project
Definition: CommandContext.h:52
SetLabelCommand::PopulateOrExchange
void PopulateOrExchange(ShuttleGui &S) override
Definition: SetLabelCommand.cpp:50
SetLabelCommand::SetLabelCommand
SetLabelCommand()
Definition: SetLabelCommand.cpp:36
ShuttleGui
Derived from ShuttleGuiBase, an Audacity specific class for shuttling data to and from GUI.
Definition: ShuttleGui.h:638
SetLabelCommand::mbSelected
bool mbSelected
Definition: SetLabelCommand.h:46