4 #include "formations.h"
8 FormationsModel::FormationsModel(sqlite3 *db, bool offense) : db(db), offense(offense)
13 QVariant FormationsModel::headerData(int section, Qt::Orientation orientation, int role) const
15 if (role != Qt::DisplayRole) {
18 if (orientation == Qt::Horizontal) {
29 QVariant FormationsModel::data(const QModelIndex &index, int role) const
31 if (role == Qt::TextAlignmentRole) {
32 return (Qt::AlignLeft | Qt::AlignVCenter).toInt();
34 if (role != Qt::DisplayRole) {
37 if (index.column() == 0) {
38 if (index.row() == 0) {
39 return QString::fromUtf8("(None/unknown)");
40 } else if (index.row() == formations.size() + 1) {
41 return QString::fromUtf8("Add new…");
43 return QString::fromUtf8(formations[index.row() - 1].name);
49 unsigned FormationsModel::insert_new(const std::string &name)
51 // Insert the new row into the database.
53 int ret = sqlite3_prepare_v2(db, "INSERT INTO formation (name, offense) VALUES (?, ?)", -1, &stmt, 0);
54 if (ret != SQLITE_OK) {
55 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
59 sqlite3_bind_text(stmt, 1, name.data(), name.size(), SQLITE_STATIC);
60 sqlite3_bind_int(stmt, 2, offense);
62 ret = sqlite3_step(stmt);
63 if (ret == SQLITE_ROW) {
64 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
68 ret = sqlite3_finalize(stmt);
69 if (ret != SQLITE_OK) {
70 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
74 beginResetModel(); // Simplest for our use, though not ideal.
76 int formation_id = sqlite3_last_insert_rowid(db);
77 formations.push_back(Formation{ formation_id, name });
84 void FormationsModel::load_data()
88 // Read the formations.
90 int ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation WHERE offense=? ORDER BY name", -1, &stmt, 0);
91 if (ret != SQLITE_OK) {
92 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
95 sqlite3_bind_int(stmt, 1, offense);
97 ret = sqlite3_step(stmt);
98 if (ret == SQLITE_ROW) {
100 f.formation_id = sqlite3_column_int(stmt, 0);
101 f.name = (const char *)sqlite3_column_text(stmt, 1);
102 formations.push_back(f);
103 } else if (ret == SQLITE_DONE) {
106 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
110 ret = sqlite3_finalize(stmt);
111 if (ret != SQLITE_OK) {
112 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));