]> git.sesse.net Git - pkanalytics/blob - formations.h
Support filtering passes by thrower and receiver.
[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                 for (unsigned i = 0; i < formations.size(); ++i) {
34                         if (formations[i].formation_id == int(formation_id)) {
35                                 return formations[i].name;
36                         }
37                 }
38                 abort();
39         }
40         unsigned get_row_from_id(unsigned formation_id) {
41                 for (unsigned i = 0; i < formations.size(); ++i) {
42                         if (formations[i].formation_id == int(formation_id)) {
43                                 return i + 1;
44                         }
45                 }
46                 abort();
47         }
48
49 private:
50         struct Formation {
51                 int formation_id;
52                 std::string name;
53         };
54         std::vector<Formation> formations;
55
56         sqlite3 *db;
57         bool offense;
58
59         void load_data();
60 };
61
62 #endif  // !defined(_FORMATIONS_H)