]> git.sesse.net Git - pkanalytics/blobdiff - mainwindow.cpp
Fix crash if there are no matches (database is empty).
[pkanalytics] / mainwindow.cpp
index 81e7e92dc7c70f41e68ecf9b2e9f76d249f5e68b..5b53d58f0f41c6f7eb143f2371d27733b0da4623 100644 (file)
@@ -67,6 +67,37 @@ string get_video_filename(sqlite3 *db, int match_id)
        return filename;
 }
 
+bool get_match_property(sqlite3 *db, int match_id, const string &prop_name)
+{
+       sqlite3_stmt *stmt;
+
+       int ret = sqlite3_prepare_v2(db, ("SELECT " + prop_name + " FROM match WHERE match=?").c_str(), -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_INTEGER) {
+               return "";
+       }
+       bool value = sqlite3_column_int(stmt, 0);
+
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+       return value;
+}
+
 void save_video_filename(sqlite3 *db, int match_id, const string &filename)
 {
        sqlite3_stmt *stmt;
@@ -82,7 +113,33 @@ void save_video_filename(sqlite3 *db, int match_id, const string &filename)
 
        ret = sqlite3_step(stmt);
        if (ret == SQLITE_ROW) {
-               fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+               fprintf(stderr, "UPDATE 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();
+       }
+}
+
+void save_match_property(sqlite3 *db, int match_id, const string &prop_name, bool value)
+{
+       sqlite3_stmt *stmt;
+
+       int ret = sqlite3_prepare_v2(db, ("UPDATE match SET " + prop_name + "=? WHERE match=?").c_str(), -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       sqlite3_bind_int64(stmt, 1, value);
+       sqlite3_bind_int64(stmt, 2, match_id);
+
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW) {
+               fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
                abort();
        }
 
@@ -212,6 +269,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        });
        connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
        connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
+       connect(ui->stallout, &QPushButton::clicked, [this]() { set_current_event_type("stallout"); });
        connect(ui->soft_plus, &QPushButton::clicked, [this, events]() {
                EventsModel::Status s = events->get_status_at(ui->video->get_position());
                if (s.attack_state == EventsModel::Status::OFFENSE) {
@@ -230,7 +288,8 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        });
        connect(ui->pull_or_was_d, &QPushButton::clicked, [this, events]() {
                EventsModel::Status s = events->get_status_at(ui->video->get_position());
-               if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
+               if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
+                   events->get_status_at(ui->video->get_position() - 1).pull_state == EventsModel::Status::SHOULD_PULL) {
                        set_current_event_type("pull");
                } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
                        insert_noplayer_event("pull_landed");
@@ -270,6 +329,19 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        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(); });
+
+       // Menus.
+       connect(ui->action_exit, &QAction::triggered, [this] { close(); });
+       connect(ui->action_export_json, &QAction::triggered, [db] { export_to_json(db, "ultimate.json"); });
+
+       ui->action_gender_rule_a->setChecked(get_match_property(db, match_id, "gender_rule_a"));
+       ui->action_gender_pull_rule->setChecked(get_match_property(db, match_id, "gender_pull_rule"));
+       connect(ui->action_gender_rule_a, &QAction::toggled, [this, db, match_id] {
+               save_match_property(db, match_id, "gender_rule_a", ui->action_gender_rule_a->isChecked());
+       });
+       connect(ui->action_gender_pull_rule, &QAction::toggled, [this, db, match_id] {
+               save_match_property(db, match_id, "gender_pull_rule", ui->action_gender_pull_rule->isChecked());
+       });
 }
 
 void MainWindow::position_changed(uint64_t pos)
@@ -350,7 +422,7 @@ void MainWindow::insert_or_change_formation(bool offense)
        QItemSelectionModel *select = ui->event_view->selectionModel();
        if (select->hasSelection()) {
                int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-               string expected_type = offense ? "formation_offense" : "formation_defense";
+               EventType expected_type = offense ? EventType::FORMATION_OFFENSE : EventType::FORMATION_DEFENSE;
                if (events->get_event_type(row) == expected_type) {
                        events->set_event_formation(row, formation_id);
                        update_ui_from_time(ui->video->get_position());
@@ -416,7 +488,7 @@ void MainWindow::update_status(uint64_t t)
        snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
                s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
        if (s.stoppage_sec > 0) {
-               char buf2[256];
+               char buf2[512];
                snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
                ui->status->setText(buf2);
        } else {
@@ -497,6 +569,7 @@ void MainWindow::update_action_buttons(uint64_t t)
                ui->throwaway->setEnabled(false);
                ui->drop->setEnabled(false);
                ui->goal->setEnabled(false);
+               ui->stallout->setEnabled(false);
                ui->soft_plus->setEnabled(false);
                ui->soft_minus->setEnabled(false);
                ui->pull_or_was_d->setEnabled(false);
@@ -519,7 +592,8 @@ void MainWindow::update_action_buttons(uint64_t t)
        ui->throwaway->setText("Throwaway (&t)");
        ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
 
-       if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
+       if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
+           (has_selection_with_player && events->get_status_at(ui->video->get_position() - 1).pull_state == EventsModel::Status::SHOULD_PULL)) {  // Can change this event to pull.
                ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
                ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE);
 
@@ -527,6 +601,7 @@ void MainWindow::update_action_buttons(uint64_t t)
                ui->throwaway->setEnabled(false);
                ui->drop->setEnabled(false);
                ui->goal->setEnabled(false);
+               ui->stallout->setEnabled(false);
                ui->soft_plus->setEnabled(false);
                ui->soft_minus->setEnabled(false);
                ui->interception->setEnabled(false);
@@ -553,6 +628,7 @@ void MainWindow::update_action_buttons(uint64_t t)
                ui->catch_->setEnabled(false);
                ui->drop->setEnabled(false);
                ui->goal->setEnabled(false);
+               ui->stallout->setEnabled(false);
                ui->soft_plus->setEnabled(false);
                ui->soft_minus->setEnabled(false);
                ui->interception->setEnabled(false);
@@ -571,6 +647,7 @@ void MainWindow::update_action_buttons(uint64_t t)
        ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
        ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
        ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
+       ui->stallout->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
        ui->soft_plus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
        ui->soft_minus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
        ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);  // Was d-ed.