]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Make it possible to change the working directory.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "disk_space_estimator.h"
5 #include "flags.h"
6 #include "player.h"
7 #include "post_to_main_thread.h"
8 #include "timebase.h"
9 #include "ui_mainwindow.h"
10
11 #include <QMouseEvent>
12 #include <QShortcut>
13 #include <QTimer>
14 #include <QWheelEvent>
15 #include <future>
16 #include <sqlite3.h>
17 #include <string>
18 #include <vector>
19
20 using namespace std;
21 using namespace std::placeholders;
22
23 MainWindow *global_mainwindow = nullptr;
24 static ClipList *cliplist_clips;
25 static PlayList *playlist_clips;
26
27 extern int64_t current_pts;
28 extern mutex frame_mu;
29 extern vector<int64_t> frames[MAX_STREAMS];
30
31 MainWindow::MainWindow()
32         : ui(new Ui::MainWindow),
33           db(global_flags.working_directory + "/futatabi.db")
34 {
35         global_mainwindow = this;
36         ui->setupUi(this);
37
38         // The menus.
39         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
40
41         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
42         disk_free_label = new QLabel(this);
43         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
44         ui->menuBar->setCornerWidget(disk_free_label);
45
46         StateProto state = db.get_state();
47
48         cliplist_clips = new ClipList(state.clip_list());
49         ui->clip_list->setModel(cliplist_clips);
50         connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
51
52         playlist_clips = new PlayList(state.play_list());
53         ui->playlist->setModel(playlist_clips);
54         connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
55
56         // For un-highlighting when we lose focus.
57         ui->clip_list->installEventFilter(this);
58
59         // For scrubbing in the pts columns.
60         ui->clip_list->viewport()->installEventFilter(this);
61         ui->playlist->viewport()->installEventFilter(this);
62
63         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
64         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
65         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
66
67         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
68         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
69         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
70
71         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
72         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
73         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
74
75         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
76         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
77         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
78
79         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
80         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
81         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
82
83         QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
84         connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
85         connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
86         connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
87         ui->input1_display->set_overlay("1");
88
89         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
90         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
91         connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
92         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
93         ui->input2_display->set_overlay("2");
94
95         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
96         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
97         connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
98         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
99         ui->input3_display->set_overlay("3");
100
101         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
102         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
103         connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
104         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
105         ui->input4_display->set_overlay("4");
106
107         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
108
109         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
110         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
111         connect(delete_key, &QShortcut::activated, [this] {
112                 if (ui->playlist->hasFocus()) {
113                         playlist_remove();
114                 }
115         });
116
117         // TODO: support drag-and-drop.
118         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
119         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
120
121         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
122                 this, &MainWindow::playlist_selection_changed);
123         playlist_selection_changed();  // First time set-up.
124
125         preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
126         live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
127         live_player->set_done_callback([this]{
128                 post_to_main_thread([this]{
129                         live_player_clip_done();
130                 });
131         });
132         live_player->set_next_clip_callback(bind(&MainWindow::live_player_get_next_clip, this));
133         live_player->set_progress_callback([this](double played_this_clip, double total_length) {
134                 post_to_main_thread([this, played_this_clip, total_length] {
135                         live_player_clip_progress(played_this_clip, total_length);
136                 });
137         });
138         set_output_status("paused");
139
140         defer_timeout = new QTimer(this);
141         defer_timeout->setSingleShot(true);
142         connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired);
143
144         connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
145                 this, &MainWindow::clip_list_selection_changed);
146 }
147
148 void MainWindow::cue_in_clicked()
149 {
150         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
151                 cliplist_clips->mutable_back()->pts_in = current_pts;
152                 return;
153         }
154         Clip clip;
155         clip.pts_in = current_pts;
156         cliplist_clips->add_clip(clip);
157         playlist_selection_changed();
158 }
159
160 void MainWindow::cue_out_clicked()
161 {
162         if (!cliplist_clips->empty()) {
163                 cliplist_clips->mutable_back()->pts_out = current_pts;
164                 // TODO: select the row in the clip list?
165         }
166 }
167
168 void MainWindow::queue_clicked()
169 {
170         if (cliplist_clips->empty()) {
171                 return;
172         }
173
174         QItemSelectionModel *selected = ui->clip_list->selectionModel();
175         if (!selected->hasSelection()) {
176                 Clip clip = *cliplist_clips->back();
177                 clip.stream_idx = 0;
178                 if (clip.pts_out != -1) {
179                         playlist_clips->add_clip(clip);
180                         playlist_selection_changed();
181                 }
182                 return;
183         }
184
185         QModelIndex index = selected->currentIndex();
186         Clip clip = *cliplist_clips->clip(index.row());
187         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
188             index.column() <= int(ClipList::Column::CAMERA_4)) {
189                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
190         } else {
191                 clip.stream_idx = ui->preview_display->get_stream_idx();
192         }
193
194         if (clip.pts_out != -1) {
195                 playlist_clips->add_clip(clip);
196                 playlist_selection_changed();
197         }
198 }
199
200 void MainWindow::preview_clicked()
201 {
202         if (cliplist_clips->empty())
203                 return;
204
205         QItemSelectionModel *selected = ui->clip_list->selectionModel();
206         if (!selected->hasSelection()) {
207                 preview_player->play_clip(*cliplist_clips->back(), 0);
208                 return;
209         }
210
211         QModelIndex index = selected->currentIndex();
212         unsigned stream_idx;
213         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
214             index.column() <= int(ClipList::Column::CAMERA_4)) {
215                 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
216         } else {
217                 stream_idx = ui->preview_display->get_stream_idx();
218         }
219         preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
220 }
221
222 void MainWindow::preview_angle_clicked(unsigned stream_idx)
223 {
224         preview_player->override_angle(stream_idx);
225
226         // Change the selection if we were previewing a clip from the clip list.
227         // (The only other thing we could be showing is a pts scrub, and if so,
228         // that would be selected.)
229         QItemSelectionModel *selected = ui->clip_list->selectionModel();
230         if (selected->hasSelection()) {
231                 QModelIndex cell = selected->selectedIndexes()[0];
232                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
233                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
234         }
235 }
236
237 void MainWindow::playlist_duplicate()
238 {
239         QItemSelectionModel *selected = ui->playlist->selectionModel();
240         if (!selected->hasSelection()) {
241                 // Should have been grayed out, but OK.
242                 return;
243         }
244         QModelIndexList rows = selected->selectedRows();
245         int first = rows.front().row(), last = rows.back().row();
246         playlist_clips->duplicate_clips(first, last);
247         playlist_selection_changed();
248 }
249
250 void MainWindow::playlist_remove()
251 {
252         QItemSelectionModel *selected = ui->playlist->selectionModel();
253         if (!selected->hasSelection()) {
254                 // Should have been grayed out, but OK.
255                 return;
256         }
257         QModelIndexList rows = selected->selectedRows();
258         int first = rows.front().row(), last = rows.back().row();
259         playlist_clips->erase_clips(first, last);
260
261         // TODO: select the next one in the list?
262
263         playlist_selection_changed();
264 }
265
266 void MainWindow::playlist_move(int delta)
267 {
268         QItemSelectionModel *selected = ui->playlist->selectionModel();
269         if (!selected->hasSelection()) {
270                 // Should have been grayed out, but OK.
271                 return;
272         }
273
274         QModelIndexList rows = selected->selectedRows();
275         int first = rows.front().row(), last = rows.back().row();
276         if ((delta == -1 && first == 0) ||
277             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
278                 // Should have been grayed out, but OK.
279                 return;
280         }
281
282         playlist_clips->move_clips(first, last, delta);
283         playlist_selection_changed();
284 }
285
286 void MainWindow::defer_timer_expired()
287 {
288         state_changed(deferred_state);
289 }
290
291 void MainWindow::content_changed()
292 {
293         if (defer_timeout->isActive() &&
294             (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
295                 // There's some deferred event waiting, but this event is unrelated.
296                 // So it's time to short-circuit that timer and do the work it wanted to do.
297                 defer_timeout->stop();
298                 state_changed(deferred_state);
299         }
300         StateProto state;
301         *state.mutable_clip_list() = cliplist_clips->serialize();
302         *state.mutable_play_list() = playlist_clips->serialize();
303         if (currently_deferring_model_changes) {
304                 deferred_change_id = current_change_id;
305                 deferred_state = std::move(state);
306                 defer_timeout->start(200);
307                 return;
308         }
309         state_changed(state);
310 }
311
312 void MainWindow::state_changed(const StateProto &state)
313 {
314         db.store_state(state);
315 }
316
317 void MainWindow::play_clicked()
318 {
319         if (playlist_clips->empty())
320                 return;
321
322         QItemSelectionModel *selected = ui->playlist->selectionModel();
323         int row;
324         if (!selected->hasSelection()) {
325                 row = 0;
326         } else {
327                 row = selected->selectedRows(0)[0].row();
328         }
329
330         const Clip &clip = *playlist_clips->clip(row);
331         live_player->play_clip(clip, clip.stream_idx);
332         playlist_clips->set_currently_playing(row, 0.0f);
333         playlist_selection_changed();
334 }
335
336 void MainWindow::live_player_clip_done()
337 {
338         int row = playlist_clips->get_currently_playing();
339         if (row == -1 || row == int(playlist_clips->size()) - 1) {
340                 set_output_status("paused");
341                 playlist_clips->set_currently_playing(-1, 0.0f);
342         } else {
343                 playlist_clips->set_currently_playing(row + 1, 0.0f);
344         }
345 }
346
347 Clip MainWindow::live_player_get_next_clip()
348 {
349         // playlist_clips can only be accessed on the main thread.
350         // Hopefully, we won't have to wait too long for this to come back.
351         promise<Clip> clip_promise;
352         future<Clip> clip = clip_promise.get_future();
353         post_to_main_thread([this, &clip_promise] {
354                 int row = playlist_clips->get_currently_playing();
355                 if (row != -1 && row < int(playlist_clips->size()) - 1) {
356                         clip_promise.set_value(*playlist_clips->clip(row + 1));
357                 } else {
358                         clip_promise.set_value(Clip());
359                 }
360         });
361         return clip.get();
362 }
363
364 static string format_duration(double t)
365 {
366         int t_ms = lrint(t * 1e3);
367
368         int ms = t_ms % 1000;
369         t_ms /= 1000;
370         int s = t_ms % 60;
371         t_ms /= 60;
372         int m = t_ms;
373
374         char buf[256];
375         snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
376         return buf;
377 }
378
379 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
380 {
381         playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
382
383         double remaining = total_length - played_this_clip;
384         for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
385                 const Clip clip = *playlist_clips->clip(row);
386                 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
387         }
388         set_output_status(format_duration(remaining) + " left");
389 }
390
391 void MainWindow::resizeEvent(QResizeEvent *event)
392 {
393         QMainWindow::resizeEvent(event);
394
395         // Ask for a relayout, but only after the event loop is done doing relayout
396         // on everything else.
397         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
398 }
399
400 void MainWindow::relayout()
401 {
402         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
403         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
404 }
405
406 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
407 {
408         pts = std::max<int64_t>(pts, 0);
409         if (clip->pts_out == -1) {
410                 pts = std::min(pts, current_pts);
411         } else {
412                 pts = std::min(pts, clip->pts_out);
413         }
414         clip->pts_in = pts;
415 }
416
417 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
418 {
419         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
420         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
421         constexpr int wheel_sensitivity = 100;  // pts units per degree.
422         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
423
424         unsigned stream_idx = ui->preview_display->get_stream_idx();
425
426         if (watched == ui->clip_list) {
427                 if (event->type() == QEvent::FocusOut) {
428                         highlight_camera_input(-1);
429                 }
430                 return false;
431         }
432
433         if (event->type() != QEvent::Wheel) {
434                 last_mousewheel_camera_row = -1;
435         }
436
437         if (event->type() == QEvent::MouseButtonPress) {
438                 QMouseEvent *mouse = (QMouseEvent *)event;
439
440                 QTableView *destination;
441                 ScrubType type;
442
443                 if (watched == ui->clip_list->viewport()) {
444                         destination = ui->clip_list;
445                         type = SCRUBBING_CLIP_LIST;
446                 } else if (watched == ui->playlist->viewport()) {
447                         destination = ui->playlist;
448                         type = SCRUBBING_PLAYLIST;
449                 } else {
450                         return false;
451                 }
452                 int column = destination->columnAt(mouse->x());
453                 int row = destination->rowAt(mouse->y());
454                 if (column == -1 || row == -1)
455                         return false;
456
457                 if (type == SCRUBBING_CLIP_LIST) {
458                         if (ClipList::Column(column) == ClipList::Column::IN) {
459                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
460                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
461                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
462                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
463                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
464                         } else {
465                                 return false;
466                         }
467                 } else {
468                         if (PlayList::Column(column) == PlayList::Column::IN) {
469                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
470                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
471                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
472                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
473                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
474                         } else {
475                                 return false;
476                         }
477                 }
478
479                 scrubbing = true;
480                 scrub_row = row;
481                 scrub_column = column;
482                 scrub_x_origin = mouse->x();
483                 scrub_type = type;
484         } else if (event->type() == QEvent::MouseMove) {
485                 if (scrubbing) {
486                         QMouseEvent *mouse = (QMouseEvent *)event;
487                         int offset = mouse->x() - scrub_x_origin;
488                         int adjusted_offset;
489                         if (offset >= dead_zone_pixels) {
490                                 adjusted_offset = offset - dead_zone_pixels;
491                         } else if (offset < -dead_zone_pixels) {
492                                 adjusted_offset = offset + dead_zone_pixels;
493                         } else {
494                                 adjusted_offset = 0;
495                         }
496
497                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
498                         currently_deferring_model_changes = true;
499                         if (scrub_type == SCRUBBING_CLIP_LIST) {
500                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
501                                 if (scrub_column == int(ClipList::Column::IN)) {
502                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
503                                         set_pts_in(pts, current_pts, clip);
504                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
505                                 } else {
506                                         current_change_id = "cliplist:out" + to_string(scrub_row);
507                                         pts = std::max(pts, clip->pts_in);
508                                         pts = std::min(pts, current_pts);
509                                         clip->pts_out = pts;
510                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
511                                 }
512                         } else {
513                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
514                                 if (scrub_column == int(PlayList::Column::IN)) {
515                                         current_change_id = "playlist:in:" + to_string(scrub_row);
516                                         set_pts_in(pts, current_pts, clip);
517                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
518                                 } else {
519                                         current_change_id = "playlist:out:" + to_string(scrub_row);
520                                         pts = std::max(pts, clip->pts_in);
521                                         pts = std::min(pts, current_pts);
522                                         clip->pts_out = pts;
523                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
524                                 }
525                         }
526                         currently_deferring_model_changes = false;
527
528                         return true;  // Don't use this mouse movement for selecting things.
529                 }
530         } else if (event->type() == QEvent::Wheel) {
531                 QWheelEvent *wheel = (QWheelEvent *)event;
532
533                 QTableView *destination;
534                 int in_column, out_column, camera_column;
535                 if (watched == ui->clip_list->viewport()) {
536                         destination = ui->clip_list;
537                         in_column = int(ClipList::Column::IN);
538                         out_column = int(ClipList::Column::OUT);
539                         camera_column = -1;
540                         last_mousewheel_camera_row = -1;
541                 } else if (watched == ui->playlist->viewport()) {
542                         destination = ui->playlist;
543                         in_column = int(PlayList::Column::IN);
544                         out_column = int(PlayList::Column::OUT);
545                         camera_column = int(PlayList::Column::CAMERA);
546                 } else {
547                         last_mousewheel_camera_row = -1;
548                         return false;
549                 }
550                 int column = destination->columnAt(wheel->x());
551                 int row = destination->rowAt(wheel->y());
552                 if (column == -1 || row == -1) return false;
553
554                 currently_deferring_model_changes = true;
555                 {
556                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
557                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
558                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
559                         if (watched == ui->playlist->viewport()) {
560                                 stream_idx = clip->stream_idx;
561                         }
562
563                         if (column != camera_column) {
564                                 last_mousewheel_camera_row = -1;
565                         }
566                         if (column == in_column) {
567                                 current_change_id += "in:" + to_string(row);
568                                 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
569                                 set_pts_in(pts, current_pts, clip);
570                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
571                         } else if (column == out_column) {
572                                 current_change_id += "out:" + to_string(row);
573                                 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
574                                 pts = std::max(pts, clip->pts_in);
575                                 pts = std::min(pts, current_pts);
576                                 clip->pts_out = pts;
577                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
578                         } else if (column == camera_column) {
579                                 current_change_id += "camera:" + to_string(row);
580                                 int angle_degrees = wheel->angleDelta().y();
581                                 if (last_mousewheel_camera_row == row) {
582                                         angle_degrees += leftover_angle_degrees;
583                                 }
584
585                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
586                                 stream_idx = std::max(stream_idx, 0);
587                                 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
588                                 clip->stream_idx = stream_idx;
589
590                                 last_mousewheel_camera_row = row;
591                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
592
593                                 // Don't update the live view, that's rarely what the operator wants.
594                         }
595                 }
596                 currently_deferring_model_changes = false;
597         } else if (event->type() == QEvent::MouseButtonRelease) {
598                 scrubbing = false;
599         }
600         return false;
601 }
602
603 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
604 {
605         if (rounding == LAST_BEFORE) {
606                 lock_guard<mutex> lock(frame_mu);
607                 if (frames[stream_idx].empty())
608                         return;
609                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
610                 if (it != frames[stream_idx].end()) {
611                         pts = *it;
612                 }
613         } else {
614                 assert(rounding == FIRST_AT_OR_AFTER);
615                 lock_guard<mutex> lock(frame_mu);
616                 if (frames[stream_idx].empty())
617                         return;
618                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
619                 if (it != frames[stream_idx].end()) {
620                         pts = *it;
621                 }
622         }
623
624         Clip fake_clip;
625         fake_clip.pts_in = pts;
626         fake_clip.pts_out = pts + 1;
627         preview_player->play_clip(fake_clip, stream_idx);
628 }
629
630 void MainWindow::playlist_selection_changed()
631 {
632         QItemSelectionModel *selected = ui->playlist->selectionModel();
633         bool any_selected = selected->hasSelection();
634         ui->playlist_duplicate_btn->setEnabled(any_selected);
635         ui->playlist_remove_btn->setEnabled(any_selected);
636         ui->playlist_move_up_btn->setEnabled(
637                 any_selected && selected->selectedRows().front().row() > 0);
638         ui->playlist_move_down_btn->setEnabled(
639                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
640         ui->play_btn->setEnabled(!playlist_clips->empty());
641
642         if (!any_selected) {
643                 set_output_status("paused");
644         } else {
645                 double remaining = 0.0;
646                 for (int row = selected->selectedRows().front().row(); row < int(playlist_clips->size()); ++row) {
647                         const Clip clip = *playlist_clips->clip(row);
648                         remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
649                 }
650                 set_output_status(format_duration(remaining) + " ready");
651         }
652 }
653
654 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
655 {
656         int camera_selected = -1;
657         if (current.column() >= int(ClipList::Column::CAMERA_1) &&
658             current.column() <= int(ClipList::Column::CAMERA_4)) {
659                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
660         }
661         highlight_camera_input(camera_selected);
662 }
663
664 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
665 {
666         char time_str[256];
667         if (estimated_seconds_left < 60.0) {
668                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
669         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
670                 int s = lrintf(estimated_seconds_left);
671                 int m = s / 60;
672                 s %= 60;
673                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
674         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
675                 int m = lrintf(estimated_seconds_left / 60.0);
676                 snprintf(time_str, sizeof(time_str), "%dm", m);
677         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
678                 int m = lrintf(estimated_seconds_left / 60.0);
679                 int h = m / 60;
680                 m %= 60;
681                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
682         } else {  // More than ten hours: Xh.
683                 int h = lrintf(estimated_seconds_left / 3600.0);
684                 snprintf(time_str, sizeof(time_str), "%dh", h);
685         }
686         char buf[256];
687         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
688
689         std::string label = buf;
690
691         post_to_main_thread([this, label] {
692                 disk_free_label->setText(QString::fromStdString(label));
693                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
694         });
695 }
696
697 void MainWindow::exit_triggered()
698 {
699         close();
700 }
701
702 void MainWindow::highlight_camera_input(int stream_idx)
703 {
704         if (stream_idx == 0) {
705                 ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
706         } else {
707                 ui->input1_frame->setStyleSheet("");
708         }
709         if (stream_idx == 1) {
710                 ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
711         } else {
712                 ui->input2_frame->setStyleSheet("");
713         }
714         if (stream_idx == 2) {
715                 ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
716         } else {
717                 ui->input3_frame->setStyleSheet("");
718         }
719         if (stream_idx == 3) {
720                 ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
721         } else {
722                 ui->input4_frame->setStyleSheet("");
723         }
724 }
725
726 void MainWindow::set_output_status(const string &status)
727 {
728         ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
729
730         lock_guard<mutex> lock(queue_status_mu);
731         queue_status = status;
732 }
733
734 pair<string, string> MainWindow::get_queue_status() const {
735         lock_guard<mutex> lock(queue_status_mu);
736         return {queue_status, "text/plain"};
737 }