Audacity  2.2.2
LabelTrack.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  LabelTrack.h
6 
7  Dominic Mazzoni
8  James Crook
9  Jun Wan
10 
11 **********************************************************************/
12 
13 #ifndef _LABELTRACK_
14 #define _LABELTRACK_
15 
16 #include "SelectedRegion.h"
17 #include "Track.h"
18 
19 #include <wx/brush.h>
20 #include <wx/event.h>
21 #include <wx/font.h>
22 #include <wx/pen.h>
23 #include <wx/string.h>
24 #include <wx/clipbrd.h>
25 
26 
27 class wxKeyEvent;
28 class wxMouseEvent;
29 class wxTextFile;
30 class wxWindow;
31 class wxIcon;
32 class wxBitmap;
33 class TrackList;
34 
35 class AudacityProject;
36 class DirManager;
37 class TimeWarper;
38 class ZoomInfo;
39 
40 
41 struct LabelTrackHit;
43 
45 {
46 public:
47  // Copies region
48  LabelStruct(const SelectedRegion& region, const wxString &aTitle);
49  // Copies region but then overwrites other times
50  LabelStruct(const SelectedRegion& region, double t0, double t1,
51  const wxString &aTitle);
52  void DrawLines( wxDC & dc, const wxRect & r) const;
53  void DrawGlyphs
54  ( wxDC & dc, const wxRect & r, int GlyphLeft, int GlyphRight) const;
55  void DrawText( wxDC & dc, const wxRect & r) const;
56  void DrawTextBox( wxDC & dc, const wxRect & r) const;
57  void DrawHighlight( wxDC & dc, int xPos1, int xPos2, int charHeight) const;
58  void getXPos( wxDC & dc, int * xPos1, int cursorPos) const;
59  const SelectedRegion &getSelectedRegion() const { return selectedRegion; }
60  double getDuration() const { return selectedRegion.duration(); }
61  double getT0() const { return selectedRegion.t0(); }
62  double getT1() const { return selectedRegion.t1(); }
63  // Returns true iff the label got inverted:
64  bool AdjustEdge( int iEdge, double fNewTime);
65  void MoveLabel( int iEdge, double fNewTime);
66 
67  struct BadFormatException {};
68  static LabelStruct Import(wxTextFile &file, int &index);
69 
70  void Export(wxTextFile &file) const;
71 
74  {
81  };
82 
87  TimeRelations RegionRelation(double reg_t0, double reg_t1,
88  const LabelTrack *parent = NULL) const;
89 
90 public:
92  wxString title;
93  mutable int width;
94 
95 // Working storage for on-screen layout.
96  mutable int x;
97  mutable int x1;
98  mutable int xText;
99  mutable int y;
100 
101  bool updated;
102 };
103 
104 using LabelArray = std::vector<LabelStruct>;
105 
106 const int NUM_GLYPH_CONFIGS = 3;
107 const int NUM_GLYPH_HIGHLIGHTS = 4;
108 const int MAX_NUM_ROWS =80;
109 
110 class LabelGlyphHandle;
111 class LabelTextHandle;
112 
113 class AUDACITY_DLL_API LabelTrack final : public Track
114 {
115  friend class LabelStruct;
116 
117  public:
118  bool IsGoodLabelFirstKey(const wxKeyEvent & evt);
119  bool IsGoodLabelEditKey(const wxKeyEvent & evt);
120  bool IsTextSelected();
121 
122  void CreateCustomGlyphs();
123  LabelTrack(const std::shared_ptr<DirManager> &projDirManager);
124  LabelTrack(const LabelTrack &orig);
125 
126  virtual ~ LabelTrack();
127 
128  std::vector<UIHandlePtr> DetailedHitTest
129  (const TrackPanelMouseState &state,
130  const AudacityProject *pProject, int currentTool, bool bMultiTool)
131  override;
132 
133  bool DoCaptureKey(wxKeyEvent &event);
134  unsigned CaptureKey
135  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
136 
137  unsigned KeyDown
138  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
139 
140  unsigned Char
141  (wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent) override;
142 
143  void SetOffset(double dOffset) override;
144 
145  static const int DefaultFontSize = 12;
146 
147  static wxFont GetFont(const wxString &faceName, int size = DefaultFontSize);
148  static void ResetFont();
149 
150  void Draw(TrackPanelDrawingContext &context, const wxRect & r,
152  const ZoomInfo &zoomInfo) const;
153 
154  int getSelectedIndex() const { return mSelIndex; }
155 
156  int GetKind() const override { return Label; }
157 
158  double GetOffset() const override;
159  double GetStartTime() const override;
160  double GetEndTime() const override;
161 
162  using Holder = std::unique_ptr<LabelTrack>;
163  Track::Holder Duplicate() const override;
164 
165  void SetSelected(bool s) override;
166 
167  bool HandleXMLTag(const wxChar *tag, const wxChar **attrs) override;
168  XMLTagHandler *HandleXMLChild(const wxChar *tag) override;
169  void WriteXML(XMLWriter &xmlFile) const override;
170 
171 #if LEGACY_PROJECT_FILE_SUPPORT
172  bool Load(wxTextFile * in, DirManager * dirManager) override;
173  bool Save(wxTextFile * out, bool overwrite) override;
174 #endif
175 
176  Track::Holder Cut (double t0, double t1) override;
177  Track::Holder Copy (double t0, double t1, bool forClipboard = true) const override;
178  void Clear(double t0, double t1) override;
179  void Paste(double t, const Track * src) override;
180  bool Repeat(double t0, double t1, int n);
181 
182  void Silence(double t0, double t1) override;
183  void InsertSilence(double t, double len) override;
184  void OverGlyph(LabelTrackHit &hit, int x, int y) const;
185  static wxBitmap & GetGlyph( int i);
186 
187 
188  struct Flags {
189  int mInitialCursorPos, mCurrentCursorPos, mSelIndex;
190  bool mRightDragging, mDrawCursor;
191  };
192  void ResetFlags();
193  Flags SaveFlags() const
194  {
195  return {
196  mInitialCursorPos, mCurrentCursorPos, mSelIndex,
197  mRightDragging, mDrawCursor
198  };
199  }
200  void RestoreFlags( const Flags& flags );
201 
202  int OverATextBox(int xx, int yy) const;
203  bool OverTextBox(const LabelStruct *pLabel, int x, int y) const;
204  bool CutSelectedText();
205  bool CopySelectedText();
206  bool PasteSelectedText(double sel0, double sel1);
207  static bool IsTextClipSupported();
208 
209  void HandleGlyphClick
210  (LabelTrackHit &hit,
211  const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
212  SelectedRegion *newSel);
213  void HandleTextClick
214  (const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
215  SelectedRegion *newSel);
216  bool HandleGlyphDragRelease
217  (LabelTrackHit &hit,
218  const wxMouseEvent & evt, wxRect & r, const ZoomInfo &zoomInfo,
219  SelectedRegion *newSel);
220  void HandleTextDragRelease(const wxMouseEvent & evt);
221 
222  bool OnKeyDown(SelectedRegion &sel, wxKeyEvent & event);
223  bool OnChar(SelectedRegion &sel, wxKeyEvent & event);
224 
225  void Import(wxTextFile & f);
226  void Export(wxTextFile & f) const;
227 
228  void Unselect();
229 
230  bool IsSelected() const;
231 
232  int GetNumLabels() const;
233  const LabelStruct *GetLabel(int index) const;
234 
235  //This returns the index of the label we just added.
236  int AddLabel(const SelectedRegion &region, const wxString &title = wxT(""),
237  int restoreFocus = -1);
238  //And this tells us the index, if there is a label already there.
239  int GetLabelIndex(double t, double t1);
240 
241  //This deletes the label at given index.
242  void DeleteLabel(int index);
243 
244  //get current cursor position,
245  // relative to the left edge of the track panel
246  bool CalcCursorX(int * x) const;
247 
248  void CalcHighlightXs(int *x1, int *x2) const;
249 
250  void MayAdjustLabel
251  ( LabelTrackHit &hit,
252  int iLabel, int iEdge, bool bAllowSwapping, double fNewTime);
253  void MayMoveLabel( int iLabel, int iEdge, double fNewTime);
254 
255  // This pastes labels without shifting existing ones
256  bool PasteOver(double t, const Track *src);
257 
258  // PRL: These functions were not used because they were not overrides! Was that right?
259  //Track::Holder SplitCut(double b, double e) /* not override */;
260  //bool SplitDelete(double b, double e) /* not override */;
261 
262  void ShiftLabelsOnInsert(double length, double pt);
263  void ChangeLabelsOnReverse(double b, double e);
264  void ScaleLabels(double b, double e, double change);
265  double AdjustTimeStampOnScale(double t, double b, double e, double change);
266  void WarpLabels(const TimeWarper &warper);
267 
268  // Returns tab-separated text of all labels completely within given region
269  wxString GetTextOfLabels(double t0, double t1) const;
270 
271  int FindNextLabel(const SelectedRegion& currentSelection);
272  int FindPrevLabel(const SelectedRegion& currentSelection);
273 
274  public:
275  void SortLabels(LabelTrackHit *pHit = nullptr);
276  private:
277  void ShowContextMenu();
278  void OnContextMenu(wxCommandEvent & evt);
279 
280  int mSelIndex;
283 
284  static int mIconHeight;
285  static int mIconWidth;
286  static int mTextHeight;
287  static bool mbGlyphsReady;
288  static wxBitmap mBoundaryGlyphs[NUM_GLYPH_CONFIGS * NUM_GLYPH_HIGHLIGHTS];
289 
290  static int mFontHeight;
293 
295  bool mDrawCursor;
296  int mRestoreFocus;
298 
300  // Set in copied label tracks
301  double mClipLen;
302 
303  int miLastLabel; // used by FindNextLabel and FindPrevLabel
304 
305  void ComputeLayout(const wxRect & r, const ZoomInfo &zoomInfo) const;
306  void ComputeTextPosition(const wxRect & r, int index) const;
307 
308 public:
309  int FindCurrentCursorPosition(int xPos);
310  void SetCurrentCursorPosition(int xPos);
311 
312 private:
313  void calculateFontHeight(wxDC & dc) const;
314  void RemoveSelectedText();
315 
316  static wxFont msFont;
317 
318  std::weak_ptr<LabelGlyphHandle> mGlyphHandle;
319  std::weak_ptr<LabelTextHandle> mTextHandle;
320 
321 protected:
322  std::shared_ptr<TrackControls> GetControls() override;
323  std::shared_ptr<TrackVRulerControls> GetVRulerControls() override;
324  friend class GetInfoCommand; // to get labels.
325  friend class SetLabelCommand; // to set labels.
326 };
327 
328 #endif
static int mIconHeight
Definition: LabelTrack.h:284
void getXPos(wxDC &dc, int *xPos1, int cursorPos) const
Definition: LabelTrack.cpp:711
virtual void Paste(double WXUNUSED(t), const Track *WXUNUSED(src))=0
A list of TrackListNode items.
Definition: Track.h:618
int mCurrentCursorPos
Definition: LabelTrack.h:291
std::unique_ptr< LabelTrack > Holder
Definition: LabelTrack.h:162
Creates and manages BlockFile objects.
Definition: DirManager.h:54
virtual Holder Copy(double WXUNUSED(t0), double WXUNUSED(t1), bool forClipboard=true) const =0
double t0() const
ViewInfo is used mainly to hold the zooming, selection and scroll information. It also has some statu...
Definition: ViewInfo.h:141
virtual double GetOffset() const =0
int mxMouseDisplacement
Keeps track of the currently selected label.
Definition: LabelTrack.h:281
virtual std::shared_ptr< TrackVRulerControls > GetVRulerControls()=0
TimeRelations RegionRelation(double reg_t0, double reg_t1, const LabelTrack *parent=NULL) const
virtual unsigned Char(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)
virtual void SetOffset(double o)
Definition: Track.h:289
virtual XMLTagHandler * HandleXMLChild(const wxChar *tag)=0
virtual double GetEndTime() const =0
wxString title
Definition: LabelTrack.h:92
int mInitialCursorPos
current cursor position
Definition: LabelTrack.h:292
int xText
Pixel position of right hand glyph.
Definition: LabelTrack.h:98
bool updated
Pixel position of label.
Definition: LabelTrack.h:101
const int MAX_NUM_ROWS
Definition: LabelTrack.h:108
virtual Holder Cut(double WXUNUSED(t0), double WXUNUSED(t1))=0
Main class to control the export function.
TimeRelations
Relationships between selection region and labels.
Definition: LabelTrack.h:73
virtual double GetStartTime() const =0
void DrawHighlight(wxDC &dc, int xPos1, int xPos2, int charHeight) const
Draws text-selected region within the label.
Definition: LabelTrack.cpp:699
double t1() const
int x1
Pixel position of left hand glyph.
Definition: LabelTrack.h:97
double getDuration() const
Definition: LabelTrack.h:60
int miLastLabel
Definition: LabelTrack.h:303
void Export(wxTextFile &file) const
static int mIconWidth
Definition: LabelTrack.h:285
LabelArray mLabels
Displacement of mouse cursor from the centre being dragged.
Definition: LabelTrack.h:282
Command that sets label information.
virtual unsigned CaptureKey(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)
virtual void SetSelected(bool s)
Definition: Track.cpp:99
std::weak_ptr< LabelTextHandle > mTextHandle
Definition: LabelTrack.h:319
A LabelTrack is a Track that holds labels (LabelStruct).
Definition: LabelTrack.h:113
virtual void WriteXML(XMLWriter &xmlFile) const =0
std::vector< LabelStruct > LabelArray
Definition: LabelTrack.h:104
double mClipLen
when done editing
Definition: LabelTrack.h:301
int x
width of the text in pixels.
Definition: LabelTrack.h:96
AudacityProject provides the main window, with tools and tracks contained within it.
Definition: Project.h:176
A LabelStruct holds information for ONE label in a LabelTrack.
Definition: LabelTrack.h:44
bool mRightDragging
initial cursor position
Definition: LabelTrack.h:294
int mSelIndex
Definition: LabelTrack.h:280
void DrawTextBox(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:648
int getSelectedIndex() const
Definition: LabelTrack.h:154
Defines a selected portion of a project.
const int NUM_GLYPH_CONFIGS
Definition: LabelTrack.h:106
std::weak_ptr< LabelGlyphHandle > mGlyphHandle
Definition: LabelTrack.h:318
static int mFontHeight
Definition: LabelTrack.h:290
LabelStruct(const SelectedRegion &region, const wxString &aTitle)
Definition: LabelTrack.cpp:528
static LabelStruct Import(wxTextFile &file, int &index)
virtual bool HandleXMLTag(const wxChar *tag, const wxChar **attrs)=0
Fundamental data object of Audacity, placed in the TrackPanel. Classes derived form it include the Wa...
Definition: Track.h:101
bool mDrawCursor
flag to tell if it's a valid dragging
Definition: LabelTrack.h:295
virtual void Clear(double WXUNUSED(t0), double WXUNUSED(t1))=0
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:70
int width
Text of the label.
Definition: LabelTrack.h:93
static bool mbGlyphsReady
Definition: LabelTrack.h:287
int GetKind() const override
Definition: LabelTrack.h:156
virtual void InsertSilence(double WXUNUSED(t), double WXUNUSED(len))=0
double getT1() const
Definition: LabelTrack.h:62
void MoveLabel(int iEdge, double fNewTime)
const SelectedRegion & getSelectedRegion() const
Definition: LabelTrack.h:59
static wxFont msFont
Definition: LabelTrack.h:316
SelectedRegion selectedRegion
Definition: LabelTrack.h:91
virtual std::vector< UIHandlePtr > DetailedHitTest(const TrackPanelMouseState &, const AudacityProject *pProject, int currentTool, bool bMultiTool)=0
const int NUM_GLYPH_HIGHLIGHTS
Definition: LabelTrack.h:107
Flags SaveFlags() const
Definition: LabelTrack.h:193
Transforms one point in time to another point. For example, a time stretching effect might use one to...
Definition: TimeWarper.h:61
static void OnChar(wxKeyEvent &event)
Definition: Contrast.cpp:155
std::unique_ptr< Track > Holder
Definition: Track.h:263
static int mTextHeight
Definition: LabelTrack.h:286
virtual void Silence(double WXUNUSED(t0), double WXUNUSED(t1))=0
double getT0() const
Definition: LabelTrack.h:61
void DrawText(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:623
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:22
int y
Pixel position of left hand side of text box.
Definition: LabelTrack.h:99
void DrawLines(wxDC &dc, const wxRect &r) const
Definition: LabelTrack.cpp:562
double duration() const
virtual std::shared_ptr< TrackControls > GetControls()=0
void DrawGlyphs(wxDC &dc, const wxRect &r, int GlyphLeft, int GlyphRight) const
Definition: LabelTrack.cpp:600
virtual Holder Duplicate() const =0
Command which outputs a list of available menu commands on the status channel.
bool AdjustEdge(int iEdge, double fNewTime)
virtual unsigned KeyDown(wxKeyEvent &event, ViewInfo &viewInfo, wxWindow *pParent)