]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Make the formation buttons actually do something.
[pkanalytics] / main.cpp
index 0a6819b2d3587396e7a4f563df4d34c12f23c5a9..2edd111c346de1dd356a5cee6f5b149a6ab66c58 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -5,6 +5,7 @@
 #include <QVideoWidget>
 #include <QShortcut>
 #include <QInputDialog>
+#include <QTimer>
 #include <algorithm>
 #include <string>
 #include <map>
@@ -15,6 +16,7 @@
 #include "ui_mainwindow.h"
 #include "events.h"
 #include "players.h"
+#include "formations.h"
 #include "json.h"
 
 using namespace std;
@@ -33,7 +35,9 @@ string format_timestamp(uint64_t pos)
        return buf;
 }
 
-MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(events), players(players)
+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"));
@@ -44,6 +48,8 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(even
        ui->setupUi(this);
 
        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) {
                        uint64_t t = events->get_time(current.row());
@@ -62,6 +68,22 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(even
        ui->player_view->setColumnWidth(1, 20);
        ui->player_view->horizontalHeader()->setStretchLastSection(true);
 
+       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());
+               });
+       };
+       ui->offensive_formation_view->setModel(offensive_formations);
+       ui->defensive_formation_view->setModel(defensive_formations);
+       connect(ui->offensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
+       connect(ui->defensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
+       connect(ui->offensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
+               formation_double_clicked(true, index.row());
+       });
+       connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
+               formation_double_clicked(false, index.row());
+       });
+
        connect(video, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
                position_changed(pos);
        });
@@ -75,6 +97,17 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(even
        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(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
        connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
 
@@ -147,6 +180,9 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(even
        connect(ui->defensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_plus"); });
        connect(ui->defensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_minus"); });
 
+       connect(ui->offensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/true); });
+       connect(ui->defensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/false); });
+
        // Misc. events
        connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); });
        connect(ui->stoppage, &QPushButton::clicked, [this, events]() {
@@ -206,9 +242,9 @@ void MainWindow::insert_player_event(int button_id)
        ui->event_view->selectionModel()->blockSignals(true);
        if (s.attack_state == EventsModel::Status::OFFENSE) {
                // TODO: Perhaps not if that player already did the last catch?
-               ui->event_view->selectRow(events->insert_event(t, player_id, "catch"));
+               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
        } else {
-               ui->event_view->selectRow(events->insert_event(t, player_id));
+               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
        }
        ui->event_view->selectionModel()->blockSignals(false);
 
@@ -220,7 +256,7 @@ void MainWindow::insert_noplayer_event(const string &type)
        uint64_t t = video->position();
 
        ui->event_view->selectionModel()->blockSignals(true);
-       ui->event_view->selectRow(events->insert_event(t, nullopt, type));
+       ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
        ui->event_view->selectionModel()->blockSignals(false);
 
        update_ui_from_time(t);
@@ -237,6 +273,39 @@ void MainWindow::set_current_event_type(const string &type)
        update_ui_from_time(video->position());
 }
 
+// Formation buttons either modify the existing formation (if we've selected
+// a formation change event), or insert a new one (if not).
+void MainWindow::insert_or_change_formation(bool offense)
+{
+       FormationsModel *formations = offense ? offensive_formations : defensive_formations;
+       QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
+       if (!formation_view->selectionModel()->hasSelection()) {
+               // This shouldn't happen; the button should not have been enabled.
+               return;
+       }
+       int formation_row = formation_view->selectionModel()->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+       int formation_id = formations->get_formation_id(formation_row);
+       if (formation_id == -1) {
+               // This also shouldn't happen (“Add new…” selected).
+               return;
+       }
+
+       QItemSelectionModel *select = ui->event_view->selectionModel();
+       if (select->hasSelection()) {
+               int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+               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());
+                       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());
+}
+
 void MainWindow::delete_current_event()
 {
        QItemSelectionModel *select = ui->event_view->selectionModel();
@@ -271,15 +340,23 @@ void MainWindow::update_status(uint64_t t)
 {
        EventsModel::Status s = events->get_status_at(t);
        char buf[256];
-       const char *offense = "not started";
+       std::string formation = "Not started";
        if (s.attack_state == EventsModel::Status::OFFENSE) {
-               offense = "offense";
+               if (s.offensive_formation != 0) {
+                       formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
+               } else {
+                       formation = "Offense";
+               }
        } else if (s.attack_state == EventsModel::Status::DEFENSE) {
-               offense = "defense";
+               if (s.defensive_formation != 0) {
+                       formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
+               } else {
+                       formation = "Defense";
+               }
        }
 
        snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
-               s.our_score, s.their_score, offense, s.num_passes, s.possession_sec);
+               s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
        if (s.stoppage_sec > 0) {
                char buf2[256];
                snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
@@ -323,6 +400,25 @@ void MainWindow::update_player_buttons(uint64_t t)
 
 void MainWindow::update_action_buttons(uint64_t t)
 {
+       {
+               QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
+               if (select->hasSelection()) {
+                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+                       ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
+               } else {
+                       ui->offensive_formation->setEnabled(false);
+               }
+       }
+       {
+               QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
+               if (select->hasSelection()) {
+                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+                       ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
+               } else {
+                       ui->defensive_formation->setEnabled(false);
+               }
+       }
+
        EventsModel::Status s = events->get_status_at(t);
 
        bool has_selection = false;
@@ -431,6 +527,27 @@ void MainWindow::update_action_buttons(uint64_t t)
        ui->their_pull->setEnabled(false);
 }
 
+void MainWindow::formation_double_clicked(bool offense, unsigned row)
+{
+       FormationsModel *formations = offense ? offensive_formations : defensive_formations;
+       int id = formations->get_formation_id(row);
+       if (id == -1) {  // “Add new” clicked.
+               bool ok;
+               QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
+               if (!ok || new_formation_str.isEmpty()) {
+                       return;
+               }
+
+               id = formations->insert_new(new_formation_str.toStdString());
+               QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
+               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);
+       }
+       update_ui_from_time(video->position());
+}
+
 sqlite3 *open_db(const char *filename)
 {
        sqlite3 *db;
@@ -449,7 +566,11 @@ sqlite3 *open_db(const char *filename)
        )", nullptr, nullptr, nullptr);  // Ignore errors.
 
        sqlite3_exec(db, R"(
-               CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, match INTEGER, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player), FOREIGN KEY (match) REFERENCES match (match));
+               CREATE TABLE IF NOT EXISTS formation (formation INTEGER PRIMARY KEY, name VARCHAR, offense BOOLEAN NOT NULL);
+       )", nullptr, nullptr, nullptr);  // Ignore errors.
+
+       sqlite3_exec(db, R"(
+               CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, match INTEGER, t INTEGER, player INTEGER, type VARCHAR, formation INTEGER, FOREIGN KEY (player) REFERENCES player(player), FOREIGN KEY (match) REFERENCES match (match), FOREIGN KEY (formation) REFERENCES formation (formation));
        )", nullptr, nullptr, nullptr);  // Ignore errors.
 
        sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
@@ -569,7 +690,8 @@ int main(int argc, char *argv[])
                return 0;
        }
 
-       MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db));
+       MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db),
+                             new FormationsModel(db, true), new FormationsModel(db, false));
        mainWindow.resize(QSize(1280, 720));
        mainWindow.show();