]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Add some 1-4 overlays on the previews.
[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         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
42         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
43         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
44
45         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
46         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
47         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
48
49         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
50         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
51         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
52
53         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
54         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
55         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
56
57         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
58         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
59         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
60
61         QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
62         connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
63         connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
64         connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
65         ui->input1_display->set_overlay("1");
66
67         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
68         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
69         connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
70         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
71         ui->input2_display->set_overlay("2");
72
73         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
74         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
75         connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
76         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
77         ui->input3_display->set_overlay("3");
78
79         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
80         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
81         connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
82         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
83         ui->input4_display->set_overlay("4");
84
85         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
86
87         // TODO: support the delete key iff the widget has focus?
88         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
89
90         // TODO: support drag-and-drop.
91         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
92         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
93
94         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
95                 this, &MainWindow::playlist_selection_changed);
96         playlist_selection_changed();  // First time set-up.
97
98         preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
99         live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
100         live_player->set_done_callback([this]{
101                 post_to_main_thread([this]{
102                         live_player_clip_done();
103                 });
104         });
105 }
106
107 void MainWindow::cue_in_clicked()
108 {
109         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
110                 cliplist_clips->back()->pts_in = current_pts;
111                 return;
112         }
113         Clip clip;
114         clip.pts_in = current_pts;
115         cliplist_clips->add_clip(clip);
116         playlist_selection_changed();
117 }
118
119 void MainWindow::cue_out_clicked()
120 {
121         if (!cliplist_clips->empty()) {
122                 cliplist_clips->back()->pts_out = current_pts;
123                 // TODO: select the row in the clip list?
124         }
125 }
126
127 void MainWindow::queue_clicked()
128 {
129         if (cliplist_clips->empty()) {
130                 return;
131         }
132
133         QItemSelectionModel *selected = ui->clip_list->selectionModel();
134         if (!selected->hasSelection()) {
135                 Clip clip = *cliplist_clips->back();
136                 clip.stream_idx = 0;
137                 playlist_clips->add_clip(clip);
138                 return;
139         }
140
141         QModelIndex index = selected->currentIndex();
142         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
143             index.column() <= int(ClipList::Column::CAMERA_4)) {
144                 Clip clip = *cliplist_clips->clip(index.row());
145                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
146                 playlist_clips->add_clip(clip);
147                 playlist_selection_changed();
148         }
149 }
150
151 void MainWindow::preview_clicked()
152 {
153         if (cliplist_clips->empty()) return;
154
155         QItemSelectionModel *selected = ui->clip_list->selectionModel();
156         if (!selected->hasSelection()) {
157                 preview_player->play_clip(*cliplist_clips->back(), 0);
158                 return;
159         }
160
161         QModelIndex index = selected->currentIndex();
162         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
163             index.column() <= int(ClipList::Column::CAMERA_4)) {
164                 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
165                 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
166         }
167 }
168
169 void MainWindow::preview_angle_clicked(unsigned stream_idx)
170 {
171         preview_player->override_angle(stream_idx);
172
173         // Change the selection if we were previewing a clip from the clip list.
174         // (The only other thing we could be showing is a pts scrub, and if so,
175         // that would be selected.)
176         QItemSelectionModel *selected = ui->clip_list->selectionModel();
177         if (selected->hasSelection()) {
178                 QModelIndex cell = selected->selectedIndexes()[0];
179                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
180                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
181         }
182 }
183
184 void MainWindow::playlist_duplicate()
185 {
186         QItemSelectionModel *selected = ui->playlist->selectionModel();
187         if (!selected->hasSelection()) {
188                 // Should have been grayed out, but OK.
189                 return;
190         }
191         QModelIndexList rows = selected->selectedRows();
192         int first = rows.front().row(), last = rows.back().row();
193         playlist_clips->duplicate_clips(first, last);
194         playlist_selection_changed();
195 }
196
197 void MainWindow::playlist_remove()
198 {
199         QItemSelectionModel *selected = ui->playlist->selectionModel();
200         if (!selected->hasSelection()) {
201                 // Should have been grayed out, but OK.
202                 return;
203         }
204         QModelIndexList rows = selected->selectedRows();
205         int first = rows.front().row(), last = rows.back().row();
206         playlist_clips->erase_clips(first, last);
207
208         // TODO: select the next one in the list?
209
210         playlist_selection_changed();
211 }
212
213 void MainWindow::playlist_move(int delta)
214 {
215         QItemSelectionModel *selected = ui->playlist->selectionModel();
216         if (!selected->hasSelection()) {
217                 // Should have been grayed out, but OK.
218                 return;
219         }
220
221         QModelIndexList rows = selected->selectedRows();
222         int first = rows.front().row(), last = rows.back().row();
223         if ((delta == -1 && first == 0) ||
224             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
225                 // Should have been grayed out, but OK.
226                 return;
227         }
228
229         playlist_clips->move_clips(first, last, delta);
230         playlist_selection_changed();
231 }
232
233 void MainWindow::play_clicked()
234 {
235         if (playlist_clips->empty()) return;
236
237         QItemSelectionModel *selected = ui->playlist->selectionModel();
238         int row;
239         if (!selected->hasSelection()) {
240                 row = 0;
241         } else {
242                 row = selected->selectedRows(0)[0].row();
243         }
244
245         const Clip &clip = *playlist_clips->clip(row);
246         live_player->play_clip(clip, clip.stream_idx);
247         playlist_clips->set_currently_playing(row);
248         playlist_selection_changed();
249 }
250
251 void MainWindow::live_player_clip_done()
252 {
253         int row = playlist_clips->get_currently_playing();
254         if (row != -1 && row < int(playlist_clips->size()) - 1) {
255                 ++row;
256                 const Clip &clip = *playlist_clips->clip(row);
257                 live_player->play_clip(clip, clip.stream_idx);
258                 playlist_clips->set_currently_playing(row);
259         } else {
260                 playlist_clips->set_currently_playing(-1);
261         }
262 }
263
264 void MainWindow::resizeEvent(QResizeEvent *event)
265 {
266         QMainWindow::resizeEvent(event);
267
268         // Ask for a relayout, but only after the event loop is done doing relayout
269         // on everything else.
270         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
271 }
272
273 void MainWindow::relayout()
274 {
275         ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
276 }
277
278 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
279 {
280         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
281         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
282         constexpr int wheel_sensitivity = 100;  // pts units per degree.
283         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
284
285         unsigned stream_idx = ui->preview_display->get_stream_idx();
286
287         if (event->type() != QEvent::Wheel) {
288                 last_mousewheel_camera_row = -1;
289         }
290
291         if (event->type() == QEvent::MouseButtonPress) {
292                 QMouseEvent *mouse = (QMouseEvent *)event;
293
294                 QTableView *destination;
295                 ScrubType type;
296
297                 if (watched == ui->clip_list->viewport()) {
298                         destination = ui->clip_list;
299                         type = SCRUBBING_CLIP_LIST;
300                 } else if (watched == ui->playlist->viewport()) {
301                         destination = ui->playlist;
302                         type = SCRUBBING_PLAYLIST;
303                 } else {
304                         return false;
305                 }
306                 int column = destination->columnAt(mouse->x());
307                 int row = destination->rowAt(mouse->y());
308                 if (column == -1 || row == -1) return false;
309
310                 if (type == SCRUBBING_CLIP_LIST) {
311                         if (ClipList::Column(column) == ClipList::Column::IN) {
312                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
313                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
314                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
315                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
316                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
317                         } else {
318                                 return false;
319                         }
320                 } else {
321                         if (PlayList::Column(column) == PlayList::Column::IN) {
322                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
323                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
324                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
325                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
326                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
327                         } else {
328                                 return false;
329                         }
330                 }
331
332                 scrubbing = true;
333                 scrub_row = row;
334                 scrub_column = column;
335                 scrub_x_origin = mouse->x();
336                 scrub_type = type;
337         } else if (event->type() == QEvent::MouseMove) {
338                 if (scrubbing) {
339                         QMouseEvent *mouse = (QMouseEvent *)event;
340                         int offset = mouse->x() - scrub_x_origin;
341                         int adjusted_offset;
342                         if (offset >= dead_zone_pixels) {
343                                 adjusted_offset = offset - dead_zone_pixels;
344                         } else if (offset < -dead_zone_pixels) {
345                                 adjusted_offset = offset + dead_zone_pixels;
346                         } else {
347                                 adjusted_offset = 0;
348                         }
349
350                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
351
352                         if (scrub_type == SCRUBBING_CLIP_LIST) {
353                                 ClipProxy clip = cliplist_clips->clip(scrub_row);
354                                 if (scrub_column == int(ClipList::Column::IN)) {
355                                         pts = std::max<int64_t>(pts, 0);
356                                         pts = std::min(pts, clip->pts_out);
357                                         clip->pts_in = pts;
358                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
359                                 } else {
360                                         pts = std::max(pts, clip->pts_in);
361                                         pts = std::min(pts, current_pts);
362                                         clip->pts_out = pts;
363                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
364                                 }
365                         } else {
366                                 ClipProxy clip = playlist_clips->clip(scrub_row);
367                                 if (scrub_column == int(PlayList::Column::IN)) {
368                                         pts = std::max<int64_t>(pts, 0);
369                                         pts = std::min(pts, clip->pts_out);
370                                         clip->pts_in = pts;
371                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
372                                 } else {
373                                         pts = std::max(pts, clip->pts_in);
374                                         pts = std::min(pts, current_pts);
375                                         clip->pts_out = pts;
376                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
377                                 }
378                         }
379
380                         return true;  // Don't use this mouse movement for selecting things.
381                 }
382         } else if (event->type() == QEvent::Wheel) {
383                 QWheelEvent *wheel = (QWheelEvent *)event;
384
385                 QTableView *destination;
386                 int in_column, out_column, camera_column;
387                 if (watched == ui->clip_list->viewport()) {
388                         destination = ui->clip_list;
389                         in_column = int(ClipList::Column::IN);
390                         out_column = int(ClipList::Column::OUT);
391                         camera_column = -1;
392                         last_mousewheel_camera_row = -1;
393                 } else if (watched == ui->playlist->viewport()) {
394                         destination = ui->playlist;
395                         in_column = int(PlayList::Column::IN);
396                         out_column = int(PlayList::Column::OUT);
397                         camera_column = int(PlayList::Column::CAMERA);
398                 } else {
399                         last_mousewheel_camera_row = -1;
400                         return false;
401                 }
402                 int column = destination->columnAt(wheel->x());
403                 int row = destination->rowAt(wheel->y());
404                 if (column == -1 || row == -1) return false;
405
406                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
407                         cliplist_clips->clip(row) : playlist_clips->clip(row);
408                 if (watched == ui->playlist->viewport()) {
409                         stream_idx = clip->stream_idx;
410                 }
411
412                 if (column != camera_column) {
413                         last_mousewheel_camera_row = -1;
414                 }
415                 if (column == in_column) {
416                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
417                         pts = std::max<int64_t>(pts, 0);
418                         pts = std::min(pts, clip->pts_out);
419                         clip->pts_in = pts;
420                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
421                 } else if (column == out_column) {
422                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
423                         pts = std::max(pts, clip->pts_in);
424                         pts = std::min(pts, current_pts);
425                         clip->pts_out = pts;
426                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
427                 } else if (column == camera_column) {
428                         int angle_degrees = wheel->angleDelta().y();
429                         if (last_mousewheel_camera_row == row) {
430                                 angle_degrees += leftover_angle_degrees;
431                         }
432
433                         int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
434                         stream_idx = std::max(stream_idx, 0);
435                         stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
436                         clip->stream_idx = stream_idx;
437
438                         last_mousewheel_camera_row = row;
439                         leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
440
441                         // Don't update the live view, that's rarely what the operator wants.
442                 }
443         } else if (event->type() == QEvent::MouseButtonRelease) {
444                 scrubbing = false;
445         }
446         return false;
447 }
448
449 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
450 {
451         if (rounding == LAST_BEFORE) {
452                 lock_guard<mutex> lock(frame_mu);
453                 if (frames[stream_idx].empty()) return;
454                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
455                 if (it != frames[stream_idx].end()) {
456                         pts = *it;
457                 }
458         } else {
459                 assert(rounding == FIRST_AT_OR_AFTER);
460                 lock_guard<mutex> lock(frame_mu);
461                 if (frames[stream_idx].empty()) return;
462                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
463                 if (it != frames[stream_idx].end()) {
464                         pts = *it;
465                 }
466         }
467
468         Clip fake_clip;
469         fake_clip.pts_in = pts;
470         fake_clip.pts_out = pts + 1;
471         preview_player->play_clip(fake_clip, stream_idx);
472 }
473
474 void MainWindow::playlist_selection_changed()
475 {
476         QItemSelectionModel *selected = ui->playlist->selectionModel();
477         bool any_selected = selected->hasSelection();
478         ui->playlist_duplicate_btn->setEnabled(any_selected);
479         ui->playlist_remove_btn->setEnabled(any_selected);
480         ui->playlist_move_up_btn->setEnabled(
481                 any_selected && selected->selectedRows().front().row() > 0);
482         ui->playlist_move_down_btn->setEnabled(
483                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
484         ui->play_btn->setEnabled(!playlist_clips->empty());
485 }