-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwwaveformviewer.cpp
325 lines (291 loc) · 11.6 KB
/
wwaveformviewer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "widget/wwaveformviewer.h"
#include <QDomNode>
#include <QDragEnterEvent>
#include <QEvent>
#include <QMimeData>
#include <QPainter>
#include <QUrl>
#include <QtDebug>
#include "control/controlobject.h"
#include "control/controlproxy.h"
#include "moc_wwaveformviewer.cpp"
#include "track/track.h"
#include "util/dnd.h"
#include "util/math.h"
#include "waveform/waveformwidgetfactory.h"
#include "waveform/widgets/nonglwaveformwidgetabstract.h"
WWaveformViewer::WWaveformViewer(
const QString& group,
UserSettingsPointer pConfig,
QWidget* parent)
: WWidget(parent),
m_group(group),
m_pConfig(pConfig),
m_zoomZoneWidth(20),
m_bScratching(false),
m_bBending(false),
m_pCueMenuPopup(make_parented<WCueMenuPopup>(pConfig, this)),
m_waveformWidget(nullptr) {
setMouseTracking(true);
setAcceptDrops(true);
m_pZoom = new ControlProxy(group, "waveform_zoom", this, ControlFlag::NoAssertIfMissing);
m_pZoom->connectValueChanged(this, &WWaveformViewer::onZoomChange);
m_pScratchPositionEnable = new ControlProxy(
group, "scratch_position_enable", this, ControlFlag::NoAssertIfMissing);
m_pScratchPosition = new ControlProxy(
group, "scratch_position", this, ControlFlag::NoAssertIfMissing);
m_pWheel = new ControlProxy(
group, "wheel", this, ControlFlag::NoAssertIfMissing);
m_pPlayEnabled = new ControlProxy(group, "play", this, ControlFlag::NoAssertIfMissing);
m_pPassthroughEnabled = make_parented<ControlProxy>(group, "passthrough", this);
m_pPassthroughEnabled->connectValueChanged(this,
&WWaveformViewer::passthroughChanged,
Qt::DirectConnection);
setAttribute(Qt::WA_OpaquePaintEvent);
setFocusPolicy(Qt::NoFocus);
}
WWaveformViewer::~WWaveformViewer() {
//qDebug() << "~WWaveformViewer";
}
void WWaveformViewer::setup(const QDomNode& node, const SkinContext& context) {
if (m_waveformWidget) {
m_waveformWidget->setup(node, context);
}
m_dimBrightThreshold = m_waveformWidget->getDimBrightThreshold();
}
void WWaveformViewer::resizeEvent(QResizeEvent* event) {
if (m_waveformWidget) {
m_waveformWidget->resize(width(), height());
}
}
void WWaveformViewer::mousePressEvent(QMouseEvent* event) {
if (!m_waveformWidget) {
return;
}
m_mouseAnchor = event->pos();
if (event->button() == Qt::LeftButton) {
// If we are pitch-bending then disable and reset because the two
// shouldn't be used at once.
if (m_bBending) {
m_pWheel->setParameter(0.5);
m_bBending = false;
}
m_bScratching = true;
int eventPosValue = m_waveformWidget->getOrientation() == Qt::Horizontal ?
event->pos().x() : event->pos().y();
double audioSamplePerPixel = m_waveformWidget->getAudioSamplePerPixel();
double targetPosition = -1.0 * eventPosValue * audioSamplePerPixel * 2;
m_pScratchPosition->set(targetPosition);
m_pScratchPositionEnable->set(1.0);
} else if (event->button() == Qt::RightButton) {
const auto currentTrack = m_waveformWidget->getTrackInfo();
if (!isPlaying() && m_pHoveredMark) {
auto cueAtClickPos = getCuePointerFromCueMark(m_pHoveredMark);
if (cueAtClickPos) {
m_pCueMenuPopup->setTrackAndCue(currentTrack, cueAtClickPos);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
m_pCueMenuPopup->popup(event->globalPosition().toPoint());
#else
m_pCueMenuPopup->popup(event->globalPos());
#endif
}
} else {
// If we are scratching then disable and reset because the two shouldn't
// be used at once.
if (m_bScratching) {
m_pScratchPositionEnable->set(0.0);
m_bScratching = false;
}
m_pWheel->setParameter(0.5);
m_bBending = true;
}
}
// Set the cursor to a hand while the mouse is down (when cue menu is not open).
if (!m_pCueMenuPopup->isVisible()) {
setCursor(Qt::ClosedHandCursor);
}
}
void WWaveformViewer::mouseMoveEvent(QMouseEvent* event) {
if (!m_waveformWidget) {
return;
}
// Only send signals for mouse moving if the left button is pressed
if (m_bScratching) {
int eventPosValue = m_waveformWidget->getOrientation() == Qt::Horizontal ?
event->pos().x() : event->pos().y();
// Adjusts for one-to-one movement.
double audioSamplePerPixel = m_waveformWidget->getAudioSamplePerPixel();
double targetPosition = -1.0 * eventPosValue * audioSamplePerPixel * 2;
//qDebug() << "Target:" << targetPosition;
m_pScratchPosition->set(targetPosition);
} else if (m_bBending) {
QPoint diff = event->pos() - m_mouseAnchor;
int diffValue = m_waveformWidget->getOrientation() == Qt::Horizontal ?
diff.x() : diff.y();
// Start at the middle of [0.0, 1.0], and emit values based on how far
// the mouse has traveled horizontally. Note, for legacy (MIDI) reasons,
// this is tuned to 127.
// NOTE(rryan): This is basically a direct connection to the "wheel"
// control since we manually connect it in LegacySkinParser regardless
// of whether the skin specifies it. See ControlTTRotaryBehavior to see
// where this value is handled.
double v = 0.5 + (diffValue / 1270.0);
// clamp to [0.0, 1.0]
v = math_clamp(v, 0.0, 1.0);
m_pWheel->setParameter(v);
} else if (!isPlaying()) {
WaveformMarkPointer pMark;
pMark = m_waveformWidget->getCueMarkAtPoint(event->pos());
if (pMark && getCuePointerFromCueMark(pMark)) {
if (!m_pHoveredMark) {
m_pHoveredMark = pMark;
highlightMark(pMark);
} else if (pMark != m_pHoveredMark) {
unhighlightMark(m_pHoveredMark);
m_pHoveredMark = pMark;
highlightMark(pMark);
}
} else {
if (m_pHoveredMark) {
unhighlightMark(m_pHoveredMark);
m_pHoveredMark = nullptr;
}
}
}
}
void WWaveformViewer::mouseReleaseEvent(QMouseEvent* /*event*/) {
if (m_bScratching) {
m_pScratchPositionEnable->set(0.0);
m_bScratching = false;
}
if (m_bBending) {
m_pWheel->setParameter(0.5);
m_bBending = false;
}
m_mouseAnchor = QPoint();
// Set the cursor back to an arrow.
setCursor(Qt::ArrowCursor);
}
void WWaveformViewer::wheelEvent(QWheelEvent* event) {
if (m_waveformWidget) {
if (event->angleDelta().y() > 0) {
onZoomChange(m_waveformWidget->getZoomFactor() / 1.05);
} else if (event->angleDelta().y() < 0) {
onZoomChange(m_waveformWidget->getZoomFactor() * 1.05);
}
}
}
void WWaveformViewer::dragEnterEvent(QDragEnterEvent* event) {
DragAndDropHelper::handleTrackDragEnterEvent(event, m_group, m_pConfig);
}
void WWaveformViewer::dropEvent(QDropEvent* event) {
DragAndDropHelper::handleTrackDropEvent(event, *this, m_group, m_pConfig);
}
bool WWaveformViewer::handleDragAndDropEventFromWindow(QEvent* ev) {
return event(ev);
}
void WWaveformViewer::leaveEvent(QEvent*) {
if (m_pHoveredMark) {
unhighlightMark(m_pHoveredMark);
m_pHoveredMark = nullptr;
}
}
void WWaveformViewer::slotTrackLoaded(TrackPointer track) {
if (m_waveformWidget) {
m_waveformWidget->setTrack(track);
}
}
void WWaveformViewer::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) {
Q_UNUSED(pNewTrack);
Q_UNUSED(pOldTrack);
if (m_waveformWidget) {
m_waveformWidget->setTrack(TrackPointer());
}
}
void WWaveformViewer::onZoomChange(double zoom) {
//qDebug() << "WaveformWidgetRenderer::onZoomChange" << this << zoom;
setZoom(zoom);
// notify back the factory to sync zoom if needed
WaveformWidgetFactory::instance()->notifyZoomChange(this);
}
void WWaveformViewer::setZoom(double zoom) {
//qDebug() << "WaveformWidgetRenderer::setZoom" << zoom;
if (m_waveformWidget) {
m_waveformWidget->setZoom(zoom);
}
// If multiple waveform widgets for the same group are created then it's
// possible that this setZoom() is coming from another waveform with the
// same group. That means that if we set the zoom control here, that
// waveform will receive the update as a call to onZoomChange which will in
// turn notify the WaveformWidgetFactory that zoom changed which will
// infinite loop because we will receive another setZoom() from
// WaveformWidgetFactory. To prevent this recursion, check for no-ops.
if (m_pZoom->get() != zoom) {
m_pZoom->set(zoom);
}
}
void WWaveformViewer::setDisplayBeatGridAlpha(int alpha) {
m_waveformWidget->setDisplayBeatGridAlpha(alpha);
}
void WWaveformViewer::setPlayMarkerPosition(double position) {
if (m_waveformWidget) {
m_waveformWidget->setPlayMarkerPosition(position);
}
}
void WWaveformViewer::setWaveformWidget(WaveformWidgetAbstract* waveformWidget) {
if (m_waveformWidget) {
QWidget* pWidget = m_waveformWidget->getWidget();
disconnect(pWidget, &QWidget::destroyed, this, &WWaveformViewer::slotWidgetDead);
}
m_waveformWidget = waveformWidget;
if (m_waveformWidget) {
QWidget* pWidget = m_waveformWidget->getWidget();
connect(pWidget, &QWidget::destroyed, this, &WWaveformViewer::slotWidgetDead);
m_waveformWidget->getWidget()->setMouseTracking(true);
#ifdef MIXXX_USE_QOPENGL
if (m_waveformWidget->getGLWidget()) {
// The OpenGLWindow used to display the waveform widget interferes with the
// normal Qt tooltip mechanism and uses it's own mechanism. We set the tooltip
// of the waveform widget to the tooltip of its parent WWaveformViewer so the
// OpenGLWindow will display it.
m_waveformWidget->getGLWidget()->setToolTip(toolTip());
// Tell the WGLWidget that this is its drag&drop target
m_waveformWidget->getGLWidget()->setTrackDropTarget(this);
}
#endif
// Make connection to show "Passthrough" label on the waveform, except for
// "Empty" waveform type
if (m_waveformWidget->getType() == WaveformWidgetType::EmptyWaveform) {
return;
}
connect(this,
&WWaveformViewer::passthroughChanged,
this,
[this](double value) {
m_waveformWidget->setPassThroughEnabled(value > 0);
});
// Make sure the label is shown after the waveform type was changed
emit passthroughChanged(m_pPassthroughEnabled->toBool());
}
}
CuePointer WWaveformViewer::getCuePointerFromCueMark(WaveformMarkPointer pMark) const {
if (m_waveformWidget && pMark) {
return m_waveformWidget->getCuePointerFromIndex(pMark->getHotCue());
}
return {};
}
void WWaveformViewer::highlightMark(WaveformMarkPointer pMark) {
QColor highlightColor = Color::chooseContrastColor(pMark->fillColor(),
m_dimBrightThreshold);
pMark->setBaseColor(highlightColor, m_dimBrightThreshold);
}
void WWaveformViewer::unhighlightMark(WaveformMarkPointer pMark) {
auto pCue = getCuePointerFromCueMark(pMark);
if (pCue) {
QColor originalColor = mixxx::RgbColor::toQColor(pCue->getColor());
pMark->setBaseColor(originalColor, m_dimBrightThreshold);
}
}
bool WWaveformViewer::isPlaying() const {
return m_pPlayEnabled->toBool();
}