]> git.sesse.net Git - nageru/blob - futatabi/mainwindow.cpp
Work around a crash on exit.
[nageru] / futatabi / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "export.h"
5 #include "flags.h"
6 #include "frame_on_disk.h"
7 #include "player.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"
15
16 #include <QDesktopServices>
17 #include <QFileDialog>
18 #include <QMessageBox>
19 #include <QMouseEvent>
20 #include <QNetworkReply>
21 #include <QShortcut>
22 #include <QTimer>
23 #include <QWheelEvent>
24 #include <future>
25 #include <sqlite3.h>
26 #include <string>
27 #include <vector>
28
29 using namespace std;
30 using namespace std::placeholders;
31
32 MainWindow *global_mainwindow = nullptr;
33 static ClipList *cliplist_clips;
34 static PlayList *playlist_clips;
35
36 extern int64_t current_pts;
37
38 namespace {
39
40 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
41 {
42         pts = std::max<int64_t>(pts, 0);
43         if (clip->pts_out == -1) {
44                 pts = std::min(pts, current_pts);
45         } else {
46                 pts = std::min(pts, clip->pts_out);
47         }
48         clip->pts_in = pts;
49 }
50
51 }  // namespace
52
53 MainWindow::MainWindow()
54         : ui(new Ui::MainWindow),
55           db(global_flags.working_directory + "/futatabi.db"),
56           midi_mapper(this)
57 {
58         global_mainwindow = this;
59         ui->setupUi(this);
60
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;
66                 }
67         }
68         if (!global_flags.cue_point_padding_set) {
69                 global_flags.cue_point_padding_seconds = settings.cue_point_padding_seconds();  // Default 0 is fine.
70         }
71         if (global_flags.interpolation_quality == 0) {
72                 // Allocate something just for simplicity; we won't be using it
73                 // unless the user changes runtime, in which case 1 is fine.
74                 flow_initialized_interpolation_quality = 1;
75         } else {
76                 flow_initialized_interpolation_quality = global_flags.interpolation_quality;
77         }
78         save_settings();
79
80         // The menus.
81         connect(ui->midi_mapping_action, &QAction::triggered, this, &MainWindow::midi_mapping_triggered);
82         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
83         connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered);
84         connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered);
85         connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered);
86         connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
87         connect(ui->undo_action, &QAction::triggered, this, &MainWindow::undo_triggered);
88         connect(ui->redo_action, &QAction::triggered, this, &MainWindow::redo_triggered);
89         ui->undo_action->setEnabled(false);
90         ui->redo_action->setEnabled(false);
91
92         // The quality group.
93         QActionGroup *quality_group = new QActionGroup(ui->interpolation_menu);
94         quality_group->addAction(ui->quality_0_action);
95         quality_group->addAction(ui->quality_1_action);
96         quality_group->addAction(ui->quality_2_action);
97         quality_group->addAction(ui->quality_3_action);
98         quality_group->addAction(ui->quality_4_action);
99         if (global_flags.interpolation_quality == 0) {
100                 ui->quality_0_action->setChecked(true);
101         } else if (global_flags.interpolation_quality == 1) {
102                 ui->quality_1_action->setChecked(true);
103         } else if (global_flags.interpolation_quality == 2) {
104                 ui->quality_2_action->setChecked(true);
105         } else if (global_flags.interpolation_quality == 3) {
106                 ui->quality_3_action->setChecked(true);
107         } else if (global_flags.interpolation_quality == 4) {
108                 ui->quality_4_action->setChecked(true);
109         } else {
110                 assert(false);
111         }
112         connect(ui->quality_0_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 0, _1));
113         connect(ui->quality_1_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 1, _1));
114         connect(ui->quality_2_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 2, _1));
115         connect(ui->quality_3_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 3, _1));
116         connect(ui->quality_4_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 4, _1));
117
118         // The cue point padding group.
119         QActionGroup *padding_group = new QActionGroup(ui->interpolation_menu);
120         padding_group->addAction(ui->padding_0_action);
121         padding_group->addAction(ui->padding_1_action);
122         padding_group->addAction(ui->padding_2_action);
123         padding_group->addAction(ui->padding_5_action);
124         if (global_flags.cue_point_padding_seconds <= 1e-3) {
125                 ui->padding_0_action->setChecked(true);
126         } else if (fabs(global_flags.cue_point_padding_seconds - 1.0) < 1e-3) {
127                 ui->padding_1_action->setChecked(true);
128         } else if (fabs(global_flags.cue_point_padding_seconds - 2.0) < 1e-3) {
129                 ui->padding_2_action->setChecked(true);
130         } else if (fabs(global_flags.cue_point_padding_seconds - 5.0) < 1e-3) {
131                 ui->padding_5_action->setChecked(true);
132         } else {
133                 // Nothing to check, which is fine.
134         }
135         connect(ui->padding_0_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 0.0, _1));
136         connect(ui->padding_1_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 1.0, _1));
137         connect(ui->padding_2_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 2.0, _1));
138         connect(ui->padding_5_action, &QAction::toggled, bind(&MainWindow::padding_toggled, this, 5.0, _1));
139
140         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
141         disk_free_label = new QLabel(this);
142         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
143         ui->menuBar->setCornerWidget(disk_free_label);
144
145         StateProto state = db.get_state();
146         undo_stack.push_back(state);  // The undo stack always has the current state on top.
147
148         cliplist_clips = new ClipList(state.clip_list());
149         ui->clip_list->setModel(cliplist_clips);
150         connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
151
152         playlist_clips = new PlayList(state.play_list());
153         ui->playlist->setModel(playlist_clips);
154         connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
155
156         // For un-highlighting when we lose focus.
157         ui->clip_list->installEventFilter(this);
158
159         // For scrubbing in the pts columns.
160         ui->clip_list->viewport()->installEventFilter(this);
161         ui->playlist->viewport()->installEventFilter(this);
162
163         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
164         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
165         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
166
167         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
168         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
169         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
170
171         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
172         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
173         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
174
175         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
176         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
177         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
178
179         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
180         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
181         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
182
183         connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
184         ui->stop_btn->setEnabled(false);
185
186         connect(ui->speed_slider, &QAbstractSlider::valueChanged, this, &MainWindow::speed_slider_changed);
187         connect(ui->speed_lock_btn, &QPushButton::clicked, this, &MainWindow::speed_lock_clicked);
188
189         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
190
191         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
192         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
193         connect(delete_key, &QShortcut::activated, [this] {
194                 if (ui->playlist->hasFocus()) {
195                         playlist_remove();
196                 }
197         });
198
199         // TODO: support drag-and-drop.
200         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this] { playlist_move(-1); });
201         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this] { playlist_move(1); });
202
203         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
204                 this, &MainWindow::playlist_selection_changed);
205         playlist_selection_changed();  // First time set-up.
206
207         preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
208         preview_player->set_done_callback([this] {
209                 post_to_main_thread([this] {
210                         preview_player_done();
211                 });
212         });
213
214         live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
215         live_player->set_done_callback([this] {
216                 post_to_main_thread([this] {
217                         live_player_done();
218                 });
219         });
220         live_player->set_progress_callback([this](const map<uint64_t, double> &progress, double time_remaining) {
221                 post_to_main_thread([this, progress, time_remaining] {
222                         live_player_clip_progress(progress, time_remaining);
223                 });
224         });
225         set_output_status("paused");
226         enable_or_disable_queue_button();
227
228         defer_timeout = new QTimer(this);
229         defer_timeout->setSingleShot(true);
230         connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
231         ui->undo_action->setEnabled(true);
232
233         lock_blink_timeout = new QTimer(this);
234         lock_blink_timeout->setSingleShot(true);
235         connect(lock_blink_timeout, &QTimer::timeout, this, &MainWindow::lock_blink_timer_expired);
236
237         connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
238                 this, &MainWindow::clip_list_selection_changed);
239         enable_or_disable_queue_button();
240
241         // Find out how many cameras we have in the existing frames;
242         // if none, we start with two cameras.
243         num_cameras = 2;
244         {
245                 lock_guard<mutex> lock(frame_mu);
246                 for (size_t stream_idx = 2; stream_idx < MAX_STREAMS; ++stream_idx) {
247                         if (!frames[stream_idx].empty()) {
248                                 num_cameras = stream_idx + 1;
249                         }
250                 }
251         }
252         change_num_cameras();
253
254         if (!global_flags.tally_url.empty()) {
255                 start_tally();
256         }
257
258         if (!global_flags.midi_mapping_filename.empty()) {
259                 MIDIMappingProto midi_mapping;
260                 if (!load_midi_mapping_from_file(global_flags.midi_mapping_filename, &midi_mapping)) {
261                         fprintf(stderr, "Couldn't load MIDI mapping '%s'; exiting.\n",
262                                 global_flags.midi_mapping_filename.c_str());
263                         exit(1);
264                 }
265                 midi_mapper.set_midi_mapping(midi_mapping);
266         }
267         midi_mapper.refresh_lights();
268         midi_mapper.start_thread();
269 }
270
271 void MainWindow::change_num_cameras()
272 {
273         assert(num_cameras >= displays.size());  // We only add, never remove.
274
275         // Make new display rows.
276         unsigned display_rows = (num_cameras + 1) / 2;
277         ui->video_displays->setStretch(1, display_rows);
278         for (unsigned i = displays.size(); i < num_cameras; ++i) {
279                 QFrame *frame = new QFrame(this);
280                 frame->setAutoFillBackground(true);
281
282                 QLayout *layout = new QGridLayout(frame);
283                 frame->setLayout(layout);
284                 layout->setContentsMargins(3, 3, 3, 3);
285
286                 JPEGFrameView *display = new JPEGFrameView(frame);
287                 display->setAutoFillBackground(true);
288                 layout->addWidget(display);
289
290                 ui->input_displays->addWidget(frame, i / 2, i % 2);
291                 display->set_overlay(to_string(i + 1));
292
293                 QPushButton *preview_btn = new QPushButton(this);
294                 preview_btn->setMaximumSize(20, 17);
295                 preview_btn->setText(QString::fromStdString(to_string(i + 1)));
296                 ui->preview_layout->addWidget(preview_btn);
297
298                 displays.emplace_back(FrameAndDisplay{ frame, display, preview_btn });
299
300                 connect(display, &JPEGFrameView::clicked, preview_btn, &QPushButton::click);
301                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
302                 connect(shortcut, &QShortcut::activated, preview_btn, &QPushButton::click);
303
304                 connect(preview_btn, &QPushButton::clicked, [this, i] { preview_angle_clicked(i); });
305         }
306
307         cliplist_clips->change_num_cameras(num_cameras);
308         playlist_clips->change_num_cameras(num_cameras);
309
310         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
311 }
312
313 MainWindow::~MainWindow()
314 {
315         // We don't have a context to release Player's OpenGL resources in here,
316         // so instead of crashing on exit, leak it.
317         live_player.release();
318         preview_player.release();
319 }
320
321 void MainWindow::cue_in_clicked()
322 {
323         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
324                 cliplist_clips->mutable_back()->pts_in = current_pts;
325         } else {
326                 Clip clip;
327                 clip.pts_in = max<int64_t>(current_pts - lrint(global_flags.cue_point_padding_seconds * TIMEBASE), 0);
328                 cliplist_clips->add_clip(clip);
329                 playlist_selection_changed();
330         }
331
332         // Select the item so that we can jog it.
333         ui->clip_list->setFocus();
334         QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::IN));
335         ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
336         ui->clip_list->scrollToBottom();
337         enable_or_disable_queue_button();
338 }
339
340 void MainWindow::cue_out_clicked()
341 {
342         if (cliplist_clips->empty()) {
343                 return;
344         }
345
346         cliplist_clips->mutable_back()->pts_out = current_pts + lrint(global_flags.cue_point_padding_seconds * TIMEBASE);
347
348         // Select the item so that we can jog it.
349         ui->clip_list->setFocus();
350         QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::OUT));
351         ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
352         ui->clip_list->scrollToBottom();
353         enable_or_disable_queue_button();
354 }
355
356 void MainWindow::queue_clicked()
357 {
358         // See also enable_or_disable_queue_button().
359
360         if (cliplist_clips->empty()) {
361                 return;
362         }
363
364         QItemSelectionModel *selected = ui->clip_list->selectionModel();
365         if (!selected->hasSelection()) {
366                 Clip clip = *cliplist_clips->back();
367                 clip.stream_idx = 0;
368                 if (clip.pts_out != -1) {
369                         playlist_clips->add_clip(clip);
370                         playlist_selection_changed();
371                         ui->playlist->scrollToBottom();
372                 }
373                 return;
374         }
375
376         QModelIndex index = selected->currentIndex();
377         Clip clip = *cliplist_clips->clip(index.row());
378         if (cliplist_clips->is_camera_column(index.column())) {
379                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
380         } else {
381                 clip.stream_idx = ui->preview_display->get_stream_idx();
382         }
383
384         if (clip.pts_out != -1) {
385                 playlist_clips->add_clip(clip);
386                 playlist_selection_changed();
387                 ui->playlist->scrollToBottom();
388                 if (!ui->playlist->selectionModel()->hasSelection()) {
389                         // TODO: Figure out why this doesn't always seem to actually select the row.
390                         QModelIndex bottom = playlist_clips->index(playlist_clips->size() - 1, 0);
391                         ui->playlist->setCurrentIndex(bottom);
392                 }
393         }
394 }
395
396 void MainWindow::preview_clicked()
397 {
398         // See also enable_or_disable_preview_button().
399
400         if (ui->playlist->hasFocus()) {
401                 // Allow the playlist as preview iff it has focus and something is selected.
402                 QItemSelectionModel *selected = ui->playlist->selectionModel();
403                 if (selected->hasSelection()) {
404                         QModelIndex index = selected->currentIndex();
405                         const Clip &clip = *playlist_clips->clip(index.row());
406                         preview_player->play(clip);
407                         preview_playing = true;
408                         enable_or_disable_preview_button();
409                         return;
410                 }
411         }
412
413         if (cliplist_clips->empty())
414                 return;
415
416         QItemSelectionModel *selected = ui->clip_list->selectionModel();
417         if (!selected->hasSelection()) {
418                 preview_player->play(*cliplist_clips->back());
419                 preview_playing = true;
420                 enable_or_disable_preview_button();
421                 return;
422         }
423
424         QModelIndex index = selected->currentIndex();
425         Clip clip = *cliplist_clips->clip(index.row());
426         if (cliplist_clips->is_camera_column(index.column())) {
427                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
428         } else {
429                 clip.stream_idx = ui->preview_display->get_stream_idx();
430         }
431         preview_player->play(clip);
432         preview_playing = true;
433         enable_or_disable_preview_button();
434 }
435
436 void MainWindow::preview_angle_clicked(unsigned stream_idx)
437 {
438         preview_player->override_angle(stream_idx);
439
440         // Change the selection if we were previewing a clip from the clip list.
441         // (The only other thing we could be showing is a pts scrub, and if so,
442         // that would be selected.)
443         QItemSelectionModel *selected = ui->clip_list->selectionModel();
444         if (selected->hasSelection()) {
445                 QModelIndex cell = selected->selectedIndexes()[0];
446                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
447                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
448         }
449 }
450
451 void MainWindow::playlist_duplicate()
452 {
453         QItemSelectionModel *selected = ui->playlist->selectionModel();
454         if (!selected->hasSelection()) {
455                 // Should have been grayed out, but OK.
456                 return;
457         }
458         QModelIndexList rows = selected->selectedRows();
459         int first = rows.front().row(), last = rows.back().row();
460         playlist_clips->duplicate_clips(first, last);
461         playlist_selection_changed();
462 }
463
464 void MainWindow::playlist_remove()
465 {
466         QItemSelectionModel *selected = ui->playlist->selectionModel();
467         if (!selected->hasSelection()) {
468                 // Should have been grayed out, but OK.
469                 return;
470         }
471         QModelIndexList rows = selected->selectedRows();
472         int first = rows.front().row(), last = rows.back().row();
473         playlist_clips->erase_clips(first, last);
474
475         // TODO: select the next one in the list?
476
477         playlist_selection_changed();
478 }
479
480 void MainWindow::playlist_move(int delta)
481 {
482         QItemSelectionModel *selected = ui->playlist->selectionModel();
483         if (!selected->hasSelection()) {
484                 // Should have been grayed out, but OK.
485                 return;
486         }
487
488         QModelIndexList rows = selected->selectedRows();
489         int first = rows.front().row(), last = rows.back().row();
490         if ((delta == -1 && first == 0) ||
491             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
492                 // Should have been grayed out, but OK.
493                 return;
494         }
495
496         playlist_clips->move_clips(first, last, delta);
497         playlist_selection_changed();
498 }
499
500 void MainWindow::jog_internal(JogDestination jog_destination, int row, int column, int stream_idx, int pts_delta)
501 {
502         constexpr int camera_pts_per_pixel = 1500;  // One click of most mice (15 degrees), multiplied by the default wheel_sensitivity.
503
504         int in_column, out_column, camera_column;
505         if (jog_destination == JOG_CLIP_LIST) {
506                 in_column = int(ClipList::Column::IN);
507                 out_column = int(ClipList::Column::OUT);
508                 camera_column = -1;
509         } else if (jog_destination == JOG_PLAYLIST) {
510                 in_column = int(PlayList::Column::IN);
511                 out_column = int(PlayList::Column::OUT);
512                 camera_column = int(PlayList::Column::CAMERA);
513         } else {
514                 assert(false);
515         }
516
517         currently_deferring_model_changes = true;
518         {
519                 current_change_id = (jog_destination == JOG_CLIP_LIST) ? "cliplist:" : "playlist:";
520                 ClipProxy clip = (jog_destination == JOG_CLIP_LIST) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
521                 if (jog_destination == JOG_PLAYLIST) {
522                         stream_idx = clip->stream_idx;
523                 }
524
525                 if (column == in_column) {
526                         current_change_id += "in:" + to_string(row);
527                         int64_t pts = clip->pts_in + pts_delta;
528                         set_pts_in(pts, current_pts, clip);
529                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
530                 } else if (column == out_column) {
531                         current_change_id += "out:" + to_string(row);
532                         int64_t pts = clip->pts_out + pts_delta;
533                         pts = std::max(pts, clip->pts_in);
534                         pts = std::min(pts, current_pts);
535                         clip->pts_out = pts;
536                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
537                 } else if (column == camera_column) {
538                         current_change_id += "camera:" + to_string(row);
539                         int angle_degrees = pts_delta;
540                         if (last_mousewheel_camera_row == row) {
541                                 angle_degrees += leftover_angle_degrees;
542                         }
543
544                         int stream_idx = clip->stream_idx + angle_degrees / camera_pts_per_pixel;
545                         stream_idx = std::max(stream_idx, 0);
546                         stream_idx = std::min<int>(stream_idx, num_cameras - 1);
547                         clip->stream_idx = stream_idx;
548
549                         last_mousewheel_camera_row = row;
550                         leftover_angle_degrees = angle_degrees % camera_pts_per_pixel;
551
552                         // Don't update the live view, that's rarely what the operator wants.
553                 }
554         }
555         currently_deferring_model_changes = false;
556 }
557
558 void MainWindow::defer_timer_expired()
559 {
560         state_changed(deferred_state);
561 }
562
563 void MainWindow::content_changed()
564 {
565         // If we are playing, update the part of the playlist that's not playing yet.
566         vector<ClipWithID> clips;
567         for (unsigned row = 0; row < playlist_clips->size(); ++row) {
568                 clips.emplace_back(*playlist_clips->clip_with_id(row));
569         }
570         live_player->splice_play(clips);
571
572         // Serialize the state.
573         if (defer_timeout->isActive() &&
574             (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
575                 // There's some deferred event waiting, but this event is unrelated.
576                 // So it's time to short-circuit that timer and do the work it wanted to do.
577                 defer_timeout->stop();
578                 state_changed(deferred_state);
579         }
580         StateProto state;
581         *state.mutable_clip_list() = cliplist_clips->serialize();
582         *state.mutable_play_list() = playlist_clips->serialize();
583         if (currently_deferring_model_changes) {
584                 deferred_change_id = current_change_id;
585                 deferred_state = std::move(state);
586                 defer_timeout->start(200);
587                 return;
588         }
589         state_changed(state);
590 }
591
592 void MainWindow::state_changed(const StateProto &state)
593 {
594         db.store_state(state);
595
596         redo_stack.clear();
597         ui->redo_action->setEnabled(false);
598
599         undo_stack.push_back(state);
600         ui->undo_action->setEnabled(undo_stack.size() > 1);
601
602         // Make sure it doesn't grow without bounds.
603         while (undo_stack.size() >= 100) {
604                 undo_stack.pop_front();
605         }
606 }
607
608 void MainWindow::save_settings()
609 {
610         SettingsProto settings;
611         settings.set_interpolation_quality(global_flags.interpolation_quality + 1);
612         settings.set_cue_point_padding_seconds(global_flags.cue_point_padding_seconds);
613         db.store_settings(settings);
614 }
615
616 void MainWindow::lock_blink_timer_expired()
617 {
618         midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked()));  // Presumably On, or the timer should have been canceled.
619 }
620
621 void MainWindow::play_clicked()
622 {
623         QItemSelectionModel *selected = ui->playlist->selectionModel();
624         if (!selected->hasSelection()) {
625                 return;
626         }
627         unsigned start_row = selected->selectedRows(0)[0].row();
628
629         vector<ClipWithID> clips;
630         for (unsigned row = start_row; row < playlist_clips->size(); ++row) {
631                 clips.emplace_back(*playlist_clips->clip_with_id(row));
632         }
633         live_player->play(clips);
634         playlist_clips->set_progress({ { start_row, 0.0f } });
635         ui->playlist->selectionModel()->clear();
636         ui->stop_btn->setEnabled(true);
637         playlist_selection_changed();
638 }
639
640 void MainWindow::stop_clicked()
641 {
642         Clip fake_clip;
643         fake_clip.pts_in = 0;
644         fake_clip.pts_out = 0;
645         playlist_clips->set_progress({});
646         live_player->play(fake_clip);
647         ui->stop_btn->setEnabled(false);
648         playlist_selection_changed();
649 }
650
651 void MainWindow::speed_slider_changed(int percent)
652 {
653         float speed = percent / 100.0f;
654         ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
655         live_player->set_master_speed(speed);
656         midi_mapper.set_speed_light(speed);
657 }
658
659 void MainWindow::speed_lock_clicked()
660 {
661         // TODO: Make for a less abrupt transition if we're not already at 100%.
662         ui->speed_slider->setValue(100);  // Also actually sets the master speed and updates the label.
663         ui->speed_slider->setEnabled(!ui->speed_lock_btn->isChecked());
664         midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked()));
665         lock_blink_timeout->stop();
666 }
667
668 void MainWindow::preview_player_done()
669 {
670         preview_playing = false;
671         enable_or_disable_preview_button();
672 }
673
674 void MainWindow::live_player_done()
675 {
676         playlist_clips->set_progress({});
677         ui->stop_btn->setEnabled(false);
678         playlist_selection_changed();
679 }
680
681 void MainWindow::live_player_clip_progress(const map<uint64_t, double> &progress, double time_remaining)
682 {
683         playlist_clips->set_progress(progress);
684         set_output_status(format_duration(time_remaining) + " left");
685 }
686
687 void MainWindow::resizeEvent(QResizeEvent *event)
688 {
689         QMainWindow::resizeEvent(event);
690
691         // Ask for a relayout, but only after the event loop is done doing relayout
692         // on everything else.
693         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
694 }
695
696 void MainWindow::relayout()
697 {
698         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
699         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
700 }
701
702 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
703 {
704         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
705         int scrub_sensitivity = 100;  // pts units per pixel.
706         int wheel_sensitivity = 100;  // pts units per degree.
707
708         if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) {
709                 enable_or_disable_preview_button();
710                 hidden_jog_column = -1;
711         }
712
713         unsigned stream_idx = ui->preview_display->get_stream_idx();
714
715         if (watched == ui->clip_list) {
716                 if (event->type() == QEvent::FocusOut) {
717                         highlight_camera_input(-1);
718                 }
719                 return false;
720         }
721
722         if (event->type() != QEvent::Wheel) {
723                 last_mousewheel_camera_row = -1;
724         }
725
726         if (event->type() == QEvent::MouseButtonPress) {
727                 QMouseEvent *mouse = (QMouseEvent *)event;
728
729                 QTableView *destination;
730                 ScrubType type;
731
732                 if (watched == ui->clip_list->viewport()) {
733                         destination = ui->clip_list;
734                         type = SCRUBBING_CLIP_LIST;
735                 } else if (watched == ui->playlist->viewport()) {
736                         destination = ui->playlist;
737                         type = SCRUBBING_PLAYLIST;
738                 } else {
739                         return false;
740                 }
741                 int column = destination->columnAt(mouse->x());
742                 int row = destination->rowAt(mouse->y());
743                 if (column == -1 || row == -1)
744                         return false;
745
746                 if (type == SCRUBBING_CLIP_LIST) {
747                         if (ClipList::Column(column) == ClipList::Column::IN) {
748                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
749                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
750                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
751                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
752                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
753                         } else {
754                                 return false;
755                         }
756                 } else {
757                         if (PlayList::Column(column) == PlayList::Column::IN) {
758                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
759                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
760                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
761                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
762                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
763                         } else {
764                                 return false;
765                         }
766                 }
767
768                 scrubbing = true;
769                 scrub_row = row;
770                 scrub_column = column;
771                 scrub_x_origin = mouse->x();
772                 scrub_type = type;
773         } else if (event->type() == QEvent::MouseMove) {
774                 QMouseEvent *mouse = (QMouseEvent *)event;
775                 if (mouse->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
776                         scrub_sensitivity *= 10;
777                         wheel_sensitivity *= 10;
778                 }
779                 if (mouse->modifiers() & Qt::KeyboardModifier::AltModifier) {  // Note: Shift + Alt cancel each other out.
780                         scrub_sensitivity /= 10;
781                         wheel_sensitivity /= 10;
782                 }
783                 if (scrubbing) {
784                         int offset = mouse->x() - scrub_x_origin;
785                         int adjusted_offset;
786                         if (offset >= dead_zone_pixels) {
787                                 adjusted_offset = offset - dead_zone_pixels;
788                         } else if (offset < -dead_zone_pixels) {
789                                 adjusted_offset = offset + dead_zone_pixels;
790                         } else {
791                                 adjusted_offset = 0;
792                         }
793
794                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
795                         currently_deferring_model_changes = true;
796                         if (scrub_type == SCRUBBING_CLIP_LIST) {
797                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
798                                 if (scrub_column == int(ClipList::Column::IN)) {
799                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
800                                         set_pts_in(pts, current_pts, clip);
801                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
802                                 } else {
803                                         current_change_id = "cliplist:out" + to_string(scrub_row);
804                                         pts = std::max(pts, clip->pts_in);
805                                         pts = std::min(pts, current_pts);
806                                         clip->pts_out = pts;
807                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
808                                 }
809                         } else {
810                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
811                                 if (scrub_column == int(PlayList::Column::IN)) {
812                                         current_change_id = "playlist:in:" + to_string(scrub_row);
813                                         set_pts_in(pts, current_pts, clip);
814                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
815                                 } else {
816                                         current_change_id = "playlist:out:" + to_string(scrub_row);
817                                         pts = std::max(pts, clip->pts_in);
818                                         pts = std::min(pts, current_pts);
819                                         clip->pts_out = pts;
820                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
821                                 }
822                         }
823                         currently_deferring_model_changes = false;
824
825                         return true;  // Don't use this mouse movement for selecting things.
826                 }
827         } else if (event->type() == QEvent::Wheel) {
828                 QWheelEvent *wheel = (QWheelEvent *)event;
829                 int angle_delta = wheel->angleDelta().y();
830                 if (wheel->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
831                         scrub_sensitivity *= 10;
832                         wheel_sensitivity *= 10;
833                 }
834                 if (wheel->modifiers() & Qt::KeyboardModifier::AltModifier) {  // Note: Shift + Alt cancel each other out.
835                         scrub_sensitivity /= 10;
836                         wheel_sensitivity /= 10;
837                         angle_delta = wheel->angleDelta().x();  // Qt ickiness.
838                 }
839
840                 QTableView *destination;
841                 JogDestination jog_destination;
842                 if (watched == ui->clip_list->viewport()) {
843                         destination = ui->clip_list;
844                         jog_destination = JOG_CLIP_LIST;
845                         last_mousewheel_camera_row = -1;
846                 } else if (watched == ui->playlist->viewport()) {
847                         destination = ui->playlist;
848                         jog_destination = JOG_PLAYLIST;
849                         if (destination->columnAt(wheel->x()) != int(PlayList::Column::CAMERA)) {
850                                 last_mousewheel_camera_row = -1;
851                         }
852                 } else {
853                         last_mousewheel_camera_row = -1;
854                         return false;
855                 }
856
857                 int column = destination->columnAt(wheel->x());
858                 int row = destination->rowAt(wheel->y());
859                 if (column == -1 || row == -1)
860                         return false;
861
862                 // Only adjust pts with the wheel if the given row is selected.
863                 if (!destination->hasFocus() ||
864                     row != destination->selectionModel()->currentIndex().row()) {
865                         return false;
866                 }
867
868                 jog_internal(jog_destination, row, column, stream_idx, angle_delta * wheel_sensitivity);
869                 return true;  // Don't scroll.
870         } else if (event->type() == QEvent::MouseButtonRelease) {
871                 scrubbing = false;
872         }
873         return false;
874 }
875
876 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
877 {
878         if (rounding == LAST_BEFORE) {
879                 lock_guard<mutex> lock(frame_mu);
880                 if (frames[stream_idx].empty())
881                         return;
882                 auto it = find_last_frame_before(frames[stream_idx], pts);
883                 if (it != frames[stream_idx].end()) {
884                         pts = it->pts;
885                 }
886         } else {
887                 assert(rounding == FIRST_AT_OR_AFTER);
888                 lock_guard<mutex> lock(frame_mu);
889                 if (frames[stream_idx].empty())
890                         return;
891                 auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
892                 if (it != frames[stream_idx].end()) {
893                         pts = it->pts;
894                 }
895         }
896
897         Clip fake_clip;
898         fake_clip.pts_in = pts;
899         fake_clip.pts_out = pts + 1;
900         fake_clip.stream_idx = stream_idx;
901         preview_player->play(fake_clip);
902 }
903
904 void MainWindow::playlist_selection_changed()
905 {
906         enable_or_disable_preview_button();
907
908         QItemSelectionModel *selected = ui->playlist->selectionModel();
909         bool any_selected = selected->hasSelection();
910         ui->playlist_duplicate_btn->setEnabled(any_selected);
911         ui->playlist_remove_btn->setEnabled(any_selected);
912         ui->playlist_move_up_btn->setEnabled(
913                 any_selected && selected->selectedRows().front().row() > 0);
914         ui->playlist_move_down_btn->setEnabled(
915                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
916
917         ui->play_btn->setEnabled(any_selected);
918         if (ui->stop_btn->isEnabled()) {  // Playing.
919                 midi_mapper.set_play_enabled(MIDIMapper::On);
920         } else if (any_selected) {
921                 midi_mapper.set_play_enabled(MIDIMapper::Blinking);
922         } else {
923                 midi_mapper.set_play_enabled(MIDIMapper::Off);
924         }
925
926         if (!any_selected) {
927                 set_output_status("paused");
928         } else {
929                 vector<ClipWithID> clips;
930                 for (size_t row = selected->selectedRows().front().row(); row < playlist_clips->size(); ++row) {
931                         clips.emplace_back(*playlist_clips->clip_with_id(row));
932                 }
933                 double remaining = compute_total_time(clips);
934                 set_output_status(format_duration(remaining) + " ready");
935         }
936 }
937
938 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &previous)
939 {
940         int camera_selected = -1;
941         if (cliplist_clips->is_camera_column(current.column())) {
942                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
943
944                 // See the comment on hidden_jog_column.
945                 if (current.row() != previous.row()) {
946                         hidden_jog_column = -1;
947                 } else if (hidden_jog_column == -1) {
948                         hidden_jog_column = previous.column();
949                 }
950         } else {
951                 hidden_jog_column = -1;
952         }
953         highlight_camera_input(camera_selected);
954         enable_or_disable_queue_button();
955 }
956
957 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
958 {
959         char time_str[256];
960         if (estimated_seconds_left < 60.0) {
961                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
962         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
963                 int s = lrintf(estimated_seconds_left);
964                 int m = s / 60;
965                 s %= 60;
966                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
967         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
968                 int m = lrintf(estimated_seconds_left / 60.0);
969                 snprintf(time_str, sizeof(time_str), "%dm", m);
970         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
971                 int m = lrintf(estimated_seconds_left / 60.0);
972                 int h = m / 60;
973                 m %= 60;
974                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
975         } else {  // More than ten hours: Xh.
976                 int h = lrintf(estimated_seconds_left / 3600.0);
977                 snprintf(time_str, sizeof(time_str), "%dh", h);
978         }
979         char buf[256];
980         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
981
982         std::string label = buf;
983
984         post_to_main_thread([this, label] {
985                 disk_free_label->setText(QString::fromStdString(label));
986                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
987         });
988 }
989
990 void MainWindow::midi_mapping_triggered()
991 {
992         MIDIMappingDialog(&midi_mapper).exec();
993 }
994
995 void MainWindow::exit_triggered()
996 {
997         close();
998 }
999
1000 void MainWindow::export_cliplist_clip_multitrack_triggered()
1001 {
1002         QItemSelectionModel *selected = ui->clip_list->selectionModel();
1003         if (!selected->hasSelection()) {
1004                 QMessageBox msgbox;
1005                 msgbox.setText("No clip selected in the clip list. Select one and try exporting again.");
1006                 msgbox.exec();
1007                 return;
1008         }
1009
1010         QModelIndex index = selected->currentIndex();
1011         Clip clip = *cliplist_clips->clip(index.row());
1012         QString filename = QFileDialog::getSaveFileName(this,
1013                 "Export multitrack clip", QString(), tr("Matroska video files (*.mkv)"));
1014         if (filename.isNull()) {
1015                 // Cancel.
1016                 return;
1017         }
1018         if (!filename.endsWith(".mkv")) {
1019                 filename += ".mkv";
1020         }
1021         export_multitrack_clip(filename.toStdString(), clip);
1022 }
1023
1024 void MainWindow::export_playlist_clip_interpolated_triggered()
1025 {
1026         QItemSelectionModel *selected = ui->playlist->selectionModel();
1027         if (!selected->hasSelection()) {
1028                 QMessageBox msgbox;
1029                 msgbox.setText("No clip selected in the playlist. Select one and try exporting again.");
1030                 msgbox.exec();
1031                 return;
1032         }
1033
1034         QString filename = QFileDialog::getSaveFileName(this,
1035                 "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)"));
1036         if (filename.isNull()) {
1037                 // Cancel.
1038                 return;
1039         }
1040         if (!filename.endsWith(".mkv")) {
1041                 filename += ".mkv";
1042         }
1043
1044         vector<Clip> clips;
1045         QModelIndexList rows = selected->selectedRows();
1046         for (QModelIndex index : rows) {
1047                 clips.push_back(*playlist_clips->clip(index.row()));
1048         }
1049         export_interpolated_clip(filename.toStdString(), clips);
1050 }
1051
1052 void MainWindow::manual_triggered()
1053 {
1054         if (!QDesktopServices::openUrl(QUrl("https://nageru.sesse.net/doc/"))) {
1055                 QMessageBox msgbox;
1056                 msgbox.setText("Could not launch manual in web browser.\nPlease see https://nageru.sesse.net/doc/ manually.");
1057                 msgbox.exec();
1058         }
1059 }
1060
1061 void MainWindow::about_triggered()
1062 {
1063         AboutDialog("Futatabi", "Multicamera slow motion video server").exec();
1064 }
1065
1066 void MainWindow::undo_triggered()
1067 {
1068         // Finish any deferred action.
1069         if (defer_timeout->isActive()) {
1070                 defer_timeout->stop();
1071                 state_changed(deferred_state);
1072         }
1073
1074         StateProto redo_state;
1075         *redo_state.mutable_clip_list() = cliplist_clips->serialize();
1076         *redo_state.mutable_play_list() = playlist_clips->serialize();
1077         redo_stack.push_back(std::move(redo_state));
1078         ui->redo_action->setEnabled(true);
1079
1080         assert(undo_stack.size() > 1);
1081
1082         // Pop off the current state, which is always at the top of the stack.
1083         undo_stack.pop_back();
1084
1085         StateProto state = undo_stack.back();
1086         ui->undo_action->setEnabled(undo_stack.size() > 1);
1087
1088         replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
1089         replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
1090
1091         db.store_state(state);
1092 }
1093
1094 void MainWindow::redo_triggered()
1095 {
1096         assert(!redo_stack.empty());
1097
1098         ui->undo_action->setEnabled(true);
1099         ui->redo_action->setEnabled(true);
1100
1101         undo_stack.push_back(std::move(redo_stack.back()));
1102         redo_stack.pop_back();
1103         ui->undo_action->setEnabled(true);
1104         ui->redo_action->setEnabled(!redo_stack.empty());
1105
1106         const StateProto &state = undo_stack.back();
1107         replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
1108         replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
1109
1110         db.store_state(state);
1111 }
1112
1113 void MainWindow::quality_toggled(int quality, bool checked)
1114 {
1115         if (!checked) {
1116                 return;
1117         }
1118         global_flags.interpolation_quality = quality;
1119         if (quality != 0 &&  // Turning interpolation off is always possible.
1120             quality != flow_initialized_interpolation_quality) {
1121                 QMessageBox msgbox;
1122                 msgbox.setText(QString::fromStdString(
1123                         "The interpolation quality for the main output cannot be changed at runtime, "
1124                         "except being turned completely off; it will take effect for exported files "
1125                         "only until next restart. The live output quality thus remains at " +
1126                         to_string(flow_initialized_interpolation_quality) + "."));
1127                 msgbox.exec();
1128         }
1129
1130         save_settings();
1131 }
1132
1133 void MainWindow::padding_toggled(double seconds, bool checked)
1134 {
1135         if (!checked) {
1136                 return;
1137         }
1138         global_flags.cue_point_padding_seconds = seconds;
1139         save_settings();
1140 }
1141
1142 void MainWindow::highlight_camera_input(int stream_idx)
1143 {
1144         for (unsigned i = 0; i < num_cameras; ++i) {
1145                 if (unsigned(stream_idx) == i) {
1146                         displays[i].frame->setStyleSheet("background: rgb(0,255,0)");
1147                 } else {
1148                         displays[i].frame->setStyleSheet("");
1149                 }
1150         }
1151         midi_mapper.highlight_camera_input(stream_idx);
1152 }
1153
1154 void MainWindow::enable_or_disable_preview_button()
1155 {
1156         // Follows the logic in preview_clicked().
1157
1158         if (ui->playlist->hasFocus()) {
1159                 // Allow the playlist as preview iff it has focus and something is selected.
1160                 // TODO: Is this part really relevant?
1161                 QItemSelectionModel *selected = ui->playlist->selectionModel();
1162                 if (selected->hasSelection()) {
1163                         ui->preview_btn->setEnabled(true);
1164                         midi_mapper.set_preview_enabled(preview_playing ? MIDIMapper::On : MIDIMapper::Blinking);
1165                         return;
1166                 }
1167         }
1168
1169         // TODO: Perhaps only enable this if something is actually selected.
1170         ui->preview_btn->setEnabled(!cliplist_clips->empty());
1171         if (preview_playing) {
1172                 midi_mapper.set_preview_enabled(MIDIMapper::On);
1173         } else {
1174                 midi_mapper.set_preview_enabled(cliplist_clips->empty() ? MIDIMapper::Off : MIDIMapper::Blinking);
1175         }
1176 }
1177
1178 void MainWindow::enable_or_disable_queue_button()
1179 {
1180         // Follows the logic in queue_clicked().
1181         // TODO: Perhaps only enable this if something is actually selected.
1182
1183         bool enabled;
1184
1185         if (cliplist_clips->empty()) {
1186                 enabled = false;
1187         } else {
1188                 QItemSelectionModel *selected = ui->clip_list->selectionModel();
1189                 if (!selected->hasSelection()) {
1190                         Clip clip = *cliplist_clips->back();
1191                         enabled = clip.pts_out != -1;
1192                 } else {
1193                         QModelIndex index = selected->currentIndex();
1194                         Clip clip = *cliplist_clips->clip(index.row());
1195                         enabled = clip.pts_out != -1;
1196                 }
1197         }
1198
1199         ui->queue_btn->setEnabled(enabled);
1200         midi_mapper.set_queue_enabled(enabled);
1201 }
1202
1203 void MainWindow::set_output_status(const string &status)
1204 {
1205         ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
1206         if (live_player != nullptr) {
1207                 live_player->set_pause_status(status);
1208         }
1209
1210         lock_guard<mutex> lock(queue_status_mu);
1211         queue_status = status;
1212 }
1213
1214 pair<string, string> MainWindow::get_queue_status() const
1215 {
1216         lock_guard<mutex> lock(queue_status_mu);
1217         return { queue_status, "text/plain" };
1218 }
1219
1220 void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame)
1221 {
1222         if (stream_idx >= MAX_STREAMS) {
1223                 fprintf(stderr, "WARNING: Ignoring too-high stream index %u.\n", stream_idx);
1224                 return;
1225         }
1226         if (stream_idx >= num_cameras) {
1227                 post_to_main_thread_and_wait([this, stream_idx] {
1228                         num_cameras = stream_idx + 1;
1229                         change_num_cameras();
1230                 });
1231         }
1232         displays[stream_idx].display->setFrame(stream_idx, frame);
1233 }
1234
1235 void MainWindow::preview()
1236 {
1237         post_to_main_thread([this] {
1238                 preview_clicked();
1239         });
1240 }
1241
1242 void MainWindow::queue()
1243 {
1244         post_to_main_thread([this] {
1245                 queue_clicked();
1246         });
1247 }
1248
1249 void MainWindow::play()
1250 {
1251         post_to_main_thread([this] {
1252                 play_clicked();
1253         });
1254 }
1255
1256 void MainWindow::toggle_lock()
1257 {
1258         post_to_main_thread([this] {
1259                 ui->speed_lock_btn->setChecked(!ui->speed_lock_btn->isChecked());
1260                 speed_lock_clicked();
1261         });
1262 }
1263
1264 void MainWindow::jog(int delta)
1265 {
1266         post_to_main_thread([this, delta] {
1267                 int64_t pts_delta = delta * (TIMEBASE / 60);  // One click = frame at 60 fps.
1268                 if (ui->playlist->hasFocus()) {
1269                         QModelIndex selected = ui->playlist->selectionModel()->currentIndex();
1270                         if (selected.column() != -1 && selected.row() != -1) {
1271                                 jog_internal(JOG_PLAYLIST, selected.row(), selected.column(), /*stream_idx=*/-1, pts_delta);
1272                         }
1273                 } else if (ui->clip_list->hasFocus()) {
1274                         QModelIndex selected = ui->clip_list->selectionModel()->currentIndex();
1275                         if (cliplist_clips->is_camera_column(selected.column()) &&
1276                             hidden_jog_column != -1) {
1277                                 // See the definition on hidden_jog_column.
1278                                 selected = selected.sibling(selected.row(), hidden_jog_column);
1279                                 ui->clip_list->selectionModel()->setCurrentIndex(selected, QItemSelectionModel::ClearAndSelect);
1280                                 hidden_jog_column = -1;
1281                         }
1282                         if (selected.column() != -1 && selected.row() != -1) {
1283                                 jog_internal(JOG_CLIP_LIST, selected.row(), selected.column(), ui->preview_display->get_stream_idx(), pts_delta);
1284                         }
1285                 }
1286         });
1287 }
1288
1289 void MainWindow::switch_camera(unsigned camera_idx)
1290 {
1291         post_to_main_thread([this, camera_idx] {
1292                 if (camera_idx < num_cameras) {  // TODO: Also make this change a highlighted clip?
1293                         preview_angle_clicked(camera_idx);
1294                 }
1295         });
1296 }
1297
1298 void MainWindow::set_master_speed(float speed)
1299 {
1300         speed = min(max(speed, 0.1f), 2.0f);
1301
1302         post_to_main_thread([this, speed] {
1303                 if (ui->speed_lock_btn->isChecked()) {
1304                         midi_mapper.set_locked(MIDIMapper::Blinking);
1305                         lock_blink_timeout->start(1000);
1306                         return;
1307                 }
1308
1309                 int percent = lrintf(speed * 100.0f);
1310                 ui->speed_slider->blockSignals(true);
1311                 ui->speed_slider->setValue(percent);
1312                 ui->speed_slider->blockSignals(false);
1313                 ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%"));
1314
1315                 live_player->set_master_speed(speed);
1316                 midi_mapper.set_speed_light(speed);
1317         });
1318 }
1319
1320 void MainWindow::cue_in()
1321 {
1322         post_to_main_thread([this] { cue_in_clicked(); });
1323 }
1324
1325 void MainWindow::cue_out()
1326 {
1327         post_to_main_thread([this] { cue_out_clicked(); });
1328 }
1329
1330 template<class Model>
1331 void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
1332 {
1333         QItemSelectionModel *old_selection_model = view->selectionModel();
1334         view->setModel(new_model);
1335         delete *model;
1336         delete old_selection_model;
1337         *model = new_model;
1338         connect(new_model, &Model::any_content_changed, this, &MainWindow::content_changed);
1339 }
1340
1341 void MainWindow::start_tally()
1342 {
1343         http_reply = http.get(QNetworkRequest(QString::fromStdString(global_flags.tally_url)));
1344         connect(http_reply, &QNetworkReply::finished, this, &MainWindow::tally_received);
1345 }
1346
1347 void MainWindow::tally_received()
1348 {
1349         unsigned time_to_next_tally_ms;
1350         if (http_reply->error()) {
1351                 fprintf(stderr, "HTTP get of '%s' failed: %s\n", global_flags.tally_url.c_str(),
1352                         http_reply->errorString().toStdString().c_str());
1353                 ui->live_frame->setStyleSheet("");
1354                 time_to_next_tally_ms = 1000;
1355         } else {
1356                 string contents = http_reply->readAll().toStdString();
1357                 ui->live_frame->setStyleSheet(QString::fromStdString("background: " + contents));
1358                 time_to_next_tally_ms = 100;
1359         }
1360         http_reply->deleteLater();
1361         http_reply = nullptr;
1362
1363         QTimer::singleShot(time_to_next_tally_ms, this, &MainWindow::start_tally);
1364 }