]> git.sesse.net Git - pkanalytics/blobdiff - mainwindow.cpp
Make it possible to have separate videos per match, instead of hardcoding one specifi...
[pkanalytics] / mainwindow.cpp
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();