]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Make it possible to change the camera angle in the playlist.
[nageru] / mainwindow.cpp
1 #include "mainwindow.h"
2
3 #include "clip_list.h"
4 #include "player.h"
5 #include "post_to_main_thread.h"
6 #include "ui_mainwindow.h"
7
8 #include <string>
9 #include <vector>
10
11 #include <QMouseEvent>
12 #include <QWheelEvent>
13 #include <QShortcut>
14
15 using namespace std;
16
17 MainWindow *global_mainwindow = nullptr;
18 ClipList *cliplist_clips;
19 PlayList *playlist_clips;
20
21 extern int64_t current_pts;
22 extern mutex frame_mu;
23 extern vector<int64_t> frames[MAX_STREAMS];
24
25 MainWindow::MainWindow()
26         : ui(new Ui::MainWindow)
27 {
28         global_mainwindow = this;
29         ui->setupUi(this);
30
31         cliplist_clips = new ClipList();
32         ui->clip_list->setModel(cliplist_clips);
33
34         playlist_clips = new PlayList();
35         ui->playlist->setModel(playlist_clips);
36
37         // For scrubbing in the pts columns.
38         ui->clip_list->viewport()->installEventFilter(this);
39         ui->playlist->viewport()->installEventFilter(this);
40
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);
44
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);
48
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);
52
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);
56
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);
60
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->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
64
65         QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
66         connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
67         connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
68
69         QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
70         connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
71         connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
72
73         QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
74         connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
75         connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
76
77         connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
78
79         // TODO: support the delete key iff the widget has focus?
80         connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove);
81
82         // TODO: support drag-and-drop.
83         connect(ui->playlist_move_up_btn, &QPushButton::clicked, [this]{ playlist_move(-1); });
84         connect(ui->playlist_move_down_btn, &QPushButton::clicked, [this]{ playlist_move(1); });
85
86         connect(ui->playlist->selectionModel(), &QItemSelectionModel::selectionChanged,
87                 this, &MainWindow::playlist_selection_changed);
88         playlist_selection_changed();  // First time set-up.
89
90         preview_player = new Player(ui->preview_display);
91         live_player = new Player(ui->live_display);
92         live_player->set_done_callback([this]{
93                 post_to_main_thread([this]{
94                         live_player_clip_done();
95                 });
96         });
97 }
98
99 void MainWindow::cue_in_clicked()
100 {
101         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
102                 cliplist_clips->back()->pts_in = current_pts;
103                 return;
104         }
105         Clip clip;
106         clip.pts_in = current_pts;
107         cliplist_clips->add_clip(clip);
108         playlist_selection_changed();
109 }
110
111 void MainWindow::cue_out_clicked()
112 {
113         if (!cliplist_clips->empty()) {
114                 cliplist_clips->back()->pts_out = current_pts;
115                 // TODO: select the row in the clip list?
116         }
117 }
118
119 void MainWindow::queue_clicked()
120 {
121         if (cliplist_clips->empty()) {
122                 return;
123         }
124
125         QItemSelectionModel *selected = ui->clip_list->selectionModel();
126         if (!selected->hasSelection()) {
127                 Clip clip = *cliplist_clips->back();
128                 clip.stream_idx = 0;
129                 playlist_clips->add_clip(clip);
130                 return;
131         }
132
133         QModelIndex index = selected->currentIndex();
134         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
135             index.column() <= int(ClipList::Column::CAMERA_4)) {
136                 Clip clip = *cliplist_clips->clip(index.row());
137                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
138                 playlist_clips->add_clip(clip);
139                 playlist_selection_changed();
140         }
141 }
142
143 void MainWindow::preview_clicked()
144 {
145         if (cliplist_clips->empty()) return;
146
147         QItemSelectionModel *selected = ui->clip_list->selectionModel();
148         if (!selected->hasSelection()) {
149                 preview_player->play_clip(*cliplist_clips->back(), 0);
150                 return;
151         }
152
153         QModelIndex index = selected->currentIndex();
154         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
155             index.column() <= int(ClipList::Column::CAMERA_4)) {
156                 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
157                 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
158         }
159 }
160
161 void MainWindow::preview_angle_clicked(unsigned stream_idx)
162 {
163         preview_player->override_angle(stream_idx);
164
165         // Change the selection if we were previewing a clip from the clip list.
166         // (The only other thing we could be showing is a pts scrub, and if so,
167         // that would be selected.)
168         QItemSelectionModel *selected = ui->clip_list->selectionModel();
169         if (selected->hasSelection()) {
170                 QModelIndex cell = selected->selectedIndexes()[0];
171                 int column = int(ClipList::Column::CAMERA_1) + stream_idx;
172                 selected->setCurrentIndex(cell.sibling(cell.row(), column), QItemSelectionModel::ClearAndSelect);
173         }
174 }
175
176 void MainWindow::playlist_duplicate()
177 {
178         QItemSelectionModel *selected = ui->playlist->selectionModel();
179         if (!selected->hasSelection()) {
180                 // Should have been grayed out, but OK.
181                 return;
182         }
183         QModelIndexList rows = selected->selectedRows();
184         int first = rows.front().row(), last = rows.back().row();
185         playlist_clips->duplicate_clips(first, last);
186         playlist_selection_changed();
187 }
188
189 void MainWindow::playlist_remove()
190 {
191         QItemSelectionModel *selected = ui->playlist->selectionModel();
192         if (!selected->hasSelection()) {
193                 // Should have been grayed out, but OK.
194                 return;
195         }
196         QModelIndexList rows = selected->selectedRows();
197         int first = rows.front().row(), last = rows.back().row();
198         playlist_clips->erase_clips(first, last);
199
200         // TODO: select the next one in the list?
201
202         playlist_selection_changed();
203 }
204
205 void MainWindow::playlist_move(int delta)
206 {
207         QItemSelectionModel *selected = ui->playlist->selectionModel();
208         if (!selected->hasSelection()) {
209                 // Should have been grayed out, but OK.
210                 return;
211         }
212
213         QModelIndexList rows = selected->selectedRows();
214         int first = rows.front().row(), last = rows.back().row();
215         if ((delta == -1 && first == 0) ||
216             (delta == 1 && size_t(last) == playlist_clips->size() - 1)) {
217                 // Should have been grayed out, but OK.
218                 return;
219         }
220
221         playlist_clips->move_clips(first, last, delta);
222         playlist_selection_changed();
223 }
224
225 void MainWindow::play_clicked()
226 {
227         if (playlist_clips->empty()) return;
228
229         QItemSelectionModel *selected = ui->playlist->selectionModel();
230         int row;
231         if (!selected->hasSelection()) {
232                 row = 0;
233         } else {
234                 row = selected->selectedRows(0)[0].row();
235         }
236
237         const Clip &clip = *playlist_clips->clip(row);
238         live_player->play_clip(clip, clip.stream_idx);
239         playlist_clips->set_currently_playing(row);
240         playlist_selection_changed();
241 }
242
243 void MainWindow::live_player_clip_done()
244 {
245         int row = playlist_clips->get_currently_playing();
246         if (row != -1 && row < int(playlist_clips->size()) - 1) {
247                 ++row;
248                 const Clip &clip = *playlist_clips->clip(row);
249                 live_player->play_clip(clip, clip.stream_idx);
250                 playlist_clips->set_currently_playing(row);
251         } else {
252                 playlist_clips->set_currently_playing(-1);
253         }
254 }
255
256 void MainWindow::resizeEvent(QResizeEvent *event)
257 {
258         QMainWindow::resizeEvent(event);
259
260         // Ask for a relayout, but only after the event loop is done doing relayout
261         // on everything else.
262         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
263 }
264
265 void MainWindow::relayout()
266 {
267         ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
268 }
269
270 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
271 {
272         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
273         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
274         constexpr int wheel_sensitivity = 100;  // pts units per degree.
275         constexpr int camera_degrees_per_pixel = 15;  // One click of most mice.
276
277         unsigned stream_idx = ui->preview_display->get_stream_idx();
278
279         if (event->type() != QEvent::Wheel) {
280                 last_mousewheel_camera_row = -1;
281         }
282
283         if (event->type() == QEvent::MouseButtonPress) {
284                 QMouseEvent *mouse = (QMouseEvent *)event;
285
286                 QTableView *destination;
287                 ScrubType type;
288
289                 if (watched == ui->clip_list->viewport()) {
290                         destination = ui->clip_list;
291                         type = SCRUBBING_CLIP_LIST;
292                 } else if (watched == ui->playlist->viewport()) {
293                         destination = ui->playlist;
294                         type = SCRUBBING_PLAYLIST;
295                 } else {
296                         return false;
297                 }
298                 int column = destination->columnAt(mouse->x());
299                 int row = destination->rowAt(mouse->y());
300                 if (column == -1 || row == -1) return false;
301
302                 if (type == SCRUBBING_CLIP_LIST) {
303                         if (ClipList::Column(column) == ClipList::Column::IN) {
304                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
305                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
306                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
307                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
308                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
309                         } else {
310                                 return false;
311                         }
312                 } else {
313                         if (PlayList::Column(column) == PlayList::Column::IN) {
314                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
315                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
316                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
317                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
318                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
319                         } else {
320                                 return false;
321                         }
322                 }
323
324                 scrubbing = true;
325                 scrub_row = row;
326                 scrub_column = column;
327                 scrub_x_origin = mouse->x();
328                 scrub_type = type;
329         } else if (event->type() == QEvent::MouseMove) {
330                 if (scrubbing) {
331                         QMouseEvent *mouse = (QMouseEvent *)event;
332                         int offset = mouse->x() - scrub_x_origin;
333                         int adjusted_offset;
334                         if (offset >= dead_zone_pixels) {
335                                 adjusted_offset = offset - dead_zone_pixels;
336                         } else if (offset < -dead_zone_pixels) {
337                                 adjusted_offset = offset + dead_zone_pixels;
338                         } else {
339                                 adjusted_offset = 0;
340                         }
341
342                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
343
344                         if (scrub_type == SCRUBBING_CLIP_LIST) {
345                                 ClipProxy clip = cliplist_clips->clip(scrub_row);
346                                 if (scrub_column == int(ClipList::Column::IN)) {
347                                         pts = std::max<int64_t>(pts, 0);
348                                         pts = std::min(pts, clip->pts_out);
349                                         clip->pts_in = pts;
350                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
351                                 } else {
352                                         pts = std::max(pts, clip->pts_in);
353                                         pts = std::min(pts, current_pts);
354                                         clip->pts_out = pts;
355                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
356                                 }
357                         } else {
358                                 ClipProxy clip = playlist_clips->clip(scrub_row);
359                                 if (scrub_column == int(PlayList::Column::IN)) {
360                                         pts = std::max<int64_t>(pts, 0);
361                                         pts = std::min(pts, clip->pts_out);
362                                         clip->pts_in = pts;
363                                         preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
364                                 } else {
365                                         pts = std::max(pts, clip->pts_in);
366                                         pts = std::min(pts, current_pts);
367                                         clip->pts_out = pts;
368                                         preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
369                                 }
370                         }
371
372                         return true;  // Don't use this mouse movement for selecting things.
373                 }
374         } else if (event->type() == QEvent::Wheel) {
375                 QWheelEvent *wheel = (QWheelEvent *)event;
376
377                 QTableView *destination;
378                 int in_column, out_column, camera_column;
379                 if (watched == ui->clip_list->viewport()) {
380                         destination = ui->clip_list;
381                         in_column = int(ClipList::Column::IN);
382                         out_column = int(ClipList::Column::OUT);
383                         camera_column = -1;
384                         last_mousewheel_camera_row = -1;
385                 } else if (watched == ui->playlist->viewport()) {
386                         destination = ui->playlist;
387                         in_column = int(PlayList::Column::IN);
388                         out_column = int(PlayList::Column::OUT);
389                         camera_column = int(PlayList::Column::CAMERA);
390                 } else {
391                         last_mousewheel_camera_row = -1;
392                         return false;
393                 }
394                 int column = destination->columnAt(wheel->x());
395                 int row = destination->rowAt(wheel->y());
396                 if (column == -1 || row == -1) return false;
397
398                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
399                         cliplist_clips->clip(row) : playlist_clips->clip(row);
400                 if (watched == ui->playlist->viewport()) {
401                         stream_idx = clip->stream_idx;
402                 }
403
404                 if (column != camera_column) {
405                         last_mousewheel_camera_row = -1;
406                 }
407                 if (column == in_column) {
408                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
409                         pts = std::max<int64_t>(pts, 0);
410                         pts = std::min(pts, clip->pts_out);
411                         clip->pts_in = pts;
412                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
413                 } else if (column == out_column) {
414                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
415                         pts = std::max(pts, clip->pts_in);
416                         pts = std::min(pts, current_pts);
417                         clip->pts_out = pts;
418                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
419                 } else if (column == camera_column) {
420                         int angle_degrees = wheel->angleDelta().y();
421                         if (last_mousewheel_camera_row == row) {
422                                 angle_degrees += leftover_angle_degrees;
423                         }
424
425                         int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
426                         stream_idx = std::max(stream_idx, 0);
427                         stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
428                         clip->stream_idx = stream_idx;
429
430                         last_mousewheel_camera_row = row;
431                         leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
432
433                         // Don't update the live view, that's rarely what the operator wants.
434                 }
435         } else if (event->type() == QEvent::MouseButtonRelease) {
436                 scrubbing = false;
437         }
438         return false;
439 }
440
441 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
442 {
443         if (rounding == LAST_BEFORE) {
444                 lock_guard<mutex> lock(frame_mu);
445                 if (frames[stream_idx].empty()) return;
446                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
447                 if (it != frames[stream_idx].end()) {
448                         pts = *it;
449                 }
450         } else {
451                 assert(rounding == FIRST_AT_OR_AFTER);
452                 lock_guard<mutex> lock(frame_mu);
453                 if (frames[stream_idx].empty()) return;
454                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
455                 if (it != frames[stream_idx].end()) {
456                         pts = *it;
457                 }
458         }
459
460         Clip fake_clip;
461         fake_clip.pts_in = pts;
462         fake_clip.pts_out = pts + 1;
463         preview_player->play_clip(fake_clip, stream_idx);
464 }
465
466 void MainWindow::playlist_selection_changed()
467 {
468         QItemSelectionModel *selected = ui->playlist->selectionModel();
469         bool any_selected = selected->hasSelection();
470         ui->playlist_duplicate_btn->setEnabled(any_selected);
471         ui->playlist_remove_btn->setEnabled(any_selected);
472         ui->playlist_move_up_btn->setEnabled(
473                 any_selected && selected->selectedRows().front().row() > 0);
474         ui->playlist_move_down_btn->setEnabled(
475                 any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1);
476         ui->play_btn->setEnabled(!playlist_clips->empty());
477 }