]> git.sesse.net Git - pkanalytics/commitdiff
Make it possible to insert new formations from the UI.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 29 May 2023 12:58:34 +0000 (14:58 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 29 May 2023 12:58:34 +0000 (14:58 +0200)
events.h
formations.cpp
formations.h
main.cpp

index 545437b16d9f181c998cf707e99307b7c849b87f..6dc2faf2f069e3d0d8c8c35e0cdf54a8c7113e90 100644 (file)
--- a/events.h
+++ b/events.h
@@ -50,6 +50,11 @@ public:
        std::vector<int> sort_team(const std::set<int> &team) const;  // Ordered first by gender, then by number.
        void set_formation_at(uint64_t t, bool offense, unsigned formation);
 
+       // Used to notify when we've inserted a new one into the database.
+       void inserted_new_formation(int formation_id, const std::string &name) {
+               formations[formation_id] = Formation{ formation_id, name };
+       }
+
 private:
        struct Player {
                int player_id;
index fa2337badaa538daf0977cb3bba95f814ea251d9..55e6de8c95f7d0429c8ff79bd19e6c109c1bb914 100644 (file)
@@ -46,6 +46,41 @@ QVariant FormationsModel::data(const QModelIndex &index, int role) const
        return QVariant();
 }
 
+unsigned FormationsModel::insert_new(const std::string &name)
+{
+       // Insert the new row into the database.
+       sqlite3_stmt *stmt;
+       int ret = sqlite3_prepare_v2(db, "INSERT INTO formation (name, offense) VALUES (?, ?)", -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       sqlite3_bind_text(stmt, 1, name.data(), name.size(), SQLITE_STATIC);
+       sqlite3_bind_int(stmt, 2, offense);
+
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW) {
+               fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       beginResetModel();  // Simplest for our use, though not ideal.
+
+       int formation_id = sqlite3_last_insert_rowid(db);
+       formations.push_back(Formation{ formation_id, name });
+
+       endResetModel();
+
+       return formation_id;
+}
+
 void FormationsModel::load_data()
 {
        formations.clear();
index 5eda723964d268bc35a1ea3eedb90a61f23f0b3c..aa85c79feb97b57838b63d8f276f927a33fa3f00 100644 (file)
@@ -18,6 +18,8 @@ public:
        QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
        QVariant data(const QModelIndex &index, int role) const override;
 
+       unsigned insert_new(const std::string &name);  // Returns the new ID.
+
        int get_formation_id(unsigned row) const {
                if (row == 0) {
                        return 0;
@@ -28,6 +30,14 @@ public:
                return formations[row - 1].formation_id;
        }
        std::string get_formation_name_by_id(unsigned formation_id);
+       unsigned get_row_from_id(unsigned formation_id) {
+               for (unsigned i = 0; i < formations.size(); ++i) {
+                       if (formations[i].formation_id == formation_id) {
+                               return i + 1;
+                       }
+               }
+               abort();
+       }
 
 private:
        struct Formation {
index d23c63e8c51d3e15be86ac36e78c433d2e1689c1..b0488a0304b54e9d9018ce1b6d259dea107952b2 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -489,7 +489,10 @@ void MainWindow::formation_double_clicked(bool offense, unsigned row)
                        return;
                }
 
-               // FIXME insert
+               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);
        }