1 #include <QMediaPlayer>
3 #include <QApplication>
5 #include <QVideoWidget>
13 #include "mainwindow.h"
14 #include "ui_mainwindow.h"
18 string format_timestamp(uint64_t pos)
28 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
32 class EventsModel : public QAbstractTableModel
35 EventsModel(sqlite3 *db) : db(db) {}
37 int rowCount(const QModelIndex &parent) const override
42 int columnCount(const QModelIndex &column) const override
46 QVariant headerData(int section, Qt::Orientation orientation, int role) const override
48 if (role != Qt::DisplayRole) {
51 if (orientation == Qt::Horizontal) {
54 } else if (section == 1) {
64 QVariant data(const QModelIndex &index, int role) const override
66 if (role != Qt::DisplayRole) {
70 if (index.column() == 0) {
71 return QString::fromUtf8(format_timestamp(events[index.row()].t));
72 } else if (index.column() == 1) {
73 optional<int> player_id = events[index.row()].player_id;
75 const Player &p = players[*player_id];
76 return QString::fromUtf8(p.name + " (" + p.number + ")");
80 } else if (index.column() == 2) {
81 return QString::fromUtf8(events[index.row()].type);
92 mutable map<int, Player> players;
96 optional<int> player_id;
99 mutable vector<Event> events;
100 mutable bool stale = true;
104 void refresh_if_needed() const;
107 void EventsModel::refresh_if_needed() const
119 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player", -1, &stmt, 0);
120 if (ret != SQLITE_OK) {
121 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
125 ret = sqlite3_step(stmt);
126 if (ret == SQLITE_ROW) {
128 p.player_id = sqlite3_column_int(stmt, 0);
129 p.number = (const char *)sqlite3_column_text(stmt, 1);
130 p.name = (const char *) sqlite3_column_text(stmt, 2);
131 players[p.player_id] = move(p);
132 } else if (ret == SQLITE_DONE) {
135 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
139 ret = sqlite3_finalize(stmt);
140 if (ret != SQLITE_OK) {
141 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
146 ret = sqlite3_prepare_v2(db, "SELECT t, player, type FROM event", -1, &stmt, 0);
147 if (ret != SQLITE_OK) {
148 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
152 ret = sqlite3_step(stmt);
153 if (ret == SQLITE_ROW) {
155 e.t = sqlite3_column_int(stmt, 0);
156 e.player_id = sqlite3_column_int(stmt, 1);
157 e.type = (const char *)sqlite3_column_text(stmt, 2);
158 events.push_back(move(e));
159 } else if (ret == SQLITE_DONE) {
162 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
166 ret = sqlite3_finalize(stmt);
167 if (ret != SQLITE_OK) {
168 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
172 // TODO what if data changes externally?
173 //emit dataChanged(QModelIndex(
176 MainWindow::MainWindow()
178 player = new QMediaPlayer;
179 //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
180 player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
183 ui = new Ui::MainWindow;
186 connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
187 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
189 player->setPosition(*buffered_seek);
190 buffered_seek.reset();
193 player->pause(); // We only played to get a picture.
197 player->setVideoOutput(ui->video);
199 connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
200 connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
202 connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
203 connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
205 // TODO: Would be nice to actually have a frame...
206 connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
207 connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
209 connect(ui->play_pause, &QPushButton::clicked, [this]() {
212 ui->play_pause->setText("Play (space)");
214 player->setPlaybackRate(1.0);
216 ui->play_pause->setText("Pause (space)");
220 // Needs to be set anew when we modify setText(), evidently.
221 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
225 void MainWindow::setModel(QAbstractItemModel *model)
227 ui->event_view->setModel(model);
230 void MainWindow::seek(int64_t delta_ms)
232 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
233 uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
236 player->setPlaybackRate(0.01);
237 player->play(); // Or Qt won't show the seek.
241 sqlite3 *open_db(const char *filename)
244 int ret = sqlite3_open(filename, &db);
245 if (ret != SQLITE_OK) {
246 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
251 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
252 )", nullptr, nullptr, nullptr); // Ignore errors.
255 CREATE TABLE IF NOT EXISTS event (t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
256 )", nullptr, nullptr, nullptr); // Ignore errors.
258 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
259 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
263 int main(int argc, char *argv[])
265 QApplication app(argc, argv);
266 sqlite3 *db = open_db("ultimate.db");
268 MainWindow mainWindow;
269 mainWindow.setModel(new EventsModel(db));
270 mainWindow.resize(QSize(1280, 720));