1 #include "mainwindow.h"
6 #include "frame_on_disk.h"
8 #include "shared/aboutdialog.h"
9 #include "shared/disk_space_estimator.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>
18 #include <QNetworkReply>
21 #include <QWheelEvent>
28 using namespace std::placeholders;
30 MainWindow *global_mainwindow = nullptr;
31 static ClipList *cliplist_clips;
32 static PlayList *playlist_clips;
34 extern int64_t current_pts;
36 MainWindow::MainWindow()
37 : ui(new Ui::MainWindow),
38 db(global_flags.working_directory + "/futatabi.db")
40 global_mainwindow = this;
43 // Load settings from database.
44 SettingsProto settings = db.get_settings();
45 if (!global_flags.interpolation_quality_set) {
46 if (settings.interpolation_quality() != 0) {
47 global_flags.interpolation_quality = settings.interpolation_quality() - 1;
50 if (!global_flags.cue_point_padding_set) {
51 global_flags.cue_point_padding_seconds = settings.cue_point_padding_seconds(); // Default 0 is fine.
53 if (global_flags.interpolation_quality == 0) {
54 // Allocate something just for simplicity; we won't be using it
55 // unless the user changes runtime, in which case 1 is fine.
56 flow_initialized_interpolation_quality = 1;
58 flow_initialized_interpolation_quality = global_flags.interpolation_quality;
63 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
64 connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered);
65 connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered);
66 connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered);
67 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
68 connect(ui->undo_action, &QAction::triggered, this, &MainWindow::undo_triggered);
69 connect(ui->redo_action, &QAction::triggered, this, &MainWindow::redo_triggered);
70 ui->undo_action->setEnabled(false);
71 ui->redo_action->setEnabled(false);
74 QActionGroup *quality_group = new QActionGroup(ui->interpolation_menu);
75 quality_group->addAction(ui->quality_0_action);
76 quality_group->addAction(ui->quality_1_action);
77 quality_group->addAction(ui->quality_2_action);
78 quality_group->addAction(ui->quality_3_action);
79 quality_group->addAction(ui->quality_4_action);
80 if (global_flags.interpolation_quality == 0) {
81 ui->quality_0_action->setChecked(true);
82 } else if (global_flags.interpolation_quality == 1) {
83 ui->quality_1_action->setChecked(true);
84 } else if (global_flags.interpolation_quality == 2) {
85 ui->quality_2_action->setChecked(true);
86 } else if (global_flags.interpolation_quality == 3) {
87 ui->quality_3_action->setChecked(true);
88 } else if (global_flags.interpolation_quality == 4) {
89 ui->quality_4_action->setChecked(true);
93 connect(ui->quality_0_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 0, _1));
94 connect(ui->quality_1_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 1, _1));
95 connect(ui->quality_2_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 2, _1));
96 connect(ui->quality_3_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 3, _1));
97 connect(ui->quality_4_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 4, _1));
99 // The cue point padding group.
100 QActionGroup *padding_group = new QActionGroup(ui->interpolation_menu);
101 padding_group->addAction(ui->padding_0_action);
102 padding_group->addAction(ui->padding_1_action);
103 padding_group->addAction(ui->padding_2_action);
104 padding_group->addAction(ui->padding_5_action);
105 if (global_flags.cue_point_padding_seconds <= 1e-3) {
106 ui->padding_0_action->setChecked(true);
107 } else if (fabs(global_flags.cue_point_padding_seconds - 1.0) < 1e-3) {
108 ui->padding_1_action->setChecked(true);
109 } else if (fabs(global_flags.cue_point_padding_seconds - 2.0) < 1e-3) {
110 ui->padding_2_action->setChecked(true);
111 } else if (fabs(global_flags.cue_point_padding_seconds - 5.0) < 1e-3) {
112 ui->padding_5_action->setChecked(true);
114 // Nothing to check, which is fine.
116 connect(ui->padding_0_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 0.0, _1));
117 connect(ui->padding_1_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 1.0, _1));
118 connect(ui->padding_2_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 2.0, _1));
119 connect(ui->padding_5_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 5.0, _1));
121 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
122 disk_free_label = new QLabel(this);
123 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
124 ui->menuBar->setCornerWidget(disk_free_label);
126 StateProto state = db.get_state();
127 undo_stack.push_back(state); // The undo stack always has the current state on top.
129 cliplist_clips = new ClipList(state.clip_list());
130 ui->clip_list->setModel(cliplist_clips);
131 connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
133 playlist_clips = new PlayList(state.play_list());
134 ui->playlist->setModel(playlist_clips);
135 connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
137 // For un-highlighting when we lose focus.
138 ui->clip_list->installEventFilter(this);
140 // For scrubbing in the pts columns.
141 ui->clip_list->viewport()->installEventFilter(this);
142 ui->playlist->viewport()->installEventFilter(this);
144 QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
145 connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
146 connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
148 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
149 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
150 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
152 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
153 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
154 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
156 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
157 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
158 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
160 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
161 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
162 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
164 connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
165 ui->stop_btn->setEnabled(false);
167 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
169 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
170 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
171 connect(delete_key, &QShortcut::activated, [this] {
172 if (ui->playlist->hasFocus()) {
177 // TODO: support drag-and-drop.
178 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this] { playlist_move(-1); });
179 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this] { playlist_move(1); });
181 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
182 this, &MainWindow::playlist_selection_changed);
183 playlist_selection_changed(); // First time set-up.
185 preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
186 live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
187 live_player->set_done_callback([this] {
188 post_to_main_thread([this] {
192 live_player->set_progress_callback([this](const map<uint64_t, double> &progress, double time_remaining) {
193 post_to_main_thread([this, progress, time_remaining] {
194 live_player_clip_progress(progress, time_remaining);
197 set_output_status("paused");
199 defer_timeout = new QTimer(this);
200 defer_timeout->setSingleShot(true);
201 connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
202 ui->undo_action->setEnabled(true);
204 connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
205 this, &MainWindow::clip_list_selection_changed);
207 // Find out how many cameras we have in the existing frames;
208 // if none, we start with two cameras.
211 lock_guard<mutex> lock(frame_mu);
212 for (size_t stream_idx = 2; stream_idx < MAX_STREAMS; ++stream_idx) {
213 if (!frames[stream_idx].empty()) {
214 num_cameras = stream_idx + 1;
218 change_num_cameras();
220 if (!global_flags.tally_url.empty()) {
225 void MainWindow::change_num_cameras()
227 assert(num_cameras >= displays.size()); // We only add, never remove.
229 // Make new display rows.
230 unsigned display_rows = (num_cameras + 1) / 2;
231 ui->video_displays->setStretch(1, display_rows);
232 for (unsigned i = displays.size(); i < num_cameras; ++i) {
233 QFrame *frame = new QFrame(this);
234 frame->setAutoFillBackground(true);
236 QLayout *layout = new QGridLayout(frame);
237 frame->setLayout(layout);
238 layout->setContentsMargins(3, 3, 3, 3);
240 JPEGFrameView *display = new JPEGFrameView(frame);
241 display->setAutoFillBackground(true);
242 layout->addWidget(display);
244 ui->input_displays->addWidget(frame, i / 2, i % 2);
245 display->set_overlay(to_string(i + 1));
247 QPushButton *preview_btn = new QPushButton(this);
248 preview_btn->setMaximumSize(20, 17);
249 preview_btn->setText(QString::fromStdString(to_string(i + 1)));
250 ui->preview_layout->addWidget(preview_btn);
252 displays.emplace_back(FrameAndDisplay{ frame, display, preview_btn });
254 connect(display, &JPEGFrameView::clicked, preview_btn, &QPushButton::click);
255 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
256 connect(shortcut, &QShortcut::activated, preview_btn, &QPushButton::click);
258 connect(preview_btn, &QPushButton::clicked, [this, i] { preview_angle_clicked(i); });
261 cliplist_clips->change_num_cameras(num_cameras);
262 playlist_clips->change_num_cameras(num_cameras);
264 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
267 MainWindow::~MainWindow()
269 // Empty so that we can forward-declare Player in the .h file.
272 void MainWindow::cue_in_clicked()
274 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
275 cliplist_clips->mutable_back()->pts_in = current_pts;
279 clip.pts_in = max<int64_t>(current_pts - lrint(global_flags.cue_point_padding_seconds * TIMEBASE), 0);
280 cliplist_clips->add_clip(clip);
281 playlist_selection_changed();
282 ui->clip_list->scrollToBottom();
285 void MainWindow::cue_out_clicked()
287 if (!cliplist_clips->empty()) {
288 cliplist_clips->mutable_back()->pts_out = current_pts + lrint(global_flags.cue_point_padding_seconds * TIMEBASE);
289 // TODO: select the row in the clip list?
293 void MainWindow::queue_clicked()
295 if (cliplist_clips->empty()) {
299 QItemSelectionModel *selected = ui->clip_list->selectionModel();
300 if (!selected->hasSelection()) {
301 Clip clip = *cliplist_clips->back();
303 if (clip.pts_out != -1) {
304 playlist_clips->add_clip(clip);
305 playlist_selection_changed();
306 ui->playlist->scrollToBottom();
311 QModelIndex index = selected->currentIndex();
312 Clip clip = *cliplist_clips->clip(index.row());
313 if (cliplist_clips->is_camera_column(index.column())) {
314 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
316 clip.stream_idx = ui->preview_display->get_stream_idx();
319 if (clip.pts_out != -1) {
320 playlist_clips->add_clip(clip);
321 playlist_selection_changed();
322 ui->playlist->scrollToBottom();
323 if (!ui->playlist->selectionModel()->hasSelection()) {
324 // TODO: Figure out why this doesn't always seem to actually select the row.
325 QModelIndex bottom = playlist_clips->index(playlist_clips->size() - 1, 0);
326 ui->playlist->setCurrentIndex(bottom);
331 void MainWindow::preview_clicked()
333 if (ui->playlist->hasFocus()) {
334 // Allow the playlist as preview iff it has focus and something is selected.
335 QItemSelectionModel *selected = ui->playlist->selectionModel();
336 if (selected->hasSelection()) {
337 QModelIndex index = selected->currentIndex();
338 const Clip &clip = *playlist_clips->clip(index.row());
339 preview_player->play(clip);
344 if (cliplist_clips->empty())
347 QItemSelectionModel *selected = ui->clip_list->selectionModel();
348 if (!selected->hasSelection()) {
349 preview_player->play(*cliplist_clips->back());
353 QModelIndex index = selected->currentIndex();
354 Clip clip = *cliplist_clips->clip(index.row());
355 if (cliplist_clips->is_camera_column(index.column())) {
356 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
358 clip.stream_idx = ui->preview_display->get_stream_idx();
360 preview_player->play(clip);
363 void MainWindow::preview_angle_clicked(unsigned stream_idx)
365 preview_player->override_angle(stream_idx);
367 // Change the selection if we were previewing a clip from the clip list.
368 // (The only other thing we could be showing is a pts scrub, and if so,
369 // that would be selected.)
370 QItemSelectionModel *selected = ui->clip_list->selectionModel();
371 if (selected->hasSelection()) {
372 QModelIndex cell = selected->selectedIndexes()[0];
373 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
374 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
378 void MainWindow::playlist_duplicate()
380 QItemSelectionModel *selected = ui->playlist->selectionModel();
381 if (!selected->hasSelection()) {
382 // Should have been grayed out, but OK.
385 QModelIndexList rows = selected->selectedRows();
386 int first = rows.front().row(), last = rows.back().row();
387 playlist_clips->duplicate_clips(first, last);
388 playlist_selection_changed();
391 void MainWindow::playlist_remove()
393 QItemSelectionModel *selected = ui->playlist->selectionModel();
394 if (!selected->hasSelection()) {
395 // Should have been grayed out, but OK.
398 QModelIndexList rows = selected->selectedRows();
399 int first = rows.front().row(), last = rows.back().row();
400 playlist_clips->erase_clips(first, last);
402 // TODO: select the next one in the list?
404 playlist_selection_changed();
407 void MainWindow::playlist_move(int delta)
409 QItemSelectionModel *selected = ui->playlist->selectionModel();
410 if (!selected->hasSelection()) {
411 // Should have been grayed out, but OK.
415 QModelIndexList rows = selected->selectedRows();
416 int first = rows.front().row(), last = rows.back().row();
417 if ((delta == -1 && first == 0) ||
418 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
419 // Should have been grayed out, but OK.
423 playlist_clips->move_clips(first, last, delta);
424 playlist_selection_changed();
427 void MainWindow::defer_timer_expired()
429 state_changed(deferred_state);
432 void MainWindow::content_changed()
434 // If we are playing, update the part of the playlist that's not playing yet.
435 vector<ClipWithID> clips;
436 for (unsigned row = 0; row < playlist_clips->size(); ++row) {
437 clips.emplace_back(*playlist_clips->clip_with_id(row));
439 live_player->splice_play(clips);
441 // Serialize the state.
442 if (defer_timeout->isActive() &&
443 (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
444 // There's some deferred event waiting, but this event is unrelated.
445 // So it's time to short-circuit that timer and do the work it wanted to do.
446 defer_timeout->stop();
447 state_changed(deferred_state);
450 *state.mutable_clip_list() = cliplist_clips->serialize();
451 *state.mutable_play_list() = playlist_clips->serialize();
452 if (currently_deferring_model_changes) {
453 deferred_change_id = current_change_id;
454 deferred_state = std::move(state);
455 defer_timeout->start(200);
458 state_changed(state);
461 void MainWindow::state_changed(const StateProto &state)
463 db.store_state(state);
466 ui->redo_action->setEnabled(false);
468 undo_stack.push_back(state);
469 ui->undo_action->setEnabled(undo_stack.size() > 1);
471 // Make sure it doesn't grow without bounds.
472 while (undo_stack.size() >= 100) {
473 undo_stack.pop_front();
477 void MainWindow::save_settings()
479 SettingsProto settings;
480 settings.set_interpolation_quality(global_flags.interpolation_quality + 1);
481 settings.set_cue_point_padding_seconds(global_flags.cue_point_padding_seconds);
482 db.store_settings(settings);
485 void MainWindow::play_clicked()
487 if (playlist_clips->empty())
490 QItemSelectionModel *selected = ui->playlist->selectionModel();
492 if (!selected->hasSelection()) {
495 start_row = selected->selectedRows(0)[0].row();
498 vector<ClipWithID> clips;
499 for (unsigned row = start_row; row < playlist_clips->size(); ++row) {
500 clips.emplace_back(*playlist_clips->clip_with_id(row));
502 live_player->play(clips);
503 playlist_clips->set_progress({ { start_row, 0.0f } });
504 ui->playlist->selectionModel()->clear();
505 playlist_selection_changed();
507 ui->stop_btn->setEnabled(true);
510 void MainWindow::stop_clicked()
513 fake_clip.pts_in = 0;
514 fake_clip.pts_out = 0;
515 playlist_clips->set_progress({});
516 live_player->play(fake_clip);
517 ui->stop_btn->setEnabled(false);
520 void MainWindow::live_player_done()
522 playlist_selection_changed();
523 playlist_clips->set_progress({});
524 ui->stop_btn->setEnabled(false);
527 void MainWindow::live_player_clip_progress(const map<uint64_t, double> &progress, double time_remaining)
529 playlist_clips->set_progress(progress);
530 set_output_status(format_duration(time_remaining) + " left");
533 void MainWindow::resizeEvent(QResizeEvent *event)
535 QMainWindow::resizeEvent(event);
537 // Ask for a relayout, but only after the event loop is done doing relayout
538 // on everything else.
539 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
542 void MainWindow::relayout()
544 ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
545 ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
548 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
550 pts = std::max<int64_t>(pts, 0);
551 if (clip->pts_out == -1) {
552 pts = std::min(pts, current_pts);
554 pts = std::min(pts, clip->pts_out);
559 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
561 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
562 constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
563 int scrub_sensitivity = 100; // pts units per pixel.
564 int wheel_sensitivity = 100; // pts units per degree.
566 unsigned stream_idx = ui->preview_display->get_stream_idx();
568 if (watched == ui->clip_list) {
569 if (event->type() == QEvent::FocusOut) {
570 highlight_camera_input(-1);
575 if (event->type() != QEvent::Wheel) {
576 last_mousewheel_camera_row = -1;
579 if (event->type() == QEvent::MouseButtonPress) {
580 QMouseEvent *mouse = (QMouseEvent *)event;
582 QTableView *destination;
585 if (watched == ui->clip_list->viewport()) {
586 destination = ui->clip_list;
587 type = SCRUBBING_CLIP_LIST;
588 } else if (watched == ui->playlist->viewport()) {
589 destination = ui->playlist;
590 type = SCRUBBING_PLAYLIST;
594 int column = destination->columnAt(mouse->x());
595 int row = destination->rowAt(mouse->y());
596 if (column == -1 || row == -1)
599 if (type == SCRUBBING_CLIP_LIST) {
600 if (ClipList::Column(column) == ClipList::Column::IN) {
601 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
602 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
603 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
604 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
605 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
610 if (PlayList::Column(column) == PlayList::Column::IN) {
611 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
612 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
613 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
614 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
615 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
623 scrub_column = column;
624 scrub_x_origin = mouse->x();
626 } else if (event->type() == QEvent::MouseMove) {
627 QMouseEvent *mouse = (QMouseEvent *)event;
628 if (mouse->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
629 scrub_sensitivity *= 10;
630 wheel_sensitivity *= 10;
632 if (mouse->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
633 scrub_sensitivity /= 10;
634 wheel_sensitivity /= 10;
637 int offset = mouse->x() - scrub_x_origin;
639 if (offset >= dead_zone_pixels) {
640 adjusted_offset = offset - dead_zone_pixels;
641 } else if (offset < -dead_zone_pixels) {
642 adjusted_offset = offset + dead_zone_pixels;
647 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
648 currently_deferring_model_changes = true;
649 if (scrub_type == SCRUBBING_CLIP_LIST) {
650 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
651 if (scrub_column == int(ClipList::Column::IN)) {
652 current_change_id = "cliplist:in:" + to_string(scrub_row);
653 set_pts_in(pts, current_pts, clip);
654 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
656 current_change_id = "cliplist:out" + to_string(scrub_row);
657 pts = std::max(pts, clip->pts_in);
658 pts = std::min(pts, current_pts);
660 preview_single_frame(pts, stream_idx, LAST_BEFORE);
663 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
664 if (scrub_column == int(PlayList::Column::IN)) {
665 current_change_id = "playlist:in:" + to_string(scrub_row);
666 set_pts_in(pts, current_pts, clip);
667 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
669 current_change_id = "playlist:out:" + to_string(scrub_row);
670 pts = std::max(pts, clip->pts_in);
671 pts = std::min(pts, current_pts);
673 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
676 currently_deferring_model_changes = false;
678 return true; // Don't use this mouse movement for selecting things.
680 } else if (event->type() == QEvent::Wheel) {
681 QWheelEvent *wheel = (QWheelEvent *)event;
682 int angle_delta = wheel->angleDelta().y();
683 if (wheel->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
684 scrub_sensitivity *= 10;
685 wheel_sensitivity *= 10;
687 if (wheel->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
688 scrub_sensitivity /= 10;
689 wheel_sensitivity /= 10;
690 angle_delta = wheel->angleDelta().x(); // Qt ickiness.
693 QTableView *destination;
694 int in_column, out_column, camera_column;
695 if (watched == ui->clip_list->viewport()) {
696 destination = ui->clip_list;
697 in_column = int(ClipList::Column::IN);
698 out_column = int(ClipList::Column::OUT);
700 last_mousewheel_camera_row = -1;
701 } else if (watched == ui->playlist->viewport()) {
702 destination = ui->playlist;
703 in_column = int(PlayList::Column::IN);
704 out_column = int(PlayList::Column::OUT);
705 camera_column = int(PlayList::Column::CAMERA);
707 last_mousewheel_camera_row = -1;
710 int column = destination->columnAt(wheel->x());
711 int row = destination->rowAt(wheel->y());
712 if (column == -1 || row == -1)
715 // Only adjust pts with the wheel if the given row is selected.
716 if (!destination->hasFocus() ||
717 row != destination->selectionModel()->currentIndex().row()) {
721 currently_deferring_model_changes = true;
723 current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
724 ClipProxy clip = (watched == ui->clip_list->viewport()) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
725 if (watched == ui->playlist->viewport()) {
726 stream_idx = clip->stream_idx;
729 if (column != camera_column) {
730 last_mousewheel_camera_row = -1;
732 if (column == in_column) {
733 current_change_id += "in:" + to_string(row);
734 int64_t pts = clip->pts_in + angle_delta * wheel_sensitivity;
735 set_pts_in(pts, current_pts, clip);
736 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
737 } else if (column == out_column) {
738 current_change_id += "out:" + to_string(row);
739 int64_t pts = clip->pts_out + angle_delta * wheel_sensitivity;
740 pts = std::max(pts, clip->pts_in);
741 pts = std::min(pts, current_pts);
743 preview_single_frame(pts, stream_idx, LAST_BEFORE);
744 } else if (column == camera_column) {
745 current_change_id += "camera:" + to_string(row);
746 int angle_degrees = angle_delta;
747 if (last_mousewheel_camera_row == row) {
748 angle_degrees += leftover_angle_degrees;
751 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
752 stream_idx = std::max(stream_idx, 0);
753 stream_idx = std::min<int>(stream_idx, num_cameras - 1);
754 clip->stream_idx = stream_idx;
756 last_mousewheel_camera_row = row;
757 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
759 // Don't update the live view, that's rarely what the operator wants.
762 currently_deferring_model_changes = false;
763 return true; // Don't scroll.
764 } else if (event->type() == QEvent::MouseButtonRelease) {
770 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
772 if (rounding == LAST_BEFORE) {
773 lock_guard<mutex> lock(frame_mu);
774 if (frames[stream_idx].empty())
776 auto it = find_last_frame_before(frames[stream_idx], pts);
777 if (it != frames[stream_idx].end()) {
781 assert(rounding == FIRST_AT_OR_AFTER);
782 lock_guard<mutex> lock(frame_mu);
783 if (frames[stream_idx].empty())
785 auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
786 if (it != frames[stream_idx].end()) {
792 fake_clip.pts_in = pts;
793 fake_clip.pts_out = pts + 1;
794 preview_player->play(fake_clip);
797 void MainWindow::playlist_selection_changed()
799 QItemSelectionModel *selected = ui->playlist->selectionModel();
800 bool any_selected = selected->hasSelection();
801 ui->playlist_duplicate_btn->setEnabled(any_selected);
802 ui->playlist_remove_btn->setEnabled(any_selected);
803 ui->playlist_move_up_btn->setEnabled(
804 any_selected && selected->selectedRows().front().row() > 0);
805 ui->playlist_move_down_btn->setEnabled(
806 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
807 ui->play_btn->setEnabled(!playlist_clips->empty());
810 set_output_status("paused");
812 vector<ClipWithID> clips;
813 for (size_t row = selected->selectedRows().front().row(); row < playlist_clips->size(); ++row) {
814 clips.emplace_back(*playlist_clips->clip_with_id(row));
816 double remaining = compute_total_time(clips);
817 set_output_status(format_duration(remaining) + " ready");
821 void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &)
823 int camera_selected = -1;
824 if (cliplist_clips->is_camera_column(current.column())) {
825 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
827 highlight_camera_input(camera_selected);
830 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
833 if (estimated_seconds_left < 60.0) {
834 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
835 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
836 int s = lrintf(estimated_seconds_left);
839 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
840 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
841 int m = lrintf(estimated_seconds_left / 60.0);
842 snprintf(time_str, sizeof(time_str), "%dm", m);
843 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
844 int m = lrintf(estimated_seconds_left / 60.0);
847 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
848 } else { // More than ten hours: Xh.
849 int h = lrintf(estimated_seconds_left / 3600.0);
850 snprintf(time_str, sizeof(time_str), "%dh", h);
853 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
855 std::string label = buf;
857 post_to_main_thread([this, label] {
858 disk_free_label->setText(QString::fromStdString(label));
859 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
863 void MainWindow::exit_triggered()
868 void MainWindow::export_cliplist_clip_multitrack_triggered()
870 QItemSelectionModel *selected = ui->clip_list->selectionModel();
871 if (!selected->hasSelection()) {
873 msgbox.setText("No clip selected in the clip list. Select one and try exporting again.");
878 QModelIndex index = selected->currentIndex();
879 Clip clip = *cliplist_clips->clip(index.row());
880 QString filename = QFileDialog::getSaveFileName(this,
881 "Export multitrack clip", QString(), tr("Matroska video files (*.mkv)"));
882 if (filename.isNull()) {
886 if (!filename.endsWith(".mkv")) {
889 export_multitrack_clip(filename.toStdString(), clip);
892 void MainWindow::export_playlist_clip_interpolated_triggered()
894 QItemSelectionModel *selected = ui->playlist->selectionModel();
895 if (!selected->hasSelection()) {
897 msgbox.setText("No clip selected in the playlist. Select one and try exporting again.");
902 QString filename = QFileDialog::getSaveFileName(this,
903 "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)"));
904 if (filename.isNull()) {
908 if (!filename.endsWith(".mkv")) {
913 QModelIndexList rows = selected->selectedRows();
914 for (QModelIndex index : rows) {
915 clips.push_back(*playlist_clips->clip(index.row()));
917 export_interpolated_clip(filename.toStdString(), clips);
920 void MainWindow::manual_triggered()
922 if (!QDesktopServices::openUrl(QUrl("https://nageru.sesse.net/doc/"))) {
924 msgbox.setText("Could not launch manual in web browser.\nPlease see https://nageru.sesse.net/doc/ manually.");
929 void MainWindow::about_triggered()
931 AboutDialog("Futatabi", "Multicamera slow motion video server").exec();
934 void MainWindow::undo_triggered()
936 // Finish any deferred action.
937 if (defer_timeout->isActive()) {
938 defer_timeout->stop();
939 state_changed(deferred_state);
942 StateProto redo_state;
943 *redo_state.mutable_clip_list() = cliplist_clips->serialize();
944 *redo_state.mutable_play_list() = playlist_clips->serialize();
945 redo_stack.push_back(std::move(redo_state));
946 ui->redo_action->setEnabled(true);
948 assert(undo_stack.size() > 1);
950 // Pop off the current state, which is always at the top of the stack.
951 undo_stack.pop_back();
953 StateProto state = undo_stack.back();
954 ui->undo_action->setEnabled(undo_stack.size() > 1);
956 replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
957 replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
959 db.store_state(state);
962 void MainWindow::redo_triggered()
964 assert(!redo_stack.empty());
966 ui->undo_action->setEnabled(true);
967 ui->redo_action->setEnabled(true);
969 undo_stack.push_back(std::move(redo_stack.back()));
970 redo_stack.pop_back();
971 ui->undo_action->setEnabled(true);
972 ui->redo_action->setEnabled(!redo_stack.empty());
974 const StateProto &state = undo_stack.back();
975 replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
976 replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
978 db.store_state(state);
981 void MainWindow::quality_toggled(int quality, bool checked)
986 global_flags.interpolation_quality = quality;
987 if (quality != 0 && // Turning interpolation off is always possible.
988 quality != flow_initialized_interpolation_quality) {
990 msgbox.setText(QString::fromStdString(
991 "The interpolation quality for the main output cannot be changed at runtime, "
992 "except being turned completely off; it will take effect for exported files "
993 "only until next restart. The live output quality thus remains at " +
994 to_string(flow_initialized_interpolation_quality) + "."));
1001 void MainWindow::padding_toggled(double seconds, bool checked)
1006 global_flags.cue_point_padding_seconds = seconds;
1010 void MainWindow::highlight_camera_input(int stream_idx)
1012 for (unsigned i = 0; i < num_cameras; ++i) {
1013 if (unsigned(stream_idx) == i) {
1014 displays[i].frame->setStyleSheet("background: rgb(0,255,0)");
1016 displays[i].frame->setStyleSheet("");
1021 void MainWindow::set_output_status(const string &status)
1023 ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
1024 if (live_player != nullptr) {
1025 live_player->set_pause_status(status);
1028 lock_guard<mutex> lock(queue_status_mu);
1029 queue_status = status;
1032 pair<string, string> MainWindow::get_queue_status() const
1034 lock_guard<mutex> lock(queue_status_mu);
1035 return { queue_status, "text/plain" };
1038 void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame)
1040 if (stream_idx >= MAX_STREAMS) {
1041 fprintf(stderr, "WARNING: Ignoring too-high stream index %u.\n", stream_idx);
1044 if (stream_idx >= num_cameras) {
1045 post_to_main_thread_and_wait([this, stream_idx] {
1046 num_cameras = stream_idx + 1;
1047 change_num_cameras();
1050 displays[stream_idx].display->setFrame(stream_idx, frame);
1053 template<class Model>
1054 void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
1056 QItemSelectionModel *old_selection_model = view->selectionModel();
1057 view->setModel(new_model);
1059 delete old_selection_model;
1061 connect(new_model, &Model::any_content_changed, this, &MainWindow::content_changed);
1064 void MainWindow::start_tally()
1066 http_reply = http.get(QNetworkRequest(QString::fromStdString(global_flags.tally_url)));
1067 connect(http_reply, &QNetworkReply::finished, this, &MainWindow::tally_received);
1070 void MainWindow::tally_received()
1072 unsigned time_to_next_tally_ms;
1073 if (http_reply->error()) {
1074 fprintf(stderr, "HTTP get of '%s' failed: %s\n", global_flags.tally_url.c_str(),
1075 http_reply->errorString().toStdString().c_str());
1076 ui->live_frame->setStyleSheet("");
1077 time_to_next_tally_ms = 1000;
1079 string contents = http_reply->readAll().toStdString();
1080 ui->live_frame->setStyleSheet(QString::fromStdString("background: " + contents));
1081 time_to_next_tally_ms = 100;
1083 http_reply->deleteLater();
1084 http_reply = nullptr;
1086 QTimer::singleShot(time_to_next_tally_ms, this, &MainWindow::start_tally);