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"); });
113 QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
114 connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
115 connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
118 void MainWindow::setModel(EventsModel *model)
120 ui->event_view->setModel(model);
124 void MainWindow::seek(int64_t delta_ms)
126 int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
127 uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
130 player->setPlaybackRate(0.01);
131 player->play(); // Or Qt won't show the seek.
135 void MainWindow::set_current_event_type(const string &type)
137 QItemSelectionModel *select = ui->event_view->selectionModel();
138 if (!select->hasSelection()) {
141 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
142 model->set_event_type(row, type);
145 void MainWindow::delete_current_event()
147 QItemSelectionModel *select = ui->event_view->selectionModel();
148 if (!select->hasSelection()) {
151 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
152 model->delete_event(row);
155 sqlite3 *open_db(const char *filename)
158 int ret = sqlite3_open(filename, &db);
159 if (ret != SQLITE_OK) {
160 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
165 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
166 )", nullptr, nullptr, nullptr); // Ignore errors.
169 CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
170 )", nullptr, nullptr, nullptr); // Ignore errors.
172 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
173 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
177 int main(int argc, char *argv[])
179 QApplication app(argc, argv);
180 sqlite3 *db = open_db("ultimate.db");
182 MainWindow mainWindow;
183 mainWindow.setModel(new EventsModel(db));
184 mainWindow.resize(QSize(1280, 720));