1 #include "mainwindow.h"
3 #include "shared/aboutdialog.h"
6 #include "shared/disk_space_estimator.h"
8 #include "frame_on_disk.h"
10 #include "shared/post_to_main_thread.h"
11 #include "shared/timebase.h"
12 #include "ui_mainwindow.h"
14 #include <QDesktopServices>
15 #include <QFileDialog>
16 #include <QMessageBox>
17 #include <QMouseEvent>
20 #include <QWheelEvent>
27 using namespace std::placeholders;
29 MainWindow *global_mainwindow = nullptr;
30 static ClipList *cliplist_clips;
31 static PlayList *playlist_clips;
33 extern int64_t current_pts;
35 MainWindow::MainWindow()
36 : ui(new Ui::MainWindow),
37 db(global_flags.working_directory + "/futatabi.db")
39 global_mainwindow = this;
43 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
44 connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered);
45 connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered);
46 connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered);
47 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
49 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
50 disk_free_label = new QLabel(this);
51 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
52 ui->menuBar->setCornerWidget(disk_free_label);
54 StateProto state = db.get_state();
56 cliplist_clips = new ClipList(state.clip_list());
57 ui->clip_list->setModel(cliplist_clips);
58 connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
60 playlist_clips = new PlayList(state.play_list());
61 ui->playlist->setModel(playlist_clips);
62 connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
64 // For un-highlighting when we lose focus.
65 ui->clip_list->installEventFilter(this);
67 // For scrubbing in the pts columns.
68 ui->clip_list->viewport()->installEventFilter(this);
69 ui->playlist->viewport()->installEventFilter(this);
71 QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
72 connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
73 connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
75 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
76 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
77 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
79 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
80 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
81 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
83 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
84 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
85 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
87 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
88 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
89 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
91 QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
92 connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
93 connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
94 connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
95 ui->input1_display->set_overlay("1");
97 QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
98 connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
99 connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
100 connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
101 ui->input2_display->set_overlay("2");
103 QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
104 connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
105 connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
106 connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
107 ui->input3_display->set_overlay("3");
109 QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
110 connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
111 connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
112 connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
113 ui->input4_display->set_overlay("4");
115 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
117 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
118 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
119 connect(delete_key, &QShortcut::activated, [this] {
120 if (ui->playlist->hasFocus()) {
125 // TODO: support drag-and-drop.
126 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
127 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
129 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
130 this, &MainWindow::playlist_selection_changed);
131 playlist_selection_changed(); // First time set-up.
133 preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
134 live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
135 live_player->set_done_callback([this]{
136 post_to_main_thread([this]{
137 live_player_clip_done();
140 live_player->set_next_clip_callback(bind(&MainWindow::live_player_get_next_clip, this));
141 live_player->set_progress_callback([this](const map<size_t, double> &progress) {
142 post_to_main_thread([this, progress] {
143 live_player_clip_progress(progress);
146 set_output_status("paused");
148 defer_timeout = new QTimer(this);
149 defer_timeout->setSingleShot(true);
150 connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
152 connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
153 this, &MainWindow::clip_list_selection_changed);
156 MainWindow::~MainWindow()
158 // Empty so that we can forward-declare Player in the .h file.
161 void MainWindow::cue_in_clicked()
163 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
164 cliplist_clips->mutable_back()->pts_in = current_pts;
168 clip.pts_in = current_pts;
169 cliplist_clips->add_clip(clip);
170 playlist_selection_changed();
171 ui->clip_list->scrollToBottom();
174 void MainWindow::cue_out_clicked()
176 if (!cliplist_clips->empty()) {
177 cliplist_clips->mutable_back()->pts_out = current_pts;
178 // TODO: select the row in the clip list?
182 void MainWindow::queue_clicked()
184 if (cliplist_clips->empty()) {
188 QItemSelectionModel *selected = ui->clip_list->selectionModel();
189 if (!selected->hasSelection()) {
190 Clip clip = *cliplist_clips->back();
192 if (clip.pts_out != -1) {
193 playlist_clips->add_clip(clip);
194 playlist_selection_changed();
195 ui->playlist->scrollToBottom();
200 QModelIndex index = selected->currentIndex();
201 Clip clip = *cliplist_clips->clip(index.row());
202 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
203 index.column() <= int(ClipList::Column::CAMERA_4)) {
204 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
206 clip.stream_idx = ui->preview_display->get_stream_idx();
209 if (clip.pts_out != -1) {
210 playlist_clips->add_clip(clip);
211 playlist_selection_changed();
212 ui->playlist->scrollToBottom();
213 if (!ui->playlist->selectionModel()->hasSelection()) {
214 // TODO: Figure out why this doesn't always seem to actually select the row.
215 QModelIndex bottom = playlist_clips->index(playlist_clips->size() - 1, 0);
216 ui->playlist->setCurrentIndex(bottom);
221 void MainWindow::preview_clicked()
223 if (ui->playlist->hasFocus()) {
224 // Allow the playlist as preview iff it has focus and something is selected.
225 QItemSelectionModel *selected = ui->playlist->selectionModel();
226 if (selected->hasSelection()) {
227 QModelIndex index = selected->currentIndex();
228 const Clip &clip = *playlist_clips->clip(index.row());
229 preview_player->play_clip(clip, index.row(), clip.stream_idx);
234 if (cliplist_clips->empty())
237 QItemSelectionModel *selected = ui->clip_list->selectionModel();
238 if (!selected->hasSelection()) {
239 preview_player->play_clip(*cliplist_clips->back(), cliplist_clips->size() - 1, 0);
243 QModelIndex index = selected->currentIndex();
245 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
246 index.column() <= int(ClipList::Column::CAMERA_4)) {
247 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
249 stream_idx = ui->preview_display->get_stream_idx();
251 preview_player->play_clip(*cliplist_clips->clip(index.row()), index.row(), stream_idx);
254 void MainWindow::preview_angle_clicked(unsigned stream_idx)
256 preview_player->override_angle(stream_idx);
258 // Change the selection if we were previewing a clip from the clip list.
259 // (The only other thing we could be showing is a pts scrub, and if so,
260 // that would be selected.)
261 QItemSelectionModel *selected = ui->clip_list->selectionModel();
262 if (selected->hasSelection()) {
263 QModelIndex cell = selected->selectedIndexes()[0];
264 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
265 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
269 void MainWindow::playlist_duplicate()
271 QItemSelectionModel *selected = ui->playlist->selectionModel();
272 if (!selected->hasSelection()) {
273 // Should have been grayed out, but OK.
276 QModelIndexList rows = selected->selectedRows();
277 int first = rows.front().row(), last = rows.back().row();
278 playlist_clips->duplicate_clips(first, last);
279 playlist_selection_changed();
282 void MainWindow::playlist_remove()
284 QItemSelectionModel *selected = ui->playlist->selectionModel();
285 if (!selected->hasSelection()) {
286 // Should have been grayed out, but OK.
289 QModelIndexList rows = selected->selectedRows();
290 int first = rows.front().row(), last = rows.back().row();
291 playlist_clips->erase_clips(first, last);
293 // TODO: select the next one in the list?
295 playlist_selection_changed();
298 void MainWindow::playlist_move(int delta)
300 QItemSelectionModel *selected = ui->playlist->selectionModel();
301 if (!selected->hasSelection()) {
302 // Should have been grayed out, but OK.
306 QModelIndexList rows = selected->selectedRows();
307 int first = rows.front().row(), last = rows.back().row();
308 if ((delta == -1 && first == 0) ||
309 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
310 // Should have been grayed out, but OK.
314 playlist_clips->move_clips(first, last, delta);
315 playlist_selection_changed();
318 void MainWindow::defer_timer_expired()
320 state_changed(deferred_state);
323 void MainWindow::content_changed()
325 if (defer_timeout->isActive() &&
326 (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
327 // There's some deferred event waiting, but this event is unrelated.
328 // So it's time to short-circuit that timer and do the work it wanted to do.
329 defer_timeout->stop();
330 state_changed(deferred_state);
333 *state.mutable_clip_list() = cliplist_clips->serialize();
334 *state.mutable_play_list() = playlist_clips->serialize();
335 if (currently_deferring_model_changes) {
336 deferred_change_id = current_change_id;
337 deferred_state = std::move(state);
338 defer_timeout->start(200);
341 state_changed(state);
344 void MainWindow::state_changed(const StateProto &state)
346 db.store_state(state);
349 void MainWindow::play_clicked()
351 if (playlist_clips->empty())
354 QItemSelectionModel *selected = ui->playlist->selectionModel();
356 if (!selected->hasSelection()) {
359 row = selected->selectedRows(0)[0].row();
362 const Clip &clip = *playlist_clips->clip(row);
363 live_player->play_clip(clip, row, clip.stream_idx);
364 playlist_clips->set_progress({{ row, 0.0f }});
365 playlist_clips->set_currently_playing(row, 0.0f);
366 playlist_selection_changed();
369 void MainWindow::live_player_clip_done()
371 int row = playlist_clips->get_currently_playing();
372 if (row == -1 || row == int(playlist_clips->size()) - 1) {
373 set_output_status("paused");
374 playlist_clips->set_progress({});
375 playlist_clips->set_currently_playing(-1, 0.0f);
377 playlist_clips->set_progress({{ row + 1, 0.0f }});
378 playlist_clips->set_currently_playing(row + 1, 0.0f);
382 pair<Clip, size_t> MainWindow::live_player_get_next_clip()
384 // playlist_clips can only be accessed on the main thread.
385 // Hopefully, we won't have to wait too long for this to come back.
387 // TODO: If MainWindow is in the process of being destroyed and waiting
388 // for Player to shut down, we could have a deadlock here.
389 promise<pair<Clip, size_t>> clip_promise;
390 future<pair<Clip, size_t>> clip = clip_promise.get_future();
391 post_to_main_thread([this, &clip_promise] {
392 int row = playlist_clips->get_currently_playing();
393 if (row != -1 && row < int(playlist_clips->size()) - 1) {
394 clip_promise.set_value(make_pair(*playlist_clips->clip(row + 1), row + 1));
396 clip_promise.set_value(make_pair(Clip(), 0));
402 static string format_duration(double t)
404 int t_ms = lrint(t * 1e3);
406 int ms = t_ms % 1000;
413 snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
417 void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
419 playlist_clips->set_progress(progress);
421 // Look at the last clip and then start counting from there.
422 assert(!progress.empty());
423 auto last_it = progress.end();
425 double remaining = 0.0;
426 double last_fade_time_seconds = 0.0;
427 for (size_t row = last_it->first; row < playlist_clips->size(); ++row) {
428 const Clip clip = *playlist_clips->clip(row);
429 double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed.
430 if (row == last_it->first) {
431 // A clip we're playing: Subtract the part we've already played.
432 remaining = clip_length * (1.0 - last_it->second);
434 // A clip we haven't played yet: Subtract the part that's overlapping
435 // with a previous clip (due to fade).
436 remaining += max(clip_length - last_fade_time_seconds, 0.0);
438 last_fade_time_seconds = min(clip_length, clip.fade_time_seconds);
440 set_output_status(format_duration(remaining) + " left");
443 void MainWindow::resizeEvent(QResizeEvent *event)
445 QMainWindow::resizeEvent(event);
447 // Ask for a relayout, but only after the event loop is done doing relayout
448 // on everything else.
449 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
452 void MainWindow::relayout()
454 ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
455 ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
458 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
460 pts = std::max<int64_t>(pts, 0);
461 if (clip->pts_out == -1) {
462 pts = std::min(pts, current_pts);
464 pts = std::min(pts, clip->pts_out);
469 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
471 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
472 constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
473 int scrub_sensitivity = 100; // pts units per pixel.
474 int wheel_sensitivity = 100; // pts units per degree.
476 unsigned stream_idx = ui->preview_display->get_stream_idx();
478 if (watched == ui->clip_list) {
479 if (event->type() == QEvent::FocusOut) {
480 highlight_camera_input(-1);
485 if (event->type() != QEvent::Wheel) {
486 last_mousewheel_camera_row = -1;
489 if (event->type() == QEvent::MouseButtonPress) {
490 QMouseEvent *mouse = (QMouseEvent *)event;
492 QTableView *destination;
495 if (watched == ui->clip_list->viewport()) {
496 destination = ui->clip_list;
497 type = SCRUBBING_CLIP_LIST;
498 } else if (watched == ui->playlist->viewport()) {
499 destination = ui->playlist;
500 type = SCRUBBING_PLAYLIST;
504 int column = destination->columnAt(mouse->x());
505 int row = destination->rowAt(mouse->y());
506 if (column == -1 || row == -1)
509 if (type == SCRUBBING_CLIP_LIST) {
510 if (ClipList::Column(column) == ClipList::Column::IN) {
511 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
512 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
513 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
514 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
515 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
520 if (PlayList::Column(column) == PlayList::Column::IN) {
521 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
522 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
523 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
524 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
525 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
533 scrub_column = column;
534 scrub_x_origin = mouse->x();
536 } else if (event->type() == QEvent::MouseMove) {
537 QMouseEvent *mouse = (QMouseEvent *)event;
538 if (mouse->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
539 scrub_sensitivity *= 10;
540 wheel_sensitivity *= 10;
542 if (mouse->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
543 scrub_sensitivity /= 10;
544 wheel_sensitivity /= 10;
547 int offset = mouse->x() - scrub_x_origin;
549 if (offset >= dead_zone_pixels) {
550 adjusted_offset = offset - dead_zone_pixels;
551 } else if (offset < -dead_zone_pixels) {
552 adjusted_offset = offset + dead_zone_pixels;
557 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
558 currently_deferring_model_changes = true;
559 if (scrub_type == SCRUBBING_CLIP_LIST) {
560 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
561 if (scrub_column == int(ClipList::Column::IN)) {
562 current_change_id = "cliplist:in:" + to_string(scrub_row);
563 set_pts_in(pts, current_pts, clip);
564 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
566 current_change_id = "cliplist:out" + to_string(scrub_row);
567 pts = std::max(pts, clip->pts_in);
568 pts = std::min(pts, current_pts);
570 preview_single_frame(pts, stream_idx, LAST_BEFORE);
573 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
574 if (scrub_column == int(PlayList::Column::IN)) {
575 current_change_id = "playlist:in:" + to_string(scrub_row);
576 set_pts_in(pts, current_pts, clip);
577 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
579 current_change_id = "playlist:out:" + to_string(scrub_row);
580 pts = std::max(pts, clip->pts_in);
581 pts = std::min(pts, current_pts);
583 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
586 currently_deferring_model_changes = false;
588 return true; // Don't use this mouse movement for selecting things.
590 } else if (event->type() == QEvent::Wheel) {
591 QWheelEvent *wheel = (QWheelEvent *)event;
592 int angle_delta = wheel->angleDelta().y();
593 if (wheel->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
594 scrub_sensitivity *= 10;
595 wheel_sensitivity *= 10;
597 if (wheel->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
598 scrub_sensitivity /= 10;
599 wheel_sensitivity /= 10;
600 angle_delta = wheel->angleDelta().x(); // Qt ickiness.
603 QTableView *destination;
604 int in_column, out_column, camera_column;
605 if (watched == ui->clip_list->viewport()) {
606 destination = ui->clip_list;
607 in_column = int(ClipList::Column::IN);
608 out_column = int(ClipList::Column::OUT);
610 last_mousewheel_camera_row = -1;
611 } else if (watched == ui->playlist->viewport()) {
612 destination = ui->playlist;
613 in_column = int(PlayList::Column::IN);
614 out_column = int(PlayList::Column::OUT);
615 camera_column = int(PlayList::Column::CAMERA);
617 last_mousewheel_camera_row = -1;
620 int column = destination->columnAt(wheel->x());
621 int row = destination->rowAt(wheel->y());
622 if (column == -1 || row == -1) return false;
624 // Only adjust pts with the wheel if the given row is selected.
625 if (!destination->hasFocus() ||
626 row != destination->selectionModel()->currentIndex().row()) {
630 currently_deferring_model_changes = true;
632 current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
633 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
634 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
635 if (watched == ui->playlist->viewport()) {
636 stream_idx = clip->stream_idx;
639 if (column != camera_column) {
640 last_mousewheel_camera_row = -1;
642 if (column == in_column) {
643 current_change_id += "in:" + to_string(row);
644 int64_t pts = clip->pts_in + angle_delta * wheel_sensitivity;
645 set_pts_in(pts, current_pts, clip);
646 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
647 } else if (column == out_column) {
648 current_change_id += "out:" + to_string(row);
649 int64_t pts = clip->pts_out + angle_delta * wheel_sensitivity;
650 pts = std::max(pts, clip->pts_in);
651 pts = std::min(pts, current_pts);
653 preview_single_frame(pts, stream_idx, LAST_BEFORE);
654 } else if (column == camera_column) {
655 current_change_id += "camera:" + to_string(row);
656 int angle_degrees = angle_delta;
657 if (last_mousewheel_camera_row == row) {
658 angle_degrees += leftover_angle_degrees;
661 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
662 stream_idx = std::max(stream_idx, 0);
663 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
664 clip->stream_idx = stream_idx;
666 last_mousewheel_camera_row = row;
667 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
669 // Don't update the live view, that's rarely what the operator wants.
672 currently_deferring_model_changes = false;
673 return true; // Don't scroll.
674 } else if (event->type() == QEvent::MouseButtonRelease) {
680 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
682 if (rounding == LAST_BEFORE) {
683 lock_guard<mutex> lock(frame_mu);
684 if (frames[stream_idx].empty())
686 auto it = find_last_frame_before(frames[stream_idx], pts);
687 if (it != frames[stream_idx].end()) {
691 assert(rounding == FIRST_AT_OR_AFTER);
692 lock_guard<mutex> lock(frame_mu);
693 if (frames[stream_idx].empty())
695 auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
696 if (it != frames[stream_idx].end()) {
702 fake_clip.pts_in = pts;
703 fake_clip.pts_out = pts + 1;
704 preview_player->play_clip(fake_clip, 0, stream_idx);
707 void MainWindow::playlist_selection_changed()
709 QItemSelectionModel *selected = ui->playlist->selectionModel();
710 bool any_selected = selected->hasSelection();
711 ui->playlist_duplicate_btn->setEnabled(any_selected);
712 ui->playlist_remove_btn->setEnabled(any_selected);
713 ui->playlist_move_up_btn->setEnabled(
714 any_selected && selected->selectedRows().front().row() > 0);
715 ui->playlist_move_down_btn->setEnabled(
716 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
717 ui->play_btn->setEnabled(!playlist_clips->empty());
720 set_output_status("paused");
722 double remaining = 0.0;
723 for (int row = selected->selectedRows().front().row(); row < int(playlist_clips->size()); ++row) {
724 const Clip clip = *playlist_clips->clip(row);
725 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed.
727 set_output_status(format_duration(remaining) + " ready");
731 void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &)
733 int camera_selected = -1;
734 if (current.column() >= int(ClipList::Column::CAMERA_1) &&
735 current.column() <= int(ClipList::Column::CAMERA_4)) {
736 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
738 highlight_camera_input(camera_selected);
741 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
744 if (estimated_seconds_left < 60.0) {
745 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
746 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
747 int s = lrintf(estimated_seconds_left);
750 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
751 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
752 int m = lrintf(estimated_seconds_left / 60.0);
753 snprintf(time_str, sizeof(time_str), "%dm", m);
754 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
755 int m = lrintf(estimated_seconds_left / 60.0);
758 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
759 } else { // More than ten hours: Xh.
760 int h = lrintf(estimated_seconds_left / 3600.0);
761 snprintf(time_str, sizeof(time_str), "%dh", h);
764 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
766 std::string label = buf;
768 post_to_main_thread([this, label] {
769 disk_free_label->setText(QString::fromStdString(label));
770 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
774 void MainWindow::exit_triggered()
779 void MainWindow::export_cliplist_clip_multitrack_triggered()
781 QItemSelectionModel *selected = ui->clip_list->selectionModel();
782 if (!selected->hasSelection()) {
784 msgbox.setText("No clip selected in the clip list. Select one and try exporting again.");
789 QModelIndex index = selected->currentIndex();
790 Clip clip = *cliplist_clips->clip(index.row());
791 QString filename = QFileDialog::getSaveFileName(this,
792 "Export multitrack clip", QString(), tr("Matroska video files (*.mkv)"));
793 if (filename.isNull()) {
797 if (!filename.endsWith(".mkv")) {
800 export_multitrack_clip(filename.toStdString(), clip);
803 void MainWindow::export_playlist_clip_interpolated_triggered()
805 QItemSelectionModel *selected = ui->playlist->selectionModel();
806 if (!selected->hasSelection()) {
808 msgbox.setText("No clip selected in the playlist. Select one and try exporting again.");
813 QModelIndex index = selected->currentIndex();
814 Clip clip = *playlist_clips->clip(index.row());
815 QString filename = QFileDialog::getSaveFileName(this,
816 "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)"));
817 if (filename.isNull()) {
821 if (!filename.endsWith(".mkv")) {
824 export_interpolated_clip(filename.toStdString(), clip);
827 void MainWindow::manual_triggered()
829 if (!QDesktopServices::openUrl(QUrl("https://nageru.sesse.net/doc/"))) {
831 msgbox.setText("Could not launch manual in web browser.\nPlease see https://nageru.sesse.net/doc/ manually.");
836 void MainWindow::about_triggered()
838 AboutDialog("Futatabi", "Multicamera slow motion video server").exec();
841 void MainWindow::highlight_camera_input(int stream_idx)
843 if (stream_idx == 0) {
844 ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
846 ui->input1_frame->setStyleSheet("");
848 if (stream_idx == 1) {
849 ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
851 ui->input2_frame->setStyleSheet("");
853 if (stream_idx == 2) {
854 ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
856 ui->input3_frame->setStyleSheet("");
858 if (stream_idx == 3) {
859 ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
861 ui->input4_frame->setStyleSheet("");
865 void MainWindow::set_output_status(const string &status)
867 ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
869 lock_guard<mutex> lock(queue_status_mu);
870 queue_status = status;
873 pair<string, string> MainWindow::get_queue_status() const {
874 lock_guard<mutex> lock(queue_status_mu);
875 return {queue_status, "text/plain"};