]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Fix displaying of progress bars and time remaining in the presence of fades.
[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](const map<size_t, double> &progress) {
134                 post_to_main_thread([this, progress] {
135                         live_player_clip_progress(progress);
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(), cliplist_clips->size() - 1, 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()), 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, row, clip.stream_idx);
332         playlist_clips->set_progress({{ row, 0.0f }});
333         playlist_clips->set_currently_playing(row, 0.0f);
334         playlist_selection_changed();
335 }
336
337 void MainWindow::live_player_clip_done()
338 {
339         int row = playlist_clips->get_currently_playing();
340         if (row == -1 || row == int(playlist_clips->size()) - 1) {
341                 set_output_status("paused");
342                 playlist_clips->set_progress({});
343                 playlist_clips->set_currently_playing(-1, 0.0f);
344         } else {
345                 playlist_clips->set_progress({{ row + 1, 0.0f }});
346                 playlist_clips->set_currently_playing(row + 1, 0.0f);
347         }
348 }
349
350 pair<Clip, size_t> MainWindow::live_player_get_next_clip()
351 {
352         // playlist_clips can only be accessed on the main thread.
353         // Hopefully, we won't have to wait too long for this to come back.
354         promise<pair<Clip, size_t>> clip_promise;
355         future<pair<Clip, size_t>> clip = clip_promise.get_future();
356         post_to_main_thread([this, &clip_promise] {
357                 int row = playlist_clips->get_currently_playing();
358                 if (row != -1 && row < int(playlist_clips->size()) - 1) {
359                         clip_promise.set_value(make_pair(*playlist_clips->clip(row + 1), row + 1));
360                 } else {
361                         clip_promise.set_value(make_pair(Clip(), 0));
362                 }
363         });
364         return clip.get();
365 }
366
367 static string format_duration(double t)
368 {
369         int t_ms = lrint(t * 1e3);
370
371         int ms = t_ms % 1000;
372         t_ms /= 1000;
373         int s = t_ms % 60;
374         t_ms /= 60;
375         int m = t_ms;
376
377         char buf[256];
378         snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
379         return buf;
380 }
381
382 void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
383 {
384         playlist_clips->set_progress(progress);
385
386         // Look at the last clip and then start counting from there.
387         assert(!progress.empty());
388         auto last_it = progress.end();
389         --last_it;
390         double remaining = 0.0;
391         double last_fade_time_seconds = 0.0;
392         for (size_t row = last_it->first; row < playlist_clips->size(); ++row) {
393                 const Clip clip = *playlist_clips->clip(row);
394                 double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
395                 if (row == last_it->first) {
396                         // A clip we're playing: Subtract the part we've already played.
397                         remaining = clip_length * (1.0 - last_it->second);
398                 } else {
399                         // A clip we haven't played yet: Subtract the part that's overlapping
400                         // with a previous clip (due to fade).
401                         remaining += max(clip_length - last_fade_time_seconds, 0.0);
402                 }
403                 last_fade_time_seconds = min(clip_length, clip.fade_time_seconds);
404         }
405         set_output_status(format_duration(remaining) + " left");
406 }
407
408 void MainWindow::resizeEvent(QResizeEvent *event)
409 {
410         QMainWindow::resizeEvent(event);
411
412         // Ask for a relayout, but only after the event loop is done doing relayout
413         // on everything else.
414         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
415 }
416
417 void MainWindow::relayout()
418 {
419         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
420         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
421 }
422
423 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
424 {
425         pts = std::max<int64_t>(pts, 0);
426         if (clip->pts_out == -1) {
427                 pts = std::min(pts, current_pts);
428         } else {
429                 pts = std::min(pts, clip->pts_out);
430         }
431         clip->pts_in = pts;
432 }
433
434 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
435 {
436         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
437         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
438         constexpr int wheel_sensitivity = 100;  // pts units per degree.
439         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
440
441         unsigned stream_idx = ui->preview_display->get_stream_idx();
442
443         if (watched == ui->clip_list) {
444                 if (event->type() == QEvent::FocusOut) {
445                         highlight_camera_input(-1);
446                 }
447                 return false;
448         }
449
450         if (event->type() != QEvent::Wheel) {
451                 last_mousewheel_camera_row = -1;
452         }
453
454         if (event->type() == QEvent::MouseButtonPress) {
455                 QMouseEvent *mouse = (QMouseEvent *)event;
456
457                 QTableView *destination;
458                 ScrubType type;
459
460                 if (watched == ui->clip_list->viewport()) {
461                         destination = ui->clip_list;
462                         type = SCRUBBING_CLIP_LIST;
463                 } else if (watched == ui->playlist->viewport()) {
464                         destination = ui->playlist;
465                         type = SCRUBBING_PLAYLIST;
466                 } else {
467                         return false;
468                 }
469                 int column = destination->columnAt(mouse->x());
470                 int row = destination->rowAt(mouse->y());
471                 if (column == -1 || row == -1)
472                         return false;
473
474                 if (type == SCRUBBING_CLIP_LIST) {
475                         if (ClipList::Column(column) == ClipList::Column::IN) {
476                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
477                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
478                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
479                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
480                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
481                         } else {
482                                 return false;
483                         }
484                 } else {
485                         if (PlayList::Column(column) == PlayList::Column::IN) {
486                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
487                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
488                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
489                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
490                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
491                         } else {
492                                 return false;
493                         }
494                 }
495
496                 scrubbing = true;
497                 scrub_row = row;
498                 scrub_column = column;
499                 scrub_x_origin = mouse->x();
500                 scrub_type = type;
501         } else if (event->type() == QEvent::MouseMove) {
502                 if (scrubbing) {
503                         QMouseEvent *mouse = (QMouseEvent *)event;
504                         int offset = mouse->x() - scrub_x_origin;
505                         int adjusted_offset;
506                         if (offset >= dead_zone_pixels) {
507                                 adjusted_offset = offset - dead_zone_pixels;
508                         } else if (offset < -dead_zone_pixels) {
509                                 adjusted_offset = offset + dead_zone_pixels;
510                         } else {
511                                 adjusted_offset = 0;
512                         }
513
514                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
515                         currently_deferring_model_changes = true;
516                         if (scrub_type == SCRUBBING_CLIP_LIST) {
517                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
518                                 if (scrub_column == int(ClipList::Column::IN)) {
519                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
520                                         set_pts_in(pts, current_pts, clip);
521                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
522                                 } else {
523                                         current_change_id = "cliplist:out" + to_string(scrub_row);
524                                         pts = std::max(pts, clip->pts_in);
525                                         pts = std::min(pts, current_pts);
526                                         clip->pts_out = pts;
527                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
528                                 }
529                         } else {
530                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
531                                 if (scrub_column == int(PlayList::Column::IN)) {
532                                         current_change_id = "playlist:in:" + to_string(scrub_row);
533                                         set_pts_in(pts, current_pts, clip);
534                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
535                                 } else {
536                                         current_change_id = "playlist:out:" + to_string(scrub_row);
537                                         pts = std::max(pts, clip->pts_in);
538                                         pts = std::min(pts, current_pts);
539                                         clip->pts_out = pts;
540                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
541                                 }
542                         }
543                         currently_deferring_model_changes = false;
544
545                         return true;  // Don't use this mouse movement for selecting things.
546                 }
547         } else if (event->type() == QEvent::Wheel) {
548                 QWheelEvent *wheel = (QWheelEvent *)event;
549
550                 QTableView *destination;
551                 int in_column, out_column, camera_column;
552                 if (watched == ui->clip_list->viewport()) {
553                         destination = ui->clip_list;
554                         in_column = int(ClipList::Column::IN);
555                         out_column = int(ClipList::Column::OUT);
556                         camera_column = -1;
557                         last_mousewheel_camera_row = -1;
558                 } else if (watched == ui->playlist->viewport()) {
559                         destination = ui->playlist;
560                         in_column = int(PlayList::Column::IN);
561                         out_column = int(PlayList::Column::OUT);
562                         camera_column = int(PlayList::Column::CAMERA);
563                 } else {
564                         last_mousewheel_camera_row = -1;
565                         return false;
566                 }
567                 int column = destination->columnAt(wheel->x());
568                 int row = destination->rowAt(wheel->y());
569                 if (column == -1 || row == -1) return false;
570
571                 // Only adjust pts with the wheel if the given row is selected.
572                 if (!destination->hasFocus() ||
573                     row != destination->selectionModel()->currentIndex().row()) {
574                         return false;
575                 }
576
577                 currently_deferring_model_changes = true;
578                 {
579                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
580                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
581                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
582                         if (watched == ui->playlist->viewport()) {
583                                 stream_idx = clip->stream_idx;
584                         }
585
586                         if (column != camera_column) {
587                                 last_mousewheel_camera_row = -1;
588                         }
589                         if (column == in_column) {
590                                 current_change_id += "in:" + to_string(row);
591                                 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
592                                 set_pts_in(pts, current_pts, clip);
593                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
594                         } else if (column == out_column) {
595                                 current_change_id += "out:" + to_string(row);
596                                 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
597                                 pts = std::max(pts, clip->pts_in);
598                                 pts = std::min(pts, current_pts);
599                                 clip->pts_out = pts;
600                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
601                         } else if (column == camera_column) {
602                                 current_change_id += "camera:" + to_string(row);
603                                 int angle_degrees = wheel->angleDelta().y();
604                                 if (last_mousewheel_camera_row == row) {
605                                         angle_degrees += leftover_angle_degrees;
606                                 }
607
608                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
609                                 stream_idx = std::max(stream_idx, 0);
610                                 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
611                                 clip->stream_idx = stream_idx;
612
613                                 last_mousewheel_camera_row = row;
614                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
615
616                                 // Don't update the live view, that's rarely what the operator wants.
617                         }
618                 }
619                 currently_deferring_model_changes = false;
620                 return true;  // Don't scroll.
621         } else if (event->type() == QEvent::MouseButtonRelease) {
622                 scrubbing = false;
623         }
624         return false;
625 }
626
627 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
628 {
629         if (rounding == LAST_BEFORE) {
630                 lock_guard<mutex> lock(frame_mu);
631                 if (frames[stream_idx].empty())
632                         return;
633                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
634                 if (it != frames[stream_idx].end()) {
635                         pts = *it;
636                 }
637         } else {
638                 assert(rounding == FIRST_AT_OR_AFTER);
639                 lock_guard<mutex> lock(frame_mu);
640                 if (frames[stream_idx].empty())
641                         return;
642                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
643                 if (it != frames[stream_idx].end()) {
644                         pts = *it;
645                 }
646         }
647
648         Clip fake_clip;
649         fake_clip.pts_in = pts;
650         fake_clip.pts_out = pts + 1;
651         preview_player->play_clip(fake_clip, 0, stream_idx);
652 }
653
654 void MainWindow::playlist_selection_changed()
655 {
656         QItemSelectionModel *selected = ui->playlist->selectionModel();
657         bool any_selected = selected->hasSelection();
658         ui->playlist_duplicate_btn->setEnabled(any_selected);
659         ui->playlist_remove_btn->setEnabled(any_selected);
660         ui->playlist_move_up_btn->setEnabled(
661                 any_selected && selected->selectedRows().front().row() > 0);
662         ui->playlist_move_down_btn->setEnabled(
663                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
664         ui->play_btn->setEnabled(!playlist_clips->empty());
665
666         if (!any_selected) {
667                 set_output_status("paused");
668         } else {
669                 double remaining = 0.0;
670                 for (int row = selected->selectedRows().front().row(); row < int(playlist_clips->size()); ++row) {
671                         const Clip clip = *playlist_clips->clip(row);
672                         remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
673                 }
674                 set_output_status(format_duration(remaining) + " ready");
675         }
676 }
677
678 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
679 {
680         int camera_selected = -1;
681         if (current.column() >= int(ClipList::Column::CAMERA_1) &&
682             current.column() <= int(ClipList::Column::CAMERA_4)) {
683                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
684         }
685         highlight_camera_input(camera_selected);
686 }
687
688 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
689 {
690         char time_str[256];
691         if (estimated_seconds_left < 60.0) {
692                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
693         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
694                 int s = lrintf(estimated_seconds_left);
695                 int m = s / 60;
696                 s %= 60;
697                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
698         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
699                 int m = lrintf(estimated_seconds_left / 60.0);
700                 snprintf(time_str, sizeof(time_str), "%dm", m);
701         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
702                 int m = lrintf(estimated_seconds_left / 60.0);
703                 int h = m / 60;
704                 m %= 60;
705                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
706         } else {  // More than ten hours: Xh.
707                 int h = lrintf(estimated_seconds_left / 3600.0);
708                 snprintf(time_str, sizeof(time_str), "%dh", h);
709         }
710         char buf[256];
711         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
712
713         std::string label = buf;
714
715         post_to_main_thread([this, label] {
716                 disk_free_label->setText(QString::fromStdString(label));
717                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
718         });
719 }
720
721 void MainWindow::exit_triggered()
722 {
723         close();
724 }
725
726 void MainWindow::highlight_camera_input(int stream_idx)
727 {
728         if (stream_idx == 0) {
729                 ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
730         } else {
731                 ui->input1_frame->setStyleSheet("");
732         }
733         if (stream_idx == 1) {
734                 ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
735         } else {
736                 ui->input2_frame->setStyleSheet("");
737         }
738         if (stream_idx == 2) {
739                 ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
740         } else {
741                 ui->input3_frame->setStyleSheet("");
742         }
743         if (stream_idx == 3) {
744                 ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
745         } else {
746                 ui->input4_frame->setStyleSheet("");
747         }
748 }
749
750 void MainWindow::set_output_status(const string &status)
751 {
752         ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
753
754         lock_guard<mutex> lock(queue_status_mu);
755         queue_status = status;
756 }
757
758 pair<string, string> MainWindow::get_queue_status() const {
759         lock_guard<mutex> lock(queue_status_mu);
760         return {queue_status, "text/plain"};
761 }