]> git.sesse.net Git - pkanalytics/blob - stats.cpp
c8f0be5ab196c186a81fc3f1fe343cab9ef465b6
[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
104 void MainWindow::setModel(EventsModel *model)
105 {
106         ui->event_view->setModel(model);
107         this->model = model;
108 }
109
110 void MainWindow::seek(int64_t delta_ms)
111 {
112         int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
113         uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
114         buffered_seek = pos;
115         if (!playing) {
116                 player->setPlaybackRate(0.01);
117                 player->play();  // Or Qt won't show the seek.
118         }
119 }
120
121 sqlite3 *open_db(const char *filename)
122 {
123         sqlite3 *db;
124         int ret = sqlite3_open(filename, &db);
125         if (ret != SQLITE_OK) {
126                 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
127                 exit(1);
128         }
129
130         sqlite3_exec(db, R"(
131                 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
132         )", nullptr, nullptr, nullptr);  // Ignore errors.
133
134         sqlite3_exec(db, R"(
135                 CREATE TABLE IF NOT EXISTS event (t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
136         )", nullptr, nullptr, nullptr);  // Ignore errors.
137
138         sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
139         sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr);  // Ignore errors.
140         return db;
141 }
142
143 int main(int argc, char *argv[])
144 {
145         QApplication app(argc, argv);
146         sqlite3 *db = open_db("ultimate.db");
147
148         MainWindow mainWindow;
149         mainWindow.setModel(new EventsModel(db));
150         mainWindow.resize(QSize(1280, 720));
151         mainWindow.show();
152
153         return app.exec();
154
155 }