]> git.sesse.net Git - pkanalytics/commitdiff
Make it possible to have separate videos per match, instead of hardcoding one specifi...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 21:00:33 +0000 (23:00 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 21:01:24 +0000 (23:01 +0200)
main.cpp
mainwindow.cpp
mainwindow.h

index 4654a928e4d84117148bd6d6ad704cf7caffcdb4..c2d6cd307382440281b75687c082c06ab7749d4f 100644 (file)
--- 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();
 
index 5a1b7456cd90e6d701a6e9479d88504fe8353b6c..af32288d8a33eb083a5ff2f4ba9f5fc58e63cbcd 100644 (file)
@@ -3,6 +3,7 @@
 #include <QApplication>
 #include <QGridLayout>
 #include <QShortcut>
+#include <QFileDialog>
 #include <QInputDialog>
 #include <QTimer>
 #include <algorithm>
@@ -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();
 
index 95dfae78e3ae060eaf7745c041275d8a5a97a11b..ea4ba37048a4e893280713276b6558a5eba82ae9 100644 (file)
@@ -2,6 +2,7 @@
 #include <QMainWindow>
 #include <QApplication>
 #include <stdint.h>
+#include <sqlite3.h>
 #include <optional>
 #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<bool> playing{true};
        std::optional<uint64_t> buffered_seek;
+       sqlite3 *db;  // TODO: Check threading
+       int match_id;
 };