]> git.sesse.net Git - nageru/blobdiff - futatabi/mainwindow.cpp
Make the UI programmatically dependent on NUM_CAMERAS.
[nageru] / futatabi / mainwindow.cpp
index 4e53590426cc38d21c348251e7ca3d7507a67f7d..8d52c06d022bfdf78349ebc29b056b609b001f13 100644 (file)
@@ -32,17 +32,6 @@ static PlayList *playlist_clips;
 
 extern int64_t current_pts;
 
-template <class Model>
-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")
@@ -50,6 +39,22 @@ MainWindow::MainWindow()
        global_mainwindow = this;
        ui->setupUi(this);
 
+       // Load settings from database if needed.
+       if (!global_flags.interpolation_quality_set) {
+               SettingsProto settings = db.get_settings();
+               if (settings.interpolation_quality() != 0) {
+                       global_flags.interpolation_quality = settings.interpolation_quality() - 1;
+               }
+       }
+       if (global_flags.interpolation_quality == 0) {
+               // Allocate something just for simplicity; we won't be using it
+               // unless the user changes runtime, in which case 1 is fine.
+               flow_initialized_interpolation_quality = 1;
+       } else {
+               flow_initialized_interpolation_quality = global_flags.interpolation_quality;
+       }
+       save_settings();
+
        // The menus.
        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);
@@ -61,6 +66,32 @@ MainWindow::MainWindow()
        ui->undo_action->setEnabled(false);
        ui->redo_action->setEnabled(false);
 
+       // The quality group.
+       QActionGroup *quality_group = new QActionGroup(ui->interpolation_menu);
+       quality_group->addAction(ui->quality_0_action);
+       quality_group->addAction(ui->quality_1_action);
+       quality_group->addAction(ui->quality_2_action);
+       quality_group->addAction(ui->quality_3_action);
+       quality_group->addAction(ui->quality_4_action);
+       if (global_flags.interpolation_quality == 0) {
+               ui->quality_0_action->setChecked(true);
+       } else if (global_flags.interpolation_quality == 1) {
+               ui->quality_1_action->setChecked(true);
+       } else if (global_flags.interpolation_quality == 2) {
+               ui->quality_2_action->setChecked(true);
+       } else if (global_flags.interpolation_quality == 3) {
+               ui->quality_3_action->setChecked(true);
+       } else if (global_flags.interpolation_quality == 4) {
+               ui->quality_4_action->setChecked(true);
+       } else {
+               assert(false);
+       }
+       connect(ui->quality_0_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 0, _1));
+       connect(ui->quality_1_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 1, _1));
+       connect(ui->quality_2_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 2, _1));
+       connect(ui->quality_3_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 3, _1));
+       connect(ui->quality_4_action, &QAction::toggled, bind(&MainWindow::quality_toggled, this, 4, _1));
+
        global_disk_space_estimator = new DiskSpaceEstimator(bind(&MainWindow::report_disk_space, this, _1, _2));
        disk_free_label = new QLabel(this);
        disk_free_label->setStyleSheet("QLabel {padding-right: 5px;}");
@@ -104,29 +135,8 @@ MainWindow::MainWindow()
        connect(play, &QShortcut::activated, ui->play_btn, &QPushButton::click);
        connect(ui->play_btn, &QPushButton::clicked, this, &MainWindow::play_clicked);
 
-       QShortcut *preview_1 = new QShortcut(QKeySequence(Qt::Key_1), this);
-       connect(preview_1, &QShortcut::activated, ui->preview_1_btn, &QPushButton::click);
-       connect(ui->input1_display, &JPEGFrameView::clicked, ui->preview_1_btn, &QPushButton::click);
-       connect(ui->preview_1_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(0); });
-       ui->input1_display->set_overlay("1");
-
-       QShortcut *preview_2 = new QShortcut(QKeySequence(Qt::Key_2), this);
-       connect(preview_2, &QShortcut::activated, ui->preview_2_btn, &QPushButton::click);
-       connect(ui->input2_display, &JPEGFrameView::clicked, ui->preview_2_btn, &QPushButton::click);
-       connect(ui->preview_2_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(1); });
-       ui->input2_display->set_overlay("2");
-
-       QShortcut *preview_3 = new QShortcut(QKeySequence(Qt::Key_3), this);
-       connect(preview_3, &QShortcut::activated, ui->preview_3_btn, &QPushButton::click);
-       connect(ui->input3_display, &JPEGFrameView::clicked, ui->preview_3_btn, &QPushButton::click);
-       connect(ui->preview_3_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(2); });
-       ui->input3_display->set_overlay("3");
-
-       QShortcut *preview_4 = new QShortcut(QKeySequence(Qt::Key_4), this);
-       connect(preview_4, &QShortcut::activated, ui->preview_4_btn, &QPushButton::click);
-       connect(ui->input4_display, &JPEGFrameView::clicked, ui->preview_4_btn, &QPushButton::click);
-       connect(ui->preview_4_btn, &QPushButton::clicked, [this]{ preview_angle_clicked(3); });
-       ui->input4_display->set_overlay("4");
+       connect(ui->stop_btn, &QPushButton::clicked, this, &MainWindow::stop_clicked);
+       ui->stop_btn->setEnabled(false);
 
        connect(ui->playlist_duplicate_btn, &QPushButton::clicked, this, &MainWindow::playlist_duplicate);
 
@@ -168,6 +178,38 @@ MainWindow::MainWindow()
 
        connect(ui->clip_list->selectionModel(), &QItemSelectionModel::currentChanged,
                this, &MainWindow::clip_list_selection_changed);
+
+       // Make the display rows.
+       unsigned display_rows = (NUM_CAMERAS + 1) / 2;
+       ui->video_displays->setStretch(1, display_rows);
+       for (unsigned i = 0; i < NUM_CAMERAS; ++i) {
+               QFrame *frame = new QFrame(this);
+               frame->setAutoFillBackground(true);
+
+               QLayout *layout = new QGridLayout(frame);
+               frame->setLayout(layout);
+               layout->setContentsMargins(3, 3, 3, 3);
+
+               JPEGFrameView *display = new JPEGFrameView(frame);
+               display->setAutoFillBackground(true);
+               layout->addWidget(display);
+
+               ui->input_displays->addWidget(frame, i / 2, i % 2);
+               display->set_overlay(to_string(i + 1));
+
+               QPushButton *preview_btn = new QPushButton(this);
+               preview_btn->setMaximumSize(20, 17);
+               preview_btn->setText(QString::fromStdString(to_string(i + 1)));
+               ui->preview_layout->addWidget(preview_btn);
+
+               displays.emplace_back(FrameAndDisplay{ frame, display, preview_btn });
+
+               connect(display, &JPEGFrameView::clicked, preview_btn, &QPushButton::click);
+               QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_1 + i), this);
+               connect(shortcut, &QShortcut::activated, preview_btn, &QPushButton::click);
+
+               connect(preview_btn, &QPushButton::clicked, [this, i]{ preview_angle_clicked(i); });
+       }
 }
 
 MainWindow::~MainWindow()
@@ -216,8 +258,7 @@ void MainWindow::queue_clicked()
 
        QModelIndex index = selected->currentIndex();
        Clip clip = *cliplist_clips->clip(index.row());
-       if (index.column() >= int(ClipList::Column::CAMERA_1) &&
-           index.column() <= int(ClipList::Column::CAMERA_4)) {
+       if (ClipList::is_camera_column(index.column())) {
                clip.stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
        } else {
                clip.stream_idx = ui->preview_display->get_stream_idx();
@@ -259,8 +300,7 @@ void MainWindow::preview_clicked()
 
        QModelIndex index = selected->currentIndex();
        unsigned stream_idx;
-       if (index.column() >= int(ClipList::Column::CAMERA_1) &&
-           index.column() <= int(ClipList::Column::CAMERA_4)) {
+       if (ClipList::is_camera_column(index.column())) {
                stream_idx = index.column() - int(ClipList::Column::CAMERA_1);
        } else {
                stream_idx = ui->preview_display->get_stream_idx();
@@ -374,6 +414,13 @@ void MainWindow::state_changed(const StateProto &state)
        }
 }
 
+void MainWindow::save_settings()
+{
+       SettingsProto settings;
+       settings.set_interpolation_quality(global_flags.interpolation_quality + 1);
+       db.store_settings(settings);
+}
+
 void MainWindow::play_clicked()
 {
        if (playlist_clips->empty())
@@ -392,6 +439,18 @@ void MainWindow::play_clicked()
        playlist_clips->set_progress({{ row, 0.0f }});
        playlist_clips->set_currently_playing(row, 0.0f);
        playlist_selection_changed();
+
+       ui->stop_btn->setEnabled(true);
+}
+
+void MainWindow::stop_clicked()
+{
+       Clip fake_clip;
+       fake_clip.pts_in = 0;
+       fake_clip.pts_out = 0;
+       size_t last_row = playlist_clips->size() - 1;
+       playlist_clips->set_currently_playing(last_row, 0.0f);
+       live_player->play_clip(fake_clip, last_row, 0);
 }
 
 void MainWindow::live_player_clip_done()
@@ -405,6 +464,7 @@ void MainWindow::live_player_clip_done()
                playlist_clips->set_progress({{ row + 1, 0.0f }});
                playlist_clips->set_currently_playing(row + 1, 0.0f);
        }
+       ui->stop_btn->setEnabled(false);
 }
 
 pair<Clip, size_t> MainWindow::live_player_get_next_clip()
@@ -416,7 +476,7 @@ pair<Clip, size_t> MainWindow::live_player_get_next_clip()
        // for Player to shut down, we could have a deadlock here.
        promise<pair<Clip, size_t>> clip_promise;
        future<pair<Clip, size_t>> clip = clip_promise.get_future();
-       post_to_main_thread([this, &clip_promise] {
+       post_to_main_thread([&clip_promise] {
                int row = playlist_clips->get_currently_playing();
                if (row != -1 && row < int(playlist_clips->size()) - 1) {
                        clip_promise.set_value(make_pair(*playlist_clips->clip(row + 1), row + 1));
@@ -745,8 +805,7 @@ void MainWindow::playlist_selection_changed()
 void MainWindow::clip_list_selection_changed(const QModelIndex &current, const QModelIndex &)
 {
        int camera_selected = -1;
-       if (current.column() >= int(ClipList::Column::CAMERA_1) &&
-           current.column() <= int(ClipList::Column::CAMERA_4)) {
+       if (ClipList::is_camera_column(current.column())) {
                camera_selected = current.column() - int(ClipList::Column::CAMERA_1);
        }
        highlight_camera_input(camera_selected);
@@ -878,8 +937,8 @@ void MainWindow::undo_triggered()
        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);
+       replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
+       replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
 
        db.store_state(state);
 }
@@ -897,33 +956,39 @@ void MainWindow::redo_triggered()
        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);
+       replace_model(ui->clip_list, &cliplist_clips, new ClipList(state.clip_list()));
+       replace_model(ui->playlist, &playlist_clips, new PlayList(state.play_list()));
 
        db.store_state(state);
 }
 
-void MainWindow::highlight_camera_input(int stream_idx)
+void MainWindow::quality_toggled(int quality, bool checked)
 {
-       if (stream_idx == 0) {
-               ui->input1_frame->setStyleSheet("background: rgb(0,255,0)");
-       } else {
-               ui->input1_frame->setStyleSheet("");
-       }
-       if (stream_idx == 1) {
-               ui->input2_frame->setStyleSheet("background: rgb(0,255,0)");
-       } else {
-               ui->input2_frame->setStyleSheet("");
+       if (!checked) {
+               return;
        }
-       if (stream_idx == 2) {
-               ui->input3_frame->setStyleSheet("background: rgb(0,255,0)");
-       } else {
-               ui->input3_frame->setStyleSheet("");
+       global_flags.interpolation_quality = quality;
+       if (quality != 0 &&  // Turning interpolation off is always possible.
+           quality != flow_initialized_interpolation_quality) {
+               QMessageBox msgbox;
+               msgbox.setText(QString::fromStdString(
+                       "The interpolation quality for the main output cannot be changed at runtime, "
+                       "except being turned completely off; it will take effect for exported files "
+                       "only until next restart. The live output quality thus remains at " + to_string(flow_initialized_interpolation_quality) + "."));
+               msgbox.exec();
        }
-       if (stream_idx == 3) {
-               ui->input4_frame->setStyleSheet("background: rgb(0,255,0)");
-       } else {
-               ui->input4_frame->setStyleSheet("");
+
+       save_settings();
+}
+
+void MainWindow::highlight_camera_input(int stream_idx)
+{
+       for (unsigned i = 0; i < NUM_CAMERAS; ++i) {
+               if (stream_idx == i) {
+                       displays[i].frame->setStyleSheet("background: rgb(0,255,0)");
+               } else {
+                       displays[i].frame->setStyleSheet("");
+               }
        }
 }
 
@@ -939,3 +1004,21 @@ pair<string, string> MainWindow::get_queue_status() const {
        lock_guard<mutex> lock(queue_status_mu);
        return {queue_status, "text/plain"};
 }
+
+void MainWindow::display_frame(unsigned stream_idx, const FrameOnDisk &frame)
+{
+       if (stream_idx < NUM_CAMERAS) {
+               displays[stream_idx].display->setFrame(stream_idx, frame);
+       }
+}
+
+template <class Model>
+void MainWindow::replace_model(QTableView *view, Model **model, Model *new_model)
+{
+       QItemSelectionModel *old_selection_model = view->selectionModel();
+       view->setModel(new_model);
+       delete *model;
+       delete old_selection_model;
+       *model = new_model;
+       connect(new_model, &Model::any_content_changed, this, &MainWindow::content_changed);
+}