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