X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=futatabi%2Fmainwindow.cpp;h=4e53590426cc38d21c348251e7ca3d7507a67f7d;hb=56255d64099f0fbaa5271bcb246bc9510fd0e5d8;hp=968493365e563e7baa19b095d51df45876d42577;hpb=c6b5db16df7871e83d94a12f7f5e56a8894d0ef6;p=nageru diff --git a/futatabi/mainwindow.cpp b/futatabi/mainwindow.cpp index 9684933..4e53590 100644 --- a/futatabi/mainwindow.cpp +++ b/futatabi/mainwindow.cpp @@ -32,6 +32,17 @@ static PlayList *playlist_clips; extern int64_t current_pts; +template +void replace_model(QTableView *view, Model **model, Model *new_model, MainWindow *main_window) +{ + QItemSelectionModel *old_selection_model = view->selectionModel(); + view->setModel(new_model); + delete *model; + delete old_selection_model; + *model = new_model; + main_window->connect(new_model, &Model::any_content_changed, main_window, &MainWindow::content_changed); +} + MainWindow::MainWindow() : ui(new Ui::MainWindow), db(global_flags.working_directory + "/futatabi.db") @@ -45,6 +56,10 @@ MainWindow::MainWindow() connect(ui->export_playlist_clip_interpolated_action, &QAction::triggered, this, &MainWindow::export_playlist_clip_interpolated_triggered); connect(ui->manual_action, &QAction::triggered, this, &MainWindow::manual_triggered); connect(ui->about_action, &QAction::triggered, this, &MainWindow::about_triggered); + connect(ui->undo_action, &QAction::triggered, this, &MainWindow::undo_triggered); + connect(ui->redo_action, &QAction::triggered, this, &MainWindow::redo_triggered); + ui->undo_action->setEnabled(false); + ui->redo_action->setEnabled(false); global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2)); disk_free_label = new QLabel(this); @@ -52,6 +67,7 @@ MainWindow::MainWindow() ui->menuBar->setCornerWidget(disk_free_label); StateProto state = db.get_state(); + undo_stack.push_back(state); // The undo stack always has the current state on top. cliplist_clips = new ClipList(state.clip_list()); ui->clip_list->setModel(cliplist_clips); @@ -148,6 +164,7 @@ MainWindow::MainWindow() defer_timeout = new QTimer(this); defer_timeout->setSingleShot(true); connect(defer_timeout, &QTimer::timeout, this, &MainWindow::defer_timer_expired); + ui->undo_action->setEnabled(true); connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged, this, &MainWindow::clip_list_selection_changed); @@ -344,6 +361,17 @@ void MainWindow::content_changed() void MainWindow::state_changed(const StateProto &state) { db.store_state(state); + + redo_stack.clear(); + ui->redo_action->setEnabled(false); + + undo_stack.push_back(state); + ui->undo_action->setEnabled(undo_stack.size() > 1); + + // Make sure it doesn't grow without bounds. + while (undo_stack.size() >= 100) { + undo_stack.pop_front(); + } } void MainWindow::play_clicked() @@ -418,25 +446,11 @@ void MainWindow::live_player_clip_progress(const map &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); + vector clips; + for (size_t row = 0; row < playlist_clips->size(); ++row) { + clips.push_back(*playlist_clips->clip(row)); } + double remaining = compute_time_left(clips, progress); set_output_status(format_duration(remaining) + " left"); } @@ -810,8 +824,6 @@ void MainWindow::export_playlist_clip_interpolated_triggered() return; } - QModelIndex index = selected->currentIndex(); - Clip clip = *playlist_clips->clip(index.row()); QString filename = QFileDialog::getSaveFileName(this, "Export interpolated clip", QString(), tr("Matroska video files (*.mkv)")); if (filename.isNull()) { @@ -821,7 +833,13 @@ void MainWindow::export_playlist_clip_interpolated_triggered() if (!filename.endsWith(".mkv")) { filename += ".mkv"; } - export_interpolated_clip(filename.toStdString(), clip); + + vector clips; + QModelIndexList rows = selected->selectedRows(); + for (QModelIndex index : rows) { + clips.push_back(*playlist_clips->clip(index.row())); + } + export_interpolated_clip(filename.toStdString(), clips); } void MainWindow::manual_triggered() @@ -838,6 +856,53 @@ void MainWindow::about_triggered() AboutDialog("Futatabi", "Multicamera slow motion video server").exec(); } +void MainWindow::undo_triggered() +{ + // Finish any deferred action. + if (defer_timeout->isActive()) { + defer_timeout->stop(); + state_changed(deferred_state); + } + + StateProto redo_state; + *redo_state.mutable_clip_list() = cliplist_clips->serialize(); + *redo_state.mutable_play_list() = playlist_clips->serialize(); + redo_stack.push_back(std::move(redo_state)); + ui->redo_action->setEnabled(true); + + assert(undo_stack.size() > 1); + + // Pop off the current state, which is always at the top of the stack. + undo_stack.pop_back(); + + StateProto state = undo_stack.back(); + ui->undo_action->setEnabled(undo_stack.size() > 1); + + replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()), this); + replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()), this); + + db.store_state(state); +} + +void MainWindow::redo_triggered() +{ + assert(!redo_stack.empty()); + + ui->undo_action->setEnabled(true); + ui->redo_action->setEnabled(true); + + undo_stack.push_back(std::move(redo_stack.back())); + redo_stack.pop_back(); + ui->undo_action->setEnabled(true); + ui->redo_action->setEnabled(!redo_stack.empty()); + + const StateProto &state = undo_stack.back(); + replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()), this); + replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()), this); + + db.store_state(state); +} + void MainWindow::highlight_camera_input(int stream_idx) { if (stream_idx == 0) {