1 #include <QMediaPlayer>
3 #include <QApplication>
5 #include <QVideoWidget>
13 #include "mainwindow.h"
14 #include "ui_mainwindow.h"
19 string format_timestamp(uint64_t pos)
29 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
33 MainWindow::MainWindow()
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"));
40 ui = new Ui::MainWindow;
43 connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
44 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
46 player->setPosition(*buffered_seek);
47 buffered_seek.reset();
50 player->pause(); // We only played to get a picture.
54 player->setVideoOutput(ui->video);
56 connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
57 connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
59 connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
60 connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
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); });
66 connect(ui->play_pause, &QPushButton::clicked, [this]() {
69 ui->play_pause->setText("Play (space)");
71 player->setPlaybackRate(1.0);
73 ui->play_pause->setText("Pause (space)");
77 // Needs to be set anew when we modify setText(), evidently.
78 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
81 connect(ui->player_1, &QPushButton::clicked, [this]() {
82 ui->event_view->selectRow(model->insert_event(player->position(), 1));
84 connect(ui->player_2, &QPushButton::clicked, [this]() {
85 ui->event_view->selectRow(model->insert_event(player->position(), 2));
87 connect(ui->player_3, &QPushButton::clicked, [this]() {
88 ui->event_view->selectRow(model->insert_event(player->position(), 3));
90 connect(ui->player_4, &QPushButton::clicked, [this]() {
91 ui->event_view->selectRow(model->insert_event(player->position(), 4));
93 connect(ui->player_5, &QPushButton::clicked, [this]() {
94 ui->event_view->selectRow(model->insert_event(player->position(), 5));
96 connect(ui->player_6, &QPushButton::clicked, [this]() {
97 ui->event_view->selectRow(model->insert_event(player->position(), 6));
99 connect(ui->player_7, &QPushButton::clicked, [this]() {
100 ui->event_view->selectRow(model->insert_event(player->position(), 7));
104 void MainWindow::setModel(EventsModel *model)
106 ui->event_view->setModel(model);
110 void MainWindow::seek(int64_t delta_ms)
112 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
113 uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
116 player->setPlaybackRate(0.01);
117 player->play(); // Or Qt won't show the seek.
121 sqlite3 *open_db(const char *filename)
124 int ret = sqlite3_open(filename, &db);
125 if (ret != SQLITE_OK) {
126 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
131 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
132 )", nullptr, nullptr, nullptr); // Ignore errors.
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.
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.
143 int main(int argc, char *argv[])
145 QApplication app(argc, argv);
146 sqlite3 *db = open_db("ultimate.db");
148 MainWindow mainWindow;
149 mainWindow.setModel(new EventsModel(db));
150 mainWindow.resize(QSize(1280, 720));