#include <QMainWindow>
#include <QApplication>
#include <QGridLayout>
-#include <QVideoWidget>
#include <QShortcut>
+#include <QInputDialog>
+#include <QTimer>
#include <algorithm>
#include <string>
#include <map>
#include <optional>
#include <sqlite3.h>
#include "mainwindow.h"
-#include "ui_mainwindow.h"
#include "events.h"
#include "players.h"
+#include "formations.h"
+#include "json.h"
using namespace std;
-string format_timestamp(uint64_t pos)
-{
- int ms = pos % 1000;
- pos /= 1000;
- int sec = pos % 60;
- pos /= 60;
- int min = pos % 60;
- int hour = pos / 60;
-
- char buf[256];
- snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
- return buf;
-}
-
-MainWindow::MainWindow(EventsModel *events, PlayersModel *players) : events(events), players(players)
+sqlite3 *open_db(const char *filename)
{
- video = new QMediaPlayer;
- //video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
- video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
- video->play();
-
- ui = new Ui::MainWindow;
- ui->setupUi(this);
-
- ui->event_view->setModel(events);
- connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
- [this, events](const QModelIndex ¤t, const QModelIndex &previous) {
- video->setPosition(events->get_time(current.row()));
- });
-
- ui->player_view->setModel(players);
-
- connect(video, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
- position_changed(pos);
- });
+ sqlite3 *db;
+ int ret = sqlite3_open(filename, &db);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
+ exit(1);
+ }
- video->setVideoOutput(ui->video);
+ sqlite3_exec(db, R"(
+ CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR, gender VARCHAR(1));
+ )", nullptr, nullptr, nullptr); // Ignore errors.
- connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
- connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
+ sqlite3_exec(db, R"(
+ CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR, video_filename VARCHAR, gender_rule_a BOOLEAN DEFAULT false, gender_pull_rule BOOLEAN DEFAULT false);
+ )", nullptr, nullptr, nullptr); // Ignore errors.
- connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
- connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
+ sqlite3_exec(db, R"(
+ CREATE TABLE IF NOT EXISTS formation (formation INTEGER PRIMARY KEY, name VARCHAR, offense BOOLEAN NOT NULL);
+ )", nullptr, nullptr, nullptr); // Ignore errors.
- // TODO: Would be nice to actually have a frame...
- connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
- connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
+ sqlite3_exec(db, R"(
+ CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, match INTEGER, t INTEGER, player INTEGER, type VARCHAR, formation INTEGER, FOREIGN KEY (player) REFERENCES player(player), FOREIGN KEY (match) REFERENCES match (match), FOREIGN KEY (formation) REFERENCES formation (formation));
+ )", nullptr, nullptr, nullptr); // Ignore errors.
- connect(ui->play_pause, &QPushButton::clicked, [this]() {
- if (playing) {
- video->pause();
- ui->play_pause->setText("Play (space)");
- } else {
- video->setPlaybackRate(1.0);
- video->play();
- ui->play_pause->setText("Pause (space)");
- }
- playing = !playing;
-
- // Needs to be set anew when we modify setText(), evidently.
- ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
- });
-
- connect(ui->player_1, &QPushButton::clicked, [this]() { insert_event(1); });
- connect(ui->player_2, &QPushButton::clicked, [this]() { insert_event(2); });
- connect(ui->player_3, &QPushButton::clicked, [this]() { insert_event(3); });
- connect(ui->player_4, &QPushButton::clicked, [this]() { insert_event(4); });
- connect(ui->player_5, &QPushButton::clicked, [this]() { insert_event(5); });
- connect(ui->player_6, &QPushButton::clicked, [this]() { insert_event(6); });
- connect(ui->player_7, &QPushButton::clicked, [this]() { insert_event(7); });
-
- // TODO: disable if nothing is selected
- connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
- connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
- connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
- connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
- connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
- connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
- connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
- connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
-
- connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); });
-
- QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
- connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
- connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
+ sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
+ sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
+ sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr); // Ignore errors.
+ return db;
}
-
-void MainWindow::position_changed(uint64_t pos)
+int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
{
- ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
- if (buffered_seek) {
- video->setPosition(*buffered_seek);
- buffered_seek.reset();
- }
- if (!playing) {
- video->pause(); // We only played to get a picture.
- }
- update_status();
-}
+ QStringList items;
+ vector<int> ids;
+ bool requested_match_ok = false;
-void MainWindow::seek(int64_t delta_ms)
-{
- int64_t current_pos = buffered_seek ? *buffered_seek : video->position();
- uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
- buffered_seek = pos;
- if (!playing) {
- video->setPlaybackRate(0.01);
- video->play(); // Or Qt won't show the seek.
+ // Read the list of matches already in the database.
+ sqlite3_stmt *stmt;
+ int ret = sqlite3_prepare_v2(db, "SELECT match, description FROM match ORDER BY match", -1, &stmt, 0);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
+ abort();
}
-}
-
-void MainWindow::insert_event(int button_id)
-{
- ui->event_view->selectionModel()->blockSignals(true);
- ui->event_view->selectRow(events->insert_event(video->position(), button_id));
- ui->event_view->selectionModel()->blockSignals(false);
-}
-
-void MainWindow::set_current_event_type(const string &type)
-{
- QItemSelectionModel *select = ui->event_view->selectionModel();
- if (!select->hasSelection()) {
- return;
+ for ( ;; ) {
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ char buf[256];
+ snprintf(buf, sizeof(buf), "%s (%d)", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 0));
+ ids.push_back(sqlite3_column_int(stmt, 0));
+ if (ids.back() == requested_match) {
+ requested_match_ok = true;
+ }
+ items.push_back(buf);
+ } else if (ret == SQLITE_DONE) {
+ break;
+ } else {
+ fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
}
- int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
- events->set_event_type(row, type);
- update_status();
-}
-
-void MainWindow::delete_current_event()
-{
- QItemSelectionModel *select = ui->event_view->selectionModel();
- if (!select->hasSelection()) {
- return;
+ ret = sqlite3_finalize(stmt);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
+ abort();
}
- int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
- ui->event_view->selectionModel()->blockSignals(true);
- events->delete_event(row);
- ui->event_view->selectionModel()->blockSignals(false);
- update_status();
-}
-
-void MainWindow::make_substitution()
-{
- QItemSelectionModel *select = ui->player_view->selectionModel();
-
- // FIXME: we should backdate t to start of point (last goal, or 0) if:
- // - no players we're removing have had actions yet
- // - there have been no other in/out events
- //
- // ...but if so, we might need to modify in/out events that are already there
- // (perhaps just overwrite them all?)
- uint64_t t = video->position();
+ items.push_back("Add new…");
- set<int> old_team = events->get_team_at(t);
- set<int> new_team;
-
- for (QModelIndex row : select->selectedRows()) {
- new_team.insert(players->get_player_id(row.row()));
+ if (requested_match_ok) {
+ return requested_match;
}
- for (int player_id : old_team) {
- if (!new_team.count(player_id)) {
- events->insert_event(t, player_id, "out");
+ QString chosen_str;
+ {
+ QInputDialog dialog(parent, Qt::WindowFlags());
+ dialog.setWindowTitle("Open game");
+ dialog.setLabelText("Choose game to analyze:");
+ dialog.setComboBoxItems(items);
+ if (items.size() >= 2) {
+ dialog.setTextValue(items[items.size() - 2]);
+ }
+ dialog.setOption(QInputDialog::UseListViewForComboBoxItems, true);
+ if (!dialog.exec()) {
+ return -1;
}
+ chosen_str = dialog.textValue();
}
- for (int player_id : new_team) {
- if (!old_team.count(player_id)) {
- events->insert_event(t, player_id, "in");
+
+ for (unsigned i = 0; i < ids.size(); ++i) {
+ if (chosen_str == items[i]) {
+ return ids[i];
}
}
-}
-void MainWindow::update_status()
-{
- EventsModel::Status s = events->get_status_at(video->position());
- char buf[256];
- snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
- s.our_score, s.their_score, s.offense ? "offense" : "defense", s.num_passes, s.possession_sec);
- ui->status->setText(buf);
-}
+ // Must be a new game. Get its name and insert it into the database.
+ bool ok;
+ QString new_game_str = QInputDialog::getText(parent, "New game", "Choose name for new game:", QLineEdit::Normal, "", &ok);
+ if (!ok || new_game_str.isEmpty()) {
+ return -1;
+ }
-sqlite3 *open_db(const char *filename)
-{
- sqlite3 *db;
- int ret = sqlite3_open(filename, &db);
+ // Insert the new row into the database.
+ ret = sqlite3_prepare_v2(db, "INSERT INTO match (description, gender_rule_a, gender_pull_rule) VALUES (?, COALESCE((SELECT gender_rule_a FROM match ORDER BY match DESC LIMIT 1),false), COALESCE((SELECT gender_pull_rule FROM match ORDER BY match DESC LIMIT 1),false))", -1, &stmt, 0);
if (ret != SQLITE_OK) {
- fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
- exit(1);
+ fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+ abort();
}
- sqlite3_exec(db, R"(
- CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
- )", nullptr, nullptr, nullptr); // Ignore errors.
+ QByteArray new_game_utf8 = new_game_str.toUtf8();
+ sqlite3_bind_text(stmt, 1, (const char *)new_game_utf8.data(), new_game_utf8.size(), SQLITE_STATIC);
- sqlite3_exec(db, R"(
- CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
- )", nullptr, nullptr, nullptr); // Ignore errors.
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
- sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
- sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
- return db;
+ ret = sqlite3_finalize(stmt);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ return sqlite3_last_insert_rowid(db);
}
int main(int argc, char *argv[])
QApplication app(argc, argv);
sqlite3 *db = open_db("ultimate.db");
- MainWindow mainWindow(new EventsModel(db), new PlayersModel(db));
+ int requested_match = -1;
+ if (argc >= 2) {
+ requested_match = atoi(argv[1]);
+ }
+
+ int match_id = get_match_id(db, nullptr, requested_match);
+ if (match_id <= 0) { // Cancel.
+ return 0;
+ }
+
+ MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db),
+ new FormationsModel(db, true), new FormationsModel(db, false),
+ db, match_id);
mainWindow.resize(QSize(1280, 720));
mainWindow.show();
- return app.exec();
+ int ret = app.exec();
+ return ret;
}