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