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