forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrubbar.cpp
More file actions
312 lines (282 loc) · 9.73 KB
/
scrubbar.cpp
File metadata and controls
312 lines (282 loc) · 9.73 KB
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
/*
* Copyright (c) 2011-2015 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "scrubbar.h"
#include "mltcontroller.h"
#include <QtWidgets>
static const int margin = 14; /// left and right margins
static const int selectionSize = 14; /// the height of the top bar
#ifndef CLAMP
#define CLAMP(x, min, max) (((x) < (min))? (min) : ((x) > (max))? (max) : (x))
#endif
ScrubBar::ScrubBar(QWidget *parent)
: QWidget(parent)
, m_head(-1)
, m_scale(-1)
, m_fps(25)
, m_max(1)
, m_in(-1)
, m_out(-1)
, m_activeControl(CONTROL_NONE)
, m_timecodeWidth(0)
{
setMouseTracking(true);
setMinimumHeight(fontMetrics().height() + selectionSize);
}
void ScrubBar::setScale(int maximum)
{
if (!m_timecodeWidth) {
const int fontSize = font().pointSize() - (font().pointSize() > 10? 2 : (font().pointSize() > 8? 1 : 0));
setFont(QFont(font().family(), fontSize * devicePixelRatio()));
m_timecodeWidth = fontMetrics().width("00:00:00:00") / devicePixelRatio();
}
m_max = maximum;
/// m_scale is the pixels per frame ratio
m_scale = (double) (width() - 2 * margin) / (double) maximum;
if (m_scale == 0) m_scale = -1;
m_secondsPerTick = qRound(double(m_timecodeWidth * 1.8) / m_scale / m_fps);
if (m_secondsPerTick > 3600)
// force to a multiple of one hour
m_secondsPerTick += 3600 - m_secondsPerTick % 3600;
else if (m_secondsPerTick > 300)
// force to a multiple of 5 minutes
m_secondsPerTick += 300 - m_secondsPerTick % 300;
else if (m_secondsPerTick > 60)
// force to a multiple of one minute
m_secondsPerTick += 60 - m_secondsPerTick % 60;
else if (m_secondsPerTick > 5)
// force to a multiple of 10 seconds
m_secondsPerTick += 10 - m_secondsPerTick % 10;
else if (m_secondsPerTick > 2)
// force to a multiple of 5 seconds
m_secondsPerTick += 5 - m_secondsPerTick % 5;
/// m_interval is the number of pixels per major tick to be labeled with time
m_interval = qRound(double(m_secondsPerTick) * m_fps * m_scale);
m_head = -1;
updatePixmap();
}
void ScrubBar::setFramerate(double fps)
{
m_fps = fps;
}
int ScrubBar::position() const
{
return m_head;
}
void ScrubBar::setInPoint(int in)
{
m_in = qMax(in, -1);
updatePixmap();
emit inChanged(in);
}
void ScrubBar::setOutPoint(int out)
{
m_out = qMin(out, m_max);
updatePixmap();
emit outChanged(out);
}
void ScrubBar::setMarkers(const QList<int> &list)
{
m_markers = list;
updatePixmap();
}
void ScrubBar::mousePressEvent(QMouseEvent * event)
{
int x = event->x() - margin;
int in = m_in * m_scale;
int out = m_out * m_scale;
int head = m_head * m_scale;
int pos = CLAMP(x / m_scale, 0, m_max);
if (m_in > -1 && m_out > -1) {
if (x >= in - 12 && x <= in + 6) {
m_activeControl = CONTROL_IN;
setInPoint(pos);
}
else if (x >= out - 6 && x <= out + 12) {
m_activeControl = CONTROL_OUT;
setOutPoint(pos);
}
}
if (m_head > -1) {
if (m_activeControl == CONTROL_NONE) {
m_activeControl = CONTROL_HEAD;
m_head = pos;
const int offset = height() / 2;
const int x = head;
const int w = qAbs(x - head);
update(margin + x - offset, 0, w + 2 * offset, height());
}
}
emit seeked(pos);
}
void ScrubBar::mouseReleaseEvent(QMouseEvent * event)
{
Q_UNUSED(event)
m_activeControl = CONTROL_NONE;
}
void ScrubBar::mouseMoveEvent(QMouseEvent * event)
{
int x = event->x() - margin;
int pos = CLAMP(x / m_scale, 0, m_max);
if (event->buttons() & Qt::LeftButton) {
if (m_activeControl == CONTROL_IN)
setInPoint(pos);
else if (m_activeControl == CONTROL_OUT)
setOutPoint(pos);
else if (m_activeControl == CONTROL_HEAD) {
const int head = m_head * m_scale;
const int offset = height() / 2;
const int x = head;
const int w = qAbs(x - head);
update(margin + x - offset, 0, w + 2 * offset, height());
m_head = pos;
}
emit seeked(pos);
}
}
bool ScrubBar::onSeek(int value)
{
if (m_activeControl != CONTROL_HEAD)
m_head = value;
int oldPos = m_cursorPosition;
m_cursorPosition = value * m_scale;
const int offset = height() / 2;
const int x = qMin(oldPos, m_cursorPosition);
const int w = qAbs(oldPos - m_cursorPosition);
update(margin + x - offset, 0, w + 2 * offset, height());
return true;
}
void ScrubBar::paintEvent(QPaintEvent *e)
{
QPen pen(QBrush(palette().text().color()), 2);
QPainter p(this);
QRect r = e->rect();
p.setClipRect(r);
p.drawPixmap(0, 0, width(), height(), m_pixmap);
if (!isEnabled()) return;
// draw pointer
QPolygon pa(3);
const int x = selectionSize / 2 - 1;
int head = margin + m_cursorPosition;
pa.setPoints(3, head - x - 1, 0, head + x, 0, head, x);
p.setBrush(palette().text().color());
p.setPen(Qt::NoPen);
p.drawPolygon(pa);
p.setPen(pen);
if (m_head >= 0) {
head = margin + m_head * m_scale;
p.drawLine(head, 0, head, height() - 1);
}
// draw in point
if (m_in > -1) {
const int in = margin + m_in * m_scale;
pa.setPoints(3, in - selectionSize / 2, 0, in - selectionSize / 2, selectionSize - 1, in - 1, selectionSize / 2);
p.setBrush(palette().text().color());
p.setPen(Qt::NoPen);
p.drawPolygon(pa);
p.setPen(pen);
p.drawLine(in, 0, in, selectionSize - 1);
}
// draw out point
if (m_out > -1) {
const int out = margin + m_out * m_scale;
pa.setPoints(3, out + selectionSize / 2, 0, out + selectionSize / 2, selectionSize - 1, out, selectionSize / 2);
p.setBrush(palette().text().color());
p.setPen(Qt::NoPen);
p.drawPolygon(pa);
p.setPen(pen);
p.drawLine(out, 0, out, selectionSize - 1);
}
}
void ScrubBar::resizeEvent(QResizeEvent *)
{
setScale(m_max);
}
bool ScrubBar::event(QEvent *event)
{
QWidget::event(event);
if (event->type() == QEvent::PaletteChange || event->type() == QEvent::StyleChange)
updatePixmap();
return false;
}
void ScrubBar::updatePixmap()
{
const int ratio = devicePixelRatio();
const int l_width = width() * ratio;
const int l_height = height() * ratio;
const int l_margin = margin * ratio;
const int l_selectionSize = selectionSize * ratio;
const int l_interval = m_interval * ratio;
const int l_timecodeWidth = m_timecodeWidth * ratio;
m_pixmap = QPixmap(l_width, l_height);
m_pixmap.fill(palette().window().color());
QPainter p(&m_pixmap);
p.setFont(font());
const int markerHeight = fontMetrics().ascent() + 2 * ratio;
if (!isEnabled()) {
p.fillRect(0, 0, l_width, l_height, palette().background().color());
p.end();
update();
return;
}
// background color
p.fillRect(l_margin, 0, l_width - 2 * l_margin, l_height, palette().base().color());
// selected region
if (m_in > -1 && m_out > m_in) {
const int in = m_in * m_scale * ratio;
const int out = m_out * m_scale * ratio;
p.fillRect(l_margin + in, 0, out - in, l_selectionSize, palette().highlight().color());
}
// draw time ticks
QPen pen = QPen(palette().text().color());
pen.setWidth(ratio);
p.setPen(pen);
if (l_interval > 2) {
for (int x = l_margin; x < l_width - l_margin; x += l_interval) {
p.drawLine(x, l_selectionSize, x, l_height - 1);
if (x + l_interval / 4 < l_width - l_margin)
p.drawLine(x + l_interval / 4, l_height - 3 * ratio, x + l_interval / 4, l_height - 1);
if (x + l_interval / 2 < l_width - l_margin)
p.drawLine(x + l_interval / 2, l_height - 7 * ratio, x + l_interval / 2, l_height - 1);
if (x + l_interval * 3 / 4 < l_width - l_margin)
p.drawLine(x + l_interval * 3 / 4, l_height - 3 * ratio, x + l_interval * 3 / 4, l_height - 1);
}
}
// draw timecode
if (l_interval > l_timecodeWidth && MLT.producer()) {
int x = l_margin;
for (int i = 0; x < l_width - l_margin - l_timecodeWidth; i++, x += l_interval) {
int y = l_selectionSize + fontMetrics().ascent() - 2 * ratio;
int frames = qRound(i * m_fps * m_secondsPerTick);
p.drawText(x + 2 * ratio, y, MLT.producer()->frames_to_time(frames));
}
}
// draw markers
if (m_in < 0 && m_out < 0) {
int i = 1;
foreach (int pos, m_markers) {
int x = l_margin + pos * m_scale * ratio;
QString s = QString::number(i++);
int markerWidth = fontMetrics().width(s) * 1.5;
p.fillRect(x, 0, 1, l_height, palette().highlight().color());
p.fillRect(x - markerWidth/2, 0, markerWidth, markerHeight, palette().highlight().color());
p.drawText(x - markerWidth/3, markerHeight - 2 * ratio, s);
}
}
p.end();
update();
}