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));
103 // TODO: disable if nothing is selected
104 connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
105 connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
106 connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
107 connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
108 connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
109 connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
110 connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
111 connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
114 void MainWindow::setModel(EventsModel *model)
116 ui->event_view->setModel(model);
120 void MainWindow::seek(int64_t delta_ms)
122 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
123 uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
126 player->setPlaybackRate(0.01);
127 player->play(); // Or Qt won't show the seek.
131 void MainWindow::set_current_event_type(const string &type)
133 QItemSelectionModel *select = ui->event_view->selectionModel();
134 if (!select->hasSelection()) {
137 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
138 model->set_event_type(row, type);
141 sqlite3 *open_db(const char *filename)
144 int ret = sqlite3_open(filename, &db);
145 if (ret != SQLITE_OK) {
146 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
151 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
152 )", nullptr, nullptr, nullptr); // Ignore errors.
155 CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
156 )", nullptr, nullptr, nullptr); // Ignore errors.
158 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
159 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
163 int main(int argc, char *argv[])
165 QApplication app(argc, argv);
166 sqlite3 *db = open_db("ultimate.db");
168 MainWindow mainWindow;
169 mainWindow.setModel(new EventsModel(db));
170 mainWindow.resize(QSize(1280, 720));