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