]> git.sesse.net Git - pkanalytics/blob - stats.cpp
9d621249cdafdb873b627bb20bb8f3fcfea4f823
[pkanalytics] / stats.cpp
1 #include <QMediaPlayer>
2 #include <QMainWindow>
3 #include <QApplication>
4 #include <QGridLayout>
5 #include <QVideoWidget>
6 #include <QShortcut>
7 #include <algorithm>
8 #include <string>
9 #include <map>
10 #include <vector>
11 #include <optional>
12 #include <sqlite3.h>
13 #include "mainwindow.h"
14 #include "ui_mainwindow.h"
15 #include "events.h"
16
17 using namespace std;
18
19 string format_timestamp(uint64_t pos)
20 {
21         int ms = pos % 1000;
22         pos /= 1000;
23         int sec = pos % 60;
24         pos /= 60;
25         int min = pos % 60;
26         int hour = pos / 60;
27
28         char buf[256];
29         snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
30         return buf;
31 }
32
33 MainWindow::MainWindow()
34 {
35         player = new QMediaPlayer;
36         //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
37         player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
38         player->play();
39
40         ui = new Ui::MainWindow;
41         ui->setupUi(this);
42
43         connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
44                 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
45                 if (buffered_seek) {
46                         player->setPosition(*buffered_seek);
47                         buffered_seek.reset();
48                 }
49                 if (!playing) {
50                         player->pause();  // We only played to get a picture.
51                 }
52         });
53
54         player->setVideoOutput(ui->video);
55
56         connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
57         connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
58
59         connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
60         connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
61
62         // TODO: Would be nice to actually have a frame...
63         connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
64         connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
65
66         connect(ui->play_pause, &QPushButton::clicked, [this]() {
67                 if (playing) {
68                         player->pause();
69                         ui->play_pause->setText("Play (space)");
70                 } else {
71                         player->setPlaybackRate(1.0);
72                         player->play();
73                         ui->play_pause->setText("Pause (space)");
74                 }
75                 playing = !playing;
76
77                 // Needs to be set anew when we modify setText(), evidently.
78                 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
79         });
80
81         connect(ui->player_1, &QPushButton::clicked, [this]() {
82                 ui->event_view->selectRow(model->insert_event(player->position(), 1));
83         });
84         connect(ui->player_2, &QPushButton::clicked, [this]() {
85                 ui->event_view->selectRow(model->insert_event(player->position(), 2));
86         });
87         connect(ui->player_3, &QPushButton::clicked, [this]() {
88                 ui->event_view->selectRow(model->insert_event(player->position(), 3));
89         });
90         connect(ui->player_4, &QPushButton::clicked, [this]() {
91                 ui->event_view->selectRow(model->insert_event(player->position(), 4));
92         });
93         connect(ui->player_5, &QPushButton::clicked, [this]() {
94                 ui->event_view->selectRow(model->insert_event(player->position(), 5));
95         });
96         connect(ui->player_6, &QPushButton::clicked, [this]() {
97                 ui->event_view->selectRow(model->insert_event(player->position(), 6));
98         });
99         connect(ui->player_7, &QPushButton::clicked, [this]() {
100                 ui->event_view->selectRow(model->insert_event(player->position(), 7));
101         });
102
103         // TODO: disable if nothing is selected
104         connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
105         connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
106         connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
107         connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
108         connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
109         connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
110         connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
111         connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
112 }
113
114 void MainWindow::setModel(EventsModel *model)
115 {
116         ui->event_view->setModel(model);
117         this->model = model;
118 }
119
120 void MainWindow::seek(int64_t delta_ms)
121 {
122         int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
123         uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
124         buffered_seek = pos;
125         if (!playing) {
126                 player->setPlaybackRate(0.01);
127                 player->play();  // Or Qt won't show the seek.
128         }
129 }
130
131 void MainWindow::set_current_event_type(const string &type)
132 {
133         QItemSelectionModel *select = ui->event_view->selectionModel();
134         if (!select->hasSelection()) {
135                 return;
136         }
137         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
138         model->set_event_type(row, type);
139 }
140
141 sqlite3 *open_db(const char *filename)
142 {
143         sqlite3 *db;
144         int ret = sqlite3_open(filename, &db);
145         if (ret != SQLITE_OK) {
146                 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
147                 exit(1);
148         }
149
150         sqlite3_exec(db, R"(
151                 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
152         )", nullptr, nullptr, nullptr);  // Ignore errors.
153
154         sqlite3_exec(db, R"(
155                 CREATE TABLE IF NOT EXISTS event (t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
156         )", nullptr, nullptr, nullptr);  // Ignore errors.
157
158         sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
159         sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr);  // Ignore errors.
160         return db;
161 }
162
163 int main(int argc, char *argv[])
164 {
165         QApplication app(argc, argv);
166         sqlite3 *db = open_db("ultimate.db");
167
168         MainWindow mainWindow;
169         mainWindow.setModel(new EventsModel(db));
170         mainWindow.resize(QSize(1280, 720));
171         mainWindow.show();
172
173         return app.exec();
174
175 }