]> git.sesse.net Git - nageru/blob - mainwindow.cpp
When choosing a camera in the clip list, highlight the appropriate input.
[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 <future>
11 #include <string>
12 #include <vector>
13
14 #include <QMouseEvent>
15 #include <QWheelEvent>
16 #include <QShortcut>
17 #include <QTimer>
18
19 #include <sqlite3.h>
20
21 using namespace std;
22 using namespace std::placeholders;
23
24 MainWindow *global_mainwindow = nullptr;
25 static ClipList *cliplist_clips;
26 static PlayList *playlist_clips;
27
28 extern int64_t current_pts;
29 extern mutex frame_mu;
30 extern vector<int64_t> frames[MAX_STREAMS];
31
32 MainWindow::MainWindow()
33         : ui(new Ui::MainWindow),
34           db("futatabi.db")
35 {
36         global_mainwindow = this;
37         ui->setupUi(this);
38
39         // The menus.
40         connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered);
41
42         global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
43         disk_free_label = new QLabel(this);
44         disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
45         ui->menuBar->setCornerWidget(disk_free_label);
46
47         StateProto state = db.get_state();
48
49         cliplist_clips = new ClipList(state.clip_list());
50         ui->clip_list->setModel(cliplist_clips);
51         connect(cliplist_clips, &ClipList::any_content_changed, this, &MainWindow::content_changed);
52
53         playlist_clips = new PlayList(state.play_list());
54         ui->playlist->setModel(playlist_clips);
55         connect(playlist_clips, &PlayList::any_content_changed, this, &MainWindow::content_changed);
56
57         // For un-highlighting when we lose focus.
58         ui->clip_list->installEventFilter(this);
59
60         // For scrubbing in the pts columns.
61         ui->clip_list->viewport()->installEventFilter(this);
62         ui->playlist->viewport()->installEventFilter(this);
63
64         QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
65         connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
66         connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
67
68         QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
69         connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
70         connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
71
72         QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
73         connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
74         connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
75
76         QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
77         connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
78         connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
79
80         QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
81         connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
82         connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
83
84         QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
85         connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
86         connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
87         connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
88         ui->input1_display->set_overlay("1");
89
90         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
91         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
92         connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
93         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
94         ui->input2_display->set_overlay("2");
95
96         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
97         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
98         connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
99         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
100         ui->input3_display->set_overlay("3");
101
102         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
103         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
104         connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
105         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
106         ui->input4_display->set_overlay("4");
107
108         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
109
110         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
111         QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
112         connect(delete_key, &QShortcut::activated, [this] {
113                 if (ui->playlist->hasFocus()) {
114                         playlist_remove();
115                 }
116         });
117
118         // TODO: support drag-and-drop.
119         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
120         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
121
122         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
123                 this, &MainWindow::playlist_selection_changed);
124         playlist_selection_changed();  // First time set-up.
125
126         preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
127         live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
128         live_player->set_done_callback([this]{
129                 post_to_main_thread([this]{
130                         live_player_clip_done();
131                 });
132         });
133         live_player->set_next_clip_callback(bind(&MainWindow::live_player_get_next_clip, this));
134         live_player->set_progress_callback([this](double played_this_clip, double total_length) {
135                 post_to_main_thread([this, played_this_clip, total_length] {
136                         live_player_clip_progress(played_this_clip, total_length);
137                 });
138         });
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()) 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()) return;
319
320         QItemSelectionModel *selected = ui->playlist->selectionModel();
321         int row;
322         if (!selected->hasSelection()) {
323                 row = 0;
324         } else {
325                 row = selected->selectedRows(0)[0].row();
326         }
327
328         const Clip &clip = *playlist_clips->clip(row);
329         live_player->play_clip(clip, clip.stream_idx);
330         playlist_clips->set_currently_playing(row, 0.0f);
331         playlist_selection_changed();
332 }
333
334 void MainWindow::live_player_clip_done()
335 {
336         int row = playlist_clips->get_currently_playing();
337         if (row == -1 || row == int(playlist_clips->size()) - 1) {
338                 ui->live_label->setText("Current output (paused)");
339                 playlist_clips->set_currently_playing(-1, 0.0f);
340         } else {
341                 playlist_clips->set_currently_playing(row + 1, 0.0f);
342         }
343 }
344
345 Clip MainWindow::live_player_get_next_clip()
346 {
347         // playlist_clips can only be accessed on the main thread.
348         // Hopefully, we won't have to wait too long for this to come back.
349         promise<Clip> clip_promise;
350         future<Clip> clip = clip_promise.get_future();
351         post_to_main_thread([this, &clip_promise] {
352                 int row = playlist_clips->get_currently_playing();
353                 if (row != -1 && row < int(playlist_clips->size()) - 1) {
354                         clip_promise.set_value(*playlist_clips->clip(row + 1));
355                 } else {
356                         clip_promise.set_value(Clip());
357                 }
358         });
359         return clip.get();
360 }
361
362 void MainWindow::live_player_clip_progress(double played_this_clip, double total_length)
363 {
364         playlist_clips->set_currently_playing(playlist_clips->get_currently_playing(), played_this_clip / total_length);
365
366         double remaining = total_length - played_this_clip;
367         for (int row = playlist_clips->get_currently_playing() + 1; row < int(playlist_clips->size()); ++row) {
368                 const Clip clip = *playlist_clips->clip(row);
369                 remaining += double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5;   // FIXME: stop hardcoding speed.
370         }
371         int remaining_ms = lrint(remaining * 1e3);
372
373         int ms = remaining_ms % 1000;
374         remaining_ms /= 1000;
375         int s = remaining_ms % 60;
376         remaining_ms /= 60;
377         int m = remaining_ms;
378
379         char buf[256];
380         snprintf(buf, sizeof(buf), "Current output (%d:%02d.%03d left)", m, s, ms);
381         ui->live_label->setText(buf);
382 }
383
384 void MainWindow::resizeEvent(QResizeEvent *event)
385 {
386         QMainWindow::resizeEvent(event);
387
388         // Ask for a relayout, but only after the event loop is done doing relayout
389         // on everything else.
390         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
391 }
392
393 void MainWindow::relayout()
394 {
395         ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
396         ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
397 }
398
399 void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
400 {
401         pts = std::max<int64_t>(pts, 0);
402         if (clip->pts_out == -1) {
403                 pts = std::min(pts, current_pts);
404         } else {
405                 pts = std::min(pts, clip->pts_out);
406         }
407         clip->pts_in = pts;
408 }
409
410 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
411 {
412         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
413         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
414         constexpr int wheel_sensitivity = 100;  // pts units per degree.
415         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
416
417         unsigned stream_idx = ui->preview_display->get_stream_idx();
418
419         if (watched == ui->clip_list) {
420                 if (event->type() == QEvent::FocusOut) {
421                         highlight_camera_input(-1);
422                 }
423                 return false;
424         }
425
426         if (event->type() != QEvent::Wheel) {
427                 last_mousewheel_camera_row = -1;
428         }
429
430         if (event->type() == QEvent::MouseButtonPress) {
431                 QMouseEvent *mouse = (QMouseEvent *)event;
432
433                 QTableView *destination;
434                 ScrubType type;
435
436                 if (watched == ui->clip_list->viewport()) {
437                         destination = ui->clip_list;
438                         type = SCRUBBING_CLIP_LIST;
439                 } else if (watched == ui->playlist->viewport()) {
440                         destination = ui->playlist;
441                         type = SCRUBBING_PLAYLIST;
442                 } else {
443                         return false;
444                 }
445                 int column = destination->columnAt(mouse->x());
446                 int row = destination->rowAt(mouse->y());
447                 if (column == -1 || row == -1) return false;
448
449                 if (type == SCRUBBING_CLIP_LIST) {
450                         if (ClipList::Column(column) == ClipList::Column::IN) {
451                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
452                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
453                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
454                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
455                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
456                         } else {
457                                 return false;
458                         }
459                 } else {
460                         if (PlayList::Column(column) == PlayList::Column::IN) {
461                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
462                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
463                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
464                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
465                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
466                         } else {
467                                 return false;
468                         }
469                 }
470
471                 scrubbing = true;
472                 scrub_row = row;
473                 scrub_column = column;
474                 scrub_x_origin = mouse->x();
475                 scrub_type = type;
476         } else if (event->type() == QEvent::MouseMove) {
477                 if (scrubbing) {
478                         QMouseEvent *mouse = (QMouseEvent *)event;
479                         int offset = mouse->x() - scrub_x_origin;
480                         int adjusted_offset;
481                         if (offset >= dead_zone_pixels) {
482                                 adjusted_offset = offset - dead_zone_pixels;
483                         } else if (offset < -dead_zone_pixels) {
484                                 adjusted_offset = offset + dead_zone_pixels;
485                         } else {
486                                 adjusted_offset = 0;
487                         }
488
489                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
490                         currently_deferring_model_changes = true;
491                         if (scrub_type == SCRUBBING_CLIP_LIST) {
492                                 ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
493                                 if (scrub_column == int(ClipList::Column::IN)) {
494                                         current_change_id = "cliplist:in:" + to_string(scrub_row);
495                                         set_pts_in(pts, current_pts, clip);
496                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
497                                 } else {
498                                         current_change_id = "cliplist:out" + to_string(scrub_row);
499                                         pts = std::max(pts, clip->pts_in);
500                                         pts = std::min(pts, current_pts);
501                                         clip->pts_out = pts;
502                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
503                                 }
504                         } else {
505                                 ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
506                                 if (scrub_column == int(PlayList::Column::IN)) {
507                                         current_change_id = "playlist:in:" + to_string(scrub_row);
508                                         set_pts_in(pts, current_pts, clip);
509                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
510                                 } else {
511                                         current_change_id = "playlist:out:" + to_string(scrub_row);
512                                         pts = std::max(pts, clip->pts_in);
513                                         pts = std::min(pts, current_pts);
514                                         clip->pts_out = pts;
515                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
516                                 }
517                         }
518                         currently_deferring_model_changes = false;
519
520                         return true;  // Don't use this mouse movement for selecting things.
521                 }
522         } else if (event->type() == QEvent::Wheel) {
523                 QWheelEvent *wheel = (QWheelEvent *)event;
524
525                 QTableView *destination;
526                 int in_column, out_column, camera_column;
527                 if (watched == ui->clip_list->viewport()) {
528                         destination = ui->clip_list;
529                         in_column = int(ClipList::Column::IN);
530                         out_column = int(ClipList::Column::OUT);
531                         camera_column = -1;
532                         last_mousewheel_camera_row = -1;
533                 } else if (watched == ui->playlist->viewport()) {
534                         destination = ui->playlist;
535                         in_column = int(PlayList::Column::IN);
536                         out_column = int(PlayList::Column::OUT);
537                         camera_column = int(PlayList::Column::CAMERA);
538                 } else {
539                         last_mousewheel_camera_row = -1;
540                         return false;
541                 }
542                 int column = destination->columnAt(wheel->x());
543                 int row = destination->rowAt(wheel->y());
544                 if (column == -1 || row == -1) return false;
545
546                 currently_deferring_model_changes = true;
547                 {
548                         current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
549                         ClipProxy clip = (watched == ui->clip_list->viewport()) ?
550                                 cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
551                         if (watched == ui->playlist->viewport()) {
552                                 stream_idx = clip->stream_idx;
553                         }
554
555                         if (column != camera_column) {
556                                 last_mousewheel_camera_row = -1;
557                         }
558                         if (column == in_column) {
559                                 current_change_id += "in:" + to_string(row);
560                                 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
561                                 set_pts_in(pts, current_pts, clip);
562                                 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
563                         } else if (column == out_column) {
564                                 current_change_id += "out:" + to_string(row);
565                                 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
566                                 pts = std::max(pts, clip->pts_in);
567                                 pts = std::min(pts, current_pts);
568                                 clip->pts_out = pts;
569                                 preview_single_frame(pts, stream_idx, LAST_BEFORE);
570                         } else if (column == camera_column) {
571                                 current_change_id += "camera:" + to_string(row);
572                                 int angle_degrees = wheel->angleDelta().y();
573                                 if (last_mousewheel_camera_row == row) {
574                                         angle_degrees += leftover_angle_degrees;
575                                 }
576
577                                 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
578                                 stream_idx = std::max(stream_idx, 0);
579                                 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
580                                 clip->stream_idx = stream_idx;
581
582                                 last_mousewheel_camera_row = row;
583                                 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
584
585                                 // Don't update the live view, that's rarely what the operator wants.
586                         }
587                 }
588                 currently_deferring_model_changes = false;
589         } else if (event->type() == QEvent::MouseButtonRelease) {
590                 scrubbing = false;
591         }
592         return false;
593 }
594
595 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
596 {
597         if (rounding == LAST_BEFORE) {
598                 lock_guard<mutex> lock(frame_mu);
599                 if (frames[stream_idx].empty()) return;
600                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
601                 if (it != frames[stream_idx].end()) {
602                         pts = *it;
603                 }
604         } else {
605                 assert(rounding == FIRST_AT_OR_AFTER);
606                 lock_guard<mutex> lock(frame_mu);
607                 if (frames[stream_idx].empty()) return;
608                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
609                 if (it != frames[stream_idx].end()) {
610                         pts = *it;
611                 }
612         }
613
614         Clip fake_clip;
615         fake_clip.pts_in = pts;
616         fake_clip.pts_out = pts + 1;
617         preview_player->play_clip(fake_clip, stream_idx);
618 }
619
620 void MainWindow::playlist_selection_changed()
621 {
622         QItemSelectionModel *selected = ui->playlist->selectionModel();
623         bool any_selected = selected->hasSelection();
624         ui->playlist_duplicate_btn->setEnabled(any_selected);
625         ui->playlist_remove_btn->setEnabled(any_selected);
626         ui->playlist_move_up_btn->setEnabled(
627                 any_selected && selected->selectedRows().front().row() > 0);
628         ui->playlist_move_down_btn->setEnabled(
629                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
630         ui->play_btn->setEnabled(!playlist_clips->empty());
631 }
632
633 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
634 {
635         int camera_selected = -1;
636         if (current.column() >= int(ClipList::Column::CAMERA_1) &&
637             current.column() <= int(ClipList::Column::CAMERA_4)) {
638                 camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
639         }
640         highlight_camera_input(camera_selected);
641 }
642
643 void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left)
644 {
645         char time_str[256];
646         if (estimated_seconds_left < 60.0) {
647                 strcpy(time_str, "<font color=\"red\">Less than a minute</font>");
648         } else if (estimated_seconds_left < 1800.0) {  // Less than half an hour: Xm Ys (red).
649                 int s = lrintf(estimated_seconds_left);
650                 int m = s / 60;
651                 s %= 60;
652                 snprintf(time_str, sizeof(time_str), "<font color=\"red\">%dm %ds</font>", m, s);
653         } else if (estimated_seconds_left < 3600.0) {  // Less than an hour: Xm.
654                 int m = lrintf(estimated_seconds_left / 60.0);
655                 snprintf(time_str, sizeof(time_str), "%dm", m);
656         } else if (estimated_seconds_left < 36000.0) {  // Less than ten hours: Xh Ym.
657                 int m = lrintf(estimated_seconds_left / 60.0);
658                 int h = m / 60;
659                 m %= 60;
660                 snprintf(time_str, sizeof(time_str), "%dh %dm", h, m);
661         } else {  // More than ten hours: Xh.
662                 int h = lrintf(estimated_seconds_left / 3600.0);
663                 snprintf(time_str, sizeof(time_str), "%dh", h);
664         }
665         char buf[256];
666         snprintf(buf, sizeof(buf), "Disk free: %'.0f MB (approx. %s)", free_bytes / 1048576.0, time_str);
667
668         std::string label = buf;
669
670         post_to_main_thread([this, label]{
671                         disk_free_label->setText(QString::fromStdString(label));
672                         ui->menuBar->setCornerWidget(disk_free_label);  // Need to set this again for the sizing to get right.
673                         });
674 }
675
676 void MainWindow::exit_triggered()
677 {
678         close();
679 }
680
681 void MainWindow::highlight_camera_input(int stream_idx)
682 {
683         if (stream_idx == 0) {
684                 ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
685         } else {
686                 ui->input1_frame->setStyleSheet("");
687         }
688         if (stream_idx == 1) {
689                 ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
690         } else {
691                 ui->input2_frame->setStyleSheet("");
692         }
693         if (stream_idx == 2) {
694                 ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
695         } else {
696                 ui->input3_frame->setStyleSheet("");
697         }
698         if (stream_idx == 3) {
699                 ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
700         } else {
701                 ui->input4_frame->setStyleSheet("");
702         }
703 }