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;
47 QVariant data(const QModelIndex &index, int role) const override;
55 mutable map<int, Player> players;
59 optional<int> player_id;
62 mutable vector<Event> events;
63 mutable bool stale = true;
67 void refresh_if_needed() const;
70 QVariant EventsModel::headerData(int section, Qt::Orientation orientation, int role) const
72 if (role != Qt::DisplayRole) {
75 if (orientation == Qt::Horizontal) {
78 } else if (section == 1) {
88 QVariant EventsModel::data(const QModelIndex &index, int role) const
90 if (role != Qt::DisplayRole) {
94 if (index.column() == 0) {
95 return QString::fromUtf8(format_timestamp(events[index.row()].t));
96 } else if (index.column() == 1) {
97 optional<int> player_id = events[index.row()].player_id;
99 const Player &p = players[*player_id];
100 return QString::fromUtf8(p.name + " (" + p.number + ")");
104 } else if (index.column() == 2) {
105 return QString::fromUtf8(events[index.row()].type);
110 void EventsModel::refresh_if_needed() const
122 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player", -1, &stmt, 0);
123 if (ret != SQLITE_OK) {
124 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
128 ret = sqlite3_step(stmt);
129 if (ret == SQLITE_ROW) {
131 p.player_id = sqlite3_column_int(stmt, 0);
132 p.number = (const char *)sqlite3_column_text(stmt, 1);
133 p.name = (const char *) sqlite3_column_text(stmt, 2);
134 players[p.player_id] = std::move(p);
135 } else if (ret == SQLITE_DONE) {
138 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
142 ret = sqlite3_finalize(stmt);
143 if (ret != SQLITE_OK) {
144 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
149 ret = sqlite3_prepare_v2(db, "SELECT t, player, type FROM event", -1, &stmt, 0);
150 if (ret != SQLITE_OK) {
151 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
155 ret = sqlite3_step(stmt);
156 if (ret == SQLITE_ROW) {
158 e.t = sqlite3_column_int(stmt, 0);
159 e.player_id = sqlite3_column_int(stmt, 1);
160 e.type = (const char *)sqlite3_column_text(stmt, 2);
161 events.push_back(std::move(e));
162 } else if (ret == SQLITE_DONE) {
165 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
169 ret = sqlite3_finalize(stmt);
170 if (ret != SQLITE_OK) {
171 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
175 // TODO what if data changes externally?
176 //emit dataChanged(QModelIndex(
179 MainWindow::MainWindow()
181 player = new QMediaPlayer;
182 //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
183 player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
186 ui = new Ui::MainWindow;
189 connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
190 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
192 player->setPosition(*buffered_seek);
193 buffered_seek.reset();
196 player->pause(); // We only played to get a picture.
200 player->setVideoOutput(ui->video);
202 connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
203 connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
205 connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
206 connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
208 // TODO: Would be nice to actually have a frame...
209 connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
210 connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
212 connect(ui->play_pause, &QPushButton::clicked, [this]() {
215 ui->play_pause->setText("Play (space)");
217 player->setPlaybackRate(1.0);
219 ui->play_pause->setText("Pause (space)");
223 // Needs to be set anew when we modify setText(), evidently.
224 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
228 void MainWindow::setModel(QAbstractItemModel *model)
230 ui->event_view->setModel(model);
233 void MainWindow::seek(int64_t delta_ms)
235 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
236 uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
239 player->setPlaybackRate(0.01);
240 player->play(); // Or Qt won't show the seek.
244 sqlite3 *open_db(const char *filename)
247 int ret = sqlite3_open(filename, &db);
248 if (ret != SQLITE_OK) {
249 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
254 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
255 )", nullptr, nullptr, nullptr); // Ignore errors.
258 CREATE TABLE IF NOT EXISTS event (t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
259 )", nullptr, nullptr, nullptr); // Ignore errors.
261 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
262 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
266 int main(int argc, char *argv[])
268 QApplication app(argc, argv);
269 sqlite3 *db = open_db("ultimate.db");
271 MainWindow mainWindow;
272 mainWindow.setModel(new EventsModel(db));
273 mainWindow.resize(QSize(1280, 720));