1 #include "mainwindow.h"
5 #include "post_to_main_thread.h"
6 #include "ui_mainwindow.h"
11 #include <QMouseEvent>
12 #include <QWheelEvent>
17 MainWindow *global_mainwindow = nullptr;
18 ClipList *cliplist_clips;
19 PlayList *playlist_clips;
21 extern int64_t current_pts;
22 extern mutex frame_mu;
23 extern vector<int64_t> frames[MAX_STREAMS];
25 MainWindow::MainWindow()
26 : ui(new Ui::MainWindow)
28 global_mainwindow = this;
31 cliplist_clips = new ClipList();
32 ui->clip_list->setModel(cliplist_clips);
34 playlist_clips = new PlayList();
35 ui->playlist->setModel(playlist_clips);
37 // For scrubbing in the pts columns.
38 ui->clip_list->viewport()->installEventFilter(this);
39 ui->playlist->viewport()->installEventFilter(this);
41 QShortcut *cue_in = new QShortcut(QKeySequence(Qt::Key_A), this);
42 connect(cue_in, &QShortcut::activated, ui->cue_in_btn, &QPushButton::click);
43 connect(ui->cue_in_btn, &QPushButton::clicked, this, &MainWindow::cue_in_clicked);
45 QShortcut *cue_out = new QShortcut(QKeySequence(Qt::Key_S), this);
46 connect(cue_out, &QShortcut::activated, ui->cue_out_btn, &QPushButton::click);
47 connect(ui->cue_out_btn, &QPushButton::clicked, this, &MainWindow::cue_out_clicked);
49 QShortcut *queue = new QShortcut(QKeySequence(Qt::Key_Q), this);
50 connect(queue, &QShortcut::activated, ui->queue_btn, &QPushButton::click);
51 connect(ui->queue_btn, &QPushButton::clicked, this, &MainWindow::queue_clicked);
53 QShortcut *preview = new QShortcut(QKeySequence(Qt::Key_W), this);
54 connect(preview, &QShortcut::activated, ui->preview_btn, &QPushButton::click);
55 connect(ui->preview_btn, &QPushButton::clicked, this, &MainWindow::preview_clicked);
57 QShortcut *play = new QShortcut(QKeySequence(Qt::Key_Space), this);
58 connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
59 connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
61 QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
62 connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
63 connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
64 connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
65 ui->input1_display->set_overlay("1");
67 QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
68 connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
69 connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
70 connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
71 ui->input2_display->set_overlay("2");
73 QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
74 connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
75 connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
76 connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
77 ui->input3_display->set_overlay("3");
79 QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
80 connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
81 connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
82 connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
83 ui->input4_display->set_overlay("4");
85 connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
87 connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
88 QShortcut *delete_key = new QShortcut(QKeySequence(Qt::Key_Delete), ui->playlist);
89 connect(delete_key, &QShortcut::activated, [this] {
90 if (ui->playlist->hasFocus()) {
95 // TODO: support drag-and-drop.
96 connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
97 connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
99 connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
100 this, &MainWindow::playlist_selection_changed);
101 playlist_selection_changed(); // First time set-up.
103 preview_player = new Player(ui->preview_display, /*also_output_to_stream=*/false);
104 live_player = new Player(ui->live_display, /*also_output_to_stream=*/true);
105 live_player->set_done_callback([this]{
106 post_to_main_thread([this]{
107 live_player_clip_done();
112 void MainWindow::cue_in_clicked()
114 if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
115 cliplist_clips->back()->pts_in = current_pts;
119 clip.pts_in = current_pts;
120 cliplist_clips->add_clip(clip);
121 playlist_selection_changed();
124 void MainWindow::cue_out_clicked()
126 if (!cliplist_clips->empty()) {
127 cliplist_clips->back()->pts_out = current_pts;
128 // TODO: select the row in the clip list?
132 void MainWindow::queue_clicked()
134 if (cliplist_clips->empty()) {
138 QItemSelectionModel *selected = ui->clip_list->selectionModel();
139 if (!selected->hasSelection()) {
140 Clip clip = *cliplist_clips->back();
142 playlist_clips->add_clip(clip);
146 QModelIndex index = selected->currentIndex();
147 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
148 index.column() <= int(ClipList::Column::CAMERA_4)) {
149 Clip clip = *cliplist_clips->clip(index.row());
150 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
151 playlist_clips->add_clip(clip);
152 playlist_selection_changed();
156 void MainWindow::preview_clicked()
158 if (cliplist_clips->empty()) return;
160 QItemSelectionModel *selected = ui->clip_list->selectionModel();
161 if (!selected->hasSelection()) {
162 preview_player->play_clip(*cliplist_clips->back(), 0);
166 QModelIndex index = selected->currentIndex();
167 if (index.column() >= int(ClipList::Column::CAMERA_1) &&
168 index.column() <= int(ClipList::Column::CAMERA_4)) {
169 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
170 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
174 void MainWindow::preview_angle_clicked(unsigned stream_idx)
176 preview_player->override_angle(stream_idx);
178 // Change the selection if we were previewing a clip from the clip list.
179 // (The only other thing we could be showing is a pts scrub, and if so,
180 // that would be selected.)
181 QItemSelectionModel *selected = ui->clip_list->selectionModel();
182 if (selected->hasSelection()) {
183 QModelIndex cell = selected->selectedIndexes()[0];
184 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
185 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
189 void MainWindow::playlist_duplicate()
191 QItemSelectionModel *selected = ui->playlist->selectionModel();
192 if (!selected->hasSelection()) {
193 // Should have been grayed out, but OK.
196 QModelIndexList rows = selected->selectedRows();
197 int first = rows.front().row(), last = rows.back().row();
198 playlist_clips->duplicate_clips(first, last);
199 playlist_selection_changed();
202 void MainWindow::playlist_remove()
204 QItemSelectionModel *selected = ui->playlist->selectionModel();
205 if (!selected->hasSelection()) {
206 // Should have been grayed out, but OK.
209 QModelIndexList rows = selected->selectedRows();
210 int first = rows.front().row(), last = rows.back().row();
211 playlist_clips->erase_clips(first, last);
213 // TODO: select the next one in the list?
215 playlist_selection_changed();
218 void MainWindow::playlist_move(int delta)
220 QItemSelectionModel *selected = ui->playlist->selectionModel();
221 if (!selected->hasSelection()) {
222 // Should have been grayed out, but OK.
226 QModelIndexList rows = selected->selectedRows();
227 int first = rows.front().row(), last = rows.back().row();
228 if ((delta == -1 && first == 0) ||
229 (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
230 // Should have been grayed out, but OK.
234 playlist_clips->move_clips(first, last, delta);
235 playlist_selection_changed();
238 void MainWindow::play_clicked()
240 if (playlist_clips->empty()) return;
242 QItemSelectionModel *selected = ui->playlist->selectionModel();
244 if (!selected->hasSelection()) {
247 row = selected->selectedRows(0)[0].row();
250 const Clip &clip = *playlist_clips->clip(row);
251 live_player->play_clip(clip, clip.stream_idx);
252 playlist_clips->set_currently_playing(row);
253 playlist_selection_changed();
256 void MainWindow::live_player_clip_done()
258 int row = playlist_clips->get_currently_playing();
259 if (row != -1 && row < int(playlist_clips->size()) - 1) {
261 const Clip &clip = *playlist_clips->clip(row);
262 live_player->play_clip(clip, clip.stream_idx);
263 playlist_clips->set_currently_playing(row);
265 playlist_clips->set_currently_playing(-1);
269 void MainWindow::resizeEvent(QResizeEvent *event)
271 QMainWindow::resizeEvent(event);
273 // Ask for a relayout, but only after the event loop is done doing relayout
274 // on everything else.
275 QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
278 void MainWindow::relayout()
280 ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
283 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
285 constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
286 constexpr int scrub_sensitivity = 100; // pts units per pixel.
287 constexpr int wheel_sensitivity = 100; // pts units per degree.
288 constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
290 unsigned stream_idx = ui->preview_display->get_stream_idx();
292 if (event->type() != QEvent::Wheel) {
293 last_mousewheel_camera_row = -1;
296 if (event->type() == QEvent::MouseButtonPress) {
297 QMouseEvent *mouse = (QMouseEvent *)event;
299 QTableView *destination;
302 if (watched == ui->clip_list->viewport()) {
303 destination = ui->clip_list;
304 type = SCRUBBING_CLIP_LIST;
305 } else if (watched == ui->playlist->viewport()) {
306 destination = ui->playlist;
307 type = SCRUBBING_PLAYLIST;
311 int column = destination->columnAt(mouse->x());
312 int row = destination->rowAt(mouse->y());
313 if (column == -1 || row == -1) return false;
315 if (type == SCRUBBING_CLIP_LIST) {
316 if (ClipList::Column(column) == ClipList::Column::IN) {
317 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
318 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
319 } else if (ClipList::Column(column) == ClipList::Column::OUT) {
320 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
321 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
326 if (PlayList::Column(column) == PlayList::Column::IN) {
327 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
328 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
329 } else if (PlayList::Column(column) == PlayList::Column::OUT) {
330 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
331 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
339 scrub_column = column;
340 scrub_x_origin = mouse->x();
342 } else if (event->type() == QEvent::MouseMove) {
344 QMouseEvent *mouse = (QMouseEvent *)event;
345 int offset = mouse->x() - scrub_x_origin;
347 if (offset >= dead_zone_pixels) {
348 adjusted_offset = offset - dead_zone_pixels;
349 } else if (offset < -dead_zone_pixels) {
350 adjusted_offset = offset + dead_zone_pixels;
355 int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
357 if (scrub_type == SCRUBBING_CLIP_LIST) {
358 ClipProxy clip = cliplist_clips->clip(scrub_row);
359 if (scrub_column == int(ClipList::Column::IN)) {
360 pts = std::max<int64_t>(pts, 0);
361 pts = std::min(pts, clip->pts_out);
363 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
365 pts = std::max(pts, clip->pts_in);
366 pts = std::min(pts, current_pts);
368 preview_single_frame(pts, stream_idx, LAST_BEFORE);
371 ClipProxy clip = playlist_clips->clip(scrub_row);
372 if (scrub_column == int(PlayList::Column::IN)) {
373 pts = std::max<int64_t>(pts, 0);
374 pts = std::min(pts, clip->pts_out);
376 preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
378 pts = std::max(pts, clip->pts_in);
379 pts = std::min(pts, current_pts);
381 preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
385 return true; // Don't use this mouse movement for selecting things.
387 } else if (event->type() == QEvent::Wheel) {
388 QWheelEvent *wheel = (QWheelEvent *)event;
390 QTableView *destination;
391 int in_column, out_column, camera_column;
392 if (watched == ui->clip_list->viewport()) {
393 destination = ui->clip_list;
394 in_column = int(ClipList::Column::IN);
395 out_column = int(ClipList::Column::OUT);
397 last_mousewheel_camera_row = -1;
398 } else if (watched == ui->playlist->viewport()) {
399 destination = ui->playlist;
400 in_column = int(PlayList::Column::IN);
401 out_column = int(PlayList::Column::OUT);
402 camera_column = int(PlayList::Column::CAMERA);
404 last_mousewheel_camera_row = -1;
407 int column = destination->columnAt(wheel->x());
408 int row = destination->rowAt(wheel->y());
409 if (column == -1 || row == -1) return false;
411 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
412 cliplist_clips->clip(row) : playlist_clips->clip(row);
413 if (watched == ui->playlist->viewport()) {
414 stream_idx = clip->stream_idx;
417 if (column != camera_column) {
418 last_mousewheel_camera_row = -1;
420 if (column == in_column) {
421 int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
422 pts = std::max<int64_t>(pts, 0);
423 pts = std::min(pts, clip->pts_out);
425 preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
426 } else if (column == out_column) {
427 int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
428 pts = std::max(pts, clip->pts_in);
429 pts = std::min(pts, current_pts);
431 preview_single_frame(pts, stream_idx, LAST_BEFORE);
432 } else if (column == camera_column) {
433 int angle_degrees = wheel->angleDelta().y();
434 if (last_mousewheel_camera_row == row) {
435 angle_degrees += leftover_angle_degrees;
438 int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
439 stream_idx = std::max(stream_idx, 0);
440 stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
441 clip->stream_idx = stream_idx;
443 last_mousewheel_camera_row = row;
444 leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
446 // Don't update the live view, that's rarely what the operator wants.
448 } else if (event->type() == QEvent::MouseButtonRelease) {
454 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
456 if (rounding == LAST_BEFORE) {
457 lock_guard<mutex> lock(frame_mu);
458 if (frames[stream_idx].empty()) return;
459 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
460 if (it != frames[stream_idx].end()) {
464 assert(rounding == FIRST_AT_OR_AFTER);
465 lock_guard<mutex> lock(frame_mu);
466 if (frames[stream_idx].empty()) return;
467 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
468 if (it != frames[stream_idx].end()) {
474 fake_clip.pts_in = pts;
475 fake_clip.pts_out = pts + 1;
476 preview_player->play_clip(fake_clip, stream_idx);
479 void MainWindow::playlist_selection_changed()
481 QItemSelectionModel *selected = ui->playlist->selectionModel();
482 bool any_selected = selected->hasSelection();
483 ui->playlist_duplicate_btn->setEnabled(any_selected);
484 ui->playlist_remove_btn->setEnabled(any_selected);
485 ui->playlist_move_up_btn->setEnabled(
486 any_selected && selected->selectedRows().front().row() > 0);
487 ui->playlist_move_down_btn->setEnabled(
488 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
489 ui->play_btn->setEnabled(!playlist_clips->empty());