1 #include <QMediaPlayer>
3 #include <QApplication>
5 #include <QVideoWidget>
12 #include "mainwindow.h"
13 #include "ui_mainwindow.h"
17 std::string format_timestamp(uint64_t pos)
27 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
31 class EventsModel : public QAbstractTableModel
34 EventsModel(sqlite3 *db) : db(db) {}
36 int rowCount(const QModelIndex &parent) const override
41 int columnCount(const QModelIndex &column) const override
45 QVariant headerData(int section, Qt::Orientation orientation, int role) const override
47 if (role != Qt::DisplayRole) {
50 if (orientation == Qt::Horizontal) {
53 } else if (section == 1) {
63 QVariant data(const QModelIndex &index, int role) const override
65 if (role != Qt::DisplayRole) {
69 if (index.column() == 0) {
70 return QString::fromUtf8(format_timestamp(events[index.row()].t));
71 } else if (index.column() == 1) {
72 optional<int> player_id = events[index.row()].player_id;
74 const Player &p = players[*player_id];
75 return QString::fromUtf8(p.name + " (" + p.number + ")");
79 } else if (index.column() == 2) {
80 return QString::fromUtf8(events[index.row()].type);
91 mutable map<int, Player> players;
95 optional<int> player_id;
98 mutable vector<Event> events;
99 mutable bool stale = true;
103 void refresh_if_needed() const;
106 void EventsModel::refresh_if_needed() const
118 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player", -1, &stmt, 0);
119 if (ret != SQLITE_OK) {
120 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
124 ret = sqlite3_step(stmt);
125 if (ret == SQLITE_ROW) {
127 p.player_id = sqlite3_column_int(stmt, 0);
128 p.number = (const char *)sqlite3_column_text(stmt, 1);
129 p.name = (const char *) sqlite3_column_text(stmt, 2);
130 players[p.player_id] = move(p);
131 } else if (ret == SQLITE_DONE) {
134 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
138 ret = sqlite3_finalize(stmt);
139 if (ret != SQLITE_OK) {
140 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
145 ret = sqlite3_prepare_v2(db, "SELECT t, player, type FROM event", -1, &stmt, 0);
146 if (ret != SQLITE_OK) {
147 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
151 ret = sqlite3_step(stmt);
152 if (ret == SQLITE_ROW) {
154 e.t = sqlite3_column_int(stmt, 0);
155 e.player_id = sqlite3_column_int(stmt, 1);
156 e.type = (const char *)sqlite3_column_text(stmt, 2);
157 events.push_back(move(e));
158 } else if (ret == SQLITE_DONE) {
161 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
165 ret = sqlite3_finalize(stmt);
166 if (ret != SQLITE_OK) {
167 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
171 // TODO what if data changes externally?
172 //emit dataChanged(QModelIndex(
175 MainWindow::MainWindow()
177 player = new QMediaPlayer;
178 //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
179 player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
182 ui = new Ui::MainWindow;
185 connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
186 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
188 player->setPosition(*buffered_seek);
189 buffered_seek.reset();
192 player->pause(); // We only played to get a picture.
196 player->setVideoOutput(ui->video);
198 QShortcut *key_k = new QShortcut(QKeySequence(Qt::Key_K), this);
199 connect(key_k, &QShortcut::activated, [this]() { seek(-10000); });
201 QShortcut *key_l = new QShortcut(QKeySequence(Qt::Key_L), this);
202 connect(key_l, &QShortcut::activated, [this]() { seek(10000); });
204 QShortcut *key_left = new QShortcut(QKeySequence(Qt::Key_Left), this);
205 connect(key_left, &QShortcut::activated, [this]() { seek(-1000); });
207 QShortcut *key_right = new QShortcut(QKeySequence(Qt::Key_Right), this);
208 connect(key_right, &QShortcut::activated, [this]() { seek(1000); });
210 // TODO: Would be nice to actually have a frame...
211 QShortcut *key_comma = new QShortcut(QKeySequence(Qt::Key_Comma), this);
212 connect(key_comma, &QShortcut::activated, [this]() { seek(-20); });
214 QShortcut *key_period = new QShortcut(QKeySequence(Qt::Key_Period), this);
215 connect(key_period, &QShortcut::activated, [this]() { seek(20); });
217 QShortcut *key_space = new QShortcut(QKeySequence(Qt::Key_Space), this);
218 connect(key_space, &QShortcut::activated, [this]() {
222 player->setPlaybackRate(1.0);
229 void MainWindow::setModel(QAbstractItemModel *model)
231 ui->event_view->setModel(model);
234 void MainWindow::seek(int64_t delta_ms)
236 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
237 uint64_t pos = std::max<int64_t>(current_pos + delta_ms, 0);
240 player->setPlaybackRate(0.01);
241 player->play(); // Or Qt won't show the seek.
245 sqlite3 *open_db(const char *filename)
248 int ret = sqlite3_open(filename, &db);
249 if (ret != SQLITE_OK) {
250 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
255 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
256 )", nullptr, nullptr, nullptr); // Ignore errors.
259 CREATE TABLE IF NOT EXISTS event (t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
260 )", nullptr, nullptr, nullptr); // Ignore errors.
262 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
263 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
267 int main(int argc, char *argv[])
269 QApplication app(argc, argv);
270 sqlite3 *db = open_db("ultimate.db");
272 MainWindow mainWindow;
273 mainWindow.setModel(new EventsModel(db));
274 mainWindow.resize(QSize(1280, 720));