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