]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Add the beginnings of formation support.
[pkanalytics] / main.cpp
index 4383f2e84958bc1dbfc06c6c72f58e9a959cba36..d23c63e8c51d3e15be86ac36e78c433d2e1689c1 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -15,6 +15,7 @@
 #include "ui_mainwindow.h"
 #include "events.h"
 #include "players.h"
+#include "formations.h"
 #include "json.h"
 
 using namespace std;
@@ -33,7 +34,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"));
@@ -62,6 +65,20 @@ 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) {
+               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);
        });
@@ -217,9 +234,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);
 
@@ -231,7 +248,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);
@@ -334,6 +351,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;
@@ -442,6 +478,23 @@ 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;
+               }
+
+               // FIXME insert
+       } else {
+               events->set_formation_at(video->position(), offense, id);
+       }
+}
+
 sqlite3 *open_db(const char *filename)
 {
        sqlite3 *db;
@@ -460,7 +513,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.
@@ -580,7 +637,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();