From fd038d3480948dca80cdb41f4d764295918db30d Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 21 Jul 2023 23:00:33 +0200 Subject: [PATCH] Make it possible to have separate videos per match, instead of hardcoding one specific name. --- main.cpp | 5 ++-- mainwindow.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++---- mainwindow.h | 6 +++- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 4654a92..c2d6cd3 100644 --- a/main.cpp +++ b/main.cpp @@ -33,7 +33,7 @@ sqlite3 *open_db(const char *filename) )", nullptr, nullptr, nullptr); // Ignore errors. sqlite3_exec(db, R"( - CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR); + CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR, video_filename VARCHAR); )", nullptr, nullptr, nullptr); // Ignore errors. sqlite3_exec(db, R"( @@ -161,7 +161,8 @@ int main(int argc, char *argv[]) } MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db), - new FormationsModel(db, true), new FormationsModel(db, false)); + new FormationsModel(db, true), new FormationsModel(db, false), + db, match_id); mainWindow.resize(QSize(1280, 720)); mainWindow.show(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 5a1b745..af32288 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -35,16 +36,85 @@ string format_timestamp(uint64_t pos) return buf; } +string get_video_filename(sqlite3 *db, int match_id) +{ + sqlite3_stmt *stmt; + + int ret = sqlite3_prepare_v2(db, "SELECT video_filename FROM match WHERE match=?", -1, &stmt, 0); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db)); + abort(); + } + + sqlite3_bind_int64(stmt, 1, match_id); + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db)); + abort(); + } + + if (sqlite3_column_type(stmt, 0) != SQLITE_TEXT) { + return ""; + } + string filename = (const char *)sqlite3_column_text(stmt, 0); + + ret = sqlite3_finalize(stmt); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db)); + abort(); + } + return filename; +} + +void save_video_filename(sqlite3 *db, int match_id, const string &filename) +{ + sqlite3_stmt *stmt; + + int ret = sqlite3_prepare_v2(db, "UPDATE match SET video_filename=? WHERE match=?", -1, &stmt, 0); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db)); + abort(); + } + + sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC); + sqlite3_bind_int64(stmt, 2, match_id); + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db)); + abort(); + } + + ret = sqlite3_finalize(stmt); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db)); + abort(); + } +} + MainWindow::MainWindow(EventsModel *events, PlayersModel *players, - FormationsModel *offensive_formations, FormationsModel *defensive_formations) - : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations) + FormationsModel *offensive_formations, FormationsModel *defensive_formations, + sqlite3 *db, int match_id) + : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations), db(db), match_id(match_id) { ui = new Ui::MainWindow; ui->setupUi(this); - if (!ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv")) { - // TODO: Pop up a dialog box here instead - fprintf(stderr, "WARNING: Video opening failed\n"); + string filename = get_video_filename(db, match_id); + bool need_save_filename = false; + for ( ;; ) { + if (!filename.empty() && ui->video->open(filename.c_str())) { + break; + } + + // TODO: Probably relativize this path, so that we can move the .db + // more easily with the videos. + filename = QFileDialog::getOpenFileName(this, "Open video").toUtf8(); + need_save_filename = true; + } + if (need_save_filename) { + save_video_filename(db, match_id, filename); } ui->video->play(); diff --git a/mainwindow.h b/mainwindow.h index 95dfae7..ea4ba37 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "ui_mainwindow.h" @@ -15,7 +16,8 @@ class MainWindow : public QMainWindow public: MainWindow(EventsModel *events, PlayersModel *players, - FormationsModel *offensive_formations, FormationsModel *defensive_formations); + FormationsModel *offensive_formations, FormationsModel *defensive_formations, + sqlite3 *db, int match_id); ~MainWindow() { if (ui && ui->video) { ui->video->stop(); @@ -46,4 +48,6 @@ private: bool seeking = false; std::atomic playing{true}; std::optional buffered_seek; + sqlite3 *db; // TODO: Check threading + int match_id; }; -- 2.39.2