]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Embed shaders into the binary.
[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 (ui->playlist->hasFocus()) {
203                 // Allow the playlist as preview iff it has focus and something is selected.
204                 QItemSelectionModel *selected = ui->playlist->selectionModel();
205                 if (selected->hasSelection()) {
206                         QModelIndex index = selected->currentIndex();
207                         const Clip &clip = *playlist_clips->clip(index.row());
208                         preview_player->play_clip(clip, index.row(), clip.stream_idx);
209                         return;
210                 }
211         }
212
213         if (cliplist_clips->empty())
214                 return;
215
216         QItemSelectionModel *selected = ui->clip_list->selectionModel();
217         if (!selected->hasSelection()) {
218                 preview_player->play_clip(*cliplist_clips->back(), cliplist_clips->size() - 1, 0);
219                 return;
220         }
221
222         QModelIndex index = selected->currentIndex();
223         unsigned stream_idx;
224         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
225             index.column() <= int(ClipList::Column::CAMERA_4)) {
226                 stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
227         } else {
228                 stream_idx = ui->preview_display->get_stream_idx();
229         }
230         preview_player->play_clip(*cliplist_clips->clip(index.row()), index.row(), stream_idx);
231 }
232
233 void MainWindow::preview_angle_clicked(unsigned stream_idx)
234 {
235         preview_player->override_angle(stream_idx);
236
237         // Change the selection if we were previewing a clip from the clip list.
238         // (The only other thing we could be showing is a pts scrub, and if so,
239         // that would be selected.)
240         QItemSelectionModel *selected = ui->clip_list->selectionModel();
241         if (selected->hasSelection()) {
242                 QModelIndex cell = selected->selectedIndexes()[0];
243                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
244                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
245         }
246 }
247
248 void MainWindow::playlist_duplicate()
249 {
250         QItemSelectionModel *selected = ui->playlist->selectionModel();
251         if (!selected->hasSelection()) {
252                 // Should have been grayed out, but OK.
253                 return;
254         }
255         QModelIndexList rows = selected->selectedRows();
256         int first = rows.front().row(), last = rows.back().row();
257         playlist_clips->duplicate_clips(first, last);
258         playlist_selection_changed();
259 }
260
261 void MainWindow::playlist_remove()
262 {
263         QItemSelectionModel *selected = ui->playlist->selectionModel();
264         if (!selected->hasSelection()) {
265                 // Should have been grayed out, but OK.
266                 return;
267         }
268         QModelIndexList rows = selected->selectedRows();
269         int first = rows.front().row(), last = rows.back().row();
270         playlist_clips->erase_clips(first, last);
271
272         // TODO: select the next one in the list?
273
274         playlist_selection_changed();
275 }
276
277 void MainWindow::playlist_move(int delta)
278 {
279         QItemSelectionModel *selected = ui->playlist->selectionModel();
280         if (!selected->hasSelection()) {
281                 // Should have been grayed out, but OK.
282                 return;
283         }
284
285         QModelIndexList rows = selected->selectedRows();
286         int first = rows.front().row(), last = rows.back().row();
287         if ((delta == -1 && first == 0) ||
288             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
289                 // Should have been grayed out, but OK.
290                 return;
291         }
292
293         playlist_clips->move_clips(first, last, delta);
294         playlist_selection_changed();
295 }
296
297 void MainWindow::defer_timer_expired()
298 {
299         state_changed(deferred_state);
300 }
301
302 void MainWindow::content_changed()
303 {
304         if (defer_timeout->isActive() &&
305             (!currently_deferring_model_changes || deferred_change_id != current_change_id)) {
306                 // There's some deferred event waiting, but this event is unrelated.
307                 // So it's time to short-circuit that timer and do the work it wanted to do.
308                 defer_timeout->stop();
309                 state_changed(deferred_state);
310         }
311         StateProto state;
312         *state.mutable_clip_list() = cliplist_clips->serialize();
313         *state.mutable_play_list() = playlist_clips->serialize();
314         if (currently_deferring_model_changes) {
315                 deferred_change_id = current_change_id;
316                 deferred_state = std::move(state);
317                 defer_timeout->start(200);
318                 return;
319         }
320         state_changed(state);
321 }
322
323 void MainWindow::state_changed(const StateProto &state)
324 {
325         db.store_state(state);
326 }
327
328 void MainWindow::play_clicked()
329 {
330         if (playlist_clips->empty())
331                 return;
332
333         QItemSelectionModel *selected = ui->playlist->selectionModel();
334         int row;
335         if (!selected->hasSelection()) {
336                 row = 0;
337         } else {
338                 row = selected->selectedRows(0)[0].row();
339         }
340
341         const Clip &clip = *playlist_clips->clip(row);
342         live_player->play_clip(clip, row, clip.stream_idx);
343         playlist_clips->set_progress({{ row, 0.0f }});
344         playlist_clips->set_currently_playing(row, 0.0f);
345         playlist_selection_changed();
346 }
347
348 void MainWindow::live_player_clip_done()
349 {
350         int row = playlist_clips->get_currently_playing();
351         if (row == -1 || row == int(playlist_clips->size()) - 1) {
352                 set_output_status("paused");
353                 playlist_clips->set_progress({});
354                 playlist_clips->set_currently_playing(-1, 0.0f);
355         } else {
356                 playlist_clips->set_progress({{ row + 1, 0.0f }});
357                 playlist_clips->set_currently_playing(row + 1, 0.0f);
358         }
359 }
360
361 pair<Clip, size_t> MainWindow::live_player_get_next_clip()
362 {
363         // playlist_clips can only be accessed on the main thread.
364         // Hopefully, we won't have to wait too long for this to come back.
365         promise<pair<Clip, size_t>> clip_promise;
366         future<pair<Clip, size_t>> clip = clip_promise.get_future();
367         post_to_main_thread([this, &clip_promise] {
368                 int row = playlist_clips->get_currently_playing();
369                 if (row != -1 && row < int(playlist_clips->size()) - 1) {
370                         clip_promise.set_value(make_pair(*playlist_clips->clip(row + 1), row + 1));
371                 } else {
372                         clip_promise.set_value(make_pair(Clip(), 0));
373                 }
374         });
375         return clip.get();
376 }
377
378 static string format_duration(double t)
379 {
380         int t_ms = lrint(t * 1e3);
381
382         int ms = t_ms % 1000;
383         t_ms /= 1000;
384         int s = t_ms % 60;
385         t_ms /= 60;
386         int m = t_ms;
387
388         char buf[256];
389         snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
390         return buf;
391 }
392
393 void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
394 {
395         playlist_clips->set_progress(progress);
396
397         // Look at the last clip and then start counting from there.
398         assert(!progress.empty());
399         auto last_it = progress.end();
400         --last_it;
401         double remaining = 0.0;
402         double last_fade_time_seconds = 0.0;
403         for (size_t row = last_it->first; row < playlist_clips->size(); ++row) {
404                 const Clip clip = *playlist_clips->clip(row);
405                 double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
406                 if (row == last_it->first) {
407                         // A clip we're playing: Subtract the part we've already played.
408                         remaining = clip_length * (1.0 - last_it->second);
409                 } else {
410                         // A clip we haven't played yet: Subtract the part that's overlapping
411                         // with a previous clip (due to fade).
412                         remaining += max(clip_length - last_fade_time_seconds, 0.0);
413                 }
414                 last_fade_time_seconds = min(clip_length, clip.fade_time_seconds);
415         }
416         set_output_status(format_duration(remaining) + " left");
417 }
418
419 void MainWindow::resizeEvent(QResizeEvent *event)
420 {
421         QMainWindow::resizeEvent(event);
422
423         // Ask for a relayout, but only after the event loop is done doing relayout
424         // on everything else.
425         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
426 }
427
428 void MainWindow::relayout()
429 {
430         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
431         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
432 }
433
434 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
435 {
436         pts = std::max<int64_t>(pts, 0);
437         if (clip->pts_out == -1) {
438                 pts = std::min(pts, current_pts);
439         } else {
440                 pts = std::min(pts, clip->pts_out);
441         }
442         clip->pts_in = pts;
443 }
444
445 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
446 {
447         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
448         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
449         constexpr int wheel_sensitivity = 100;  // pts units per degree.
450         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
451
452         unsigned stream_idx = ui->preview_display->get_stream_idx();
453
454         if (watched == ui->clip_list) {
455                 if (event->type() == QEvent::FocusOut) {
456                         highlight_camera_input(-1);
457                 }
458                 return false;
459         }
460
461         if (event->type() != QEvent::Wheel) {
462                 last_mousewheel_camera_row = -1;
463         }
464
465         if (event->type() == QEvent::MouseButtonPress) {
466                 QMouseEvent *mouse = (QMouseEvent *)event;
467
468                 QTableView *destination;
469                 ScrubType type;
470
471                 if (watched == ui->clip_list->viewport()) {
472                         destination = ui->clip_list;
473                         type = SCRUBBING_CLIP_LIST;
474                 } else if (watched == ui->playlist->viewport()) {
475                         destination = ui->playlist;
476                         type = SCRUBBING_PLAYLIST;
477                 } else {
478                         return false;
479                 }
480                 int column = destination->columnAt(mouse->x());
481                 int row = destination->rowAt(mouse->y());
482                 if (column == -1 || row == -1)
483                         return false;
484
485                 if (type == SCRUBBING_CLIP_LIST) {
486                         if (ClipList::Column(column) == ClipList::Column::IN) {
487                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
488                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
489                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
490                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
491                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
492                         } else {
493                                 return false;
494                         }
495                 } else {
496                         if (PlayList::Column(column) == PlayList::Column::IN) {
497                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
498                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
499                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
500                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
501                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
502                         } else {
503                                 return false;
504                         }
505                 }
506
507                 scrubbing = true;
508                 scrub_row = row;
509                 scrub_column = column;
510                 scrub_x_origin = mouse->x();
511                 scrub_type = type;
512         } else if (event->type() == QEvent::MouseMove) {
513                 if (scrubbing) {
514                         QMouseEvent *mouse = (QMouseEvent *)event;
515                         int offset = mouse->x() - scrub_x_origin;
516                         int adjusted_offset;
517                         if (offset >= dead_zone_pixels) {
518                                 adjusted_offset = offset - dead_zone_pixels;
519                         } else if (offset < -dead_zone_pixels) {
520                                 adjusted_offset = offset + dead_zone_pixels;
521                         } else {
522                                 adjusted_offset = 0;
523                         }
524
525                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
526                         currently_deferring_model_changes = true;
527                         if (scrub_type == SCRUBBING_CLIP_LIST) {
528                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
529                                 if (scrub_column == int(ClipList::Column::IN)) {
530                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
531                                         set_pts_in(pts, current_pts, clip);
532                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
533                                 } else {
534                                         current_change_id = "cliplist:out" + to_string(scrub_row);
535                                         pts = std::max(pts, clip->pts_in);
536                                         pts = std::min(pts, current_pts);
537                                         clip->pts_out = pts;
538                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
539                                 }
540                         } else {
541                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
542                                 if (scrub_column == int(PlayList::Column::IN)) {
543                                         current_change_id = "playlist:in:" + to_string(scrub_row);
544                                         set_pts_in(pts, current_pts, clip);
545                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
546                                 } else {
547                                         current_change_id = "playlist:out:" + to_string(scrub_row);
548                                         pts = std::max(pts, clip->pts_in);
549                                         pts = std::min(pts, current_pts);
550                                         clip->pts_out = pts;
551                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
552                                 }
553                         }
554                         currently_deferring_model_changes = false;
555
556                         return true;  // Don't use this mouse movement for selecting things.
557                 }
558         } else if (event->type() == QEvent::Wheel) {
559                 QWheelEvent *wheel = (QWheelEvent *)event;
560
561                 QTableView *destination;
562                 int in_column, out_column, camera_column;
563                 if (watched == ui->clip_list->viewport()) {
564                         destination = ui->clip_list;
565                         in_column = int(ClipList::Column::IN);
566                         out_column = int(ClipList::Column::OUT);
567                         camera_column = -1;
568                         last_mousewheel_camera_row = -1;
569                 } else if (watched == ui->playlist->viewport()) {
570                         destination = ui->playlist;
571                         in_column = int(PlayList::Column::IN);
572                         out_column = int(PlayList::Column::OUT);
573                         camera_column = int(PlayList::Column::CAMERA);
574                 } else {
575                         last_mousewheel_camera_row = -1;
576                         return false;
577                 }
578                 int column = destination->columnAt(wheel->x());
579                 int row = destination->rowAt(wheel->y());
580                 if (column == -1 || row == -1) return false;
581
582                 // Only adjust pts with the wheel if the given row is selected.
583                 if (!destination->hasFocus() ||
584                     row != destination->selectionModel()->currentIndex().row()) {
585                         return false;
586                 }
587
588                 currently_deferring_model_changes = true;
589                 {
590                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
591                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
592                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
593                         if (watched == ui->playlist->viewport()) {
594                                 stream_idx = clip->stream_idx;
595                         }
596
597                         if (column != camera_column) {
598                                 last_mousewheel_camera_row = -1;
599                         }
600                         if (column == in_column) {
601                                 current_change_id += "in:" + to_string(row);
602                                 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
603                                 set_pts_in(pts, current_pts, clip);
604                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
605                         } else if (column == out_column) {
606                                 current_change_id += "out:" + to_string(row);
607                                 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
608                                 pts = std::max(pts, clip->pts_in);
609                                 pts = std::min(pts, current_pts);
610                                 clip->pts_out = pts;
611                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
612                         } else if (column == camera_column) {
613                                 current_change_id += "camera:" + to_string(row);
614                                 int angle_degrees = wheel->angleDelta().y();
615                                 if (last_mousewheel_camera_row == row) {
616                                         angle_degrees += leftover_angle_degrees;
617                                 }
618
619                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
620                                 stream_idx = std::max(stream_idx, 0);
621                                 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
622                                 clip->stream_idx = stream_idx;
623
624                                 last_mousewheel_camera_row = row;
625                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
626
627                                 // Don't update the live view, that's rarely what the operator wants.
628                         }
629                 }
630                 currently_deferring_model_changes = false;
631                 return true;  // Don't scroll.
632         } else if (event->type() == QEvent::MouseButtonRelease) {
633                 scrubbing = false;
634         }
635         return false;
636 }
637
638 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
639 {
640         if (rounding == LAST_BEFORE) {
641                 lock_guard<mutex> lock(frame_mu);
642                 if (frames[stream_idx].empty())
643                         return;
644                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
645                 if (it != frames[stream_idx].end()) {
646                         pts = *it;
647                 }
648         } else {
649                 assert(rounding == FIRST_AT_OR_AFTER);
650                 lock_guard<mutex> lock(frame_mu);
651                 if (frames[stream_idx].empty())
652                         return;
653                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
654                 if (it != frames[stream_idx].end()) {
655                         pts = *it;
656                 }
657         }
658
659         Clip fake_clip;
660         fake_clip.pts_in = pts;
661         fake_clip.pts_out = pts + 1;
662         preview_player->play_clip(fake_clip, 0, stream_idx);
663 }
664
665 void MainWindow::playlist_selection_changed()
666 {
667         QItemSelectionModel *selected = ui->playlist->selectionModel();
668         bool any_selected = selected->hasSelection();
669         ui->playlist_duplicate_btn->setEnabled(any_selected);
670         ui->playlist_remove_btn->setEnabled(any_selected);
671         ui->playlist_move_up_btn->setEnabled(
672                 any_selected && selected->selectedRows().front().row() > 0);
673         ui->playlist_move_down_btn->setEnabled(
674                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
675         ui->play_btn->setEnabled(!playlist_clips->empty());
676
677         if (!any_selected) {
678                 set_output_status("paused");
679         } else {
680                 double remaining = 0.0;
681                 for (int row = selected->selectedRows().front().row(); row < int(playlist_clips->size()); ++row) {
682                         const Clip clip = *playlist_clips->clip(row);
683                         remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;  // FIXME: stop hardcoding speed.
684                 }
685                 set_output_status(format_duration(remaining) + " ready");
686         }
687 }
688
689 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
690 {
691         int camera_selected = -1;
692         if (current.column() >= int(ClipList::Column::CAMERA_1) &&
693             current.column() <= int(ClipList::Column::CAMERA_4)) {
694                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
695         }
696         highlight_camera_input(camera_selected);
697 }
698
699 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
700 {
701         char time_str[256];
702         if (estimated_seconds_left < 60.0) {
703                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
704         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
705                 int s = lrintf(estimated_seconds_left);
706                 int m = s / 60;
707                 s %= 60;
708                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
709         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
710                 int m = lrintf(estimated_seconds_left / 60.0);
711                 snprintf(time_str, sizeof(time_str), "%dm", m);
712         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
713                 int m = lrintf(estimated_seconds_left / 60.0);
714                 int h = m / 60;
715                 m %= 60;
716                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
717         } else {  // More than ten hours: Xh.
718                 int h = lrintf(estimated_seconds_left / 3600.0);
719                 snprintf(time_str, sizeof(time_str), "%dh", h);
720         }
721         char buf[256];
722         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
723
724         std::string label = buf;
725
726         post_to_main_thread([this, label] {
727                 disk_free_label->setText(QString::fromStdString(label));
728                 ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
729         });
730 }
731
732 void MainWindow::exit_triggered()
733 {
734         close();
735 }
736
737 void MainWindow::highlight_camera_input(int stream_idx)
738 {
739         if (stream_idx == 0) {
740                 ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
741         } else {
742                 ui->input1_frame->setStyleSheet("");
743         }
744         if (stream_idx == 1) {
745                 ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
746         } else {
747                 ui->input2_frame->setStyleSheet("");
748         }
749         if (stream_idx == 2) {
750                 ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
751         } else {
752                 ui->input3_frame->setStyleSheet("");
753         }
754         if (stream_idx == 3) {
755                 ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
756         } else {
757                 ui->input4_frame->setStyleSheet("");
758         }
759 }
760
761 void MainWindow::set_output_status(const string &status)
762 {
763         ui->live_label->setText(QString::fromStdString("Current output (" + status + ")"));
764
765         lock_guard<mutex> lock(queue_status_mu);
766         queue_status = status;
767 }
768
769 pair<string, string> MainWindow::get_queue_status() const {
770         lock_guard<mutex> lock(queue_status_mu);
771         return {queue_status, "text/plain"};
772 }