X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=futatabi%2Fmainwindow.cpp;h=350b1b2966d5327b20f0980b21e3eda31de401d3;hp=3d3d8c11c908c13c53bd42a1bec5f3a9c26187e0;hb=a6f3a2275ad116e6ab338e583ab8ef1b1141b468;hpb=e64a84bb856263242278afa9770ae5d05e21b055 diff --git a/futatabi/mainwindow.cpp b/futatabi/mainwindow.cpp index 3d3d8c1..350b1b2 100644 --- a/futatabi/mainwindow.cpp +++ b/futatabi/mainwindow.cpp @@ -5,6 +5,7 @@ #include "flags.h" #include "frame_on_disk.h" #include "player.h" +#include "futatabi_midi_mapping.pb.h" #include "shared/aboutdialog.h" #include "shared/disk_space_estimator.h" #include "shared/post_to_main_thread.h" @@ -33,9 +34,25 @@ static PlayList *playlist_clips; extern int64_t current_pts; +namespace { + +void set_pts_in(int64_t pts, int64_t current_pts, ClipProxy &clip) +{ + pts = std::max(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; +} + +} // namespace + MainWindow::MainWindow() : ui(new Ui::MainWindow), - db(global_flags.working_directory + "/futatabi.db") + db(global_flags.working_directory + "/futatabi.db"), + midi_mapper(this) { global_mainwindow = this; ui->setupUi(this); @@ -189,12 +206,13 @@ MainWindow::MainWindow() live_player_done(); }); }); - live_player->set_progress_callback([this](const map &progress, double time_remaining) { + live_player->set_progress_callback([this](const map &progress, double time_remaining) { post_to_main_thread([this, progress, time_remaining] { live_player_clip_progress(progress, time_remaining); }); }); set_output_status("paused"); + enable_or_disable_queue_button(); defer_timeout = new QTimer(this); defer_timeout->setSingleShot(true); @@ -203,6 +221,7 @@ MainWindow::MainWindow() connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged, this, &MainWindow::clip_list_selection_changed); + enable_or_disable_queue_button(); // Find out how many cameras we have in the existing frames; // if none, we start with two cameras. @@ -220,6 +239,18 @@ MainWindow::MainWindow() if (!global_flags.tally_url.empty()) { start_tally(); } + + if (!global_flags.midi_mapping_filename.empty()) { + MIDIMappingProto midi_mapping; + if (!load_midi_mapping_from_file(global_flags.midi_mapping_filename, &midi_mapping)) { + fprintf(stderr, "Couldn't load MIDI mapping '%s'; exiting.\n", + global_flags.midi_mapping_filename.c_str()); + exit(1); + } + midi_mapper.set_midi_mapping(midi_mapping); + } + midi_mapper.refresh_lights(); + midi_mapper.start_thread(); } void MainWindow::change_num_cameras() @@ -273,25 +304,41 @@ void MainWindow::cue_in_clicked() { if (!cliplist_clips->empty() && cliplist_clips->back()->pts_out < 0) { cliplist_clips->mutable_back()->pts_in = current_pts; - return; + } else { + Clip clip; + clip.pts_in = max(current_pts - lrint(global_flags.cue_point_padding_seconds * TIMEBASE), 0); + cliplist_clips->add_clip(clip); + playlist_selection_changed(); } - Clip clip; - clip.pts_in = max(current_pts - lrint(global_flags.cue_point_padding_seconds * TIMEBASE), 0); - cliplist_clips->add_clip(clip); - playlist_selection_changed(); + + // Select the item so that we can jog it. + ui->clip_list->setFocus(); + QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::IN)); + ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); ui->clip_list->scrollToBottom(); + enable_or_disable_queue_button(); } void MainWindow::cue_out_clicked() { - if (!cliplist_clips->empty()) { - cliplist_clips->mutable_back()->pts_out = current_pts + lrint(global_flags.cue_point_padding_seconds * TIMEBASE); - // TODO: select the row in the clip list? + if (cliplist_clips->empty()) { + return; } + + cliplist_clips->mutable_back()->pts_out = current_pts + lrint(global_flags.cue_point_padding_seconds * TIMEBASE); + + // Select the item so that we can jog it. + ui->clip_list->setFocus(); + QModelIndex index = cliplist_clips->index(cliplist_clips->size() - 1, int(ClipList::Column::OUT)); + ui->clip_list->selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); + ui->clip_list->scrollToBottom(); + enable_or_disable_queue_button(); } void MainWindow::queue_clicked() { + // See also enable_or_disable_queue_button(). + if (cliplist_clips->empty()) { return; } @@ -330,6 +377,8 @@ void MainWindow::queue_clicked() void MainWindow::preview_clicked() { + // See also enable_or_disable_preview_button(). + if (ui->playlist->hasFocus()) { // Allow the playlist as preview iff it has focus and something is selected. QItemSelectionModel *selected = ui->playlist->selectionModel(); @@ -424,6 +473,64 @@ void MainWindow::playlist_move(int delta) playlist_selection_changed(); } +void MainWindow::jog_internal(JogDestination jog_destination, int row, int column, int stream_idx, int pts_delta) +{ + constexpr int camera_pts_per_pixel = 1500; // One click of most mice (15 degrees), multiplied by the default wheel_sensitivity. + + int in_column, out_column, camera_column; + if (jog_destination == JOG_CLIP_LIST) { + in_column = int(ClipList::Column::IN); + out_column = int(ClipList::Column::OUT); + camera_column = -1; + } else if (jog_destination == JOG_PLAYLIST) { + in_column = int(PlayList::Column::IN); + out_column = int(PlayList::Column::OUT); + camera_column = int(PlayList::Column::CAMERA); + } else { + assert(false); + } + + currently_deferring_model_changes = true; + { + current_change_id = (jog_destination == JOG_CLIP_LIST) ? "cliplist:" : "playlist:"; + ClipProxy clip = (jog_destination == JOG_CLIP_LIST) ? cliplist_clips->mutable_clip(row) : playlist_clips->mutable_clip(row); + if (jog_destination == JOG_PLAYLIST) { + stream_idx = clip->stream_idx; + } + + if (column == in_column) { + current_change_id += "in:" + to_string(row); + int64_t pts = clip->pts_in + pts_delta; + 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 + pts_delta; + 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 = pts_delta; + if (last_mousewheel_camera_row == row) { + angle_degrees += leftover_angle_degrees; + } + + int stream_idx = clip->stream_idx + angle_degrees / camera_pts_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_pts_per_pixel; + + // Don't update the live view, that's rarely what the operator wants. + } + } + currently_deferring_model_changes = false; +} + void MainWindow::defer_timer_expired() { state_changed(deferred_state); @@ -431,6 +538,14 @@ void MainWindow::defer_timer_expired() void MainWindow::content_changed() { + // If we are playing, update the part of the playlist that's not playing yet. + vector clips; + for (unsigned row = 0; row < playlist_clips->size(); ++row) { + clips.emplace_back(*playlist_clips->clip_with_id(row)); + } + live_player->splice_play(clips); + + // Serialize the state. if (defer_timeout->isActive() && (!currently_deferring_model_changes || deferred_change_id != current_change_id)) { // There's some deferred event waiting, but this event is unrelated. @@ -487,12 +602,13 @@ void MainWindow::play_clicked() start_row = selected->selectedRows(0)[0].row(); } - vector clips; + vector clips; for (unsigned row = start_row; row < playlist_clips->size(); ++row) { - clips.emplace_back(Player::ClipWithRow{ *playlist_clips->clip(row), row }); + clips.emplace_back(*playlist_clips->clip_with_id(row)); } live_player->play(clips); playlist_clips->set_progress({ { start_row, 0.0f } }); + ui->playlist->selectionModel()->clear(); playlist_selection_changed(); ui->stop_btn->setEnabled(true); @@ -503,32 +619,19 @@ void MainWindow::stop_clicked() Clip fake_clip; fake_clip.pts_in = 0; fake_clip.pts_out = 0; + playlist_clips->set_progress({}); live_player->play(fake_clip); + ui->stop_btn->setEnabled(false); } void MainWindow::live_player_done() { - set_output_status("paused"); + playlist_selection_changed(); playlist_clips->set_progress({}); ui->stop_btn->setEnabled(false); } -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 &progress, double time_remaining) +void MainWindow::live_player_clip_progress(const map &progress, double time_remaining) { playlist_clips->set_progress(progress); set_output_status(format_duration(time_remaining) + " left"); @@ -549,24 +652,17 @@ void MainWindow::relayout() 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(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 camera_degrees_per_pixel = 15; // One click of most mice. int scrub_sensitivity = 100; // pts units per pixel. int wheel_sensitivity = 100; // pts units per degree. + if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) { + enable_or_disable_preview_button(); + hidden_jog_column = -1; + } + unsigned stream_idx = ui->preview_display->get_stream_idx(); if (watched == ui->clip_list) { @@ -695,22 +791,22 @@ bool MainWindow::eventFilter(QObject *watched, QEvent *event) } QTableView *destination; - int in_column, out_column, camera_column; + JogDestination jog_destination; 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; + jog_destination = JOG_CLIP_LIST; 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); + jog_destination = JOG_PLAYLIST; + if (destination->columnAt(wheel->x()) != int(PlayList::Column::CAMERA)) { + last_mousewheel_camera_row = -1; + } } 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) @@ -722,48 +818,7 @@ bool MainWindow::eventFilter(QObject *watched, QEvent *event) 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 + angle_delta * 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 + angle_delta * 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 = angle_delta; - 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. - } - } - currently_deferring_model_changes = false; + jog_internal(jog_destination, row, column, stream_idx, angle_delta * wheel_sensitivity); return true; // Don't scroll. } else if (event->type() == QEvent::MouseButtonRelease) { scrubbing = false; @@ -795,11 +850,14 @@ void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWind Clip fake_clip; fake_clip.pts_in = pts; fake_clip.pts_out = pts + 1; + fake_clip.stream_idx = stream_idx; preview_player->play(fake_clip); } void MainWindow::playlist_selection_changed() { + enable_or_disable_preview_button(); + QItemSelectionModel *selected = ui->playlist->selectionModel(); bool any_selected = selected->hasSelection(); ui->playlist_duplicate_btn->setEnabled(any_selected); @@ -808,27 +866,39 @@ void MainWindow::playlist_selection_changed() any_selected && selected->selectedRows().front().row() > 0); ui->playlist_move_down_btn->setEnabled( any_selected && selected->selectedRows().back().row() < int(playlist_clips->size()) - 1); + ui->play_btn->setEnabled(!playlist_clips->empty()); + midi_mapper.set_play_enabled(!playlist_clips->empty()); if (!any_selected) { set_output_status("paused"); } else { - vector clips; + vector clips; for (size_t row = selected->selectedRows().front().row(); row < playlist_clips->size(); ++row) { - clips.emplace_back(Player::ClipWithRow{ *playlist_clips->clip(row), row }); + clips.emplace_back(*playlist_clips->clip_with_id(row)); } double remaining = compute_total_time(clips); set_output_status(format_duration(remaining) + " ready"); } } -void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &) +void MainWindow::clip_list_selection_changed(const QModelIndex ¤t, const QModelIndex &previous) { int camera_selected = -1; if (cliplist_clips->is_camera_column(current.column())) { camera_selected = current.column() - int(ClipList::Column::CAMERA_1); + + // See the comment on hidden_jog_column. + if (current.row() != previous.row()) { + hidden_jog_column = -1; + } else if (hidden_jog_column == -1) { + hidden_jog_column = previous.column(); + } + } else { + hidden_jog_column = -1; } highlight_camera_input(camera_selected); + enable_or_disable_queue_button(); } void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_left) @@ -1020,11 +1090,60 @@ void MainWindow::highlight_camera_input(int stream_idx) displays[i].frame->setStyleSheet(""); } } + midi_mapper.highlight_camera_input(stream_idx); +} + +void MainWindow::enable_or_disable_preview_button() +{ + // Follows the logic in preview_clicked(). + + if (ui->playlist->hasFocus()) { + // Allow the playlist as preview iff it has focus and something is selected. + // TODO: Is this part really relevant? + QItemSelectionModel *selected = ui->playlist->selectionModel(); + if (selected->hasSelection()) { + ui->preview_btn->setEnabled(true); + midi_mapper.set_preview_enabled(true); + return; + } + } + + // TODO: Perhaps only enable this if something is actually selected. + ui->preview_btn->setEnabled(!cliplist_clips->empty()); + midi_mapper.set_preview_enabled(!cliplist_clips->empty()); +} + +void MainWindow::enable_or_disable_queue_button() +{ + // Follows the logic in queue_clicked(). + // TODO: Perhaps only enable this if something is actually selected. + + bool enabled; + + if (cliplist_clips->empty()) { + enabled = false; + } else { + QItemSelectionModel *selected = ui->clip_list->selectionModel(); + if (!selected->hasSelection()) { + Clip clip = *cliplist_clips->back(); + enabled = clip.pts_out != -1; + } else { + QModelIndex index = selected->currentIndex(); + Clip clip = *cliplist_clips->clip(index.row()); + enabled = clip.pts_out != -1; + } + } + + ui->queue_btn->setEnabled(enabled); + midi_mapper.set_queue_enabled(enabled); } void MainWindow::set_output_status(const string &status) { ui->live_label->setText(QString::fromStdString("Current output (" + status + ")")); + if (live_player != nullptr) { + live_player->set_pause_status(status); + } lock_guard lock(queue_status_mu); queue_status = status; @@ -1051,6 +1170,71 @@ void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame) displays[stream_idx].display->setFrame(stream_idx, frame); } +void MainWindow::preview() +{ + post_to_main_thread([this] { + preview_clicked(); + }); +} + +void MainWindow::queue() +{ + post_to_main_thread([this] { + queue_clicked(); + }); +} + +void MainWindow::play() +{ + post_to_main_thread([this] { + play_clicked(); + }); +} + +void MainWindow::jog(int delta) +{ + post_to_main_thread([this, delta] { + int64_t pts_delta = delta * (TIMEBASE / 60); // One click = frame at 60 fps. + if (ui->playlist->hasFocus()) { + QModelIndex selected = ui->playlist->selectionModel()->currentIndex(); + if (selected.column() != -1 && selected.row() != -1) { + jog_internal(JOG_PLAYLIST, selected.row(), selected.column(), /*stream_idx=*/-1, pts_delta); + } + } else if (ui->clip_list->hasFocus()) { + QModelIndex selected = ui->clip_list->selectionModel()->currentIndex(); + if (cliplist_clips->is_camera_column(selected.column()) && + hidden_jog_column != -1) { + // See the definition on hidden_jog_column. + selected = selected.sibling(selected.row(), hidden_jog_column); + ui->clip_list->selectionModel()->setCurrentIndex(selected, QItemSelectionModel::ClearAndSelect); + hidden_jog_column = -1; + } + if (selected.column() != -1 && selected.row() != -1) { + jog_internal(JOG_CLIP_LIST, selected.row(), selected.column(), ui->preview_display->get_stream_idx(), pts_delta); + } + } + }); +} + +void MainWindow::switch_camera(unsigned camera_idx) +{ + post_to_main_thread([this, camera_idx] { + if (camera_idx < num_cameras) { // TODO: Also make this change a highlighted clip? + preview_angle_clicked(camera_idx); + } + }); +} + +void MainWindow::cue_in() +{ + post_to_main_thread([this] { cue_in_clicked(); }); +} + +void MainWindow::cue_out() +{ + post_to_main_thread([this] { cue_out_clicked(); }); +} + template void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model) {