1 #include "mainwindow.h"
5 #include "post_to_main_thread.h"
7 #include "ui_mainwindow.h"
12 #include <QMouseEvent>
13 #include <QWheelEvent>
18 MainWindow *global_mainwindow = nullptr;
19 ClipList *cliplist_clips;
20 PlayList *playlist_clips;
22 extern int64_t current_pts;
23 extern mutex frame_mu;
24 extern vector<int64_t> frames[MAX_STREAMS];
26 MainWindow::MainWindow()
27 : ui(new Ui::MainWindow)
29 global_mainwindow = this;
32 cliplist_clips = new ClipList();
33 ui->clip_list->setModel(cliplist_clips);
35 playlist_clips = new PlayList();
36 ui->playlist->setModel(playlist_clips);
38 // For scrubbing in the pts columns.
39 ui->clip_list->viewport()->installEventFilter(this);
40 ui->playlist->viewport()->installEventFilter(this);
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, this, &MainWindow::cue_in_clicked);
46 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
47 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
48 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
50 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
51 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
52 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
54 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
55 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
56 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
58 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
59 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
60 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
62 QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
63 connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
64 connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
65 connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
66 ui->input1_display->set_overlay("1");
68 QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
69 connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
70 connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
71 connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
72 ui->input2_display->set_overlay("2");
74 QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
75 connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
76 connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
77 connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
78 ui->input3_display->set_overlay("3");
80 QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
81 connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
82 connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
83 connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
84 ui->input4_display->set_overlay("4");
86 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
88 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
89 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
90 connect(delete_key, &QShortcut::activated, [this] {
91 if (ui->playlist->hasFocus()) {
96 // TODO: support drag-and-drop.
97 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
98 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
100 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
101 this, &MainWindow::playlist_selection_changed);
102 playlist_selection_changed(); // First time set-up.
104 preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
105 live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
106 live_player->set_done_callback([this]{
107 post_to_main_thread([this]{
108 live_player_clip_done();
111 live_player->set_progress_callback([this](double played_this_clip, double total_length) {
112 post_to_main_thread([this, played_this_clip, total_length] {
113 live_player_clip_progress(played_this_clip, total_length);
118 void MainWindow::cue_in_clicked()
120 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
121 cliplist_clips->mutable_back()->pts_in = current_pts;
125 clip.pts_in = current_pts;
126 cliplist_clips->add_clip(clip);
127 playlist_selection_changed();
130 void MainWindow::cue_out_clicked()
132 if (!cliplist_clips->empty()) {
133 cliplist_clips->mutable_back()->pts_out = current_pts;
134 // TODO: select the row in the clip list?
138 void MainWindow::queue_clicked()
140 if (cliplist_clips->empty()) {
144 QItemSelectionModel *selected = ui->clip_list->selectionModel();
145 if (!selected->hasSelection()) {
146 Clip clip = *cliplist_clips->back();
148 if (clip.pts_out != -1) {
149 playlist_clips->add_clip(clip);
150 playlist_selection_changed();
155 QModelIndex index = selected->currentIndex();
156 Clip clip = *cliplist_clips->clip(index.row());
157 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
158 index.column() <= int(ClipList::Column::CAMERA_4)) {
159 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
161 clip.stream_idx = ui->preview_display->get_stream_idx();
164 if (clip.pts_out != -1) {
165 playlist_clips->add_clip(clip);
166 playlist_selection_changed();
170 void MainWindow::preview_clicked()
172 if (cliplist_clips->empty()) return;
174 QItemSelectionModel *selected = ui->clip_list->selectionModel();
175 if (!selected->hasSelection()) {
176 preview_player->play_clip(*cliplist_clips->back(), 0);
180 QModelIndex index = selected->currentIndex();
182 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
183 index.column() <= int(ClipList::Column::CAMERA_4)) {
184 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
186 stream_idx = ui->preview_display->get_stream_idx();
188 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
191 void MainWindow::preview_angle_clicked(unsigned stream_idx)
193 preview_player->override_angle(stream_idx);
195 // Change the selection if we were previewing a clip from the clip list.
196 // (The only other thing we could be showing is a pts scrub, and if so,
197 // that would be selected.)
198 QItemSelectionModel *selected = ui->clip_list->selectionModel();
199 if (selected->hasSelection()) {
200 QModelIndex cell = selected->selectedIndexes()[0];
201 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
202 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
206 void MainWindow::playlist_duplicate()
208 QItemSelectionModel *selected = ui->playlist->selectionModel();
209 if (!selected->hasSelection()) {
210 // Should have been grayed out, but OK.
213 QModelIndexList rows = selected->selectedRows();
214 int first = rows.front().row(), last = rows.back().row();
215 playlist_clips->duplicate_clips(first, last);
216 playlist_selection_changed();
219 void MainWindow::playlist_remove()
221 QItemSelectionModel *selected = ui->playlist->selectionModel();
222 if (!selected->hasSelection()) {
223 // Should have been grayed out, but OK.
226 QModelIndexList rows = selected->selectedRows();
227 int first = rows.front().row(), last = rows.back().row();
228 playlist_clips->erase_clips(first, last);
230 // TODO: select the next one in the list?
232 playlist_selection_changed();
235 void MainWindow::playlist_move(int delta)
237 QItemSelectionModel *selected = ui->playlist->selectionModel();
238 if (!selected->hasSelection()) {
239 // Should have been grayed out, but OK.
243 QModelIndexList rows = selected->selectedRows();
244 int first = rows.front().row(), last = rows.back().row();
245 if ((delta == -1 && first == 0) ||
246 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
247 // Should have been grayed out, but OK.
251 playlist_clips->move_clips(first, last, delta);
252 playlist_selection_changed();
255 void MainWindow::play_clicked()
257 if (playlist_clips->empty()) return;
259 QItemSelectionModel *selected = ui->playlist->selectionModel();
261 if (!selected->hasSelection()) {
264 row = selected->selectedRows(0)[0].row();
267 const Clip &clip = *playlist_clips->clip(row);
268 live_player->play_clip(clip, clip.stream_idx);
269 playlist_clips->set_currently_playing(row, 0.0f);
270 playlist_selection_changed();
273 void MainWindow::live_player_clip_done()
275 int row = playlist_clips->get_currently_playing();
276 if (row != -1 && row < int(playlist_clips->size()) - 1) {
278 const Clip &clip = *playlist_clips->clip(row);
279 live_player->play_clip(clip, clip.stream_idx);
280 playlist_clips->set_currently_playing(row, 0.0f);
282 playlist_clips->set_currently_playing(-1, 0.0f);
283 ui->live_label->setText("Current output (paused)");
287 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
289 playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
291 double remaining = total_length - played_this_clip;
292 for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
293 const Clip clip = *playlist_clips->clip(row);
294 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed.
296 int remaining_ms = lrint(remaining * 1e3);
298 int ms = remaining_ms % 1000;
299 remaining_ms /= 1000;
300 int s = remaining_ms % 60;
302 int m = remaining_ms;
305 snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
306 ui->live_label->setText(buf);
309 void MainWindow::resizeEvent(QResizeEvent *event)
311 QMainWindow::resizeEvent(event);
313 // Ask for a relayout, but only after the event loop is done doing relayout
314 // on everything else.
315 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
318 void MainWindow::relayout()
320 ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
321 ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
324 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
326 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
327 constexpr int scrub_sensitivity = 100; // pts units per pixel.
328 constexpr int wheel_sensitivity = 100; // pts units per degree.
329 constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
331 unsigned stream_idx = ui->preview_display->get_stream_idx();
333 if (event->type() != QEvent::Wheel) {
334 last_mousewheel_camera_row = -1;
337 if (event->type() == QEvent::MouseButtonPress) {
338 QMouseEvent *mouse = (QMouseEvent *)event;
340 QTableView *destination;
343 if (watched == ui->clip_list->viewport()) {
344 destination = ui->clip_list;
345 type = SCRUBBING_CLIP_LIST;
346 } else if (watched == ui->playlist->viewport()) {
347 destination = ui->playlist;
348 type = SCRUBBING_PLAYLIST;
352 int column = destination->columnAt(mouse->x());
353 int row = destination->rowAt(mouse->y());
354 if (column == -1 || row == -1) return false;
356 if (type == SCRUBBING_CLIP_LIST) {
357 if (ClipList::Column(column) == ClipList::Column::IN) {
358 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
359 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
360 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
361 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
362 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
367 if (PlayList::Column(column) == PlayList::Column::IN) {
368 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
369 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
370 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
371 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
372 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
380 scrub_column = column;
381 scrub_x_origin = mouse->x();
383 } else if (event->type() == QEvent::MouseMove) {
385 QMouseEvent *mouse = (QMouseEvent *)event;
386 int offset = mouse->x() - scrub_x_origin;
388 if (offset >= dead_zone_pixels) {
389 adjusted_offset = offset - dead_zone_pixels;
390 } else if (offset < -dead_zone_pixels) {
391 adjusted_offset = offset + dead_zone_pixels;
396 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
398 if (scrub_type == SCRUBBING_CLIP_LIST) {
399 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
400 if (scrub_column == int(ClipList::Column::IN)) {
401 pts = std::max<int64_t>(pts, 0);
402 pts = std::min(pts, clip->pts_out);
404 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
406 pts = std::max(pts, clip->pts_in);
407 pts = std::min(pts, current_pts);
409 preview_single_frame(pts, stream_idx, LAST_BEFORE);
412 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
413 if (scrub_column == int(PlayList::Column::IN)) {
414 pts = std::max<int64_t>(pts, 0);
415 pts = std::min(pts, clip->pts_out);
417 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
419 pts = std::max(pts, clip->pts_in);
420 pts = std::min(pts, current_pts);
422 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
426 return true; // Don't use this mouse movement for selecting things.
428 } else if (event->type() == QEvent::Wheel) {
429 QWheelEvent *wheel = (QWheelEvent *)event;
431 QTableView *destination;
432 int in_column, out_column, camera_column;
433 if (watched == ui->clip_list->viewport()) {
434 destination = ui->clip_list;
435 in_column = int(ClipList::Column::IN);
436 out_column = int(ClipList::Column::OUT);
438 last_mousewheel_camera_row = -1;
439 } else if (watched == ui->playlist->viewport()) {
440 destination = ui->playlist;
441 in_column = int(PlayList::Column::IN);
442 out_column = int(PlayList::Column::OUT);
443 camera_column = int(PlayList::Column::CAMERA);
445 last_mousewheel_camera_row = -1;
448 int column = destination->columnAt(wheel->x());
449 int row = destination->rowAt(wheel->y());
450 if (column == -1 || row == -1) return false;
452 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
453 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
454 if (watched == ui->playlist->viewport()) {
455 stream_idx = clip->stream_idx;
458 if (column != camera_column) {
459 last_mousewheel_camera_row = -1;
461 if (column == in_column) {
462 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
463 pts = std::max<int64_t>(pts, 0);
464 pts = std::min(pts, clip->pts_out);
466 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
467 } else if (column == out_column) {
468 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
469 pts = std::max(pts, clip->pts_in);
470 pts = std::min(pts, current_pts);
472 preview_single_frame(pts, stream_idx, LAST_BEFORE);
473 } else if (column == camera_column) {
474 int angle_degrees = wheel->angleDelta().y();
475 if (last_mousewheel_camera_row == row) {
476 angle_degrees += leftover_angle_degrees;
479 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
480 stream_idx = std::max(stream_idx, 0);
481 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
482 clip->stream_idx = stream_idx;
484 last_mousewheel_camera_row = row;
485 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
487 // Don't update the live view, that's rarely what the operator wants.
489 } else if (event->type() == QEvent::MouseButtonRelease) {
495 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
497 if (rounding == LAST_BEFORE) {
498 lock_guard<mutex> lock(frame_mu);
499 if (frames[stream_idx].empty()) return;
500 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
501 if (it != frames[stream_idx].end()) {
505 assert(rounding == FIRST_AT_OR_AFTER);
506 lock_guard<mutex> lock(frame_mu);
507 if (frames[stream_idx].empty()) return;
508 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
509 if (it != frames[stream_idx].end()) {
515 fake_clip.pts_in = pts;
516 fake_clip.pts_out = pts + 1;
517 preview_player->play_clip(fake_clip, stream_idx);
520 void MainWindow::playlist_selection_changed()
522 QItemSelectionModel *selected = ui->playlist->selectionModel();
523 bool any_selected = selected->hasSelection();
524 ui->playlist_duplicate_btn->setEnabled(any_selected);
525 ui->playlist_remove_btn->setEnabled(any_selected);
526 ui->playlist_move_up_btn->setEnabled(
527 any_selected && selected->selectedRows().front().row() > 0);
528 ui->playlist_move_down_btn->setEnabled(
529 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
530 ui->play_btn->setEnabled(!playlist_clips->empty());