+static string format_duration(double t)
+{
+ int t_ms = lrint(t * 1e3);
+
+ int ms = t_ms % 1000;
+ t_ms /= 1000;
+ int s = t_ms % 60;
+ t_ms /= 60;
+ int m = t_ms;
+
+ char buf[256];
+ snprintf(buf, sizeof(buf), "%d:%02d.%03d", m, s, ms);
+ return buf;
+}
+
+void MainWindow::live_player_clip_progress(const map<size_t, double> &progress)
+{
+ playlist_clips->set_progress(progress);
+
+ // Look at the last clip and then start counting from there.
+ assert(!progress.empty());
+ auto last_it = progress.end();
+ --last_it;
+ double remaining = 0.0;
+ double last_fade_time_seconds = 0.0;
+ for (size_t row = last_it->first; row < playlist_clips->size(); ++row) {
+ const Clip clip = *playlist_clips->clip(row);
+ double clip_length = double(clip.pts_out - clip.pts_in) / TIMEBASE / 0.5; // FIXME: stop hardcoding speed.
+ if (row == last_it->first) {
+ // A clip we're playing: Subtract the part we've already played.
+ remaining = clip_length * (1.0 - last_it->second);
+ } else {
+ // A clip we haven't played yet: Subtract the part that's overlapping
+ // with a previous clip (due to fade).
+ remaining += max(clip_length - last_fade_time_seconds, 0.0);
+ }
+ last_fade_time_seconds = min(clip_length, clip.fade_time_seconds);
+ }
+ set_output_status(format_duration(remaining) + " left");
+}
+
+void MainWindow::resizeEvent(QResizeEvent *event)
+{
+ QMainWindow::resizeEvent(event);
+
+ // Ask for a relayout, but only after the event loop is done doing relayout
+ // on everything else.
+ QMetaObject::invokeMethod(this, "relayout", Qt::QueuedConnection);
+}
+
+void MainWindow::relayout()
+{
+ ui->live_display->setMinimumWidth(ui->live_display->height() * 16 / 9);
+ ui->preview_display->setMinimumWidth(ui->preview_display->height() * 16 / 9);
+}
+
+void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip)
+{
+ pts = std::max<int64_t>(pts, 0);
+ if (clip->pts_out == -1) {
+ pts = std::min(pts, current_pts);
+ } else {
+ pts = std::min(pts, clip->pts_out);
+ }
+ clip->pts_in = pts;
+}
+
+bool MainWindow::eventFilter(QObject *watched, QEvent *event)
+{
+ constexpr int dead_zone_pixels = 3; // To avoid that simple clicks get misinterpreted.
+ constexpr int scrub_sensitivity = 100; // pts units per pixel.
+ constexpr int wheel_sensitivity = 100; // pts units per degree.
+ constexpr int camera_degrees_per_pixel = 15; // One click of most mice.
+
+ unsigned stream_idx = ui->preview_display->get_stream_idx();
+
+ if (watched == ui->clip_list) {
+ if (event->type() == QEvent::FocusOut) {
+ highlight_camera_input(-1);
+ }
+ return false;
+ }
+
+ if (event->type() != QEvent::Wheel) {
+ last_mousewheel_camera_row = -1;
+ }
+
+ if (event->type() == QEvent::MouseButtonPress) {
+ QMouseEvent *mouse = (QMouseEvent *)event;
+
+ QTableView *destination;
+ ScrubType type;
+
+ if (watched == ui->clip_list->viewport()) {
+ destination = ui->clip_list;
+ type = SCRUBBING_CLIP_LIST;
+ } else if (watched == ui->playlist->viewport()) {
+ destination = ui->playlist;
+ type = SCRUBBING_PLAYLIST;
+ } else {
+ return false;
+ }
+ int column = destination->columnAt(mouse->x());
+ int row = destination->rowAt(mouse->y());
+ if (column == -1 || row == -1)
+ return false;
+
+ if (type == SCRUBBING_CLIP_LIST) {
+ if (ClipList::Column(column) == ClipList::Column::IN) {
+ scrub_pts_origin = cliplist_clips->clip(row)->pts_in;
+ preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
+ } else if (ClipList::Column(column) == ClipList::Column::OUT) {
+ scrub_pts_origin = cliplist_clips->clip(row)->pts_out;
+ preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
+ } else {
+ return false;
+ }
+ } else {
+ if (PlayList::Column(column) == PlayList::Column::IN) {
+ scrub_pts_origin = playlist_clips->clip(row)->pts_in;
+ preview_single_frame(scrub_pts_origin, stream_idx, FIRST_AT_OR_AFTER);
+ } else if (PlayList::Column(column) == PlayList::Column::OUT) {
+ scrub_pts_origin = playlist_clips->clip(row)->pts_out;
+ preview_single_frame(scrub_pts_origin, stream_idx, LAST_BEFORE);
+ } else {
+ return false;
+ }
+ }
+
+ scrubbing = true;
+ scrub_row = row;
+ scrub_column = column;
+ scrub_x_origin = mouse->x();
+ scrub_type = type;
+ } else if (event->type() == QEvent::MouseMove) {
+ if (scrubbing) {
+ QMouseEvent *mouse = (QMouseEvent *)event;
+ int offset = mouse->x() - scrub_x_origin;
+ int adjusted_offset;
+ if (offset >= dead_zone_pixels) {
+ adjusted_offset = offset - dead_zone_pixels;
+ } else if (offset < -dead_zone_pixels) {
+ adjusted_offset = offset + dead_zone_pixels;
+ } else {
+ adjusted_offset = 0;
+ }
+
+ int64_t pts = scrub_pts_origin + adjusted_offset * scrub_sensitivity;
+ currently_deferring_model_changes = true;
+ if (scrub_type == SCRUBBING_CLIP_LIST) {
+ ClipProxy clip = cliplist_clips->mutable_clip(scrub_row);
+ if (scrub_column == int(ClipList::Column::IN)) {
+ current_change_id = "cliplist:in:" + to_string(scrub_row);
+ set_pts_in(pts, current_pts, clip);
+ preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
+ } else {
+ current_change_id = "cliplist:out" + to_string(scrub_row);
+ pts = std::max(pts, clip->pts_in);
+ pts = std::min(pts, current_pts);
+ clip->pts_out = pts;
+ preview_single_frame(pts, stream_idx, LAST_BEFORE);
+ }
+ } else {
+ ClipProxy clip = playlist_clips->mutable_clip(scrub_row);
+ if (scrub_column == int(PlayList::Column::IN)) {
+ current_change_id = "playlist:in:" + to_string(scrub_row);
+ set_pts_in(pts, current_pts, clip);
+ preview_single_frame(pts, clip->stream_idx, FIRST_AT_OR_AFTER);
+ } else {
+ current_change_id = "playlist:out:" + to_string(scrub_row);
+ pts = std::max(pts, clip->pts_in);
+ pts = std::min(pts, current_pts);
+ clip->pts_out = pts;
+ preview_single_frame(pts, clip->stream_idx, LAST_BEFORE);
+ }
+ }
+ currently_deferring_model_changes = false;
+
+ return true; // Don't use this mouse movement for selecting things.
+ }
+ } else if (event->type() == QEvent::Wheel) {
+ QWheelEvent *wheel = (QWheelEvent *)event;
+
+ QTableView *destination;
+ int in_column, out_column, camera_column;
+ if (watched == ui->clip_list->viewport()) {
+ destination = ui->clip_list;
+ in_column = int(ClipList::Column::IN);
+ out_column = int(ClipList::Column::OUT);
+ camera_column = -1;
+ last_mousewheel_camera_row = -1;
+ } else if (watched == ui->playlist->viewport()) {
+ destination = ui->playlist;
+ in_column = int(PlayList::Column::IN);
+ out_column = int(PlayList::Column::OUT);
+ camera_column = int(PlayList::Column::CAMERA);
+ } else {
+ last_mousewheel_camera_row = -1;
+ return false;
+ }
+ int column = destination->columnAt(wheel->x());
+ int row = destination->rowAt(wheel->y());
+ if (column == -1 || row == -1) return false;
+
+ // Only adjust pts with the wheel if the given row is selected.
+ if (!destination->hasFocus() ||
+ row != destination->selectionModel()->currentIndex().row()) {
+ return false;
+ }
+
+ currently_deferring_model_changes = true;
+ {
+ current_change_id = (watched == ui->clip_list->viewport()) ? "cliplist:" : "playlist:";
+ ClipProxy clip = (watched == ui->clip_list->viewport()) ?
+ cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row);
+ if (watched == ui->playlist->viewport()) {
+ stream_idx = clip->stream_idx;
+ }
+
+ if (column != camera_column) {
+ last_mousewheel_camera_row = -1;
+ }
+ if (column == in_column) {
+ current_change_id += "in:" + to_string(row);
+ int64_t pts = clip->pts_in + wheel->angleDelta().y() * wheel_sensitivity;
+ set_pts_in(pts, current_pts, clip);
+ preview_single_frame(pts, stream_idx, FIRST_AT_OR_AFTER);
+ } else if (column == out_column) {
+ current_change_id += "out:" + to_string(row);
+ int64_t pts = clip->pts_out + wheel->angleDelta().y() * wheel_sensitivity;
+ pts = std::max(pts, clip->pts_in);
+ pts = std::min(pts, current_pts);
+ clip->pts_out = pts;
+ preview_single_frame(pts, stream_idx, LAST_BEFORE);
+ } else if (column == camera_column) {
+ current_change_id += "camera:" + to_string(row);
+ int angle_degrees = wheel->angleDelta().y();
+ if (last_mousewheel_camera_row == row) {
+ angle_degrees += leftover_angle_degrees;
+ }
+
+ int stream_idx = clip->stream_idx + angle_degrees / camera_degrees_per_pixel;
+ stream_idx = std::max(stream_idx, 0);
+ stream_idx = std::min(stream_idx, NUM_CAMERAS - 1);
+ clip->stream_idx = stream_idx;
+
+ last_mousewheel_camera_row = row;
+ leftover_angle_degrees = angle_degrees % camera_degrees_per_pixel;
+
+ // Don't update the live view, that's rarely what the operator wants.
+ }