| 1 |
/********************************************************************** |
| 2 |
|
| 3 |
Audacity: A Digital Audio Editor |
| 4 |
|
| 5 |
SpectrumVRulerControls.cpp |
| 6 |
|
| 7 |
Paul Licameli split from WaveChannelVRulerControls.cpp |
| 8 |
|
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "SpectrumVRulerControls.h" |
| 12 |
|
| 13 |
#include "SpectrumVZoomHandle.h" |
| 14 |
#include "WaveChannelVRulerControls.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 "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 |
|
| 29 |
SpectrumVRulerControls::~SpectrumVRulerControls() = default; |
| 30 |
|
| 31 |
std::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 |
if (const auto pChannel = FindWaveChannel()) { |
| 39 |
auto result = std::make_shared<SpectrumVZoomHandle>( |
| 40 |
pChannel, st.rect, st.state.m_y); |
| 41 |
result = AssignUIHandlePtr(mVZoomHandle, result); |
| 42 |
results.push_back(result); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
auto more = ChannelVRulerControls::HitTest(st, pProject); |
| 47 |
std::copy(more.begin(), more.end(), std::back_inserter(results)); |
| 48 |
|
| 49 |
return results; |
| 50 |
} |
| 51 |
|
| 52 |
unsigned SpectrumVRulerControls::HandleWheelRotation( |
| 53 |
const TrackPanelMouseEvent &evt, AudacityProject *pProject) |
| 54 |
{ |
| 55 |
using namespace RefreshCode; |
| 56 |
const auto pChannel = FindWaveChannel(); |
| 57 |
if (!pChannel) |
| 58 |
return RefreshNone; |
| 59 |
return DoHandleWheelRotation(evt, pProject, *pChannel); |
| 60 |
} |
| 61 |
|
| 62 |
std::shared_ptr<WaveChannel> SpectrumVRulerControls::FindWaveChannel() |
| 63 |
{ |
| 64 |
return FindChannel<WaveChannel>(); |
| 65 |
} |
| 66 |
|
| 67 |
unsigned SpectrumVRulerControls::DoHandleWheelRotation( |
| 68 |
const TrackPanelMouseEvent &evt, AudacityProject *pProject, WaveChannel &wc) |
| 69 |
{ |
| 70 |
using namespace RefreshCode; |
| 71 |
const wxMouseEvent &event = evt.event; |
| 72 |
|
| 73 |
if (!(event.ShiftDown() || event.CmdDown())) |
| 74 |
return RefreshNone; |
| 75 |
|
| 76 |
// Always stop propagation even if the ruler didn't change. The ruler |
| 77 |
// is a narrow enough target. |
| 78 |
evt.event.Skip(false); |
| 79 |
|
| 80 |
auto steps = evt.steps; |
| 81 |
|
| 82 |
using namespace WaveChannelViewConstants; |
| 83 |
if (event.CmdDown() && !event.ShiftDown()) { |
| 84 |
const int yy = event.m_y; |
| 85 |
SpectrumVZoomHandle::DoZoom( |
| 86 |
pProject, wc, |
| 87 |
(steps < 0) |
| 88 |
? kZoomOut |
| 89 |
: kZoomIn, |
| 90 |
evt.rect, yy, yy, true); |
| 91 |
} |
| 92 |
else if (!event.CmdDown() && event.ShiftDown()) { |
| 93 |
// Scroll some fixed number of pixels, independent of zoom level or track height: |
| 94 |
static const float movement = 10.0f; |
| 95 |
const int height = evt.rect.GetHeight(); |
| 96 |
{ |
| 97 |
const float delta = steps * movement / height; |
| 98 |
SpectrogramSettings &settings = SpectrogramSettings::Own(wc); |
| 99 |
const bool isLinear = settings.scaleType == SpectrogramSettings::stLinear; |
| 100 |
float bottom, top; |
| 101 |
SpectrogramBounds::Get(wc).GetBounds(wc, bottom, top); |
| 102 |
const double rate = wc.GetRate(); |
| 103 |
const float bound = rate / 2; |
| 104 |
const NumberScale numberScale(settings.GetScale(bottom, top)); |
| 105 |
float newTop = |
| 106 |
std::min(bound, numberScale.PositionToValue(1.0f + delta)); |
| 107 |
const float newBottom = |
| 108 |
std::max((isLinear ? 0.0f : 1.0f), |
| 109 |
numberScale.PositionToValue(numberScale.ValueToPosition(newTop) - 1.0f)); |
| 110 |
newTop = |
| 111 |
std::min(bound, |
| 112 |
numberScale.PositionToValue(numberScale.ValueToPosition(newBottom) + 1.0f)); |
| 113 |
|
| 114 |
SpectrogramBounds::Get(wc).SetBounds(newBottom, newTop); |
| 115 |
} |
| 116 |
} |
| 117 |
else |
| 118 |
return RefreshNone; |
| 119 |
|
| 120 |
ProjectHistory::Get( *pProject ).ModifyState(true); |
| 121 |
|
| 122 |
return RefreshCell | UpdateVRuler; |
| 123 |
} |
| 124 |
|
| 125 |
void SpectrumVRulerControls::Draw( |
| 126 |
TrackPanelDrawingContext &context, |
| 127 |
const wxRect &rect_, unsigned iPass ) |
| 128 |
{ |
| 129 |
ChannelVRulerControls::Draw(context, rect_, iPass); |
| 130 |
WaveChannelVRulerControls::DoDraw(*this, context, rect_, iPass); |
| 131 |
} |
| 132 |
|
| 133 |
void SpectrumVRulerControls::UpdateRuler(const wxRect &rect) |
| 134 |
{ |
| 135 |
const auto pChannel = FindWaveChannel(); |
| 136 |
if (!pChannel) |
| 137 |
return; |
| 138 |
DoUpdateVRuler(rect, *pChannel); |
| 139 |
} |
| 140 |
|
| 141 |
void SpectrumVRulerControls::DoUpdateVRuler( |
| 142 |
const wxRect &rect, const WaveChannel &wc) |
| 143 |
{ |
| 144 |
auto vruler = &WaveChannelVRulerControls::ScratchRuler(); |
| 145 |
const auto &settings = SpectrogramSettings::Get(wc); |
| 146 |
float minFreq, maxFreq; |
| 147 |
SpectrogramBounds::Get(wc).GetBounds(wc, minFreq, maxFreq); |
| 148 |
vruler->SetDbMirrorValue(0.0); |
| 149 |
|
| 150 |
switch (settings.scaleType) { |
| 151 |
default: |
| 152 |
wxASSERT(false); |
| 153 |
case SpectrogramSettings::stLinear: |
| 154 |
{ |
| 155 |
// Spectrum |
| 156 |
|
| 157 |
/* |
| 158 |
draw the ruler |
| 159 |
we will use Hz if maxFreq is < 2000, otherwise we represent kHz, |
| 160 |
and append to the numbers a "k" |
| 161 |
*/ |
| 162 |
vruler->SetBounds( |
| 163 |
rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1); |
| 164 |
vruler->SetOrientation(wxVERTICAL); |
| 165 |
vruler->SetFormat(&RealFormat::LinearInstance()); |
| 166 |
vruler->SetLabelEdges(true); |
| 167 |
// use kHz in scale, if appropriate |
| 168 |
if (maxFreq >= 2000) { |
| 169 |
vruler->SetRange((maxFreq / 1000.), (minFreq / 1000.)); |
| 170 |
/* i18n-hint k abbreviating kilo meaning thousands */ |
| 171 |
vruler->SetUnits(XO("k")); |
| 172 |
} |
| 173 |
else { |
| 174 |
// use Hz |
| 175 |
vruler->SetRange((int)(maxFreq), (int)(minFreq)); |
| 176 |
vruler->SetUnits({}); |
| 177 |
} |
| 178 |
vruler->SetUpdater(&LinearUpdater::Instance()); |
| 179 |
} |
| 180 |
break; |
| 181 |
case SpectrogramSettings::stLogarithmic: |
| 182 |
case SpectrogramSettings::stMel: |
| 183 |
case SpectrogramSettings::stBark: |
| 184 |
case SpectrogramSettings::stErb: |
| 185 |
case SpectrogramSettings::stPeriod: |
| 186 |
{ |
| 187 |
// SpectrumLog |
| 188 |
|
| 189 |
/* |
| 190 |
draw the ruler |
| 191 |
we will use Hz if maxFreq is < 2000, otherwise we represent kHz, |
| 192 |
and append to the numbers a "k" |
| 193 |
*/ |
| 194 |
vruler->SetBounds( |
| 195 |
rect.x, rect.y, rect.x + rect.width, rect.y + rect.height - 1); |
| 196 |
vruler->SetOrientation(wxVERTICAL); |
| 197 |
vruler->SetFormat(&IntFormat::Instance()); |
| 198 |
vruler->SetLabelEdges(true); |
| 199 |
vruler->SetRange(maxFreq, minFreq); |
| 200 |
vruler->SetUnits({}); |
| 201 |
vruler->SetUpdater(&LogarithmicUpdater::Instance()); |
| 202 |
NumberScale scale(settings.GetScale(minFreq, maxFreq).Reversal()); |
| 203 |
vruler->SetNumberScale(scale); |
| 204 |
} |
| 205 |
break; |
| 206 |
} |
| 207 |
auto &size = ChannelView::Get(wc).vrulerSize; |
| 208 |
vruler->GetMaxSize(&size.first, &size.second); |
| 209 |
} |