Debian Codegraph @

src/tracks/playabletrack/wavetrack/ui/CutlineHandle.cpp

raw
1 /**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 CutlineHandle.cpp
6
7 Paul 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
29 CutlineHandle::CutlineHandle(
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
39 void CutlineHandle::Enter(bool, AudacityProject *)
40 {
41 #ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
42 mChangeHighlight = RefreshCode::RefreshCell;
43 #endif
44 }
45
46 HitTestPreview CutlineHandle::HitPreview(bool unsafe)
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 }
54 namespace
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
83 UIHandlePtr CutlineHandle::HitTest(
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);
90 /// method that tells us if the mouse event landed on an
91 /// editable Cutline
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
104 CutlineHandle::~CutlineHandle()
105 {
106 }
107
108 std::shared_ptr<const Track> CutlineHandle::FindTrack() const
109 {
110 return mpTrack;
111 }
112
113 bool CutlineHandle::HandlesRightClick()
114 {
115 return true;
116 }
117
118 UIHandle::Result CutlineHandle::Click
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
133 /// Someone has just clicked the mouse. What do we do?
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.
139 UIHandle::Result result = RefreshCell;
140
141 if (event.LeftDown())
142 {
143 mOperation = Expand;
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;
149 WaveTrackUtilities::ExpandCutLine(*mpTrack,
150 mLocation.pos, &cutlineStart, &cutlineEnd);
151 viewInfo.selectedRegion.setTimes(cutlineStart, cutlineEnd);
152 }
153 else if (event.RightDown())
154 {
155 bool removed = WaveTrackUtilities::RemoveCutLine(*mpTrack, mLocation.pos);
156 if (!removed)
157 // Nothing happened, make no Undo item
158 return Cancelled;
159 mOperation = Remove;
160 }
161 else
162 result = RefreshNone;
163
164 return result;
165 }
166
167 UIHandle::Result CutlineHandle::Drag
168 (const TrackPanelMouseEvent &, AudacityProject *)
169 {
170 return RefreshCode::RefreshNone;
171 }
172
173 HitTestPreview CutlineHandle::Preview
174 (const TrackPanelMouseState &, AudacityProject *pProject)
175 {
176 const bool unsafe = ProjectAudioIO::Get( *pProject ).IsAudioActive();
177 return HitPreview( unsafe );
178 }
179
180 UIHandle::Result CutlineHandle::Release
181 (const TrackPanelMouseEvent &, AudacityProject *pProject, wxWindow *)
182 {
183 UIHandle::Result result = RefreshCode::RefreshNone;
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
203 UIHandle::Result CutlineHandle::Cancel(AudacityProject *pProject)
204 {
205 using namespace RefreshCode;
206 UIHandle::Result result = RefreshCell;
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 }