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;
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();
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;
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 {
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);
}