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 position_changed(pos);
47 player->setVideoOutput(ui->video);
49 connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
50 connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
52 connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
53 connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
55 // TODO: Would be nice to actually have a frame...
56 connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
57 connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
59 connect(ui->play_pause, &QPushButton::clicked, [this]() {
62 ui->play_pause->setText("Play (space)");
64 player->setPlaybackRate(1.0);
66 ui->play_pause->setText("Pause (space)");
70 // Needs to be set anew when we modify setText(), evidently.
71 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
74 connect(ui->player_1, &QPushButton::clicked, [this]() { insert_event(1); });
75 connect(ui->player_2, &QPushButton::clicked, [this]() { insert_event(2); });
76 connect(ui->player_3, &QPushButton::clicked, [this]() { insert_event(3); });
77 connect(ui->player_4, &QPushButton::clicked, [this]() { insert_event(4); });
78 connect(ui->player_5, &QPushButton::clicked, [this]() { insert_event(5); });
79 connect(ui->player_6, &QPushButton::clicked, [this]() { insert_event(6); });
80 connect(ui->player_7, &QPushButton::clicked, [this]() { insert_event(7); });
82 // TODO: disable if nothing is selected
83 connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
84 connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
85 connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
86 connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
87 connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
88 connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
89 connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
90 connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
92 QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
93 connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
94 connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
97 void MainWindow::position_changed(uint64_t pos)
99 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
101 player->setPosition(*buffered_seek);
102 buffered_seek.reset();
105 player->pause(); // We only played to get a picture.
110 void MainWindow::setModel(EventsModel *model)
112 ui->event_view->setModel(model);
114 connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
115 [this, model](const QModelIndex ¤t, const QModelIndex &previous) {
116 player->setPosition(model->get_time(current.row()));
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::insert_event(int button_id)
133 ui->event_view->selectionModel()->blockSignals(true);
134 ui->event_view->selectRow(model->insert_event(player->position(), button_id));
135 ui->event_view->selectionModel()->blockSignals(false);
138 void MainWindow::set_current_event_type(const string &type)
140 QItemSelectionModel *select = ui->event_view->selectionModel();
141 if (!select->hasSelection()) {
144 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
145 model->set_event_type(row, type);
149 void MainWindow::delete_current_event()
151 QItemSelectionModel *select = ui->event_view->selectionModel();
152 if (!select->hasSelection()) {
155 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
156 ui->event_view->selectionModel()->blockSignals(true);
157 model->delete_event(row);
158 ui->event_view->selectionModel()->blockSignals(false);
162 void MainWindow::update_status()
164 EventsModel::Status s = model->get_status_at(player->position());
166 snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
167 s.our_score, s.their_score, s.offense ? "offense" : "defense", s.num_passes, s.possession_sec);
168 ui->status->setText(buf);
171 sqlite3 *open_db(const char *filename)
174 int ret = sqlite3_open(filename, &db);
175 if (ret != SQLITE_OK) {
176 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
181 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
182 )", nullptr, nullptr, nullptr); // Ignore errors.
185 CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
186 )", nullptr, nullptr, nullptr); // Ignore errors.
188 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
189 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
193 int main(int argc, char *argv[])
195 QApplication app(argc, argv);
196 sqlite3 *db = open_db("ultimate.db");
198 MainWindow mainWindow;
199 mainWindow.setModel(new EventsModel(db));
200 mainWindow.resize(QSize(1280, 720));