X-Git-Url: https://git.sesse.net/?p=nageru;a=blobdiff_plain;f=futatabi%2Fmainwindow.cpp;h=f465468b1dbafe68ee2217a786e8db35add06511;hp=4a4c0d658f2a54f478d84847295020ec37888163;hb=55bc1e47c47dfdeeec06c55de9a53b95bdbbd326;hpb=f22a41cb05c5cc1bbe8ce391fa918707138afad3 diff --git a/futatabi/mainwindow.cpp b/futatabi/mainwindow.cpp index 4a4c0d6..f465468 100644 --- a/futatabi/mainwindow.cpp +++ b/futatabi/mainwindow.cpp @@ -5,6 +5,8 @@ #include "flags.h" #include "frame_on_disk.h" #include "player.h" +#include "futatabi_midi_mapping.pb.h" +#include "midi_mapping_dialog.h" #include "shared/aboutdialog.h" #include "shared/disk_space_estimator.h" #include "shared/post_to_main_thread.h" @@ -33,9 +35,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); @@ -60,6 +78,7 @@ MainWindow::MainWindow() save_settings(); // The menus. + connect(ui->midi_mapping_action, &QAction::triggered, this, &MainWindow::midi_mapping_triggered); connect(ui->exit_action, &QAction::triggered, this, &MainWindow::exit_triggered); connect(ui->export_cliplist_clip_multitrack_action, &QAction::triggered, this, &MainWindow::export_cliplist_clip_multitrack_triggered); connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered); @@ -164,6 +183,9 @@ MainWindow::MainWindow() connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked); ui->stop_btn->setEnabled(false); + connect(ui->speed_slider, &QAbstractSlider::valueChanged, this, &MainWindow::speed_slider_changed); + connect(ui->speed_lock_btn, &QPushButton::clicked, this, &MainWindow::speed_lock_clicked); + connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate); connect(ui->playlist_remove_btn, &QPushButton::clicked, this, &MainWindow::playlist_remove); @@ -183,6 +205,12 @@ MainWindow::MainWindow() playlist_selection_changed(); // First time set-up. preview_player.reset(new Player(ui->preview_display, Player::NO_STREAM_OUTPUT)); + preview_player->set_done_callback([this] { + post_to_main_thread([this] { + preview_player_done(); + }); + }); + live_player.reset(new Player(ui->live_display, Player::HTTPD_STREAM_OUTPUT)); live_player->set_done_callback([this] { post_to_main_thread([this] { @@ -195,14 +223,20 @@ MainWindow::MainWindow() }); }); set_output_status("paused"); + enable_or_disable_queue_button(); defer_timeout = new QTimer(this); defer_timeout->setSingleShot(true); connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired); ui->undo_action->setEnabled(true); + lock_blink_timeout = new QTimer(this); + lock_blink_timeout->setSingleShot(true); + connect(lock_blink_timeout, &QTimer::timeout, this, &MainWindow::lock_blink_timer_expired); + 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 +254,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() @@ -285,6 +331,7 @@ void MainWindow::cue_in_clicked() 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() @@ -300,10 +347,13 @@ void MainWindow::cue_out_clicked() 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; } @@ -351,6 +401,8 @@ void MainWindow::preview_clicked() QModelIndex index = selected->currentIndex(); const Clip &clip = *playlist_clips->clip(index.row()); preview_player->play(clip); + preview_playing = true; + enable_or_disable_preview_button(); return; } } @@ -361,6 +413,8 @@ void MainWindow::preview_clicked() QItemSelectionModel *selected = ui->clip_list->selectionModel(); if (!selected->hasSelection()) { preview_player->play(*cliplist_clips->back()); + preview_playing = true; + enable_or_disable_preview_button(); return; } @@ -372,6 +426,8 @@ void MainWindow::preview_clicked() clip.stream_idx = ui->preview_display->get_stream_idx(); } preview_player->play(clip); + preview_playing = true; + enable_or_disable_preview_button(); } void MainWindow::preview_angle_clicked(unsigned stream_idx) @@ -438,6 +494,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); @@ -496,18 +610,18 @@ void MainWindow::save_settings() db.store_settings(settings); } -void MainWindow::play_clicked() +void MainWindow::lock_blink_timer_expired() { - if (playlist_clips->empty()) - return; + midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked())); // Presumably On, or the timer should have been canceled. +} +void MainWindow::play_clicked() +{ QItemSelectionModel *selected = ui->playlist->selectionModel(); - unsigned start_row; if (!selected->hasSelection()) { - start_row = 0; - } else { - start_row = selected->selectedRows(0)[0].row(); + return; } + unsigned start_row = selected->selectedRows(0)[0].row(); vector clips; for (unsigned row = start_row; row < playlist_clips->size(); ++row) { @@ -516,9 +630,8 @@ void MainWindow::play_clicked() live_player->play(clips); playlist_clips->set_progress({ { start_row, 0.0f } }); ui->playlist->selectionModel()->clear(); - playlist_selection_changed(); - ui->stop_btn->setEnabled(true); + playlist_selection_changed(); } void MainWindow::stop_clicked() @@ -529,13 +642,37 @@ void MainWindow::stop_clicked() playlist_clips->set_progress({}); live_player->play(fake_clip); ui->stop_btn->setEnabled(false); + playlist_selection_changed(); +} + +void MainWindow::speed_slider_changed(int percent) +{ + float speed = percent / 100.0f; + ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%")); + live_player->set_master_speed(speed); + midi_mapper.set_speed_light(speed); +} + +void MainWindow::speed_lock_clicked() +{ + // TODO: Make for a less abrupt transition if we're not already at 100%. + ui->speed_slider->setValue(100); // Also actually sets the master speed and updates the label. + ui->speed_slider->setEnabled(!ui->speed_lock_btn->isChecked()); + midi_mapper.set_locked(MIDIMapper::LightState(ui->speed_lock_btn->isChecked())); + lock_blink_timeout->stop(); +} + +void MainWindow::preview_player_done() +{ + preview_playing = false; + enable_or_disable_preview_button(); } void MainWindow::live_player_done() { - playlist_selection_changed(); playlist_clips->set_progress({}); ui->stop_btn->setEnabled(false); + playlist_selection_changed(); } void MainWindow::live_player_clip_progress(const map &progress, double time_remaining) @@ -559,26 +696,15 @@ 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(); @@ -709,22 +835,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) @@ -736,48 +862,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; @@ -825,7 +910,15 @@ 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()); + + ui->play_btn->setEnabled(any_selected); + if (ui->stop_btn->isEnabled()) { // Playing. + midi_mapper.set_play_enabled(MIDIMapper::On); + } else if (any_selected) { + midi_mapper.set_play_enabled(MIDIMapper::Blinking); + } else { + midi_mapper.set_play_enabled(MIDIMapper::Off); + } if (!any_selected) { set_output_status("paused"); @@ -839,13 +932,23 @@ void MainWindow::playlist_selection_changed() } } -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) @@ -881,6 +984,11 @@ void MainWindow::report_disk_space(off_t free_bytes, double estimated_seconds_le }); } +void MainWindow::midi_mapping_triggered() +{ + MIDIMappingDialog(&midi_mapper).exec(); +} + void MainWindow::exit_triggered() { close(); @@ -1037,6 +1145,7 @@ 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() @@ -1049,12 +1158,43 @@ void MainWindow::enable_or_disable_preview_button() QItemSelectionModel *selected = ui->playlist->selectionModel(); if (selected->hasSelection()) { ui->preview_btn->setEnabled(true); + midi_mapper.set_preview_enabled(preview_playing ? MIDIMapper::On : MIDIMapper::Blinking); return; } } // TODO: Perhaps only enable this if something is actually selected. ui->preview_btn->setEnabled(!cliplist_clips->empty()); + if (preview_playing) { + midi_mapper.set_preview_enabled(MIDIMapper::On); + } else { + midi_mapper.set_preview_enabled(cliplist_clips->empty() ? MIDIMapper::Off : MIDIMapper::Blinking); + } +} + +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) @@ -1089,6 +1229,101 @@ 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::toggle_lock() +{ + post_to_main_thread([this] { + ui->speed_lock_btn->setChecked(!ui->speed_lock_btn->isChecked()); + speed_lock_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::set_master_speed(float speed) +{ + speed = min(max(speed, 0.1f), 2.0f); + + post_to_main_thread([this, speed] { + if (ui->speed_lock_btn->isChecked()) { + midi_mapper.set_locked(MIDIMapper::Blinking); + lock_blink_timeout->start(1000); + return; + } + + int percent = lrintf(speed * 100.0f); + ui->speed_slider->blockSignals(true); + ui->speed_slider->setValue(percent); + ui->speed_slider->blockSignals(false); + ui->speed_lock_btn->setText(QString::fromStdString(" " + to_string(percent) + "%")); + + live_player->set_master_speed(speed); + midi_mapper.set_speed_light(speed); + }); +} + +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) {