-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
wwaveformviewer.cpp
217 lines (188 loc) · 7.14 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
#include <QtDebug>
#include <QDomNode>
#include <QEvent>
#include <QDragEnterEvent>
#include <QUrl>
#include <QPainter>
#include <QMimeData>
#include "controlobject.h"
#include "controlobjectthread.h"
#include "trackinfoobject.h"
#include "waveform/widgets/waveformwidgetabstract.h"
#include "widget/wwaveformviewer.h"
#include "waveform/waveformwidgetfactory.h"
WWaveformViewer::WWaveformViewer(const char *group, ConfigObject<ConfigValue>* pConfig, QWidget * parent)
: WWidget(parent),
m_pGroup(group),
m_pConfig(pConfig) {
setAcceptDrops(true);
m_bScratching = false;
m_bBending = false;
m_pZoom = new ControlObjectThread(group, "waveform_zoom");
connect(m_pZoom, SIGNAL(valueChanged(double)),
this, SLOT(onZoomChange(double)));
m_pScratchPositionEnable = new ControlObjectThread(
group, "scratch_position_enable");
m_pScratchPosition = new ControlObjectThread(
group, "scratch_position");
setAttribute(Qt::WA_OpaquePaintEvent);
m_zoomZoneWidth = 20;
m_waveformWidget = NULL;
}
WWaveformViewer::~WWaveformViewer() {
//qDebug() << "~WWaveformViewer";
delete m_pZoom;
delete m_pScratchPositionEnable;
delete m_pScratchPosition;
}
void WWaveformViewer::setup(QDomNode node, const SkinContext& context) {
Q_UNUSED(context);
if (m_waveformWidget)
m_waveformWidget->setup(node, context);
}
void WWaveformViewer::resizeEvent(QResizeEvent* /*event*/) {
if (m_waveformWidget) {
m_waveformWidget->resize(width(),height());
}
}
void WWaveformViewer::mousePressEvent(QMouseEvent* event) {
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) {
setControlParameterRightDown(0.5);
m_bBending = false;
}
m_bScratching = true;
double audioSamplePerPixel = m_waveformWidget->getAudioSamplePerPixel();
double targetPosition = -1.0 * event->pos().x() * audioSamplePerPixel * 2;
m_pScratchPosition->slotSet(targetPosition);
m_pScratchPositionEnable->slotSet(1.0);
} else if (event->button() == Qt::RightButton) {
// If we are scratching then disable and reset because the two shouldn't
// be used at once.
if (m_bScratching) {
m_pScratchPositionEnable->slotSet(0.0);
m_bScratching = false;
}
setControlParameterRightDown(0.5);
m_bBending = true;
}
// Set the cursor to a hand while the mouse is down.
setCursor(Qt::ClosedHandCursor);
}
void WWaveformViewer::mouseMoveEvent(QMouseEvent* event) {
// Only send signals for mouse moving if the left button is pressed
if (m_bScratching && m_waveformWidget) {
// Adjusts for one-to-one movement.
double audioSamplePerPixel = m_waveformWidget->getAudioSamplePerPixel();
double targetPosition = -1.0 * event->pos().x() * audioSamplePerPixel * 2;
//qDebug() << "Target:" << targetPosition;
m_pScratchPosition->slotSet(targetPosition);
} else if (m_bBending) {
QPoint diff = event->pos() - m_mouseAnchor;
// 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 + (diff.x() / 1270.0);
// clamp to [0.0, 1.0]
v = math_min(1.0, math_max(0.0, v));
setControlParameterRightDown(v);
}
}
void WWaveformViewer::mouseReleaseEvent(QMouseEvent* /*event*/) {
if (m_bScratching) {
m_pScratchPositionEnable->slotSet(0.0);
m_bScratching = false;
}
if (m_bBending) {
setControlParameterRightDown(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) {
//NOTE: (vrince) to limit the zoom action area uncomment the following line
//if (event->x() > width() - m_zoomZoneWidth) {
if (event->delta() > 0) {
//qDebug() << "WaveformWidgetRenderer::wheelEvent +1";
onZoomChange(m_waveformWidget->getZoomFactor()+1);
}
else {
//qDebug() << "WaveformWidgetRenderer::wheelEvent -1";
onZoomChange(m_waveformWidget->getZoomFactor()-1);
}
//}
}
}
/** DRAG AND DROP **/
void WWaveformViewer::dragEnterEvent(QDragEnterEvent * event) {
// Accept the enter event if the thing is a filepath.
if (event->mimeData()->hasUrls() &&
event->mimeData()->urls().size() > 0) {
// Accept if the Deck isn't playing or the settings allow to interrupt a playing deck
if ((!ControlObject::get(ConfigKey(m_pGroup, "play")) ||
m_pConfig->getValueString(ConfigKey("[Controls]","AllowTrackLoadToPlayingDeck")).toInt())) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
}
void WWaveformViewer::dropEvent(QDropEvent * event) {
if (event->mimeData()->hasUrls() &&
event->mimeData()->urls().size() > 0) {
QList<QUrl> urls(event->mimeData()->urls());
QUrl url = urls.first();
QString name = url.toLocalFile();
//If the file is on a network share, try just converting the URL to a string...
if (name == "")
name = url.toString();
event->accept();
emit(trackDropped(name, m_pGroup));
} else {
event->ignore();
}
}
void WWaveformViewer::onTrackLoaded( TrackPointer track) {
if (m_waveformWidget)
m_waveformWidget->setTrack(track);
}
void WWaveformViewer::onTrackUnloaded( TrackPointer /*track*/) {
if (m_waveformWidget)
m_waveformWidget->setTrack(TrackPointer(0));
}
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(int zoom) {
//qDebug() << "WaveformWidgetRenderer::setZoom" << zoom;
if (m_waveformWidget) {
m_waveformWidget->setZoom(zoom);
m_pZoom->slotSet(zoom);
}
}
void WWaveformViewer::setWaveformWidget(WaveformWidgetAbstract* waveformWidget) {
if (m_waveformWidget) {
QWidget* pWidget = m_waveformWidget->getWidget();
disconnect(pWidget, SIGNAL(destroyed()),
this, SLOT(slotWidgetDead()));
}
m_waveformWidget = waveformWidget;
if (m_waveformWidget) {
QWidget* pWidget = m_waveformWidget->getWidget();
connect(pWidget, SIGNAL(destroyed()),
this, SLOT(slotWidgetDead()));
}
}