]> git.sesse.net Git - pkanalytics/blob - main.cpp
Make it possible to seek to an event by clicking on it.
[pkanalytics] / main.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                 position_changed(pos);
45         });
46
47         player->setVideoOutput(ui->video);
48
49         connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
50         connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
51
52         connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
53         connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
54
55         // TODO: Would be nice to actually have a frame...
56         connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
57         connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
58
59         connect(ui->play_pause, &QPushButton::clicked, [this]() {
60                 if (playing) {
61                         player->pause();
62                         ui->play_pause->setText("Play (space)");
63                 } else {
64                         player->setPlaybackRate(1.0);
65                         player->play();
66                         ui->play_pause->setText("Pause (space)");
67                 }
68                 playing = !playing;
69
70                 // Needs to be set anew when we modify setText(), evidently.
71                 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
72         });
73
74         connect(ui->player_1, &QPushButton::clicked, [this]() { insert_event(1); });
75         connect(ui->player_2, &QPushButton::clicked, [this]() { insert_event(2); });
76         connect(ui->player_3, &QPushButton::clicked, [this]() { insert_event(3); });
77         connect(ui->player_4, &QPushButton::clicked, [this]() { insert_event(4); });
78         connect(ui->player_5, &QPushButton::clicked, [this]() { insert_event(5); });
79         connect(ui->player_6, &QPushButton::clicked, [this]() { insert_event(6); });
80         connect(ui->player_7, &QPushButton::clicked, [this]() { insert_event(7); });
81
82         // TODO: disable if nothing is selected
83         connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
84         connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
85         connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
86         connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
87         connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
88         connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
89         connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
90         connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
91
92         QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
93         connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
94         connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
95 }
96
97 void MainWindow::position_changed(uint64_t pos)
98 {
99         ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
100         if (buffered_seek) {
101                 player->setPosition(*buffered_seek);
102                 buffered_seek.reset();
103         }
104         if (!playing) {
105                 player->pause();  // We only played to get a picture.
106         }
107         update_status();
108 }
109
110 void MainWindow::setModel(EventsModel *model)
111 {
112         ui->event_view->setModel(model);
113         this->model = model;
114         connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
115                 [this, model](const QModelIndex &current, const QModelIndex &previous) {
116                         player->setPosition(model->get_time(current.row()));
117                 });
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::insert_event(int button_id)
132 {
133         ui->event_view->selectionModel()->blockSignals(true);
134         ui->event_view->selectRow(model->insert_event(player->position(), button_id));
135         ui->event_view->selectionModel()->blockSignals(false);
136 }
137
138 void MainWindow::set_current_event_type(const string &type)
139 {
140         QItemSelectionModel *select = ui->event_view->selectionModel();
141         if (!select->hasSelection()) {
142                 return;
143         }
144         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
145         model->set_event_type(row, type);
146         update_status();
147 }
148
149 void MainWindow::delete_current_event()
150 {
151         QItemSelectionModel *select = ui->event_view->selectionModel();
152         if (!select->hasSelection()) {
153                 return;
154         }
155         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
156         ui->event_view->selectionModel()->blockSignals(true);
157         model->delete_event(row);
158         ui->event_view->selectionModel()->blockSignals(false);
159         update_status();
160 }
161
162 void MainWindow::update_status()
163 {
164         EventsModel::Status s = model->get_status_at(player->position());
165         char buf[256];
166         snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
167                 s.our_score, s.their_score, s.offense ? "offense" : "defense", s.num_passes, s.possession_sec);
168         ui->status->setText(buf);
169 }
170
171 sqlite3 *open_db(const char *filename)
172 {
173         sqlite3 *db;
174         int ret = sqlite3_open(filename, &db);
175         if (ret != SQLITE_OK) {
176                 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
177                 exit(1);
178         }
179
180         sqlite3_exec(db, R"(
181                 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
182         )", nullptr, nullptr, nullptr);  // Ignore errors.
183
184         sqlite3_exec(db, R"(
185                 CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
186         )", nullptr, nullptr, nullptr);  // Ignore errors.
187
188         sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
189         sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr);  // Ignore errors.
190         return db;
191 }
192
193 int main(int argc, char *argv[])
194 {
195         QApplication app(argc, argv);
196         sqlite3 *db = open_db("ultimate.db");
197
198         MainWindow mainWindow;
199         mainWindow.setModel(new EventsModel(db));
200         mainWindow.resize(QSize(1280, 720));
201         mainWindow.show();
202
203         return app.exec();
204
205 }