1 #include "mainwindow.h"
6 #include "frame_on_disk.h"
8 #include "futatabi_midi_mapping.pb.h"
9 #include "midi_mapping_dialog.h"
10 #include "shared/aboutdialog.h"
11 #include "shared/disk_space_estimator.h"
12 #include "shared/post_to_main_thread.h"
13 #include "shared/timebase.h"
14 #include "ui_mainwindow.h"
16 #include <QDesktopServices>
17 #include <QFileDialog>
18 #include <QMessageBox>
19 #include <QMouseEvent>
20 #include <QNetworkReply>
23 #include <QWheelEvent>
30 using namespace std::placeholders;
32 MainWindow *global_mainwindow = nullptr;
33 static ClipList *cliplist_clips;
34 static PlayList *playlist_clips;
36 extern int64_t current_pts;
40 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
42 pts = std::max<int64_t>(pts, 0);
43 if (clip->pts_out == -1) {
44 pts = std::min(pts, current_pts);
46 pts = std::min(pts, clip->pts_out);
53 MainWindow::MainWindow()
54 : ui(new Ui::MainWindow),
55 db(global_flags.working_directory + "/futatabi.db"),
58 global_mainwindow = this;
61 // Load settings from database.
62 SettingsProto settings = db.get_settings();
63 if (!global_flags.interpolation_quality_set) {
64 if (settings.interpolation_quality() != 0) {
65 global_flags.interpolation_quality = settings.interpolation_quality() - 1;
68 if (!global_flags.cue_in_point_padding_set) {
69 global_flags.cue_in_point_padding_seconds = settings.cue_in_point_padding_seconds(); // Default 0 is fine.
71 if (!global_flags.cue_out_point_padding_set) {
72 global_flags.cue_out_point_padding_seconds = settings.cue_out_point_padding_seconds(); // Default 0 is fine.
74 if (global_flags.interpolation_quality == 0) {
75 // Allocate something just for simplicity; we won't be using it
76 // unless the user changes runtime, in which case 1 is fine.
77 flow_initialized_interpolation_quality = 1;
79 flow_initialized_interpolation_quality = global_flags.interpolation_quality;
84 connect(ui->midi_mapping_action, &QAction::triggered, this, &MainWindow::midi_mapping_triggered);
85 connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
86 connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered);
87 connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered);
88 connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered);
89 connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
90 connect(ui->undo_action, &QAction::triggered, this, &MainWindow::undo_triggered);
91 connect(ui->redo_action, &QAction::triggered, this, &MainWindow::redo_triggered);
92 ui->undo_action->setEnabled(false);
93 ui->redo_action->setEnabled(false);
96 QActionGroup *quality_group = new QActionGroup(ui->interpolation_menu);
97 quality_group->addAction(ui->quality_0_action);
98 quality_group->addAction(ui->quality_1_action);
99 quality_group->addAction(ui->quality_2_action);
100 quality_group->addAction(ui->quality_3_action);
101 quality_group->addAction(ui->quality_4_action);
102 if (global_flags.interpolation_quality == 0) {
103 ui->quality_0_action->setChecked(true);
104 } else if (global_flags.interpolation_quality == 1) {
105 ui->quality_1_action->setChecked(true);
106 } else if (global_flags.interpolation_quality == 2) {
107 ui->quality_2_action->setChecked(true);
108 } else if (global_flags.interpolation_quality == 3) {
109 ui->quality_3_action->setChecked(true);
110 } else if (global_flags.interpolation_quality == 4) {
111 ui->quality_4_action->setChecked(true);
115 connect(ui->quality_0_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 0, _1));
116 connect(ui->quality_1_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 1, _1));
117 connect(ui->quality_2_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 2, _1));
118 connect(ui->quality_3_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 3, _1));
119 connect(ui->quality_4_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 4, _1));
121 // The cue-in point padding group.
122 QActionGroup *in_padding_group = new QActionGroup(ui->in_padding_menu);
123 in_padding_group->addAction(ui->in_padding_0_action);
124 in_padding_group->addAction(ui->in_padding_1_action);
125 in_padding_group->addAction(ui->in_padding_2_action);
126 in_padding_group->addAction(ui->in_padding_5_action);
127 if (global_flags.cue_in_point_padding_seconds <= 1e-3) {
128 ui->in_padding_0_action->setChecked(true);
129 } else if (fabs(global_flags.cue_in_point_padding_seconds - 1.0) < 1e-3) {
130 ui->in_padding_1_action->setChecked(true);
131 } else if (fabs(global_flags.cue_in_point_padding_seconds - 2.0) < 1e-3) {
132 ui->in_padding_2_action->setChecked(true);
133 } else if (fabs(global_flags.cue_in_point_padding_seconds - 5.0) < 1e-3) {
134 ui->in_padding_5_action->setChecked(true);
136 // Nothing to check, which is fine.
138 connect(ui->in_padding_0_action, &QAction::toggled, bind(&MainWindow::in_padding_toggled, this, 0.0, _1));
139 connect(ui->in_padding_1_action, &QAction::toggled, bind(&MainWindow::in_padding_toggled, this, 1.0, _1));
140 connect(ui->in_padding_2_action, &QAction::toggled, bind(&MainWindow::in_padding_toggled, this, 2.0, _1));
141 connect(ui->in_padding_5_action, &QAction::toggled, bind(&MainWindow::in_padding_toggled, this, 5.0, _1));
143 // Same for the cue-out padding.
144 QActionGroup *out_padding_group = new QActionGroup(ui->out_padding_menu);
145 out_padding_group->addAction(ui->out_padding_0_action);
146 out_padding_group->addAction(ui->out_padding_1_action);
147 out_padding_group->addAction(ui->out_padding_2_action);
148 out_padding_group->addAction(ui->out_padding_5_action);
149 if (global_flags.cue_out_point_padding_seconds <= 1e-3) {
150 ui->out_padding_0_action->setChecked(true);
151 } else if (fabs(global_flags.cue_out_point_padding_seconds - 1.0) < 1e-3) {
152 ui->out_padding_1_action->setChecked(true);
153 } else if (fabs(global_flags.cue_out_point_padding_seconds - 2.0) < 1e-3) {
154 ui->out_padding_2_action->setChecked(true);
155 } else if (fabs(global_flags.cue_out_point_padding_seconds - 5.0) < 1e-3) {
156 ui->out_padding_5_action->setChecked(true);
158 // Nothing to check, which is fine.
160 connect(ui->out_padding_0_action, &QAction::toggled, bind(&MainWindow::out_padding_toggled, this, 0.0, _1));
161 connect(ui->out_padding_1_action, &QAction::toggled, bind(&MainWindow::out_padding_toggled, this, 1.0, _1));
162 connect(ui->out_padding_2_action, &QAction::toggled, bind(&MainWindow::out_padding_toggled, this, 2.0, _1));
163 connect(ui->out_padding_5_action, &QAction::toggled, bind(&MainWindow::out_padding_toggled, this, 5.0, _1));
165 global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
166 disk_free_label = new QLabel(this);
167 disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
168 ui->menuBar->setCornerWidget(disk_free_label);
170 StateProto state = db.get_state();
171 undo_stack.push_back(state); // The undo stack always has the current state on top.
173 cliplist_clips = new ClipList(state.clip_list());
174 ui->clip_list->setModel(cliplist_clips);
175 connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
177 playlist_clips = new PlayList(state.play_list());
178 ui->playlist->setModel(playlist_clips);
179 connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
181 // For un-highlighting when we lose focus.
182 ui->clip_list->installEventFilter(this);
184 // For scrubbing in the pts columns.
185 ui->clip_list->viewport()->installEventFilter(this);
186 ui->playlist->viewport()->installEventFilter(this);
188 QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
189 connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
190 connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
192 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
193 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
194 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
196 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
197 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
198 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
200 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
201 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
202 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
204 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
205 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
206 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
208 QShortcut *next = new QShortcut(QKeySequence(Qt::Key_N), this);
209 connect(next, &QShortcut::activated, ui->next_btn, &QPushButton::click);
210 connect(ui->next_btn, &QPushButton::clicked, this, &MainWindow::next_clicked);
212 connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
213 ui->stop_btn->setEnabled(false);
215 connect(ui->speed_slider, &QAbstractSlider::valueChanged, this, &MainWindow::speed_slider_changed);
216 connect(ui->speed_lock_btn, &QPushButton::clicked, this, &MainWindow::speed_lock_clicked);
218 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
220 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
221 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
222 connect(delete_key, &QShortcut::activated, [this] {
223 if (ui->playlist->hasFocus()) {
228 // TODO: support drag-and-drop.
229 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this] { playlist_move(-1); });
230 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this] { playlist_move(1); });
232 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
233 this, &MainWindow::playlist_selection_changed);
234 playlist_selection_changed(); // First time set-up.
236 preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
237 preview_player->set_done_callback([this] {
238 post_to_main_thread([this] {
239 preview_player_done();
243 live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
244 live_player->set_done_callback([this] {
245 post_to_main_thread([this] {
249 live_player->set_progress_callback([this](const map<uint64_t, double> &progress, TimeRemaining time_remaining) {
250 post_to_main_thread([this, progress, time_remaining] {
251 live_player_clip_progress(progress, time_remaining);
254 set_output_status("paused");
255 enable_or_disable_queue_button();
257 defer_timeout = new QTimer(this);
258 defer_timeout->setSingleShot(true);
259 connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
260 ui->undo_action->setEnabled(true);
262 lock_blink_timeout = new QTimer(this);
263 lock_blink_timeout->setSingleShot(true);
264 connect(lock_blink_timeout, &QTimer::timeout, this, &MainWindow::lock_blink_timer_expired);
266 connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
267 this, &MainWindow::clip_list_selection_changed);
268 enable_or_disable_queue_button();
270 // Find out how many cameras we have in the existing frames;
271 // if none, we start with two cameras.
274 lock_guard<mutex> lock(frame_mu);
275 for (size_t stream_idx = 2; stream_idx < MAX_STREAMS; ++stream_idx) {
276 if (!frames[stream_idx].empty()) {
277 num_cameras = stream_idx + 1;
281 change_num_cameras();
283 if (!global_flags.tally_url.empty()) {
287 if (!global_flags.midi_mapping_filename.empty()) {
288 MIDIMappingProto midi_mapping;
289 if (!load_midi_mapping_from_file(global_flags.midi_mapping_filename, &midi_mapping)) {
290 fprintf(stderr, "Couldn't load MIDI mapping '%s'; exiting.\n",
291 global_flags.midi_mapping_filename.c_str());
294 midi_mapper.set_midi_mapping(midi_mapping);
296 midi_mapper.refresh_lights();
297 midi_mapper.start_thread();
300 void MainWindow::change_num_cameras()
302 assert(num_cameras >= displays.size()); // We only add, never remove.
304 // Make new entries to hide the displays.
305 for (unsigned i = displays.size(); i < num_cameras; ++i) {
307 snprintf(title, sizeof(title), "Camera %u", i + 1);
308 QAction *hide_action = ui->hide_camera_menu->addAction(title);
309 hide_action->setCheckable(true);
310 hide_action->setChecked(false);
311 connect(hide_action, &QAction::toggled, bind(&MainWindow::hide_camera_toggled, this, i, _1));
314 // Make new display rows.
315 for (unsigned i = displays.size(); i < num_cameras; ++i) {
316 QFrame *frame = new QFrame(this);
317 frame->setAutoFillBackground(true);
319 QLayout *layout = new QGridLayout(frame);
320 frame->setLayout(layout);
321 layout->setContentsMargins(3, 3, 3, 3);
323 JPEGFrameView *display = new JPEGFrameView(frame);
324 display->setAutoFillBackground(true);
325 layout->addWidget(display);
327 display->set_overlay(to_string(i + 1));
329 QPushButton *preview_btn = new QPushButton(this);
330 preview_btn->setMaximumSize(20, 17);
331 preview_btn->setText(QString::fromStdString(to_string(i + 1)));
332 ui->preview_layout->addWidget(preview_btn);
334 displays.emplace_back(FrameAndDisplay{ frame, display, preview_btn, /*hidden=*/false });
336 connect(display, &JPEGFrameView::clicked, preview_btn, &QPushButton::click);
337 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
338 connect(shortcut, &QShortcut::activated, preview_btn, &QPushButton::click);
340 connect(preview_btn, &QPushButton::clicked, [this, i] { preview_angle_clicked(i); });
344 cliplist_clips->change_num_cameras(num_cameras);
345 playlist_clips->change_num_cameras(num_cameras);
347 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
350 void MainWindow::relayout_displays()
352 while (ui->input_displays->count() > 0) {
353 QLayoutItem *item = ui->input_displays->takeAt(0);
354 ui->input_displays->removeWidget(item->widget());
357 unsigned cell_idx = 0;
358 for (unsigned i = 0; i < displays.size(); ++i) {
359 if (displays[i].hidden) {
360 displays[i].frame->setVisible(false);
362 displays[i].frame->setVisible(true);
363 ui->input_displays->addWidget(displays[i].frame, cell_idx / 2, cell_idx % 2);
367 ui->video_displays->setStretch(1, (cell_idx + 1) / 2);
369 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
372 MainWindow::~MainWindow()
374 // We don't have a context to release Player's OpenGL resources in here,
375 // so instead of crashing on exit, leak it.
376 live_player.release();
377 preview_player.release();
380 void MainWindow::cue_in_clicked()
382 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
383 cliplist_clips->mutable_back()->pts_in = current_pts;
386 clip.pts_in = max<int64_t>(current_pts - lrint(global_flags.cue_in_point_padding_seconds * TIMEBASE), 0);
387 cliplist_clips->add_clip(clip);
388 playlist_selection_changed();
391 // Select the item so that we can jog it.
392 ui->clip_list->setFocus();
393 QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::IN));
394 ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
395 ui->clip_list->scrollToBottom();
396 enable_or_disable_queue_button();
399 void MainWindow::cue_out_clicked()
401 if (cliplist_clips->empty()) {
405 cliplist_clips->mutable_back()->pts_out = current_pts + lrint(global_flags.cue_out_point_padding_seconds * TIMEBASE);
407 // Select the item so that we can jog it.
408 ui->clip_list->setFocus();
409 QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::OUT));
410 ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
411 ui->clip_list->scrollToBottom();
412 enable_or_disable_queue_button();
415 void MainWindow::queue_clicked()
417 // See also enable_or_disable_queue_button().
419 if (cliplist_clips->empty()) {
423 QItemSelectionModel *selected = ui->clip_list->selectionModel();
424 if (!selected->hasSelection()) {
425 Clip clip = *cliplist_clips->back();
427 playlist_clips->add_clip(clip);
428 playlist_selection_changed();
429 ui->playlist->scrollToBottom();
433 QModelIndex index = selected->currentIndex();
434 Clip clip = *cliplist_clips->clip(index.row());
435 if (cliplist_clips->is_camera_column(index.column())) {
436 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
438 clip.stream_idx = ui->preview_display->get_stream_idx();
441 playlist_clips->add_clip(clip);
442 playlist_selection_changed();
443 ui->playlist->scrollToBottom();
444 if (!ui->playlist->selectionModel()->hasSelection()) {
445 // TODO: Figure out why this doesn't always seem to actually select the row.
446 QModelIndex bottom = playlist_clips->index(playlist_clips->size() - 1, 0);
447 ui->playlist->setCurrentIndex(bottom);
451 void MainWindow::preview_clicked()
453 // See also enable_or_disable_preview_button().
455 if (ui->playlist->hasFocus()) {
456 // Allow the playlist as preview iff it has focus and something is selected.
457 QItemSelectionModel *selected = ui->playlist->selectionModel();
458 if (selected->hasSelection()) {
459 QModelIndex index = selected->currentIndex();
460 const Clip &clip = *playlist_clips->clip(index.row());
461 preview_player->play(clip);
462 preview_playing = true;
463 enable_or_disable_preview_button();
468 if (cliplist_clips->empty())
471 QItemSelectionModel *selected = ui->clip_list->selectionModel();
472 if (!selected->hasSelection()) {
473 preview_player->play(*cliplist_clips->back());
474 preview_playing = true;
475 enable_or_disable_preview_button();
479 QModelIndex index = selected->currentIndex();
480 Clip clip = *cliplist_clips->clip(index.row());
481 if (cliplist_clips->is_camera_column(index.column())) {
482 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
484 clip.stream_idx = ui->preview_display->get_stream_idx();
486 if (clip.pts_out == -1) {
487 clip.pts_out = clip.pts_in + int64_t(TIMEBASE) * 86400 * 7; // One week; effectively infinite, but without overflow issues.
489 preview_player->play(clip);
490 preview_playing = true;
491 enable_or_disable_preview_button();
494 void MainWindow::preview_angle_clicked(unsigned stream_idx)
496 preview_player->override_angle(stream_idx);
498 // Change the selection if we were previewing a clip from the clip list.
499 // (The only other thing we could be showing is a pts scrub, and if so,
500 // that would be selected.)
501 QItemSelectionModel *selected = ui->clip_list->selectionModel();
502 if (selected->hasSelection()) {
503 QModelIndex cell = selected->selectedIndexes()[0];
504 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
505 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
509 void MainWindow::playlist_duplicate()
511 QItemSelectionModel *selected = ui->playlist->selectionModel();
512 if (!selected->hasSelection()) {
513 // Should have been grayed out, but OK.
516 QModelIndexList rows = selected->selectedRows();
517 int first = rows.front().row(), last = rows.back().row();
518 playlist_clips->duplicate_clips(first, last);
519 playlist_selection_changed();
522 void MainWindow::playlist_remove()
524 QItemSelectionModel *selected = ui->playlist->selectionModel();
525 if (!selected->hasSelection()) {
526 // Should have been grayed out, but OK.
529 QModelIndexList rows = selected->selectedRows();
530 int first = rows.front().row(), last = rows.back().row();
531 playlist_clips->erase_clips(first, last);
533 // TODO: select the next one in the list?
535 playlist_selection_changed();
538 void MainWindow::playlist_move(int delta)
540 QItemSelectionModel *selected = ui->playlist->selectionModel();
541 if (!selected->hasSelection()) {
542 // Should have been grayed out, but OK.
546 QModelIndexList rows = selected->selectedRows();
547 int first = rows.front().row(), last = rows.back().row();
548 if ((delta == -1 && first == 0) ||
549 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
550 // Should have been grayed out, but OK.
554 playlist_clips->move_clips(first, last, delta);
555 playlist_selection_changed();
558 void MainWindow::jog_internal(JogDestination jog_destination, int row, int column, int stream_idx, int pts_delta)
560 constexpr int camera_pts_per_pixel = 1500; // One click of most mice (15 degrees), multiplied by the default wheel_sensitivity.
562 int in_column, out_column, camera_column;
563 if (jog_destination == JOG_CLIP_LIST) {
564 in_column = int(ClipList::Column::IN);
565 out_column = int(ClipList::Column::OUT);
567 } else if (jog_destination == JOG_PLAYLIST) {
568 in_column = int(PlayList::Column::IN);
569 out_column = int(PlayList::Column::OUT);
570 camera_column = int(PlayList::Column::CAMERA);
575 currently_deferring_model_changes = true;
577 current_change_id = (jog_destination == JOG_CLIP_LIST) ? "cliplist:" : "playlist:";
578 ClipProxy clip = (jog_destination == JOG_CLIP_LIST) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
579 if (jog_destination == JOG_PLAYLIST) {
580 stream_idx = clip->stream_idx;
583 if (column == in_column) {
584 current_change_id += "in:" + to_string(row);
585 int64_t pts = clip->pts_in + pts_delta;
586 set_pts_in(pts, current_pts, clip);
587 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
588 } else if (column == out_column) {
589 current_change_id += "out:" + to_string(row);
590 int64_t pts = clip->pts_out + pts_delta;
591 pts = std::max(pts, clip->pts_in);
592 pts = std::min(pts, current_pts);
594 preview_single_frame(pts, stream_idx, LAST_BEFORE);
595 } else if (column == camera_column) {
596 current_change_id += "camera:" + to_string(row);
597 int angle_degrees = pts_delta;
598 if (last_mousewheel_camera_row == row) {
599 angle_degrees += leftover_angle_degrees;
602 int stream_idx = clip->stream_idx + angle_degrees / camera_pts_per_pixel;
603 stream_idx = std::max(stream_idx, 0);
604 stream_idx = std::min<int>(stream_idx, num_cameras - 1);
605 clip->stream_idx = stream_idx;
607 last_mousewheel_camera_row = row;
608 leftover_angle_degrees = angle_degrees % camera_pts_per_pixel;
610 // Don't update the live view, that's rarely what the operator wants.
613 currently_deferring_model_changes = false;
616 void MainWindow::defer_timer_expired()
618 state_changed(deferred_state);
621 void MainWindow::content_changed()
623 // If we are playing, update the part of the playlist that's not playing yet.
624 vector<ClipWithID> clips;
625 for (unsigned row = 0; row < playlist_clips->size(); ++row) {
626 clips.emplace_back(*playlist_clips->clip_with_id(row));
628 live_player->splice_play(clips);
630 // Serialize the state.
631 if (defer_timeout->isActive() &&
632 (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
633 // There's some deferred event waiting, but this event is unrelated.
634 // So it's time to short-circuit that timer and do the work it wanted to do.
635 defer_timeout->stop();
636 state_changed(deferred_state);
639 *state.mutable_clip_list() = cliplist_clips->serialize();
640 *state.mutable_play_list() = playlist_clips->serialize();
641 if (currently_deferring_model_changes) {
642 deferred_change_id = current_change_id;
643 deferred_state = std::move(state);
644 defer_timeout->start(200);
647 state_changed(state);
650 void MainWindow::state_changed(const StateProto &state)
652 db.store_state(state);
655 ui->redo_action->setEnabled(false);
657 undo_stack.push_back(state);
658 ui->undo_action->setEnabled(undo_stack.size() > 1);
660 // Make sure it doesn't grow without bounds.
661 while (undo_stack.size() >= 100) {
662 undo_stack.pop_front();
666 void MainWindow::save_settings()
668 SettingsProto settings;
669 settings.set_interpolation_quality(global_flags.interpolation_quality + 1);
670 settings.set_cue_in_point_padding_seconds(global_flags.cue_in_point_padding_seconds);
671 settings.set_cue_out_point_padding_seconds(global_flags.cue_out_point_padding_seconds);
672 db.store_settings(settings);
675 void MainWindow::lock_blink_timer_expired()
677 midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked())); // Presumably On, or the timer should have been canceled.
680 void MainWindow::play_clicked()
682 QItemSelectionModel *selected = ui->playlist->selectionModel();
683 if (!selected->hasSelection()) {
686 unsigned start_row = selected->selectedRows(0)[0].row();
688 vector<ClipWithID> clips;
689 for (unsigned row = start_row; row < playlist_clips->size(); ++row) {
690 ClipWithID clip = *playlist_clips->clip_with_id(row);
691 if (clip.clip.pts_out == -1) {
692 clip.clip.pts_out = clip.clip.pts_in + int64_t(TIMEBASE) * 86400 * 7; // One week; effectively infinite, but without overflow issues.
694 clips.emplace_back(clip);
696 live_player->play(clips);
697 playlist_clips->set_progress({ { start_row, 0.0f } });
698 ui->playlist->selectionModel()->clear();
699 ui->stop_btn->setEnabled(true);
700 playlist_selection_changed();
703 void MainWindow::next_clicked()
705 live_player->skip_to_next();
708 void MainWindow::stop_clicked()
711 fake_clip.pts_in = 0;
712 fake_clip.pts_out = 0;
713 playlist_clips->set_progress({});
714 live_player->play(fake_clip);
715 ui->stop_btn->setEnabled(false);
716 playlist_selection_changed();
719 void MainWindow::speed_slider_changed(int percent)
721 float speed = percent / 100.0f;
722 ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
723 live_player->set_master_speed(speed);
724 midi_mapper.set_speed_light(speed);
727 void MainWindow::speed_lock_clicked()
729 // TODO: Make for a less abrupt transition if we're not already at 100%.
730 ui->speed_slider->setValue(100); // Also actually sets the master speed and updates the label.
731 ui->speed_slider->setEnabled(!ui->speed_lock_btn->isChecked());
732 midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked()));
733 lock_blink_timeout->stop();
736 void MainWindow::preview_player_done()
738 preview_playing = false;
739 enable_or_disable_preview_button();
742 void MainWindow::live_player_done()
744 playlist_clips->set_progress({});
745 ui->stop_btn->setEnabled(false);
746 playlist_selection_changed();
749 void MainWindow::live_player_clip_progress(const map<uint64_t, double> &progress, TimeRemaining time_remaining)
751 playlist_clips->set_progress(progress);
752 set_output_status(format_duration(time_remaining) + " left");
755 void MainWindow::resizeEvent(QResizeEvent *event)
757 QMainWindow::resizeEvent(event);
759 // Ask for a relayout, but only after the event loop is done doing relayout
760 // on everything else.
761 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
764 void MainWindow::relayout()
766 ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
767 ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
770 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
772 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
773 int scrub_sensitivity = 100; // pts units per pixel.
774 int wheel_sensitivity = 100; // pts units per degree.
776 if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
777 enable_or_disable_preview_button();
778 playlist_selection_changed();
779 hidden_jog_column = -1;
782 unsigned stream_idx = ui->preview_display->get_stream_idx();
784 if (watched == ui->clip_list) {
785 if (event->type() == QEvent::FocusOut) {
786 highlight_camera_input(-1);
791 if (event->type() != QEvent::Wheel) {
792 last_mousewheel_camera_row = -1;
795 if (event->type() == QEvent::MouseButtonPress) {
796 QMouseEvent *mouse = (QMouseEvent *)event;
798 QTableView *destination;
801 if (watched == ui->clip_list->viewport()) {
802 destination = ui->clip_list;
803 type = SCRUBBING_CLIP_LIST;
804 } else if (watched == ui->playlist->viewport()) {
805 destination = ui->playlist;
806 type = SCRUBBING_PLAYLIST;
810 int column = destination->columnAt(mouse->x());
811 int row = destination->rowAt(mouse->y());
812 if (column == -1 || row == -1)
815 if (type == SCRUBBING_CLIP_LIST) {
816 if (ClipList::Column(column) == ClipList::Column::IN) {
817 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
818 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
819 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
820 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
821 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
826 if (PlayList::Column(column) == PlayList::Column::IN) {
827 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
828 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
829 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
830 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
831 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
839 scrub_column = column;
840 scrub_x_origin = mouse->x();
842 } else if (event->type() == QEvent::MouseMove) {
843 QMouseEvent *mouse = (QMouseEvent *)event;
844 if (mouse->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
845 scrub_sensitivity *= 10;
846 wheel_sensitivity *= 10;
847 if (mouse->modifiers() & Qt::KeyboardModifier::ControlModifier) {
848 // Ctrl+Shift is a super-modifier, meant only for things like “go back two hours”.
849 scrub_sensitivity *= 100;
850 wheel_sensitivity *= 100;
853 if (mouse->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
854 scrub_sensitivity /= 10;
855 wheel_sensitivity /= 10;
858 int offset = mouse->x() - scrub_x_origin;
860 if (offset >= dead_zone_pixels) {
861 adjusted_offset = offset - dead_zone_pixels;
862 } else if (offset < -dead_zone_pixels) {
863 adjusted_offset = offset + dead_zone_pixels;
868 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
869 currently_deferring_model_changes = true;
870 if (scrub_type == SCRUBBING_CLIP_LIST) {
871 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
872 if (scrub_column == int(ClipList::Column::IN)) {
873 current_change_id = "cliplist:in:" + to_string(scrub_row);
874 set_pts_in(pts, current_pts, clip);
875 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
877 current_change_id = "cliplist:out" + to_string(scrub_row);
878 pts = std::max(pts, clip->pts_in);
879 pts = std::min(pts, current_pts);
881 preview_single_frame(pts, stream_idx, LAST_BEFORE);
884 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
885 if (scrub_column == int(PlayList::Column::IN)) {
886 current_change_id = "playlist:in:" + to_string(scrub_row);
887 set_pts_in(pts, current_pts, clip);
888 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
890 current_change_id = "playlist:out:" + to_string(scrub_row);
891 pts = std::max(pts, clip->pts_in);
892 pts = std::min(pts, current_pts);
894 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
897 currently_deferring_model_changes = false;
899 return true; // Don't use this mouse movement for selecting things.
901 } else if (event->type() == QEvent::Wheel) {
902 QWheelEvent *wheel = (QWheelEvent *)event;
903 int angle_delta = wheel->angleDelta().y();
904 if (wheel->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
905 scrub_sensitivity *= 10;
906 wheel_sensitivity *= 10;
907 if (wheel->modifiers() & Qt::KeyboardModifier::ControlModifier) {
908 // Ctrl+Shift is a super-modifier, meant only for things like “go back two hours”.
909 scrub_sensitivity *= 100;
910 wheel_sensitivity *= 100;
913 if (wheel->modifiers() & Qt::KeyboardModifier::AltModifier) { // Note: Shift + Alt cancel each other out.
914 scrub_sensitivity /= 10;
915 wheel_sensitivity /= 10;
916 angle_delta = wheel->angleDelta().x(); // Qt ickiness.
919 QTableView *destination;
920 JogDestination jog_destination;
921 if (watched == ui->clip_list->viewport()) {
922 destination = ui->clip_list;
923 jog_destination = JOG_CLIP_LIST;
924 last_mousewheel_camera_row = -1;
925 } else if (watched == ui->playlist->viewport()) {
926 destination = ui->playlist;
927 jog_destination = JOG_PLAYLIST;
928 if (destination->columnAt(wheel->x()) != int(PlayList::Column::CAMERA)) {
929 last_mousewheel_camera_row = -1;
932 last_mousewheel_camera_row = -1;
936 int column = destination->columnAt(wheel->x());
937 int row = destination->rowAt(wheel->y());
938 if (column == -1 || row == -1)
941 // Only adjust pts with the wheel if the given row is selected.
942 if (!destination->hasFocus() ||
943 row != destination->selectionModel()->currentIndex().row()) {
947 jog_internal(jog_destination, row, column, stream_idx, angle_delta * wheel_sensitivity);
948 return true; // Don't scroll.
949 } else if (event->type() == QEvent::MouseButtonRelease) {
955 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
957 if (rounding == LAST_BEFORE) {
958 lock_guard<mutex> lock(frame_mu);
959 if (frames[stream_idx].empty())
961 auto it = find_last_frame_before(frames[stream_idx], pts);
962 if (it != frames[stream_idx].end()) {
966 assert(rounding == FIRST_AT_OR_AFTER);
967 lock_guard<mutex> lock(frame_mu);
968 if (frames[stream_idx].empty())
970 auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
971 if (it != frames[stream_idx].end()) {
977 fake_clip.pts_in = pts;
978 fake_clip.pts_out = pts + 1;
979 fake_clip.stream_idx = stream_idx;
980 preview_player->play(fake_clip);
983 void MainWindow::playlist_selection_changed()
985 enable_or_disable_preview_button();
987 QItemSelectionModel *selected = ui->playlist->selectionModel();
988 bool any_selected = selected->hasSelection();
989 ui->playlist_duplicate_btn->setEnabled(any_selected);
990 ui->playlist_remove_btn->setEnabled(any_selected);
991 ui->playlist_move_up_btn->setEnabled(
992 any_selected && selected->selectedRows().front().row() > 0);
993 ui->playlist_move_down_btn->setEnabled(
994 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
996 ui->play_btn->setEnabled(any_selected);
997 ui->next_btn->setEnabled(ui->stop_btn->isEnabled()); // TODO: Perhaps not if we're on the last clip?
998 midi_mapper.set_next_ready(ui->next_btn->isEnabled() ? MIDIMapper::On : MIDIMapper::Off);
1000 // NOTE: The hidden button is still reachable by keyboard or MIDI.
1002 ui->play_btn->setVisible(true);
1003 } else if (ui->stop_btn->isEnabled()) { // Playing.
1004 ui->play_btn->setVisible(false);
1006 ui->play_btn->setVisible(true);
1008 ui->next_btn->setVisible(!ui->play_btn->isVisible());
1010 if (ui->stop_btn->isEnabled()) { // Playing.
1011 midi_mapper.set_play_enabled(MIDIMapper::On);
1012 } else if (any_selected) {
1013 midi_mapper.set_play_enabled(MIDIMapper::Blinking);
1015 midi_mapper.set_play_enabled(MIDIMapper::Off);
1018 if (!any_selected) {
1019 set_output_status("paused");
1021 vector<ClipWithID> clips;
1022 for (size_t row = selected->selectedRows().front().row(); row < playlist_clips->size(); ++row) {
1023 clips.emplace_back(*playlist_clips->clip_with_id(row));
1025 TimeRemaining remaining = compute_total_time(clips);
1026 set_output_status(format_duration(remaining) + " ready");
1030 void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &previous)
1032 int camera_selected = -1;
1033 if (cliplist_clips->is_camera_column(current.column())) {
1034 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
1036 // See the comment on hidden_jog_column.
1037 if (current.row() != previous.row()) {
1038 hidden_jog_column = -1;
1039 } else if (hidden_jog_column == -1) {
1040 hidden_jog_column = previous.column();
1043 hidden_jog_column = -1;
1045 highlight_camera_input(camera_selected);
1046 enable_or_disable_queue_button();
1049 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
1052 if (estimated_seconds_left < 60.0) {
1053 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
1054 } else if (estimated_seconds_left < 1800.0) { // Less than half an hour: Xm Ys (red).
1055 int s = lrintf(estimated_seconds_left);
1058 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
1059 } else if (estimated_seconds_left < 3600.0) { // Less than an hour: Xm.
1060 int m = lrintf(estimated_seconds_left / 60.0);
1061 snprintf(time_str, sizeof(time_str), "%dm", m);
1062 } else if (estimated_seconds_left < 36000.0) { // Less than ten hours: Xh Ym.
1063 int m = lrintf(estimated_seconds_left / 60.0);
1066 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
1067 } else { // More than ten hours: Xh.
1068 int h = lrintf(estimated_seconds_left / 3600.0);
1069 snprintf(time_str, sizeof(time_str), "%dh", h);
1072 snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
1074 std::string label = buf;
1076 post_to_main_thread([this, label] {
1077 disk_free_label->setText(QString::fromStdString(label));
1078 ui->menuBar->setCornerWidget(disk_free_label); // Need to set this again for the sizing to get right.
1082 void MainWindow::midi_mapping_triggered()
1084 MIDIMappingDialog(&midi_mapper).exec();
1087 void MainWindow::exit_triggered()
1092 void MainWindow::export_cliplist_clip_multitrack_triggered()
1094 QItemSelectionModel *selected = ui->clip_list->selectionModel();
1095 if (!selected->hasSelection()) {
1097 msgbox.setText("No clip selected in the clip list. Select one and try exporting again.");
1102 QModelIndex index = selected->currentIndex();
1103 Clip clip = *cliplist_clips->clip(index.row());
1104 QString filename = QFileDialog::getSaveFileName(this,
1105 "Export multitrack clip", QString(), tr("Matroska video files (*.mkv)"));
1106 if (filename.isNull()) {
1110 if (!filename.endsWith(".mkv")) {
1113 export_multitrack_clip(filename.toStdString(), clip);
1116 void MainWindow::export_playlist_clip_interpolated_triggered()
1118 QItemSelectionModel *selected = ui->playlist->selectionModel();
1119 if (!selected->hasSelection()) {
1121 msgbox.setText("No clip selected in the playlist. Select one and try exporting again.");
1126 QString filename = QFileDialog::getSaveFileName(this,
1127 "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)"));
1128 if (filename.isNull()) {
1132 if (!filename.endsWith(".mkv")) {
1137 QModelIndexList rows = selected->selectedRows();
1138 for (QModelIndex index : rows) {
1139 clips.push_back(*playlist_clips->clip(index.row()));
1141 export_interpolated_clip(filename.toStdString(), clips);
1144 void MainWindow::manual_triggered()
1146 if (!QDesktopServices::openUrl(QUrl("https://nageru.sesse.net/doc/"))) {
1148 msgbox.setText("Could not launch manual in web browser.\nPlease see https://nageru.sesse.net/doc/ manually.");
1153 void MainWindow::about_triggered()
1155 AboutDialog("Futatabi", "Multicamera slow motion video server").exec();
1158 void MainWindow::undo_triggered()
1160 // Finish any deferred action.
1161 if (defer_timeout->isActive()) {
1162 defer_timeout->stop();
1163 state_changed(deferred_state);
1166 StateProto redo_state;
1167 *redo_state.mutable_clip_list() = cliplist_clips->serialize();
1168 *redo_state.mutable_play_list() = playlist_clips->serialize();
1169 redo_stack.push_back(std::move(redo_state));
1170 ui->redo_action->setEnabled(true);
1172 assert(undo_stack.size() > 1);
1174 // Pop off the current state, which is always at the top of the stack.
1175 undo_stack.pop_back();
1177 StateProto state = undo_stack.back();
1178 ui->undo_action->setEnabled(undo_stack.size() > 1);
1180 replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
1181 replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
1183 db.store_state(state);
1186 void MainWindow::redo_triggered()
1188 assert(!redo_stack.empty());
1190 ui->undo_action->setEnabled(true);
1191 ui->redo_action->setEnabled(true);
1193 undo_stack.push_back(std::move(redo_stack.back()));
1194 redo_stack.pop_back();
1195 ui->undo_action->setEnabled(true);
1196 ui->redo_action->setEnabled(!redo_stack.empty());
1198 const StateProto &state = undo_stack.back();
1199 replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
1200 replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
1202 db.store_state(state);
1205 void MainWindow::quality_toggled(int quality, bool checked)
1210 global_flags.interpolation_quality = quality;
1211 if (quality != 0 && // Turning interpolation off is always possible.
1212 quality != flow_initialized_interpolation_quality) {
1214 msgbox.setText(QString::fromStdString(
1215 "The interpolation quality for the main output cannot be changed at runtime, "
1216 "except being turned completely off; it will take effect for exported files "
1217 "only until next restart. The live output quality thus remains at " +
1218 to_string(flow_initialized_interpolation_quality) + "."));
1225 void MainWindow::in_padding_toggled(double seconds, bool checked)
1230 global_flags.cue_in_point_padding_seconds = seconds;
1234 void MainWindow::out_padding_toggled(double seconds, bool checked)
1239 global_flags.cue_out_point_padding_seconds = seconds;
1243 void MainWindow::hide_camera_toggled(unsigned camera_idx, bool checked)
1245 displays[camera_idx].hidden = checked;
1246 relayout_displays();
1249 void MainWindow::highlight_camera_input(int stream_idx)
1251 for (unsigned i = 0; i < num_cameras; ++i) {
1252 if (unsigned(stream_idx) == i) {
1253 displays[i].frame->setStyleSheet("background: rgb(0,255,0)");
1255 displays[i].frame->setStyleSheet("");
1258 midi_mapper.highlight_camera_input(stream_idx);
1261 void MainWindow::enable_or_disable_preview_button()
1263 // Follows the logic in preview_clicked().
1265 if (ui->playlist->hasFocus()) {
1266 // Allow the playlist as preview iff it has focus and something is selected.
1267 // TODO: Is this part really relevant?
1268 QItemSelectionModel *selected = ui->playlist->selectionModel();
1269 if (selected->hasSelection()) {
1270 ui->preview_btn->setEnabled(true);
1271 midi_mapper.set_preview_enabled(preview_playing ? MIDIMapper::On : MIDIMapper::Blinking);
1276 // TODO: Perhaps only enable this if something is actually selected.
1277 ui->preview_btn->setEnabled(!cliplist_clips->empty());
1278 if (preview_playing) {
1279 midi_mapper.set_preview_enabled(MIDIMapper::On);
1281 midi_mapper.set_preview_enabled(cliplist_clips->empty() ? MIDIMapper::Off : MIDIMapper::Blinking);
1285 void MainWindow::enable_or_disable_queue_button()
1287 // Follows the logic in queue_clicked().
1288 // TODO: Perhaps only enable this if something is actually selected.
1292 if (cliplist_clips->empty()) {
1298 ui->queue_btn->setEnabled(enabled);
1299 midi_mapper.set_queue_enabled(enabled);
1302 void MainWindow::set_output_status(const string &status)
1304 ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
1305 if (live_player != nullptr) {
1306 live_player->set_pause_status(status);
1309 lock_guard<mutex> lock(queue_status_mu);
1310 queue_status = status;
1313 pair<string, string> MainWindow::get_queue_status() const
1315 lock_guard<mutex> lock(queue_status_mu);
1316 return { queue_status, "text/plain" };
1319 void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame)
1321 if (stream_idx >= MAX_STREAMS) {
1322 fprintf(stderr, "WARNING: Ignoring too-high stream index %u.\n", stream_idx);
1325 if (stream_idx >= num_cameras) {
1326 post_to_main_thread_and_wait([this, stream_idx] {
1327 num_cameras = stream_idx + 1;
1328 change_num_cameras();
1331 displays[stream_idx].display->setFrame(stream_idx, frame);
1334 void MainWindow::preview()
1336 post_to_main_thread([this] {
1341 void MainWindow::queue()
1343 post_to_main_thread([this] {
1348 void MainWindow::play()
1350 post_to_main_thread([this] {
1355 void MainWindow::next()
1357 post_to_main_thread([this] {
1362 void MainWindow::toggle_lock()
1364 post_to_main_thread([this] {
1365 ui->speed_lock_btn->setChecked(!ui->speed_lock_btn->isChecked());
1366 speed_lock_clicked();
1370 void MainWindow::jog(int delta)
1372 post_to_main_thread([this, delta] {
1373 int64_t pts_delta = delta * (TIMEBASE / 60); // One click = frame at 60 fps.
1374 if (ui->playlist->hasFocus()) {
1375 QModelIndex selected = ui->playlist->selectionModel()->currentIndex();
1376 if (selected.column() != -1 && selected.row() != -1) {
1377 jog_internal(JOG_PLAYLIST, selected.row(), selected.column(), /*stream_idx=*/-1, pts_delta);
1379 } else if (ui->clip_list->hasFocus()) {
1380 QModelIndex selected = ui->clip_list->selectionModel()->currentIndex();
1381 if (cliplist_clips->is_camera_column(selected.column()) &&
1382 hidden_jog_column != -1) {
1383 // See the definition on hidden_jog_column.
1384 selected = selected.sibling(selected.row(), hidden_jog_column);
1385 ui->clip_list->selectionModel()->setCurrentIndex(selected, QItemSelectionModel::ClearAndSelect);
1386 hidden_jog_column = -1;
1388 if (selected.column() != -1 && selected.row() != -1) {
1389 jog_internal(JOG_CLIP_LIST, selected.row(), selected.column(), ui->preview_display->get_stream_idx(), pts_delta);
1395 void MainWindow::switch_camera(unsigned camera_idx)
1397 post_to_main_thread([this, camera_idx] {
1398 if (camera_idx < num_cameras) { // TODO: Also make this change a highlighted clip?
1399 preview_angle_clicked(camera_idx);
1404 void MainWindow::set_master_speed(float speed)
1406 speed = min(max(speed, 0.1f), 2.0f);
1408 post_to_main_thread([this, speed] {
1409 if (ui->speed_lock_btn->isChecked()) {
1410 midi_mapper.set_locked(MIDIMapper::Blinking);
1411 lock_blink_timeout->start(1000);
1415 int percent = lrintf(speed * 100.0f);
1416 ui->speed_slider->blockSignals(true);
1417 ui->speed_slider->setValue(percent);
1418 ui->speed_slider->blockSignals(false);
1419 ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
1421 live_player->set_master_speed(speed);
1422 midi_mapper.set_speed_light(speed);
1426 void MainWindow::cue_in()
1428 post_to_main_thread([this] { cue_in_clicked(); });
1431 void MainWindow::cue_out()
1433 post_to_main_thread([this] { cue_out_clicked(); });
1436 template<class Model>
1437 void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
1439 QItemSelectionModel *old_selection_model = view->selectionModel();
1440 view->setModel(new_model);
1442 delete old_selection_model;
1444 connect(new_model, &Model::any_content_changed, this, &MainWindow::content_changed);
1447 void MainWindow::start_tally()
1449 http_reply = http.get(QNetworkRequest(QString::fromStdString(global_flags.tally_url)));
1450 connect(http_reply, &QNetworkReply::finished, this, &MainWindow::tally_received);
1453 void MainWindow::tally_received()
1455 unsigned time_to_next_tally_ms;
1456 if (http_reply->error()) {
1457 fprintf(stderr, "HTTP get of '%s' failed: %s\n", global_flags.tally_url.c_str(),
1458 http_reply->errorString().toStdString().c_str());
1459 ui->live_frame->setStyleSheet("");
1460 time_to_next_tally_ms = 1000;
1462 string contents = http_reply->readAll().toStdString();
1463 ui->live_frame->setStyleSheet(QString::fromStdString("background: " + contents));
1464 time_to_next_tally_ms = 100;
1466 http_reply->deleteLater();
1467 http_reply = nullptr;
1469 QTimer::singleShot(time_to_next_tally_ms, this, &MainWindow::start_tally);