1 #include "mainwindow.h"
4 #include "disk_space_estimator.h"
6 #include "post_to_main_thread.h"
8 #include "ui_mainwindow.h"
13 #include <QMouseEvent>
14 #include <QWheelEvent>
21 using namespace std::placeholders;
23 MainWindow *global_mainwindow = nullptr;
24 ClipList *cliplist_clips;
25 PlayList *playlist_clips;
27 extern int64_t current_pts;
28 extern mutex frame_mu;
29 extern vector<int64_t> frames[MAX_STREAMS];
31 MainWindow::MainWindow()
32 : ui(new Ui::MainWindow),
35 global_mainwindow = this;
39 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
41 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
42 disk_free_label = new QLabel(this);
43 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
44 ui->menuBar->setCornerWidget(disk_free_label);
46 StateProto state = db.get_state();
48 cliplist_clips = new ClipList(state.clip_list());
49 ui->clip_list->setModel(cliplist_clips);
50 connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
52 playlist_clips = new PlayList(state.play_list());
53 ui->playlist->setModel(playlist_clips);
54 connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
56 // For scrubbing in the pts columns.
57 ui->clip_list->viewport()->installEventFilter(this);
58 ui->playlist->viewport()->installEventFilter(this);
60 QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
61 connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
62 connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
64 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
65 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
66 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
68 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
69 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
70 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
72 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
73 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
74 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
76 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
77 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
78 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
80 QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
81 connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
82 connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
83 connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
84 ui->input1_display->set_overlay("1");
86 QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
87 connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
88 connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
89 connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
90 ui->input2_display->set_overlay("2");
92 QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
93 connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
94 connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
95 connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
96 ui->input3_display->set_overlay("3");
98 QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
99 connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
100 connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
101 connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
102 ui->input4_display->set_overlay("4");
104 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
106 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
107 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
108 connect(delete_key, &QShortcut::activated, [this] {
109 if (ui->playlist->hasFocus()) {
114 // TODO: support drag-and-drop.
115 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
116 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
118 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
119 this, &MainWindow::playlist_selection_changed);
120 playlist_selection_changed(); // First time set-up.
122 preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
123 live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
124 live_player->set_done_callback([this]{
125 post_to_main_thread([this]{
126 live_player_clip_done();
129 live_player->set_progress_callback([this](double played_this_clip, double total_length) {
130 post_to_main_thread([this, played_this_clip, total_length] {
131 live_player_clip_progress(played_this_clip, total_length);
135 defer_timeout = new QTimer(this);
136 defer_timeout->setSingleShot(true);
137 connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
140 void MainWindow::cue_in_clicked()
142 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
143 cliplist_clips->mutable_back()->pts_in = current_pts;
147 clip.pts_in = current_pts;
148 cliplist_clips->add_clip(clip);
149 playlist_selection_changed();
152 void MainWindow::cue_out_clicked()
154 if (!cliplist_clips->empty()) {
155 cliplist_clips->mutable_back()->pts_out = current_pts;
156 // TODO: select the row in the clip list?
160 void MainWindow::queue_clicked()
162 if (cliplist_clips->empty()) {
166 QItemSelectionModel *selected = ui->clip_list->selectionModel();
167 if (!selected->hasSelection()) {
168 Clip clip = *cliplist_clips->back();
170 if (clip.pts_out != -1) {
171 playlist_clips->add_clip(clip);
172 playlist_selection_changed();
177 QModelIndex index = selected->currentIndex();
178 Clip clip = *cliplist_clips->clip(index.row());
179 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
180 index.column() <= int(ClipList::Column::CAMERA_4)) {
181 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
183 clip.stream_idx = ui->preview_display->get_stream_idx();
186 if (clip.pts_out != -1) {
187 playlist_clips->add_clip(clip);
188 playlist_selection_changed();
192 void MainWindow::preview_clicked()
194 if (cliplist_clips->empty()) return;
196 QItemSelectionModel *selected = ui->clip_list->selectionModel();
197 if (!selected->hasSelection()) {
198 preview_player->play_clip(*cliplist_clips->back(), 0);
202 QModelIndex index = selected->currentIndex();
204 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
205 index.column() <= int(ClipList::Column::CAMERA_4)) {
206 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
208 stream_idx = ui->preview_display->get_stream_idx();
210 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
213 void MainWindow::preview_angle_clicked(unsigned stream_idx)
215 preview_player->override_angle(stream_idx);
217 // Change the selection if we were previewing a clip from the clip list.
218 // (The only other thing we could be showing is a pts scrub, and if so,
219 // that would be selected.)
220 QItemSelectionModel *selected = ui->clip_list->selectionModel();
221 if (selected->hasSelection()) {
222 QModelIndex cell = selected->selectedIndexes()[0];
223 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
224 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
228 void MainWindow::playlist_duplicate()
230 QItemSelectionModel *selected = ui->playlist->selectionModel();
231 if (!selected->hasSelection()) {
232 // Should have been grayed out, but OK.
235 QModelIndexList rows = selected->selectedRows();
236 int first = rows.front().row(), last = rows.back().row();
237 playlist_clips->duplicate_clips(first, last);
238 playlist_selection_changed();
241 void MainWindow::playlist_remove()
243 QItemSelectionModel *selected = ui->playlist->selectionModel();
244 if (!selected->hasSelection()) {
245 // Should have been grayed out, but OK.
248 QModelIndexList rows = selected->selectedRows();
249 int first = rows.front().row(), last = rows.back().row();
250 playlist_clips->erase_clips(first, last);
252 // TODO: select the next one in the list?
254 playlist_selection_changed();
257 void MainWindow::playlist_move(int delta)
259 QItemSelectionModel *selected = ui->playlist->selectionModel();
260 if (!selected->hasSelection()) {
261 // Should have been grayed out, but OK.
265 QModelIndexList rows = selected->selectedRows();
266 int first = rows.front().row(), last = rows.back().row();
267 if ((delta == -1 && first == 0) ||
268 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
269 // Should have been grayed out, but OK.
273 playlist_clips->move_clips(first, last, delta);
274 playlist_selection_changed();
277 void MainWindow::defer_timer_expired()
279 state_changed(deferred_state);
282 void MainWindow::content_changed()
284 if (defer_timeout->isActive() &&
285 (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
286 // There's some deferred event waiting, but this event is unrelated.
287 // So it's time to short-circuit that timer and do the work it wanted to do.
288 defer_timeout->stop();
289 state_changed(deferred_state);
292 *state.mutable_clip_list() = cliplist_clips->serialize();
293 *state.mutable_play_list() = playlist_clips->serialize();
294 if (currently_deferring_model_changes) {
295 deferred_change_id = current_change_id;
296 deferred_state = std::move(state);
297 defer_timeout->start(200);
300 state_changed(state);
303 void MainWindow::state_changed(const StateProto &state)
305 db.store_state(state);
308 void MainWindow::play_clicked()
310 if (playlist_clips->empty()) return;
312 QItemSelectionModel *selected = ui->playlist->selectionModel();
314 if (!selected->hasSelection()) {
317 row = selected->selectedRows(0)[0].row();
320 const Clip &clip = *playlist_clips->clip(row);
321 live_player->play_clip(clip, clip.stream_idx);
322 playlist_clips->set_currently_playing(row, 0.0f);
323 playlist_selection_changed();
326 void MainWindow::live_player_clip_done()
328 int row = playlist_clips->get_currently_playing();
329 if (row != -1 && row < int(playlist_clips->size()) - 1) {
331 const Clip &clip = *playlist_clips->clip(row);
332 live_player->play_clip(clip, clip.stream_idx);
333 playlist_clips->set_currently_playing(row, 0.0f);
335 playlist_clips->set_currently_playing(-1, 0.0f);
336 ui->live_label->setText("Current output (paused)");
340 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
342 playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
344 double remaining = total_length - played_this_clip;
345 for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
346 const Clip clip = *playlist_clips->clip(row);
347 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed.
349 int remaining_ms = lrint(remaining * 1e3);
351 int ms = remaining_ms % 1000;
352 remaining_ms /= 1000;
353 int s = remaining_ms % 60;
355 int m = remaining_ms;
358 snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
359 ui->live_label->setText(buf);
362 void MainWindow::resizeEvent(QResizeEvent *event)
364 QMainWindow::resizeEvent(event);
366 // Ask for a relayout, but only after the event loop is done doing relayout
367 // on everything else.
368 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
371 void MainWindow::relayout()
373 ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
374 ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
377 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
379 pts = std::max<int64_t>(pts, 0);
380 if (clip->pts_out == -1) {
381 pts = std::min(pts, current_pts);
383 pts = std::min(pts, clip->pts_out);
388 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
390 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
391 constexpr int scrub_sensitivity = 100; // pts units per pixel.
392 constexpr int wheel_sensitivity = 100; // pts units per degree.
393 constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
395 unsigned stream_idx = ui->preview_display->get_stream_idx();
397 if (event->type() != QEvent::Wheel) {
398 last_mousewheel_camera_row = -1;
401 if (event->type() == QEvent::MouseButtonPress) {
402 QMouseEvent *mouse = (QMouseEvent *)event;
404 QTableView *destination;
407 if (watched == ui->clip_list->viewport()) {
408 destination = ui->clip_list;
409 type = SCRUBBING_CLIP_LIST;
410 } else if (watched == ui->playlist->viewport()) {
411 destination = ui->playlist;
412 type = SCRUBBING_PLAYLIST;
416 int column = destination->columnAt(mouse->x());
417 int row = destination->rowAt(mouse->y());
418 if (column == -1 || row == -1) return false;
420 if (type == SCRUBBING_CLIP_LIST) {
421 if (ClipList::Column(column) == ClipList::Column::IN) {
422 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
423 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
424 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
425 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
426 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
431 if (PlayList::Column(column) == PlayList::Column::IN) {
432 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
433 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
434 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
435 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
436 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
444 scrub_column = column;
445 scrub_x_origin = mouse->x();
447 } else if (event->type() == QEvent::MouseMove) {
449 QMouseEvent *mouse = (QMouseEvent *)event;
450 int offset = mouse->x() - scrub_x_origin;
452 if (offset >= dead_zone_pixels) {
453 adjusted_offset = offset - dead_zone_pixels;
454 } else if (offset < -dead_zone_pixels) {
455 adjusted_offset = offset + dead_zone_pixels;
460 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
461 currently_deferring_model_changes = true;
462 if (scrub_type == SCRUBBING_CLIP_LIST) {
463 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
464 if (scrub_column == int(ClipList::Column::IN)) {
465 current_change_id = "cliplist:in:" + to_string(scrub_row);
466 set_pts_in(pts, current_pts, clip);
467 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
469 current_change_id = "cliplist:out" + to_string(scrub_row);
470 pts = std::max(pts, clip->pts_in);
471 pts = std::min(pts, current_pts);
473 preview_single_frame(pts, stream_idx, LAST_BEFORE);
476 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
477 if (scrub_column == int(PlayList::Column::IN)) {
478 current_change_id = "playlist:in:" + to_string(scrub_row);
479 set_pts_in(pts, current_pts, clip);
480 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
482 current_change_id = "playlist:out:" + to_string(scrub_row);
483 pts = std::max(pts, clip->pts_in);
484 pts = std::min(pts, current_pts);
486 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
489 currently_deferring_model_changes = false;
491 return true; // Don't use this mouse movement for selecting things.
493 } else if (event->type() == QEvent::Wheel) {
494 QWheelEvent *wheel = (QWheelEvent *)event;
496 QTableView *destination;
497 int in_column, out_column, camera_column;
498 if (watched == ui->clip_list->viewport()) {
499 destination = ui->clip_list;
500 in_column = int(ClipList::Column::IN);
501 out_column = int(ClipList::Column::OUT);
503 last_mousewheel_camera_row = -1;
504 } else if (watched == ui->playlist->viewport()) {
505 destination = ui->playlist;
506 in_column = int(PlayList::Column::IN);
507 out_column = int(PlayList::Column::OUT);
508 camera_column = int(PlayList::Column::CAMERA);
510 last_mousewheel_camera_row = -1;
513 int column = destination->columnAt(wheel->x());
514 int row = destination->rowAt(wheel->y());
515 if (column == -1 || row == -1) return false;
517 currently_deferring_model_changes = true;
519 current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
520 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
521 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
522 if (watched == ui->playlist->viewport()) {
523 stream_idx = clip->stream_idx;
526 if (column != camera_column) {
527 last_mousewheel_camera_row = -1;
529 if (column == in_column) {
530 current_change_id += "in:" + to_string(row);
531 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
532 set_pts_in(pts, current_pts, clip);
533 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
534 } else if (column == out_column) {
535 current_change_id += "out:" + to_string(row);
536 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
537 pts = std::max(pts, clip->pts_in);
538 pts = std::min(pts, current_pts);
540 preview_single_frame(pts, stream_idx, LAST_BEFORE);
541 } else if (column == camera_column) {
542 current_change_id += "camera:" + to_string(row);
543 int angle_degrees = wheel->angleDelta().y();
544 if (last_mousewheel_camera_row == row) {
545 angle_degrees += leftover_angle_degrees;
548 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
549 stream_idx = std::max(stream_idx, 0);
550 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
551 clip->stream_idx = stream_idx;
553 last_mousewheel_camera_row = row;
554 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
556 // Don't update the live view, that's rarely what the operator wants.
559 currently_deferring_model_changes = false;
560 } else if (event->type() == QEvent::MouseButtonRelease) {
566 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
568 if (rounding == LAST_BEFORE) {
569 lock_guard<mutex> lock(frame_mu);
570 if (frames[stream_idx].empty()) return;
571 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
572 if (it != frames[stream_idx].end()) {
576 assert(rounding == FIRST_AT_OR_AFTER);
577 lock_guard<mutex> lock(frame_mu);
578 if (frames[stream_idx].empty()) return;
579 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
580 if (it != frames[stream_idx].end()) {
586 fake_clip.pts_in = pts;
587 fake_clip.pts_out = pts + 1;
588 preview_player->play_clip(fake_clip, stream_idx);
591 void MainWindow::playlist_selection_changed()
593 QItemSelectionModel *selected = ui->playlist->selectionModel();
594 bool any_selected = selected->hasSelection();
595 ui->playlist_duplicate_btn->setEnabled(any_selected);
596 ui->playlist_remove_btn->setEnabled(any_selected);
597 ui->playlist_move_up_btn->setEnabled(
598 any_selected && selected->selectedRows().front().row() > 0);
599 ui->playlist_move_down_btn->setEnabled(
600 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
601 ui->play_btn->setEnabled(!playlist_clips->empty());
604 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
607 if (estimated_seconds_left < 60.0) {
608 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
609 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
610 int s = lrintf(estimated_seconds_left);
613 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
614 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
615 int m = lrintf(estimated_seconds_left / 60.0);
616 snprintf(time_str, sizeof(time_str), "%dm", m);
617 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
618 int m = lrintf(estimated_seconds_left / 60.0);
621 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
622 } else { // More than ten hours: Xh.
623 int h = lrintf(estimated_seconds_left / 3600.0);
624 snprintf(time_str, sizeof(time_str), "%dh", h);
627 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
629 std::string label = buf;
631 post_to_main_thread([this, label]{
632 disk_free_label->setText(QString::fromStdString(label));
633 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
637 void MainWindow::exit_triggered()