Audacity 3.2.0
CutlineHandle.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5CutlineHandle.cpp
6
7Paul Licameli split from TrackPanel.cpp
8
9**********************************************************************/
10
11
12#include "CutlineHandle.h"
13
14#include <wx/cursor.h>
15#include <wx/event.h>
16
17#include "../../../../HitTestResult.h"
18#include "ProjectAudioIO.h"
19#include "ProjectHistory.h"
20#include "../../../../RefreshCode.h"
21#include "Snap.h" // for kPixelTolerance
22#include "../../../../TrackPanelMouseEvent.h"
23#include "UndoManager.h"
24#include "ViewInfo.h"
25#include "WaveTrack.h"
26#include "WaveTrackUtilities.h"
27#include "../../../../../images/Cursors.h"
28
30 const std::shared_ptr<WaveTrack> &pTrack,
31 WaveTrackLocations locations, WaveTrackLocation location
32)
33 : mpTrack{ pTrack }
34 , mLocations{ move(locations) }
35 , mLocation{ location }
36{
37}
38
40{
41#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
43#endif
44}
45
47{
48 static auto disabledCursor =
49 ::MakeCursor(wxCURSOR_NO_ENTRY, DisabledCursorXpm, 16, 16);
50 static wxCursor arrowCursor{ wxCURSOR_ARROW };
51 return { XO("Left-Click to expand, Right-Click to remove"),
52 (unsafe ? &*disabledCursor : &arrowCursor) };
53}
54namespace
55{
56 bool IsOverCutline(const WaveTrackLocations &locations,
57 const ViewInfo &viewInfo,
58 const wxRect &rect, const wxMouseState &state,
59 WaveTrackLocation *pmLocation)
60 {
61 for (auto loc: locations) {
62 const double x = viewInfo.TimeToPosition(loc.pos);
63 if (x >= 0 && x < rect.width)
64 {
65 wxRect locRect;
66 locRect.width = 2 * kPixelTolerance - 1;
67 locRect.x = (int)(rect.x + x) - locRect.width / 2;
68 locRect.y = rect.y;
69 locRect.height = rect.height;
70 if (locRect.Contains(state.m_x, state.m_y))
71 {
72 if (pmLocation)
73 *pmLocation = loc;
74 return true;
75 }
76 }
77 }
78
79 return false;
80 }
81}
82
84 std::weak_ptr<CutlineHandle> &holder,
85 const wxMouseState &state, const wxRect &rect,
86 const AudacityProject *pProject,
87 std::shared_ptr<WaveTrack> pTrack)
88{
89 auto &viewInfo = ViewInfo::Get(*pProject);
92
93 auto locations = FindWaveTrackLocations(*pTrack);
94 WaveTrackLocation location;
95 if (!IsOverCutline(locations, viewInfo, rect, state, &location))
96 return {};
97
98 auto result =
99 std::make_shared<CutlineHandle>(pTrack, move(locations), location);
100 result = AssignUIHandlePtr(holder, result);
101 return result;
102}
103
105{
106}
107
108std::shared_ptr<const Track> CutlineHandle::FindTrack() const
109{
110 return mpTrack;
111}
112
114{
115 return true;
116}
117
119(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
120{
121 using namespace RefreshCode;
122 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
123 if ( unsafe )
124 return Cancelled;
125
126 const wxMouseEvent &event = evt.event;
127 auto &viewInfo = ViewInfo::Get( *pProject );
128
129 // Can affect the track by merging clips, expanding a cutline, or
130 // deleting a cutline.
131 // All the change is done at button-down. Button-up just commits the undo item.
132
134
135 // FIXME: Disable this and return true when CutLines aren't showing?
136 // (Don't use gPrefs-> for the fix as registry access is slow).
137
138 // Cutline data changed on either branch, so refresh the track display.
140
141 if (event.LeftDown())
142 {
144 mStartTime = viewInfo.selectedRegion.t0();
145 mEndTime = viewInfo.selectedRegion.t1();
146
147 // When user presses left button on cut line, expand the line again
148 double cutlineStart = 0, cutlineEnd = 0;
150 mLocation.pos, &cutlineStart, &cutlineEnd);
151 viewInfo.selectedRegion.setTimes(cutlineStart, cutlineEnd);
152 }
153 else if (event.RightDown())
154 {
156 if (!removed)
157 // Nothing happened, make no Undo item
158 return Cancelled;
160 }
161 else
162 result = RefreshNone;
163
164 return result;
165}
166
169{
171}
172
174(const TrackPanelMouseState &, AudacityProject *pProject)
175{
176 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
177 return HitPreview( unsafe );
178}
179
181(const TrackPanelMouseEvent &, AudacityProject *pProject, wxWindow *)
182{
184
185 // Only now commit the result to the undo stack
186 switch (mOperation) {
187 default:
188 wxASSERT(false);
189 case Expand:
190 ProjectHistory::Get( *pProject )
191 .PushState(XO("Expanded Cut Line"), XO("Expand"));
192 break;
193 case Remove:
194 ProjectHistory::Get( *pProject )
195 .PushState(XO("Removed Cut Line"), XO("Remove"));
196 break;
197 }
198
199 // Nothing to do for the display
200 return result;
201}
202
204{
205 using namespace RefreshCode;
207 ProjectHistory::Get( *pProject ).RollbackState();
208 if (mOperation == Expand) {
209 AudacityProject &project = *pProject;
210 auto &selectedRegion = ViewInfo::Get( project ).selectedRegion;
211 selectedRegion.setTimes( mStartTime, mEndTime );
212 }
213 return result;
214}
std::shared_ptr< UIHandle > UIHandlePtr
Definition: CellularPanel.h:28
XO("Cut/Copy/Paste")
const int kPixelTolerance
Definition: Snap.h:28
const auto project
std::unique_ptr< wxCursor > MakeCursor(int WXUNUSED(CursorId), const char *const pXpm[36], int HotX, int HotY)
Definition: TrackPanel.cpp:189
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:164
WaveTrackLocations FindWaveTrackLocations(const WaveTrack &track)
std::vector< WaveTrackLocation > WaveTrackLocations
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Operation mOperation
Definition: CutlineHandle.h:72
CutlineHandle(const CutlineHandle &)=delete
Result Release(const TrackPanelMouseEvent &event, AudacityProject *pProject, wxWindow *pParent) override
std::shared_ptr< const Track > FindTrack() const override
WaveTrackLocation mLocation
Definition: CutlineHandle.h:75
HitTestPreview Preview(const TrackPanelMouseState &state, AudacityProject *pProject) override
std::shared_ptr< WaveTrack > mpTrack
Definition: CutlineHandle.h:70
static UIHandlePtr HitTest(std::weak_ptr< CutlineHandle > &holder, const wxMouseState &state, const wxRect &rect, const AudacityProject *pProject, std::shared_ptr< WaveTrack > pTrack)
virtual ~CutlineHandle()
double mStartTime
Definition: CutlineHandle.h:73
Result Drag(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
static HitTestPreview HitPreview(bool unsafe)
void Enter(bool forward, AudacityProject *) override
bool HandlesRightClick() override
Whether the handle has any special right-button handling.
Result Click(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
Result Cancel(AudacityProject *pProject) override
bool setTimes(double t0, double t1)
Definition: ViewInfo.cpp:51
bool IsAudioActive() const
static ProjectAudioIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
Result mChangeHighlight
Definition: UIHandle.h:152
unsigned Result
Definition: UIHandle.h:40
NotifyingSelectedRegion selectedRegion
Definition: ViewInfo.h:215
static ViewInfo & Get(AudacityProject &project)
Definition: ViewInfo.cpp:235
int64 TimeToPosition(double time, int64 origin=0, bool ignoreFisheye=false) const
STM: Converts a project time to screen x position.
Definition: ZoomInfo.cpp:44
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
WAVE_TRACK_API void ExpandCutLine(WaveTrack &track, double cutLinePosition, double *cutlineStart=nullptr, double *cutlineEnd=nullptr)
WAVE_TRACK_API bool RemoveCutLine(WaveTrack &track, double cutLinePosition)
Remove cut line, without expanding the audio in it.
bool IsOverCutline(const WaveTrackLocations &locations, const ViewInfo &viewInfo, const wxRect &rect, const wxMouseState &state, WaveTrackLocation *pmLocation)