Audacity 3.2.0
SpectrumVRulerControls.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SpectrumVRulerControls.cpp
6
7Paul Licameli split from WaveChannelVRulerControls.cpp
8
9**********************************************************************/
10
12
13#include "SpectrumVZoomHandle.h"
15
16#include "../../../ui/ChannelView.h"
17#include "NumberScale.h"
18#include "ProjectHistory.h"
19#include "../../../../RefreshCode.h"
20#include "../../../../TrackPanelMouseEvent.h"
21#include "WaveTrack.h"
22#include "../../../../prefs/SpectrogramSettings.h"
23#include "../../../../widgets/Ruler.h"
24#include "../../../../widgets/LinearUpdater.h"
25#include "../../../../widgets/LogarithmicUpdater.h"
26#include "../../../../widgets/IntFormat.h"
27#include "../../../../widgets/RealFormat.h"
28
30
31std::vector<UIHandlePtr> SpectrumVRulerControls::HitTest(
32 const TrackPanelMouseState &st,
33 const AudacityProject *pProject)
34{
35 std::vector<UIHandlePtr> results;
36
37 if ( st.state.GetX() <= st.rect.GetRight() - kGuard ) {
38 auto pTrack = FindTrack()->SharedPointer<WaveTrack>( );
39 if (pTrack) {
40 auto result = std::make_shared<SpectrumVZoomHandle>(
41 pTrack, st.rect, st.state.m_y );
42 result = AssignUIHandlePtr(mVZoomHandle, result);
43 results.push_back(result);
44 }
45 }
46
47 auto more = ChannelVRulerControls::HitTest(st, pProject);
48 std::copy(more.begin(), more.end(), std::back_inserter(results));
49
50 return results;
51}
52
54 const TrackPanelMouseEvent &evt, AudacityProject *pProject)
55{
56 using namespace RefreshCode;
57 const auto pTrack = FindTrack();
58 if (!pTrack)
59 return RefreshNone;
60 const auto wt = static_cast<WaveTrack*>(pTrack.get());
61 return DoHandleWheelRotation( evt, pProject, wt );
62}
63
65 const TrackPanelMouseEvent &evt, AudacityProject *pProject, WaveTrack *wt)
66{
67 using namespace RefreshCode;
68 const wxMouseEvent &event = evt.event;
69
70 if (!(event.ShiftDown() || event.CmdDown()))
71 return RefreshNone;
72
73 // Always stop propagation even if the ruler didn't change. The ruler
74 // is a narrow enough target.
75 evt.event.Skip(false);
76
77 auto steps = evt.steps;
78
79 using namespace WaveChannelViewConstants;
80 if (event.CmdDown() && !event.ShiftDown()) {
81 const int yy = event.m_y;
83 pProject, wt,
84 (steps < 0)
85 ? kZoomOut
86 : kZoomIn,
87 evt.rect, yy, yy, true);
88 }
89 else if (!event.CmdDown() && event.ShiftDown()) {
90 // Scroll some fixed number of pixels, independent of zoom level or track height:
91 static const float movement = 10.0f;
92 const int height = evt.rect.GetHeight();
93 {
94 const float delta = steps * movement / height;
96 const bool isLinear = settings.scaleType == SpectrogramSettings::stLinear;
97 float bottom, top;
98 SpectrogramBounds::Get(*wt).GetBounds(*wt, bottom, top);
99 const double rate = wt->GetRate();
100 const float bound = rate / 2;
101 const NumberScale numberScale(settings.GetScale(bottom, top));
102 float newTop =
103 std::min(bound, numberScale.PositionToValue(1.0f + delta));
104 const float newBottom =
105 std::max((isLinear ? 0.0f : 1.0f),
106 numberScale.PositionToValue(numberScale.ValueToPosition(newTop) - 1.0f));
107 newTop =
108 std::min(bound,
109 numberScale.PositionToValue(numberScale.ValueToPosition(newBottom) + 1.0f));
110
111 SpectrogramBounds::Get(*wt).SetBounds(newBottom, newTop);
112 }
113 }
114 else
115 return RefreshNone;
116
117 ProjectHistory::Get( *pProject ).ModifyState(true);
118
119 return RefreshCell | UpdateVRuler;
120}
121
124 const wxRect &rect_, unsigned iPass )
125{
126 ChannelVRulerControls::Draw(context, rect_, iPass);
127 WaveChannelVRulerControls::DoDraw(*this, context, rect_, iPass);
128}
129
130void SpectrumVRulerControls::UpdateRuler( const wxRect &rect )
131{
132 const auto wt = std::static_pointer_cast< WaveTrack >( FindTrack() );
133 if (!wt)
134 return;
135 DoUpdateVRuler( rect, wt.get() );
136}
137
139 const wxRect &rect, const WaveTrack *wt )
140{
142 const auto &settings = SpectrogramSettings::Get(*wt);
143 float minFreq, maxFreq;
144 SpectrogramBounds::Get(*wt).GetBounds(*wt, minFreq, maxFreq);
145 vruler->SetDbMirrorValue(0.0);
146
147 switch (settings.scaleType) {
148 default:
149 wxASSERT(false);
151 {
152 // Spectrum
153
154 /*
155 draw the ruler
156 we will use Hz if maxFreq is < 2000, otherwise we represent kHz,
157 and append to the numbers a "k"
158 */
159 vruler->SetBounds(
160 rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1);
161 vruler->SetOrientation(wxVERTICAL);
162 vruler->SetFormat(&RealFormat::LinearInstance());
163 vruler->SetLabelEdges(true);
164 // use kHz in scale, if appropriate
165 if (maxFreq >= 2000) {
166 vruler->SetRange((maxFreq / 1000.), (minFreq / 1000.));
167 /* i18n-hint k abbreviating kilo meaning thousands */
168 vruler->SetUnits(XO("k"));
169 }
170 else {
171 // use Hz
172 vruler->SetRange((int)(maxFreq), (int)(minFreq));
173 vruler->SetUnits({});
174 }
175 vruler->SetUpdater(&LinearUpdater::Instance());
176 }
177 break;
183 {
184 // SpectrumLog
185
186 /*
187 draw the ruler
188 we will use Hz if maxFreq is < 2000, otherwise we represent kHz,
189 and append to the numbers a "k"
190 */
191 vruler->SetBounds(
192 rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1);
193 vruler->SetOrientation(wxVERTICAL);
194 vruler->SetFormat(&IntFormat::Instance());
195 vruler->SetLabelEdges(true);
196 vruler->SetRange(maxFreq, minFreq);
197 vruler->SetUnits({});
198 vruler->SetUpdater(&LogarithmicUpdater::Instance());
199 NumberScale scale(settings.GetScale(minFreq, maxFreq).Reversal());
200 vruler->SetNumberScale(scale);
201 }
202 break;
203 }
204 auto &size = ChannelView::Get(*wt).vrulerSize;
205 vruler->GetMaxSize(&size.first, &size.second);
206}
const int kGuard
int min(int a, int b)
XO("Cut/Copy/Paste")
static Settings & settings()
Definition: TrackInfo.cpp:69
std::shared_ptr< Subclass > AssignUIHandlePtr(std::weak_ptr< Subclass > &holder, const std::shared_ptr< Subclass > &pNew)
Definition: UIHandle.h:159
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
std::vector< UIHandlePtr > HitTest(const TrackPanelMouseState &state, const AudacityProject *pProject) override
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
static ChannelView & Get(Channel &channel)
std::pair< int, int > vrulerSize
Definition: ChannelView.h:128
std::shared_ptr< Track > FindTrack()
static const IntFormat & Instance()
Definition: IntFormat.cpp:14
static const LinearUpdater & Instance()
static const LogarithmicUpdater & Instance()
float PositionToValue(float pp) const
Definition: NumberScale.h:155
float ValueToPosition(float val) const
Definition: NumberScale.h:256
void ModifyState(bool bWantsAutoSave)
static ProjectHistory & Get(AudacityProject &project)
static const RealFormat & LinearInstance()
Definition: RealFormat.cpp:14
void GetBounds(const WaveTrack &wt, float &min, float &max) const
void SetBounds(float min, float max)
static SpectrogramBounds & Get(WaveTrack &track)
Get either the global default settings, or the track's own if previously created.
Spectrogram settings, either for one track or as defaults.
static SpectrogramSettings & Get(const WaveTrack &track)
Mutative access to attachment even if the track argument is const.
static SpectrogramSettings & Own(WaveTrack &track)
static void DoUpdateVRuler(const wxRect &rect, const WaveTrack *wt)
std::weak_ptr< SpectrumVZoomHandle > mVZoomHandle
static unsigned DoHandleWheelRotation(const TrackPanelMouseEvent &evt, AudacityProject *pProject, WaveTrack *wt)
unsigned HandleWheelRotation(const TrackPanelMouseEvent &event, AudacityProject *pProject) override
void UpdateRuler(const wxRect &rect) override
~SpectrumVRulerControls() override
std::vector< UIHandlePtr > HitTest(const TrackPanelMouseState &state, const AudacityProject *) override
void Draw(TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass) override
static void DoZoom(AudacityProject *pProject, WaveTrack *pTrack, WaveChannelViewConstants::ZoomActions ZoomKind, const wxRect &rect, int zoomStart, int zoomEnd, bool fixedMousePoint)
A Track that contains audio waveform data.
Definition: WaveTrack.h:227
double GetRate() const override
Definition: WaveTrack.cpp:1128
Namespace containing an enum 'what to do on a refresh?'.
Definition: RefreshCode.h:16
AUDACITY_DLL_API Ruler & ScratchRuler()
AUDACITY_DLL_API void DoDraw(ChannelVRulerControls &controls, TrackPanelDrawingContext &context, const wxRect &rect, unsigned iPass)
void copy(const T *src, T *dst, int32_t n)
Definition: VectorOps.h:40