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() == int(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));
73 int formation_id = sqlite3_last_insert_rowid(db);
75 beginResetModel(); // Simplest for our use, though not ideal.
82 void FormationsModel::load_data()
86 // Read the formations.
88 int ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation WHERE offense=? ORDER BY name", -1, &stmt, 0);
89 if (ret != SQLITE_OK) {
90 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
93 sqlite3_bind_int(stmt, 1, offense);
95 ret = sqlite3_step(stmt);
96 if (ret == SQLITE_ROW) {
98 f.formation_id = sqlite3_column_int(stmt, 0);
99 f.name = (const char *)sqlite3_column_text(stmt, 1);
100 formations.push_back(f);
101 } else if (ret == SQLITE_DONE) {
104 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
108 ret = sqlite3_finalize(stmt);
109 if (ret != SQLITE_OK) {
110 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));