]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Drop QVideoWidget.
[pkanalytics] / main.cpp
index 1c0f2e3017075034b570b28ee041a4e9c76e6231..560a9a658676b09723edab126b6230a5ac8c3190 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -2,7 +2,6 @@
 #include <QMainWindow>
 #include <QApplication>
 #include <QGridLayout>
-#include <QVideoWidget>
 #include <QShortcut>
 #include <QInputDialog>
 #include <QTimer>
@@ -18,6 +17,7 @@
 #include "players.h"
 #include "formations.h"
 #include "json.h"
+#include "video_widget.h"
 
 using namespace std;
 
@@ -39,22 +39,20 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
                        FormationsModel *offensive_formations, FormationsModel *defensive_formations)
        : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations)
 {
-       video = new QMediaPlayer;
-       //video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
-       video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
-       video->play();
-
        ui = new Ui::MainWindow;
        ui->setupUi(this);
 
+       ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv");
+       ui->video->play();
+
        ui->event_view->setModel(events);
        ui->event_view->setColumnWidth(1, 150);
        ui->event_view->setColumnWidth(2, 150);
        connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
                [this, events](const QModelIndex &current, const QModelIndex &previous) {
-                       int64_t t = events->get_time(current.row());
-                       if (t != video->position()) {
-                               video->setPosition(events->get_time(current.row()));
+                       uint64_t t = events->get_time(current.row());
+                       if (t != ui->video->get_position()) {
+                               ui->video->seek_absolute(events->get_time(current.row()));
                        } else {
                                // Selection could have changed, so we still need to update.
                                // (Just calling setPosition() would not give us the signal
@@ -70,7 +68,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
 
        auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
                QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
-                       update_action_buttons(video->position());
+                       update_action_buttons(ui->video->get_position());
                });
        };
        ui->offensive_formation_view->setModel(offensive_formations);
@@ -84,47 +82,33 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
                formation_double_clicked(false, index.row());
        });
 
-       connect(video, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
+       connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
                position_changed(pos);
        });
 
-       video->setVideoOutput(ui->video);
 
        // It's not really clear whether PgUp should be forwards or backwards,
        // but mpv does at least up = forwards, so that's probably standard.
        QShortcut *pgdown = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
-       connect(pgdown, &QShortcut::activated, [this]() { seek(-120000); });
+       connect(pgdown, &QShortcut::activated, [this]() { ui->video->seek(-120000); });
        QShortcut *pgup = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
-       connect(pgup, &QShortcut::activated, [this]() { seek(120000); });
-
-       // Ugh. Used when Qt messes up and hangs the video.
-       QShortcut *f5 = new QShortcut(QKeySequence(Qt::Key_F5), this);
-       connect(f5, &QShortcut::activated, [this]() {
-               QVideoWidget *nvw = new QVideoWidget(ui->video->parentWidget());
-               nvw->setObjectName("video");
-               nvw->setMinimumSize(QSize(320, 240));
-               video->setVideoOutput(nvw);
-               ui->main_grid->replaceWidget(ui->video, nvw);
-               ui->video = nvw;
-       });
+       connect(pgup, &QShortcut::activated, [this]() { ui->video->seek(120000); });
 
-       connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
-       connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
+       connect(ui->minus10s, &QPushButton::clicked, [this]() { ui->video->seek(-10000); });
+       connect(ui->plus10s, &QPushButton::clicked, [this]() { ui->video->seek(10000); });
 
-       connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
-       connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
+       connect(ui->minus2s, &QPushButton::clicked, [this]() { ui->video->seek(-2000); });
+       connect(ui->plus2s, &QPushButton::clicked, [this]() { ui->video->seek(2000); });
 
-       // TODO: Would be nice to actually have a frame...
-       connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
-       connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
+       connect(ui->minus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(-1); });
+       connect(ui->plus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(1); });
 
        connect(ui->play_pause, &QPushButton::clicked, [this]() {
                if (playing) {
-                       video->pause();
+                       ui->video->pause();
                        ui->play_pause->setText("Play (space)");
                } else {
-                       video->setPlaybackRate(1.0);
-                       video->play();
+                       ui->video->play();
                        ui->play_pause->setText("Pause (space)");
                }
                playing = !playing;
@@ -145,7 +129,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        connect(ui->offense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_offense"); });
        connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
        connect(ui->throwaway, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(video->position());
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
                if (s.attack_state == EventsModel::Status::DEFENSE && s.pull_state == EventsModel::Status::PULL_IN_AIR) {
                        insert_noplayer_event("pull_oob");
                } else {
@@ -157,7 +141,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
        connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
        connect(ui->pull, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(video->position());
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
                if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
                        set_current_event_type("pull");
                } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
@@ -171,7 +155,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        connect(ui->their_throwaway, &QPushButton::clicked, [this]() { insert_noplayer_event("their_throwaway"); });
        connect(ui->their_goal, &QPushButton::clicked, [this]() { insert_noplayer_event("their_goal"); });
        connect(ui->their_pull, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(video->position());
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
                if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
                        insert_noplayer_event("their_pull");
                }
@@ -186,7 +170,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        // Misc. events
        connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); });
        connect(ui->stoppage, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(video->position());
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
                if (s.stoppage) {
                        insert_noplayer_event("restart");
                } else {
@@ -203,34 +187,19 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
 void MainWindow::position_changed(uint64_t pos)
 {
        ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
-       if (buffered_seek) {
-               video->setPosition(*buffered_seek);
-               buffered_seek.reset();
-       }
        if (!playing) {
-               video->pause();  // We only played to get a picture.
+               ui->video->pause();  // We only played to get a picture.
        }
        if (playing) {
-               QModelIndex row = events->get_last_event_qt(video->position());
+               QModelIndex row = events->get_last_event_qt(ui->video->get_position());
                ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
        }
        update_ui_from_time(pos);
 }
 
-void MainWindow::seek(int64_t delta_ms)
-{
-       int64_t current_pos = buffered_seek ? *buffered_seek : video->position();
-       uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
-       buffered_seek = pos;
-       if (!playing) {
-               video->setPlaybackRate(0.01);
-               video->play();  // Or Qt won't show the seek.
-       }
-}
-
 void MainWindow::insert_player_event(int button_id)
 {
-       uint64_t t = video->position();
+       uint64_t t = ui->video->get_position();
        vector<int> team = events->sort_team(events->get_team_at(t));
        if (unsigned(button_id) >= team.size()) {
                return;
@@ -253,7 +222,7 @@ void MainWindow::insert_player_event(int button_id)
 
 void MainWindow::insert_noplayer_event(const string &type)
 {
-       uint64_t t = video->position();
+       uint64_t t = ui->video->get_position();
 
        ui->event_view->selectionModel()->blockSignals(true);
        ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
@@ -270,7 +239,7 @@ void MainWindow::set_current_event_type(const string &type)
        }
        int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
        events->set_event_type(row, type);
-       update_ui_from_time(video->position());
+       update_ui_from_time(ui->video->get_position());
 }
 
 // Formation buttons either modify the existing formation (if we've selected
@@ -296,14 +265,14 @@ void MainWindow::insert_or_change_formation(bool offense)
                string expected_type = offense ? "formation_offense" : "formation_defense";
                if (events->get_event_type(row) == expected_type) {
                        events->set_event_formation(row, formation_id);
-                       update_ui_from_time(video->position());
+                       update_ui_from_time(ui->video->get_position());
                        return;
                }
        }
 
        // Insert a new formation event instead (same as double-click on the selected one).
-       events->set_formation_at(video->position(), offense, formation_id);
-       update_ui_from_time(video->position());
+       events->set_formation_at(ui->video->get_position(), offense, formation_id);
+       update_ui_from_time(ui->video->get_position());
 }
 
 void MainWindow::delete_current_event()
@@ -316,7 +285,7 @@ void MainWindow::delete_current_event()
        ui->event_view->selectionModel()->blockSignals(true);
        events->delete_event(row);
        ui->event_view->selectionModel()->blockSignals(false);
-       update_ui_from_time(video->position());
+       update_ui_from_time(ui->video->get_position());
 }
 
 void MainWindow::make_substitution()
@@ -326,7 +295,7 @@ void MainWindow::make_substitution()
        for (QModelIndex row : select->selectedRows()) {
                new_team.insert(players->get_player_id(row.row()));
        }
-       events->set_team_at(video->position(), new_team);
+       events->set_team_at(ui->video->get_position(), new_team);
 }
 
 void MainWindow::update_ui_from_time(uint64_t t)
@@ -543,9 +512,9 @@ void MainWindow::formation_double_clicked(bool offense, unsigned row)
                view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
                events->inserted_new_formation(id, new_formation_str.toStdString());
        } else {
-               events->set_formation_at(video->position(), offense, id);
+               events->set_formation_at(ui->video->get_position(), offense, id);
        }
-       update_ui_from_time(video->position());
+       update_ui_from_time(ui->video->get_position());
 }
 
 sqlite3 *open_db(const char *filename)