]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Implement scrubbing for the playlist.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "player.h"
5 #include "post_to_main_thread.h"
6 #include "ui_mainwindow.h"
7
8 #include <string>
9 #include <vector>
10
11 #include <QMouseEvent>
12 #include <QWheelEvent>
13 #include <QShortcut>
14
15 using namespace std;
16
17 MainWindow *global_mainwindow = nullptr;
18 ClipList *cliplist_clips;
19 PlayList *playlist_clips;
20
21 extern int64_t current_pts;
22 extern mutex frame_mu;
23 extern vector<int64_t> frames[MAX_STREAMS];
24
25 MainWindow::MainWindow()
26         : ui(new Ui::MainWindow)
27 {
28         global_mainwindow = this;
29         ui->setupUi(this);
30
31         cliplist_clips = new ClipList();
32         ui->clip_list->setModel(cliplist_clips);
33
34         playlist_clips = new PlayList();
35         ui->playlist->setModel(playlist_clips);
36
37         // For scrubbing in the pts columns.
38         ui->clip_list->viewport()->installEventFilter(this);
39         ui->playlist->viewport()->installEventFilter(this);
40
41         // TODO: These are too big for lambdas.
42         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
43         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
44         connect(ui->cue_in_btn, &QPushButton::clicked, []{
45                 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
46                         cliplist_clips->back()->pts_in = current_pts;
47                         return;
48                 }
49                 Clip clip;
50                 clip.pts_in = current_pts;
51                 cliplist_clips->add_clip(clip);
52         });
53
54         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
55         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
56         connect(ui->cue_out_btn, &QPushButton::clicked, []{
57                 if (!cliplist_clips->empty()) {
58                         cliplist_clips->back()->pts_out = current_pts;
59                         // TODO: select the row in the clip list?
60                 }
61         });
62
63         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
64         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
65         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
66
67         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
68         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
69         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
70
71         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
72         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
73         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
74
75         preview_player = new Player(ui->preview_display);
76         live_player = new Player(ui->live_display);
77         live_player->set_done_callback([this]{
78                 post_to_main_thread([this]{
79                         live_player_clip_done();
80                 });
81         });
82 }
83
84 void MainWindow::queue_clicked()
85 {
86         QItemSelectionModel *selected = ui->clip_list->selectionModel();
87         if (!selected->hasSelection()) {
88                 Clip clip = *cliplist_clips->back();
89                 clip.stream_idx = 0;
90                 playlist_clips->add_clip(clip);
91                 return;
92         }
93
94         QModelIndex index = selected->currentIndex();
95         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
96             index.column() <= int(ClipList::Column::CAMERA_4)) {
97                 Clip clip = *cliplist_clips->clip(index.row());
98                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
99                 playlist_clips->add_clip(clip);
100         }
101 }
102
103 void MainWindow::preview_clicked()
104 {
105         if (cliplist_clips->empty()) return;
106
107         QItemSelectionModel *selected = ui->clip_list->selectionModel();
108         if (!selected->hasSelection()) {
109                 preview_player->play_clip(*cliplist_clips->back(), 0);
110                 return;
111         }
112
113         QModelIndex index = selected->currentIndex();
114         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
115             index.column() <= int(ClipList::Column::CAMERA_4)) {
116                 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
117                 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
118         }
119 }
120
121 void MainWindow::play_clicked()
122 {
123         if (playlist_clips->empty()) return;
124
125         QItemSelectionModel *selected = ui->playlist->selectionModel();
126         int row;
127         if (!selected->hasSelection()) {
128                 row = 0;
129         } else {
130                 row = selected->selectedRows(0)[0].row();
131         }
132
133         const Clip &clip = *playlist_clips->clip(row);
134         live_player->play_clip(clip, clip.stream_idx);
135         playlist_clips->set_currently_playing(row);
136 }
137
138 void MainWindow::live_player_clip_done()
139 {
140         int row = playlist_clips->get_currently_playing();
141         if (row != -1 && row < int(playlist_clips->size()) - 1) {
142                 ++row;
143                 const Clip &clip = *playlist_clips->clip(row);
144                 live_player->play_clip(clip, clip.stream_idx);
145                 playlist_clips->set_currently_playing(row);
146         } else {
147                 playlist_clips->set_currently_playing(-1);
148         }
149 }
150
151 void MainWindow::resizeEvent(QResizeEvent *event)
152 {
153         QMainWindow::resizeEvent(event);
154
155         // Ask for a relayout, but only after the event loop is done doing relayout
156         // on everything else.
157         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
158 }
159
160 void MainWindow::relayout()
161 {
162         ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
163 }
164
165 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
166 {
167         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
168         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
169         constexpr int wheel_sensitivity = 100;  // pts units per degree.
170
171         unsigned stream_idx = ui->preview_display->get_stream_idx();
172
173         if (event->type() == QEvent::MouseButtonPress) {
174                 QMouseEvent *mouse = (QMouseEvent *)event;
175
176                 QTableView *destination;
177                 ScrubType type;
178
179                 if (watched == ui->clip_list->viewport()) {
180                         destination = ui->clip_list;
181                         type = SCRUBBING_CLIP_LIST;
182                 } else if (watched == ui->playlist->viewport()) {
183                         destination = ui->playlist;
184                         type = SCRUBBING_PLAYLIST;
185                 } else {
186                         return false;
187                 }
188                 int column = destination->columnAt(mouse->x());
189                 int row = destination->rowAt(mouse->y());
190                 if (column == -1 || row == -1) return false;
191
192                 if (type == SCRUBBING_CLIP_LIST) {
193                         if (ClipList::Column(column) == ClipList::Column::IN) {
194                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
195                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
196                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
197                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
198                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
199                         } else {
200                                 return false;
201                         }
202                 } else {
203                         if (PlayList::Column(column) == PlayList::Column::IN) {
204                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
205                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
206                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
207                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
208                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
209                         } else {
210                                 return false;
211                         }
212                 }
213
214                 scrubbing = true;
215                 scrub_row = row;
216                 scrub_column = column;
217                 scrub_x_origin = mouse->x();
218                 scrub_type = type;
219         } else if (event->type() == QEvent::MouseMove) {
220                 if (scrubbing) {
221                         QMouseEvent *mouse = (QMouseEvent *)event;
222                         int offset = mouse->x() - scrub_x_origin;
223                         int adjusted_offset;
224                         if (offset >= dead_zone_pixels) {
225                                 adjusted_offset = offset - dead_zone_pixels;
226                         } else if (offset < -dead_zone_pixels) {
227                                 adjusted_offset = offset + dead_zone_pixels;
228                         } else {
229                                 adjusted_offset = 0;
230                         }
231
232                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
233
234                         if (scrub_type == SCRUBBING_CLIP_LIST) {
235                                 if (scrub_column == int(ClipList::Column::IN)) {
236                                         pts = std::max<int64_t>(pts, 0);
237                                         pts = std::min(pts, cliplist_clips->clip(scrub_row)->pts_out);
238                                         cliplist_clips->clip(scrub_row)->pts_in = pts;
239                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
240                                 } else {
241                                         pts = std::max(pts, cliplist_clips->clip(scrub_row)->pts_in);
242                                         pts = std::min(pts, current_pts);
243                                         cliplist_clips->clip(scrub_row)->pts_out = pts;
244                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
245                                 }
246                         } else {
247                                 if (scrub_column == int(PlayList::Column::IN)) {
248                                         pts = std::max<int64_t>(pts, 0);
249                                         pts = std::min(pts, playlist_clips->clip(scrub_row)->pts_out);
250                                         playlist_clips->clip(scrub_row)->pts_in = pts;
251                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
252                                 } else {
253                                         pts = std::max(pts, playlist_clips->clip(scrub_row)->pts_in);
254                                         pts = std::min(pts, current_pts);
255                                         playlist_clips->clip(scrub_row)->pts_out = pts;
256                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
257                                 }
258                         }
259
260                         return true;  // Don't use this mouse movement for selecting things.
261                 }
262         } else if (event->type() == QEvent::Wheel) {
263                 QWheelEvent *wheel = (QWheelEvent *)event;
264
265                 QTableView *destination;
266                 int in_column, out_column;
267                 if (watched == ui->clip_list->viewport()) {
268                         destination = ui->clip_list;
269                         in_column = int(ClipList::Column::IN);
270                         out_column = int(ClipList::Column::OUT);
271                 } else if (watched == ui->playlist->viewport()) {
272                         destination = ui->playlist;
273                         in_column = int(PlayList::Column::IN);
274                         out_column = int(PlayList::Column::OUT);
275                 } else {
276                         return false;
277                 }
278                 int column = destination->columnAt(wheel->x());
279                 int row = destination->rowAt(wheel->y());
280                 if (column == -1 || row == -1) return false;
281
282                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
283                         cliplist_clips->clip(row) : playlist_clips->clip(row);
284
285                 if (column == in_column) {
286                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
287                         pts = std::max<int64_t>(pts, 0);
288                         pts = std::min(pts, clip->pts_out);
289                         clip->pts_in = pts;
290                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
291                 } else if (column == out_column) {
292                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
293                         pts = std::max(pts, clip->pts_in);
294                         pts = std::min(pts, current_pts);
295                         clip->pts_out = pts;
296                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
297                 }
298         } else if (event->type() == QEvent::MouseButtonRelease) {
299                 scrubbing = false;
300         }
301         return false;
302 }
303
304 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
305 {
306         if (rounding == LAST_BEFORE) {
307                 lock_guard<mutex> lock(frame_mu);
308                 if (frames[stream_idx].empty()) return;
309                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
310                 if (it != frames[stream_idx].end()) {
311                         pts = *it;
312                 }
313         } else {
314                 assert(rounding == FIRST_AT_OR_AFTER);
315                 lock_guard<mutex> lock(frame_mu);
316                 if (frames[stream_idx].empty()) return;
317                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
318                 if (it != frames[stream_idx].end()) {
319                         pts = *it;
320                 }
321         }
322
323         Clip fake_clip;
324         fake_clip.pts_in = pts;
325         fake_clip.pts_out = pts + 1;
326         preview_player->play_clip(fake_clip, stream_idx);
327 }