From 54bd286ed213509da23c43d0c80f731dd1bb7a18 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 29 May 2023 14:58:34 +0200 Subject: [PATCH 1/1] Make it possible to insert new formations from the UI. --- events.h | 5 +++++ formations.cpp | 35 +++++++++++++++++++++++++++++++++++ formations.h | 10 ++++++++++ main.cpp | 5 ++++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/events.h b/events.h index 545437b..6dc2faf 100644 --- a/events.h +++ b/events.h @@ -50,6 +50,11 @@ public: std::vector sort_team(const std::set &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; diff --git a/formations.cpp b/formations.cpp index fa2337b..55e6de8 100644 --- a/formations.cpp +++ b/formations.cpp @@ -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(); diff --git a/formations.h b/formations.h index 5eda723..aa85c79 100644 --- a/formations.h +++ b/formations.h @@ -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 { diff --git a/main.cpp b/main.cpp index d23c63e..b0488a0 100644 --- 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); } -- 2.39.5