]> git.sesse.net Git - nageru/blob - mainwindow.cpp
Move some lambdas out into functions.
[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         preview_player = new Player(ui->preview_display);
62         live_player = new Player(ui->live_display);
63         live_player->set_done_callback([this]{
64                 post_to_main_thread([this]{
65                         live_player_clip_done();
66                 });
67         });
68 }
69
70 void MainWindow::cue_in_clicked()
71 {
72         if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) {
73                 cliplist_clips->back()->pts_in = current_pts;
74                 return;
75         }
76         Clip clip;
77         clip.pts_in = current_pts;
78         cliplist_clips->add_clip(clip);
79 }
80
81 void MainWindow::cue_out_clicked()
82 {
83         if (!cliplist_clips->empty()) {
84                 cliplist_clips->back()->pts_out = current_pts;
85                 // TODO: select the row in the clip list?
86         }
87 }
88
89 void MainWindow::queue_clicked()
90 {
91         QItemSelectionModel *selected = ui->clip_list->selectionModel();
92         if (!selected->hasSelection()) {
93                 Clip clip = *cliplist_clips->back();
94                 clip.stream_idx = 0;
95                 playlist_clips->add_clip(clip);
96                 return;
97         }
98
99         QModelIndex index = selected->currentIndex();
100         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
101             index.column() <= int(ClipList::Column::CAMERA_4)) {
102                 Clip clip = *cliplist_clips->clip(index.row());
103                 clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
104                 playlist_clips->add_clip(clip);
105         }
106 }
107
108 void MainWindow::preview_clicked()
109 {
110         if (cliplist_clips->empty()) return;
111
112         QItemSelectionModel *selected = ui->clip_list->selectionModel();
113         if (!selected->hasSelection()) {
114                 preview_player->play_clip(*cliplist_clips->back(), 0);
115                 return;
116         }
117
118         QModelIndex index = selected->currentIndex();
119         if (index.column() >= int(ClipList::Column::CAMERA_1) &&
120             index.column() <= int(ClipList::Column::CAMERA_4)) {
121                 unsigned stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
122                 preview_player->play_clip(*cliplist_clips->clip(index.row()), stream_idx);
123         }
124 }
125
126 void MainWindow::play_clicked()
127 {
128         if (playlist_clips->empty()) return;
129
130         QItemSelectionModel *selected = ui->playlist->selectionModel();
131         int row;
132         if (!selected->hasSelection()) {
133                 row = 0;
134         } else {
135                 row = selected->selectedRows(0)[0].row();
136         }
137
138         const Clip &clip = *playlist_clips->clip(row);
139         live_player->play_clip(clip, clip.stream_idx);
140         playlist_clips->set_currently_playing(row);
141 }
142
143 void MainWindow::live_player_clip_done()
144 {
145         int row = playlist_clips->get_currently_playing();
146         if (row != -1 && row < int(playlist_clips->size()) - 1) {
147                 ++row;
148                 const Clip &clip = *playlist_clips->clip(row);
149                 live_player->play_clip(clip, clip.stream_idx);
150                 playlist_clips->set_currently_playing(row);
151         } else {
152                 playlist_clips->set_currently_playing(-1);
153         }
154 }
155
156 void MainWindow::resizeEvent(QResizeEvent *event)
157 {
158         QMainWindow::resizeEvent(event);
159
160         // Ask for a relayout, but only after the event loop is done doing relayout
161         // on everything else.
162         QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
163 }
164
165 void MainWindow::relayout()
166 {
167         ui->live_display->setMinimumHeight(ui->live_display->width() * 9 / 16);
168 }
169
170 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
171 {
172         constexpr int dead_zone_pixels = 3;  // To avoid that simple clicks get misinterpreted.
173         constexpr int scrub_sensitivity = 100;  // pts units per pixel.
174         constexpr int wheel_sensitivity = 100;  // pts units per degree.
175
176         unsigned stream_idx = ui->preview_display->get_stream_idx();
177
178         if (event->type() == QEvent::MouseButtonPress) {
179                 QMouseEvent *mouse = (QMouseEvent *)event;
180
181                 QTableView *destination;
182                 ScrubType type;
183
184                 if (watched == ui->clip_list->viewport()) {
185                         destination = ui->clip_list;
186                         type = SCRUBBING_CLIP_LIST;
187                 } else if (watched == ui->playlist->viewport()) {
188                         destination = ui->playlist;
189                         type = SCRUBBING_PLAYLIST;
190                 } else {
191                         return false;
192                 }
193                 int column = destination->columnAt(mouse->x());
194                 int row = destination->rowAt(mouse->y());
195                 if (column == -1 || row == -1) return false;
196
197                 if (type == SCRUBBING_CLIP_LIST) {
198                         if (ClipList::Column(column) == ClipList::Column::IN) {
199                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
200                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
201                         } else if (ClipList::Column(column) == ClipList::Column::OUT) {
202                                 scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
203                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
204                         } else {
205                                 return false;
206                         }
207                 } else {
208                         if (PlayList::Column(column) == PlayList::Column::IN) {
209                                 scrub_pts_origin = playlist_clips->clip(row)->pts_in;
210                                 preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
211                         } else if (PlayList::Column(column) == PlayList::Column::OUT) {
212                                 scrub_pts_origin = playlist_clips->clip(row)->pts_out;
213                                 preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
214                         } else {
215                                 return false;
216                         }
217                 }
218
219                 scrubbing = true;
220                 scrub_row = row;
221                 scrub_column = column;
222                 scrub_x_origin = mouse->x();
223                 scrub_type = type;
224         } else if (event->type() == QEvent::MouseMove) {
225                 if (scrubbing) {
226                         QMouseEvent *mouse = (QMouseEvent *)event;
227                         int offset = mouse->x() - scrub_x_origin;
228                         int adjusted_offset;
229                         if (offset >= dead_zone_pixels) {
230                                 adjusted_offset = offset - dead_zone_pixels;
231                         } else if (offset < -dead_zone_pixels) {
232                                 adjusted_offset = offset + dead_zone_pixels;
233                         } else {
234                                 adjusted_offset = 0;
235                         }
236
237                         int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
238
239                         if (scrub_type == SCRUBBING_CLIP_LIST) {
240                                 if (scrub_column == int(ClipList::Column::IN)) {
241                                         pts = std::max<int64_t>(pts, 0);
242                                         pts = std::min(pts, cliplist_clips->clip(scrub_row)->pts_out);
243                                         cliplist_clips->clip(scrub_row)->pts_in = pts;
244                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
245                                 } else {
246                                         pts = std::max(pts, cliplist_clips->clip(scrub_row)->pts_in);
247                                         pts = std::min(pts, current_pts);
248                                         cliplist_clips->clip(scrub_row)->pts_out = pts;
249                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
250                                 }
251                         } else {
252                                 if (scrub_column == int(PlayList::Column::IN)) {
253                                         pts = std::max<int64_t>(pts, 0);
254                                         pts = std::min(pts, playlist_clips->clip(scrub_row)->pts_out);
255                                         playlist_clips->clip(scrub_row)->pts_in = pts;
256                                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
257                                 } else {
258                                         pts = std::max(pts, playlist_clips->clip(scrub_row)->pts_in);
259                                         pts = std::min(pts, current_pts);
260                                         playlist_clips->clip(scrub_row)->pts_out = pts;
261                                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
262                                 }
263                         }
264
265                         return true;  // Don't use this mouse movement for selecting things.
266                 }
267         } else if (event->type() == QEvent::Wheel) {
268                 QWheelEvent *wheel = (QWheelEvent *)event;
269
270                 QTableView *destination;
271                 int in_column, out_column;
272                 if (watched == ui->clip_list->viewport()) {
273                         destination = ui->clip_list;
274                         in_column = int(ClipList::Column::IN);
275                         out_column = int(ClipList::Column::OUT);
276                 } else if (watched == ui->playlist->viewport()) {
277                         destination = ui->playlist;
278                         in_column = int(PlayList::Column::IN);
279                         out_column = int(PlayList::Column::OUT);
280                 } else {
281                         return false;
282                 }
283                 int column = destination->columnAt(wheel->x());
284                 int row = destination->rowAt(wheel->y());
285                 if (column == -1 || row == -1) return false;
286
287                 ClipProxy clip = (watched == ui->clip_list->viewport()) ?
288                         cliplist_clips->clip(row) : playlist_clips->clip(row);
289
290                 if (column == in_column) {
291                         int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
292                         pts = std::max<int64_t>(pts, 0);
293                         pts = std::min(pts, clip->pts_out);
294                         clip->pts_in = pts;
295                         preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
296                 } else if (column == out_column) {
297                         int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
298                         pts = std::max(pts, clip->pts_in);
299                         pts = std::min(pts, current_pts);
300                         clip->pts_out = pts;
301                         preview_single_frame(pts, stream_idx, LAST_BEFORE);
302                 }
303         } else if (event->type() == QEvent::MouseButtonRelease) {
304                 scrubbing = false;
305         }
306         return false;
307 }
308
309 void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWindow::Rounding rounding)
310 {
311         if (rounding == LAST_BEFORE) {
312                 lock_guard<mutex> lock(frame_mu);
313                 if (frames[stream_idx].empty()) return;
314                 auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts);
315                 if (it != frames[stream_idx].end()) {
316                         pts = *it;
317                 }
318         } else {
319                 assert(rounding == FIRST_AT_OR_AFTER);
320                 lock_guard<mutex> lock(frame_mu);
321                 if (frames[stream_idx].empty()) return;
322                 auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1);
323                 if (it != frames[stream_idx].end()) {
324                         pts = *it;
325                 }
326         }
327
328         Clip fake_clip;
329         fake_clip.pts_in = pts;
330         fake_clip.pts_out = pts + 1;
331         preview_player->play_clip(fake_clip, stream_idx);
332 }