]> git.sesse.net Git - nageru/blob - futatabi/mainwindow.cpp
Make the number of cameras dynamic as the frames come in.
[nageru] / futatabi / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "shared/aboutdialog.h"
4 #include "clip_list.h"
5 #include "export.h"
6 #include "shared/disk_space_estimator.h"
7 #include "flags.h"
8 #include "frame_on_disk.h"
9 #include "player.h"
10 #include "shared/post_to_main_thread.h"
11 #include "shared/timebase.h"
12 #include "ui_mainwindow.h"
13
14 #include <QDesktopServices>
15 #include <QFileDialog>
16 #include <QMessageBox>
17 #include <QMouseEvent>
18 #include <QShortcut>
19 #include <QTimer>
20 #include <QWheelEvent>
21 #include <future>
22 #include <sqlite3.h>
23 #include <string>
24 #include <vector>
25
26 using namespace std;
27 using namespace std::placeholders;
28
29 MainWindow *global_mainwindow = nullptr;
30 static ClipList *cliplist_clips;
31 static PlayList *playlist_clips;
32
33 extern int64_t current_pts;
34
35 MainWindow::MainWindow()
36         : ui(new Ui::MainWindow),
37           db(global_flags.working_directory + "/futatabi.db")
38 {
39         global_mainwindow = this;
40         ui->setupUi(this);
41
42         // Load settings from database if needed.
43         if (!global_flags.interpolation_quality_set) {
44                 SettingsProto settings = db.get_settings();
45                 if (settings.interpolation_quality() != 0) {
46                         global_flags.interpolation_quality = settings.interpolation_quality() - 1;
47                 }
48         }
49         if (global_flags.interpolation_quality == 0) {
50                 // Allocate something just for simplicity; we won't be using it
51                 // unless the user changes runtime, in which case 1 is fine.
52                 flow_initialized_interpolation_quality = 1;
53         } else {
54                 flow_initialized_interpolation_quality = global_flags.interpolation_quality;
55         }
56         save_settings();
57
58         // The menus.
59         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
60         connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered);
61         connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered);
62         connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered);
63         connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered);
64         connect(ui->undo_action, &QAction::triggered, this, &MainWindow::undo_triggered);
65         connect(ui->redo_action, &QAction::triggered, this, &MainWindow::redo_triggered);
66         ui->undo_action->setEnabled(false);
67         ui->redo_action->setEnabled(false);
68
69         // The quality group.
70         QActionGroup *quality_group = new QActionGroup(ui->interpolation_menu);
71         quality_group->addAction(ui->quality_0_action);
72         quality_group->addAction(ui->quality_1_action);
73         quality_group->addAction(ui->quality_2_action);
74         quality_group->addAction(ui->quality_3_action);
75         quality_group->addAction(ui->quality_4_action);
76         if (global_flags.interpolation_quality == 0) {
77                 ui->quality_0_action->setChecked(true);
78         } else if (global_flags.interpolation_quality == 1) {
79                 ui->quality_1_action->setChecked(true);
80         } else if (global_flags.interpolation_quality == 2) {
81                 ui->quality_2_action->setChecked(true);
82         } else if (global_flags.interpolation_quality == 3) {
83                 ui->quality_3_action->setChecked(true);
84         } else if (global_flags.interpolation_quality == 4) {
85                 ui->quality_4_action->setChecked(true);
86         } else {
87                 assert(false);
88         }
89         connect(ui->quality_0_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 0, _1));
90         connect(ui->quality_1_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 1, _1));
91         connect(ui->quality_2_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 2, _1));
92         connect(ui->quality_3_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 3, _1));
93         connect(ui->quality_4_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 4, _1));
94
95         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
96         disk_free_label = new QLabel(this);
97         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
98         ui->menuBar->setCornerWidget(disk_free_label);
99
100         StateProto state = db.get_state();
101         undo_stack.push_back(state);  // The undo stack always has the current state on top.
102
103         cliplist_clips = new ClipList(state.clip_list());
104         ui->clip_list->setModel(cliplist_clips);
105         connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
106
107         playlist_clips = new PlayList(state.play_list());
108         ui->playlist->setModel(playlist_clips);
109         connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
110
111         // For un-highlighting when we lose focus.
112         ui->clip_list->installEventFilter(this);
113
114         // For scrubbing in the pts columns.
115         ui->clip_list->viewport()->installEventFilter(this);
116         ui->playlist->viewport()->installEventFilter(this);
117
118         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
119         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
120         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
121
122         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
123         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
124         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
125
126         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
127         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
128         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
129
130         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
131         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
132         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
133
134         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
135         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
136         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
137
138         connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
139         ui->stop_btn->setEnabled(false);
140
141         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
142
143         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
144         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
145         connect(delete_key, &QShortcut::activated, [this] {
146                 if (ui->playlist->hasFocus()) {
147                         playlist_remove();
148                 }
149         });
150
151         // TODO: support drag-and-drop.
152         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
153         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
154
155         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
156                 this, &MainWindow::playlist_selection_changed);
157         playlist_selection_changed();  // First time set-up.
158
159         preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT));
160         live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT));
161         live_player->set_done_callback([this]{
162                 post_to_main_thread([this]{
163                         live_player_clip_done();
164                 });
165         });
166         live_player->set_next_clip_callback(bind(&MainWindow::live_player_get_next_clip, this));
167         live_player->set_progress_callback([this](const map<size_t, double> &progress) {
168                 post_to_main_thread([this, progress] {
169                         live_player_clip_progress(progress);
170                 });
171         });
172         set_output_status("paused");
173
174         defer_timeout = new QTimer(this);
175         defer_timeout->setSingleShot(true);
176         connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
177         ui->undo_action->setEnabled(true);
178
179         connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
180                 this, &MainWindow::clip_list_selection_changed);
181
182         // Find out how many cameras we have in the existing frames;
183         // if none, we start with a single camera.
184         num_cameras = 1;
185         {
186                 lock_guard<mutex> lock(frame_mu);
187                 for (size_t stream_idx = 1; stream_idx < MAX_STREAMS; ++stream_idx) {
188                         if (!frames[stream_idx].empty()) {
189                                 num_cameras = stream_idx + 1;
190                         }
191                 }
192         }
193         change_num_cameras();
194 }
195
196 void MainWindow::change_num_cameras()
197 {
198         assert(num_cameras >= displays.size());  // We only add, never remove.
199
200         // Make new display rows.
201         unsigned display_rows = (num_cameras + 1) / 2;
202         ui->video_displays->setStretch(1, display_rows);
203         for (unsigned i = displays.size(); i < num_cameras; ++i) {
204                 QFrame *frame = new QFrame(this);
205                 frame->setAutoFillBackground(true);
206
207                 QLayout *layout = new QGridLayout(frame);
208                 frame->setLayout(layout);
209                 layout->setContentsMargins(3, 3, 3, 3);
210
211                 JPEGFrameView *display = new JPEGFrameView(frame);
212                 display->setAutoFillBackground(true);
213                 layout->addWidget(display);
214
215                 ui->input_displays->addWidget(frame, i / 2, i % 2);
216                 display->set_overlay(to_string(i + 1));
217
218                 QPushButton *preview_btn = new QPushButton(this);
219                 preview_btn->setMaximumSize(20, 17);
220                 preview_btn->setText(QString::fromStdString(to_string(i + 1)));
221                 ui->preview_layout->addWidget(preview_btn);
222
223                 displays.emplace_back(FrameAndDisplay{ frame, display, preview_btn });
224
225                 connect(display, &JPEGFrameView::clicked, preview_btn, &QPushButton::click);
226                 QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
227                 connect(shortcut, &QShortcut::activated, preview_btn, &QPushButton::click);
228
229                 connect(preview_btn, &QPushButton::clicked, [this, i]{ preview_angle_clicked(i); });
230         }
231
232         cliplist_clips->change_num_cameras(num_cameras);
233         playlist_clips->change_num_cameras(num_cameras);
234
235         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
236 }
237
238 MainWindow::~MainWindow()
239 {
240         // Empty so that we can forward-declare Player in the .h file.
241 }
242
243 void MainWindow::cue_in_clicked()
244 {
245         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
246                 cliplist_clips->mutable_back()->pts_in = current_pts;
247                 return;
248         }
249         Clip clip;
250         clip.pts_in = current_pts;
251         cliplist_clips->add_clip(clip);
252         playlist_selection_changed();
253         ui->clip_list->scrollToBottom();
254 }
255
256 void MainWindow::cue_out_clicked()
257 {
258         if (!cliplist_clips->empty()) {
259                 cliplist_clips->mutable_back()->pts_out = current_pts;
260                 // TODO: select the row in the clip list?
261         }
262 }
263
264 void MainWindow::queue_clicked()
265 {
266         if (cliplist_clips->empty()) {
267                 return;
268         }
269
270         QItemSelectionModel *selected = ui->clip_list->selectionModel();
271         if (!selected->hasSelection()) {
272                 Clip clip = *cliplist_clips->back();
273                 clip.stream_idx = 0;
274                 if (clip.pts_out != -1) {
275                         playlist_clips->add_clip(clip);
276                         playlist_selection_changed();
277                         ui->playlist->scrollToBottom();
278                 }
279                 return;
280         }
281
282         QModelIndex index = selected->currentIndex();
283         Clip clip = *cliplist_clips->clip(index.row());
284         if (cliplist_clips->is_camera_column(index.column())) {
285                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
286         } else {
287                 clip.stream_idx = ui->preview_display->get_stream_idx();
288         }
289
290         if (clip.pts_out != -1) {
291                 playlist_clips->add_clip(clip);
292                 playlist_selection_changed();
293                 ui->playlist->scrollToBottom();
294                 if (!ui->playlist->selectionModel()->hasSelection()) {
295                         // TODO: Figure out why this doesn't always seem to actually select the row.
296                         QModelIndex bottom = playlist_clips->index(playlist_clips->size() - 1, 0);
297                         ui->playlist->setCurrentIndex(bottom);
298                 }
299         }
300 }
301
302 void MainWindow::preview_clicked()
303 {
304         if (ui->playlist->hasFocus()) {
305                 // Allow the playlist as preview iff it has focus and something is selected.
306                 QItemSelectionModel *selected = ui->playlist->selectionModel();
307                 if (selected->hasSelection()) {
308                         QModelIndex index = selected->currentIndex();
309                         const Clip &clip = *playlist_clips->clip(index.row());
310                         preview_player->play_clip(clip, index.row(), clip.stream_idx);
311                         return;
312                 }
313         }
314
315         if (cliplist_clips->empty())
316                 return;
317
318         QItemSelectionModel *selected = ui->clip_list->selectionModel();
319         if (!selected->hasSelection()) {
320                 preview_player->play_clip(*cliplist_clips->back(), cliplist_clips->size() - 1, 0);
321                 return;
322         }
323
324         QModelIndex index = selected->currentIndex();
325         unsigned stream_idx;
326         if (cliplist_clips->is_camera_column(index.column())) {
327                 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
328         } else {
329                 stream_idx = ui->preview_display->get_stream_idx();
330         }
331         preview_player->play_clip(*cliplist_clips->clip(index.row()), index.row(), stream_idx);
332 }
333
334 void MainWindow::preview_angle_clicked(unsigned stream_idx)
335 {
336         preview_player->override_angle(stream_idx);
337
338         // Change the selection if we were previewing a clip from the clip list.
339         // (The only other thing we could be showing is a pts scrub, and if so,
340         // that would be selected.)
341         QItemSelectionModel *selected = ui->clip_list->selectionModel();
342         if (selected->hasSelection()) {
343                 QModelIndex cell = selected->selectedIndexes()[0];
344                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
345                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
346         }
347 }
348
349 void MainWindow::playlist_duplicate()
350 {
351         QItemSelectionModel *selected = ui->playlist->selectionModel();
352         if (!selected->hasSelection()) {
353                 // Should have been grayed out, but OK.
354                 return;
355         }
356         QModelIndexList rows = selected->selectedRows();
357         int first = rows.front().row(), last = rows.back().row();
358         playlist_clips->duplicate_clips(first, last);
359         playlist_selection_changed();
360 }
361
362 void MainWindow::playlist_remove()
363 {
364         QItemSelectionModel *selected = ui->playlist->selectionModel();
365         if (!selected->hasSelection()) {
366                 // Should have been grayed out, but OK.
367                 return;
368         }
369         QModelIndexList rows = selected->selectedRows();
370         int first = rows.front().row(), last = rows.back().row();
371         playlist_clips->erase_clips(first, last);
372
373         // TODO: select the next one in the list?
374
375         playlist_selection_changed();
376 }
377
378 void MainWindow::playlist_move(int delta)
379 {
380         QItemSelectionModel *selected = ui->playlist->selectionModel();
381         if (!selected->hasSelection()) {
382                 // Should have been grayed out, but OK.
383                 return;
384         }
385
386         QModelIndexList rows = selected->selectedRows();
387         int first = rows.front().row(), last = rows.back().row();
388         if ((delta == -1 && first == 0) ||
389             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
390                 // Should have been grayed out, but OK.
391                 return;
392         }
393
394         playlist_clips->move_clips(first, last, delta);
395         playlist_selection_changed();
396 }
397
398 void MainWindow::defer_timer_expired()
399 {
400         state_changed(deferred_state);
401 }
402
403 void MainWindow::content_changed()
404 {
405         if (defer_timeout->isActive() &&
406             (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
407                 // There's some deferred event waiting, but this event is unrelated.
408                 // So it's time to short-circuit that timer and do the work it wanted to do.
409                 defer_timeout->stop();
410                 state_changed(deferred_state);
411         }
412         StateProto state;
413         *state.mutable_clip_list() = cliplist_clips->serialize();
414         *state.mutable_play_list() = playlist_clips->serialize();
415         if (currently_deferring_model_changes) {
416                 deferred_change_id = current_change_id;
417                 deferred_state = std::move(state);
418                 defer_timeout->start(200);
419                 return;
420         }
421         state_changed(state);
422 }
423
424 void MainWindow::state_changed(const StateProto &state)
425 {
426         db.store_state(state);
427
428         redo_stack.clear();
429         ui->redo_action->setEnabled(false);
430
431         undo_stack.push_back(state);
432         ui->undo_action->setEnabled(undo_stack.size() > 1);
433
434         // Make sure it doesn't grow without bounds.
435         while (undo_stack.size() >= 100) {
436                 undo_stack.pop_front();
437         }
438 }
439
440 void MainWindow::save_settings()
441 {
442         SettingsProto settings;
443         settings.set_interpolation_quality(global_flags.interpolation_quality + 1);
444         db.store_settings(settings);
445 }
446
447 void MainWindow::play_clicked()
448 {
449         if (playlist_clips->empty())
450                 return;
451
452         QItemSelectionModel *selected = ui->playlist->selectionModel();
453         int row;
454         if (!selected->hasSelection()) {
455                 row = 0;
456         } else {
457                 row = selected->selectedRows(0)[0].row();
458         }
459
460         const Clip &clip = *playlist_clips->clip(row);
461         live_player->play_clip(clip, row, clip.stream_idx);
462         playlist_clips->set_progress({{ row, 0.0f }});
463         playlist_clips->set_currently_playing(row, 0.0f);
464         playlist_selection_changed();
465
466         ui->stop_btn->setEnabled(true);
467 }
468
469 void MainWindow::stop_clicked()
470 {
471         Clip fake_clip;
472         fake_clip.pts_in = 0;
473         fake_clip.pts_out = 0;
474         size_t last_row = playlist_clips->size() - 1;
475         playlist_clips->set_currently_playing(last_row, 0.0f);
476         live_player->play_clip(fake_clip, last_row, 0);
477 }
478
479 void MainWindow::live_player_clip_done()
480 {
481         int row = playlist_clips->get_currently_playing();
482         if (row == -1 || row == int(playlist_clips->size()) - 1) {
483                 set_output_status("paused");
484                 playlist_clips->set_progress({});
485                 playlist_clips->set_currently_playing(-1, 0.0f);
486         } else {
487                 playlist_clips->set_progress({{ row + 1, 0.0f }});
488                 playlist_clips->set_currently_playing(row + 1, 0.0f);
489         }
490         ui->stop_btn->setEnabled(false);
491 }
492
493 pair<Clip, size_t> MainWindow::live_player_get_next_clip()
494 {
495         // playlist_clips can only be accessed on the main thread.
496         // Hopefully, we won't have to wait too long for this to come back.
497         //
498         // TODO: If MainWindow is in the process of being destroyed and waiting
499         // for Player to shut down, we could have a deadlock here.
500         promise<pair<Clip, size_t>> clip_promise;
501         future<pair<Clip, size_t>> clip = clip_promise.get_future();
502         post_to_main_thread([&clip_promise] {
503                 int row = playlist_clips->get_currently_playing();
504                 if (row != -1 && row < int(playlist_clips->size()) - 1) {
505                         clip_promise.set_value(make_pair(*playlist_clips->clip(row + 1), row + 1));
506                 } else {
507                         clip_promise.set_value(make_pair(Clip(), 0));
508                 }
509         });
510         return clip.get();
511 }
512
513 static string format_duration(double t)
514 {
515         int t_ms = lrint(t * 1e3);
516
517         int ms = t_ms % 1000;
518         t_ms /= 1000;
519         int s = t_ms % 60;
520         t_ms /= 60;
521         int m = t_ms;
522
523         char buf[256];
524         snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
525         return buf;
526 }
527
528 void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
529 {
530         playlist_clips->set_progress(progress);
531
532         vector<Clip> clips;
533         for (size_t row = 0; row < playlist_clips->size(); ++row) {
534                 clips.push_back(*playlist_clips->clip(row));
535         }
536         double remaining = compute_time_left(clips, progress);
537         set_output_status(format_duration(remaining) + " left");
538 }
539
540 void MainWindow::resizeEvent(QResizeEvent *event)
541 {
542         QMainWindow::resizeEvent(event);
543
544         // Ask for a relayout, but only after the event loop is done doing relayout
545         // on everything else.
546         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
547 }
548
549 void MainWindow::relayout()
550 {
551         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
552         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
553 }
554
555 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
556 {
557         pts = std::max<int64_t>(pts, 0);
558         if (clip->pts_out == -1) {
559                 pts = std::min(pts, current_pts);
560         } else {
561                 pts = std::min(pts, clip->pts_out);
562         }
563         clip->pts_in = pts;
564 }
565
566 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
567 {
568         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
569         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
570         int scrub_sensitivity = 100;  // pts units per pixel.
571         int wheel_sensitivity = 100;  // pts units per degree.
572
573         unsigned stream_idx = ui->preview_display->get_stream_idx();
574
575         if (watched == ui->clip_list) {
576                 if (event->type() == QEvent::FocusOut) {
577                         highlight_camera_input(-1);
578                 }
579                 return false;
580         }
581
582         if (event->type() != QEvent::Wheel) {
583                 last_mousewheel_camera_row = -1;
584         }
585
586         if (event->type() == QEvent::MouseButtonPress) {
587                 QMouseEvent *mouse = (QMouseEvent *)event;
588
589                 QTableView *destination;
590                 ScrubType type;
591
592                 if (watched == ui->clip_list->viewport()) {
593                         destination = ui->clip_list;
594                         type = SCRUBBING_CLIP_LIST;
595                 } else if (watched == ui->playlist->viewport()) {
596                         destination = ui->playlist;
597                         type = SCRUBBING_PLAYLIST;
598                 } else {
599                         return false;
600                 }
601                 int column = destination->columnAt(mouse->x());
602                 int row = destination->rowAt(mouse->y());
603                 if (column == -1 || row == -1)
604                         return false;
605
606                 if (type == SCRUBBING_CLIP_LIST) {
607                         if (ClipList::Column(column) == ClipList::Column::IN) {
608                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
609                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
610                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
611                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
612                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
613                         } else {
614                                 return false;
615                         }
616                 } else {
617                         if (PlayList::Column(column) == PlayList::Column::IN) {
618                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
619                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
620                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
621                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
622                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
623                         } else {
624                                 return false;
625                         }
626                 }
627
628                 scrubbing = true;
629                 scrub_row = row;
630                 scrub_column = column;
631                 scrub_x_origin = mouse->x();
632                 scrub_type = type;
633         } else if (event->type() == QEvent::MouseMove) {
634                 QMouseEvent *mouse = (QMouseEvent *)event;
635                 if (mouse->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
636                         scrub_sensitivity *= 10;
637                         wheel_sensitivity *= 10;
638                 }
639                 if (mouse->modifiers() & Qt::KeyboardModifier::AltModifier) {  // Note: Shift + Alt cancel each other out.
640                         scrub_sensitivity /= 10;
641                         wheel_sensitivity /= 10;
642                 }
643                 if (scrubbing) {
644                         int offset = mouse->x() - scrub_x_origin;
645                         int adjusted_offset;
646                         if (offset >= dead_zone_pixels) {
647                                 adjusted_offset = offset - dead_zone_pixels;
648                         } else if (offset < -dead_zone_pixels) {
649                                 adjusted_offset = offset + dead_zone_pixels;
650                         } else {
651                                 adjusted_offset = 0;
652                         }
653
654                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
655                         currently_deferring_model_changes = true;
656                         if (scrub_type == SCRUBBING_CLIP_LIST) {
657                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
658                                 if (scrub_column == int(ClipList::Column::IN)) {
659                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
660                                         set_pts_in(pts, current_pts, clip);
661                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
662                                 } else {
663                                         current_change_id = "cliplist:out" + to_string(scrub_row);
664                                         pts = std::max(pts, clip->pts_in);
665                                         pts = std::min(pts, current_pts);
666                                         clip->pts_out = pts;
667                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
668                                 }
669                         } else {
670                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
671                                 if (scrub_column == int(PlayList::Column::IN)) {
672                                         current_change_id = "playlist:in:" + to_string(scrub_row);
673                                         set_pts_in(pts, current_pts, clip);
674                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
675                                 } else {
676                                         current_change_id = "playlist:out:" + to_string(scrub_row);
677                                         pts = std::max(pts, clip->pts_in);
678                                         pts = std::min(pts, current_pts);
679                                         clip->pts_out = pts;
680                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
681                                 }
682                         }
683                         currently_deferring_model_changes = false;
684
685                         return true;  // Don't use this mouse movement for selecting things.
686                 }
687         } else if (event->type() == QEvent::Wheel) {
688                 QWheelEvent *wheel = (QWheelEvent *)event;
689                 int angle_delta = wheel->angleDelta().y();
690                 if (wheel->modifiers() & Qt::KeyboardModifier::ShiftModifier) {
691                         scrub_sensitivity *= 10;
692                         wheel_sensitivity *= 10;
693                 }
694                 if (wheel->modifiers() & Qt::KeyboardModifier::AltModifier) {  // Note: Shift + Alt cancel each other out.
695                         scrub_sensitivity /= 10;
696                         wheel_sensitivity /= 10;
697                         angle_delta = wheel->angleDelta().x();  // Qt ickiness.
698                 }
699
700                 QTableView *destination;
701                 int in_column, out_column, camera_column;
702                 if (watched == ui->clip_list->viewport()) {
703                         destination = ui->clip_list;
704                         in_column = int(ClipList::Column::IN);
705                         out_column = int(ClipList::Column::OUT);
706                         camera_column = -1;
707                         last_mousewheel_camera_row = -1;
708                 } else if (watched == ui->playlist->viewport()) {
709                         destination = ui->playlist;
710                         in_column = int(PlayList::Column::IN);
711                         out_column = int(PlayList::Column::OUT);
712                         camera_column = int(PlayList::Column::CAMERA);
713                 } else {
714                         last_mousewheel_camera_row = -1;
715                         return false;
716                 }
717                 int column = destination->columnAt(wheel->x());
718                 int row = destination->rowAt(wheel->y());
719                 if (column == -1 || row == -1) return false;
720
721                 // Only adjust pts with the wheel if the given row is selected.
722                 if (!destination->hasFocus() ||
723                     row != destination->selectionModel()->currentIndex().row()) {
724                         return false;
725                 }
726
727                 currently_deferring_model_changes = true;
728                 {
729                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
730                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
731                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
732                         if (watched == ui->playlist->viewport()) {
733                                 stream_idx = clip->stream_idx;
734                         }
735
736                         if (column != camera_column) {
737                                 last_mousewheel_camera_row = -1;
738                         }
739                         if (column == in_column) {
740                                 current_change_id += "in:" + to_string(row);
741                                 int64_t pts = clip->pts_in + angle_delta * wheel_sensitivity;
742                                 set_pts_in(pts, current_pts, clip);
743                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
744                         } else if (column == out_column) {
745                                 current_change_id += "out:" + to_string(row);
746                                 int64_t pts = clip->pts_out + angle_delta * wheel_sensitivity;
747                                 pts = std::max(pts, clip->pts_in);
748                                 pts = std::min(pts, current_pts);
749                                 clip->pts_out = pts;
750                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
751                         } else if (column == camera_column) {
752                                 current_change_id += "camera:" + to_string(row);
753                                 int angle_degrees = angle_delta;
754                                 if (last_mousewheel_camera_row == row) {
755                                         angle_degrees += leftover_angle_degrees;
756                                 }
757
758                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
759                                 stream_idx = std::max(stream_idx, 0);
760                                 stream_idx = std::min<int>(stream_idx, num_cameras - 1);
761                                 clip->stream_idx = stream_idx;
762
763                                 last_mousewheel_camera_row = row;
764                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
765
766                                 // Don't update the live view, that's rarely what the operator wants.
767                         }
768                 }
769                 currently_deferring_model_changes = false;
770                 return true;  // Don't scroll.
771         } else if (event->type() == QEvent::MouseButtonRelease) {
772                 scrubbing = false;
773         }
774         return false;
775 }
776
777 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
778 {
779         if (rounding == LAST_BEFORE) {
780                 lock_guard<mutex> lock(frame_mu);
781                 if (frames[stream_idx].empty())
782                         return;
783                 auto it = find_last_frame_before(frames[stream_idx], pts);
784                 if (it != frames[stream_idx].end()) {
785                         pts = it->pts;
786                 }
787         } else {
788                 assert(rounding == FIRST_AT_OR_AFTER);
789                 lock_guard<mutex> lock(frame_mu);
790                 if (frames[stream_idx].empty())
791                         return;
792                 auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
793                 if (it != frames[stream_idx].end()) {
794                         pts = it->pts;
795                 }
796         }
797
798         Clip fake_clip;
799         fake_clip.pts_in = pts;
800         fake_clip.pts_out = pts + 1;
801         preview_player->play_clip(fake_clip, 0, stream_idx);
802 }
803
804 void MainWindow::playlist_selection_changed()
805 {
806         QItemSelectionModel *selected = ui->playlist->selectionModel();
807         bool any_selected = selected->hasSelection();
808         ui->playlist_duplicate_btn->setEnabled(any_selected);
809         ui->playlist_remove_btn->setEnabled(any_selected);
810         ui->playlist_move_up_btn->setEnabled(
811                 any_selected && selected->selectedRows().front().row() > 0);
812         ui->playlist_move_down_btn->setEnabled(
813                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
814         ui->play_btn->setEnabled(!playlist_clips->empty());
815
816         if (!any_selected) {
817                 set_output_status("paused");
818         } else {
819                 vector<Clip> clips;
820                 for (size_t row = 0; row < playlist_clips->size(); ++row) {
821                         clips.push_back(*playlist_clips->clip(row));
822                 }
823                 double remaining = compute_time_left(clips, {{selected->selectedRows().front().row(), 0.0}});
824                 set_output_status(format_duration(remaining) + " ready");
825         }
826 }
827
828 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
829 {
830         int camera_selected = -1;
831         if (cliplist_clips->is_camera_column(current.column())) {
832                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
833         }
834         highlight_camera_input(camera_selected);
835 }
836
837 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
838 {
839         char time_str[256];
840         if (estimated_seconds_left < 60.0) {
841                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
842         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
843                 int s = lrintf(estimated_seconds_left);
844                 int m = s / 60;
845                 s %= 60;
846                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
847         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
848                 int m = lrintf(estimated_seconds_left / 60.0);
849                 snprintf(time_str, sizeof(time_str), "%dm", m);
850         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
851                 int m = lrintf(estimated_seconds_left / 60.0);
852                 int h = m / 60;
853                 m %= 60;
854                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
855         } else {  // More than ten hours: Xh.
856                 int h = lrintf(estimated_seconds_left / 3600.0);
857                 snprintf(time_str, sizeof(time_str), "%dh", h);
858         }
859         char buf[256];
860         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
861
862         std::string label = buf;
863
864         post_to_main_thread([this, label] {
865                 disk_free_label->setText(QString::fromStdString(label));
866                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
867         });
868 }
869
870 void MainWindow::exit_triggered()
871 {
872         close();
873 }
874
875 void MainWindow::export_cliplist_clip_multitrack_triggered()
876 {
877         QItemSelectionModel *selected = ui->clip_list->selectionModel();
878         if (!selected->hasSelection()) {
879                 QMessageBox msgbox;
880                 msgbox.setText("No clip selected in the clip list. Select one and try exporting again.");
881                 msgbox.exec();
882                 return;
883         }
884
885         QModelIndex index = selected->currentIndex();
886         Clip clip = *cliplist_clips->clip(index.row());
887         QString filename = QFileDialog::getSaveFileName(this,
888                 "Export multitrack clip", QString(), tr("Matroska video files (*.mkv)"));
889         if (filename.isNull()) {
890                 // Cancel.
891                 return;
892         }
893         if (!filename.endsWith(".mkv")) {
894                 filename += ".mkv";
895         }
896         export_multitrack_clip(filename.toStdString(), clip);
897 }
898
899 void MainWindow::export_playlist_clip_interpolated_triggered()
900 {
901         QItemSelectionModel *selected = ui->playlist->selectionModel();
902         if (!selected->hasSelection()) {
903                 QMessageBox msgbox;
904                 msgbox.setText("No clip selected in the playlist. Select one and try exporting again.");
905                 msgbox.exec();
906                 return;
907         }
908
909         QString filename = QFileDialog::getSaveFileName(this,
910                 "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)"));
911         if (filename.isNull()) {
912                 // Cancel.
913                 return;
914         }
915         if (!filename.endsWith(".mkv")) {
916                 filename += ".mkv";
917         }
918
919         vector<Clip> clips;
920         QModelIndexList rows = selected->selectedRows();
921         for (QModelIndex index : rows) {
922                 clips.push_back(*playlist_clips->clip(index.row()));
923         }
924         export_interpolated_clip(filename.toStdString(), clips);
925 }
926
927 void MainWindow::manual_triggered()
928 {
929         if (!QDesktopServices::openUrl(QUrl("https://nageru.sesse.net/doc/"))) {
930                 QMessageBox msgbox;
931                 msgbox.setText("Could not launch manual in web browser.\nPlease see https://nageru.sesse.net/doc/ manually.");
932                 msgbox.exec();
933         }
934 }
935
936 void MainWindow::about_triggered()
937 {
938         AboutDialog("Futatabi", "Multicamera slow motion video server").exec();
939 }
940
941 void MainWindow::undo_triggered()
942 {
943         // Finish any deferred action.
944         if (defer_timeout->isActive()) {
945                 defer_timeout->stop();
946                 state_changed(deferred_state);
947         }
948
949         StateProto redo_state;
950         *redo_state.mutable_clip_list() = cliplist_clips->serialize();
951         *redo_state.mutable_play_list() = playlist_clips->serialize();
952         redo_stack.push_back(std::move(redo_state));
953         ui->redo_action->setEnabled(true);
954
955         assert(undo_stack.size() > 1);
956
957         // Pop off the current state, which is always at the top of the stack.
958         undo_stack.pop_back();
959
960         StateProto state = undo_stack.back();
961         ui->undo_action->setEnabled(undo_stack.size() > 1);
962
963         replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
964         replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
965
966         db.store_state(state);
967 }
968
969 void MainWindow::redo_triggered()
970 {
971         assert(!redo_stack.empty());
972
973         ui->undo_action->setEnabled(true);
974         ui->redo_action->setEnabled(true);
975
976         undo_stack.push_back(std::move(redo_stack.back()));
977         redo_stack.pop_back();
978         ui->undo_action->setEnabled(true);
979         ui->redo_action->setEnabled(!redo_stack.empty());
980
981         const StateProto &state = undo_stack.back();
982         replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
983         replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
984
985         db.store_state(state);
986 }
987
988 void MainWindow::quality_toggled(int quality, bool checked)
989 {
990         if (!checked) {
991                 return;
992         }
993         global_flags.interpolation_quality = quality;
994         if (quality != 0 &&  // Turning interpolation off is always possible.
995             quality != flow_initialized_interpolation_quality) {
996                 QMessageBox msgbox;
997                 msgbox.setText(QString::fromStdString(
998                         "The interpolation quality for the main output cannot be changed at runtime, "
999                         "except being turned completely off; it will take effect for exported files "
1000                         "only until next restart. The live output quality thus remains at " + to_string(flow_initialized_interpolation_quality) + "."));
1001                 msgbox.exec();
1002         }
1003
1004         save_settings();
1005 }
1006
1007 void MainWindow::highlight_camera_input(int stream_idx)
1008 {
1009         for (unsigned i = 0; i < num_cameras; ++i) {
1010                 if (stream_idx == i) {
1011                         displays[i].frame->setStyleSheet("background: rgb(0,255,0)");
1012                 } else {
1013                         displays[i].frame->setStyleSheet("");
1014                 }
1015         }
1016 }
1017
1018 void MainWindow::set_output_status(const string &status)
1019 {
1020         ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
1021
1022         lock_guard<mutex> lock(queue_status_mu);
1023         queue_status = status;
1024 }
1025
1026 pair<string, string> MainWindow::get_queue_status() const {
1027         lock_guard<mutex> lock(queue_status_mu);
1028         return {queue_status, "text/plain"};
1029 }
1030
1031 void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame)
1032 {
1033         if (stream_idx >= MAX_STREAMS) {
1034                 fprintf(stderr, "WARNING: Ignoring too-high stream index %u.\n", stream_idx);
1035                 return;
1036         }
1037         if (stream_idx >= num_cameras) {
1038                 post_to_main_thread_and_wait([this, stream_idx]{
1039                         num_cameras = stream_idx + 1;
1040                         change_num_cameras();
1041                 });
1042         }
1043         displays[stream_idx].display->setFrame(stream_idx, frame);
1044 }
1045
1046 template <class Model>
1047 void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
1048 {
1049         QItemSelectionModel *old_selection_model = view->selectionModel();
1050         view->setModel(new_model);
1051         delete *model;
1052         delete old_selection_model;
1053         *model = new_model;
1054         connect(new_model, &Model::any_content_changed, this, &MainWindow::content_changed);
1055 }