-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiregionwaveform.cpp
148 lines (133 loc) · 4.52 KB
/
multiregionwaveform.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
#include "multiregionwaveform.h"
#include "utils.h"
#include <iostream>
#include <QMouseEvent>
#include "settings.h"
#include "settingsdialog.h"
MultiRegionWaveform::MultiRegionWaveform(QWidget *parent) :
Waveform (parent),
_group (nullptr),
_currentlyDraggingSelection (false),
_locked (false)
{
}
void MultiRegionWaveform::AddRegion (QString name, qint64 startMillis, qint64 endMillis, QColor color)
{
if (_locked) {
throw std::logic_error("Cannot add regions when MultiRegionWaveform is locked. Call Reset or ClearRegions first.");
}
_regions.push_back(Region());
auto &&r = _regions.back();
r.startMillis = startMillis;
r.endMillis = endMillis;
if (_totalLength > 0) {
r.pixelWidth = int(_scene.width() * (double(endMillis-startMillis)/_totalLength));
r.pixelStart = int(_scene.width() * (double(startMillis)/_totalLength));
}
r.penColor = color;
r.brushColor = QColor (color.red(), color.green(), color.blue(), int(0.3 * color.alpha()));
r.pen = QPen (r.penColor);
r.brush = QBrush (r.brushColor);
r.rect = _scene.addRect(r.pixelStart,0,r.pixelWidth,this->height(),r.pen, r.brush);
r.text = _scene.addText(name);
r.text->setDefaultTextColor (r.penColor);
r.text->setX(r.pixelStart);
if (_totalLength == 0) {
r.rect->hide();
r.text->hide();
}
QList<QGraphicsItem *> groupedItems;
groupedItems.append(r.text);
groupedItems.append(r.rect);
if (_group == nullptr) {
_group = _scene.createItemGroup(groupedItems);
} else {
_group->addToGroup(r.rect);
_group->addToGroup(r.text);
}
// Every time we add a new region, check to see if the new text object intersects any
// of the others, and if it does, stagger it down
for (unsigned int regionId = 0; regionId < _regions.size()-1; regionId++) {
Region ®ion = _regions[regionId];
QGraphicsTextItem *t = region.text;
QRectF checkRect = t->mapRectFromScene(t->boundingRect());
QRectF newTextRect = r.text->mapRectFromScene(r.text->boundingRect());
if (newTextRect.intersects(checkRect)) {
r.text->setY(r.text->y()+r.text->boundingRect().height()+1);
}
}
}
void MultiRegionWaveform::DoneAddingRegions ()
{
int64_t selectionMillis (0);
for (auto region: _regions) {
selectionMillis = std::max (selectionMillis, region.endMillis);
}
this->setSelectionLength(selectionMillis);
_group->setX(_selectionStart);
_locked = true;
}
void MultiRegionWaveform::ClearRegions ()
{
for (auto &&r:_regions) {
_group->removeFromGroup(r.text);
_group->removeFromGroup(r.rect);
_scene.removeItem(r.text);
_scene.removeItem(r.rect);
_scene.removeItem(_group);
}
_regions.clear();
_group = nullptr;
_locked = false;
}
void MultiRegionWaveform::reset()
{
ClearRegions();
Waveform::reset();
}
void MultiRegionWaveform::mousePressEvent(QMouseEvent *event)
{
_dragStartTime.restart();
_dragStartX = event->pos().x();
if (event->x() >= _selectionStart &&
event->x() <= _selectionStart + _selectionLength) {
// The mouse was pressed within the selection region
_currentlyDraggingSelection = true;
_cursorLine->hide();
}
// Skip over Waveform, it will want to allow arbitary selection regions,
// go straight to its parent...
QGraphicsView::mousePressEvent (event);
}
void MultiRegionWaveform::mouseReleaseEvent(QMouseEvent *event)
{
if (abs(_dragStartX - event->x()) <= 1) {
// Maybe this was really a click... how long was the button down?
if (_dragStartTime.elapsed() < 500) {
// Let our parent handle it if it wants to
Waveform::mouseReleaseEvent(event);
}
} else if (_currentlyDraggingSelection) {
int actualX = event->pos().x();
int dragDistance = actualX - _dragStartX;
_selectionStart += dragDistance;
_group->setX(_selectionStart);
QGraphicsView::mouseReleaseEvent (event);
} else {
Waveform::mouseReleaseEvent(event);
}
_currentlyDraggingSelection = false;
_cursorLine->show();
}
void MultiRegionWaveform::mouseMoveEvent(QMouseEvent *event)
{
_cursorLine->setX (event->x());
QGraphicsView::mouseMoveEvent (event);
if (_currentlyDraggingSelection) {
int actualX = event->pos().x();
int dragDistance = actualX - _dragStartX;
_group->setX(_selectionStart + dragDistance);
} else {
_cursorLine->show();
}
}