-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariableselectionwaveform.cpp
86 lines (77 loc) · 2.66 KB
/
variableselectionwaveform.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
#include "variableselectionwaveform.h"
#include <QMouseEvent>
VariableSelectionWaveform::VariableSelectionWaveform(QWidget *parent) :
Waveform(parent),
_selectionRegion(nullptr),
_currentlyDragging (false)
{
connect (this, &Waveform::selectionRegionChanged, this, &VariableSelectionWaveform::onSelectionRegionChanged);
}
void VariableSelectionWaveform::reset ()
{
_selectionRegion = nullptr; // Lose the pointer, we don't own the item and it's about to get deleted
_currentlyDragging = false;
Waveform::reset();
}
void VariableSelectionWaveform::mousePressEvent(QMouseEvent *event)
{
_selectionStart = event->pos().x();
_selectionLength = 1;
_dragStartTime.start();
_dragStartX = int(_selectionStart);
_currentlyDragging = true;
emit (selectionRegionChanged(_selectionStart, _selectionLength));
Waveform::mousePressEvent(event);
}
void VariableSelectionWaveform::mouseReleaseEvent(QMouseEvent *event)
{
_currentlyDragging = false;
// Make our selection always positive...
if (_selectionLength < 0) {
_selectionStart += _selectionLength;
_selectionLength = abs(_selectionLength);
}
emit (selectionRegionChanged(_selectionStart, _selectionLength));
setPlayheadPosition(pixelsToMillis(int(_selectionStart)));
emit (playheadManuallyChanged(pixelsToMillis(int(_selectionStart))));
Waveform::mouseReleaseEvent(event);
}
void VariableSelectionWaveform::mouseMoveEvent(QMouseEvent *event)
{
if (_currentlyDragging) {
qint64 currentX = event->pos().x();
_selectionLength = currentX - _dragStartX;
emit (selectionRegionChanged(_selectionStart, _selectionLength));
}
Waveform::mouseMoveEvent(event);
}
void VariableSelectionWaveform::onSelectionRegionChanged (qint64 start, qint64 length)
{
qint64 rStart, rWidth;
if (length >= 0) {
rStart = start;
rWidth = length;
} else {
rStart = start + length;
rWidth = abs(length);
}
QRectF r (rStart, 0, rWidth, _scene.height());
if (_selectionRegion && _scene.items().contains(_selectionRegion)) {
_selectionRegion->setRect (r);
} else {
QPen pen (Qt::black);
QBrush brush (QColor(0,0,0,70));
_selectionRegion = _scene.addRect (r, pen, brush);
_selectionRegion->setZValue(100);
}
}
qint64 VariableSelectionWaveform::getSelectionLength () const
{
// If the selection is super small, assume it wasn't meant to be a selection at all
if (_selectionLength < 5) {
auto fullWidth = this->width() - _selectionStart;
return pixelsToMillis(fullWidth);
} else {
return pixelsToMillis(_selectionLength);
}
}