]> git.sesse.net Git - pkanalytics/blob - formations.h
Format events slightly more nicely.
[pkanalytics] / formations.h
1 #ifndef _FORMATIONS_H
2 #define _FORMATIONS_H 1
3
4 #include <sqlite3.h>
5 #include <QAbstractListModel>
6 #include <string>
7 #include <vector>
8
9 class FormationsModel : public QAbstractListModel
10 {
11 public:
12         FormationsModel(sqlite3 *db, bool offense);
13
14         int rowCount(const QModelIndex &parent) const override
15         {
16                 return formations.size() + 2;
17         }
18         QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
19         QVariant data(const QModelIndex &index, int role) const override;
20
21         unsigned insert_new(const std::string &name);  // Returns the new ID.
22
23         int get_formation_id(unsigned row) const {
24                 if (row == 0) {
25                         return 0;
26                 }
27                 if (row == formations.size() + 1) {
28                         return -1;
29                 }
30                 return formations[row - 1].formation_id;
31         }
32         std::string get_formation_name_by_id(unsigned formation_id);
33         unsigned get_row_from_id(unsigned formation_id) {
34                 for (unsigned i = 0; i < formations.size(); ++i) {
35                         if (formations[i].formation_id == formation_id) {
36                                 return i + 1;
37                         }
38                 }
39                 abort();
40         }
41
42 private:
43         struct Formation {
44                 int formation_id;
45                 std::string name;
46         };
47         std::vector<Formation> formations;
48
49         sqlite3 *db;
50         bool offense;
51
52         void load_data();
53 };
54
55 #endif  // !defined(_FORMATIONS_H)