]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Fix some -Wsign-compare warnings.
[pkanalytics] / main.cpp
index 42d42cc0899300aefd254b5f3682e3856f308388..1c0f2e3017075034b570b28ee041a4e9c76e6231 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -5,6 +5,7 @@
 #include <QVideoWidget>
 #include <QShortcut>
 #include <QInputDialog>
+#include <QTimer>
 #include <algorithm>
 #include <string>
 #include <map>
@@ -51,7 +52,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        ui->event_view->setColumnWidth(2, 150);
        connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
                [this, events](const QModelIndex &current, const QModelIndex &previous) {
-                       uint64_t t = events->get_time(current.row());
+                       int64_t t = events->get_time(current.row());
                        if (t != video->position()) {
                                video->setPosition(events->get_time(current.row()));
                        } else {
@@ -68,7 +69,9 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        ui->player_view->horizontalHeader()->setStretchLastSection(true);
 
        auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
-               update_action_buttons(video->position());
+               QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
+                       update_action_buttons(video->position());
+               });
        };
        ui->offensive_formation_view->setModel(offensive_formations);
        ui->defensive_formation_view->setModel(defensive_formations);
@@ -157,7 +160,7 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
                EventsModel::Status s = events->get_status_at(video->position());
                if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
                        set_current_event_type("pull");
-               } else if (EventsModel::Status::PULL_IN_AIR) {
+               } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
                        insert_noplayer_event("pull_landed");
                }
        });
@@ -177,6 +180,9 @@ MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
        connect(ui->defensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_plus"); });
        connect(ui->defensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_minus"); });
 
+       connect(ui->offensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/true); });
+       connect(ui->defensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/false); });
+
        // Misc. events
        connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); });
        connect(ui->stoppage, &QPushButton::clicked, [this, events]() {
@@ -226,7 +232,7 @@ void MainWindow::insert_player_event(int button_id)
 {
        uint64_t t = video->position();
        vector<int> team = events->sort_team(events->get_team_at(t));
-       if (button_id >= team.size()) {
+       if (unsigned(button_id) >= team.size()) {
                return;
        }
        int player_id = team[button_id];
@@ -267,6 +273,39 @@ void MainWindow::set_current_event_type(const string &type)
        update_ui_from_time(video->position());
 }
 
+// Formation buttons either modify the existing formation (if we've selected
+// a formation change event), or insert a new one (if not).
+void MainWindow::insert_or_change_formation(bool offense)
+{
+       FormationsModel *formations = offense ? offensive_formations : defensive_formations;
+       QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
+       if (!formation_view->selectionModel()->hasSelection()) {
+               // This shouldn't happen; the button should not have been enabled.
+               return;
+       }
+       int formation_row = formation_view->selectionModel()->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+       int formation_id = formations->get_formation_id(formation_row);
+       if (formation_id == -1) {
+               // This also shouldn't happen (“Add new…” selected).
+               return;
+       }
+
+       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";
+               if (events->get_event_type(row) == expected_type) {
+                       events->set_event_formation(row, formation_id);
+                       update_ui_from_time(video->position());
+                       return;
+               }
+       }
+
+       // Insert a new formation event instead (same as double-click on the selected one).
+       events->set_formation_at(video->position(), offense, formation_id);
+       update_ui_from_time(video->position());
+}
+
 void MainWindow::delete_current_event()
 {
        QItemSelectionModel *select = ui->event_view->selectionModel();
@@ -301,15 +340,23 @@ void MainWindow::update_status(uint64_t t)
 {
        EventsModel::Status s = events->get_status_at(t);
        char buf[256];
-       const char *offense = "not started";
+       std::string formation = "Not started";
        if (s.attack_state == EventsModel::Status::OFFENSE) {
-               offense = "offense";
+               if (s.offensive_formation != 0) {
+                       formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
+               } else {
+                       formation = "Offense";
+               }
        } else if (s.attack_state == EventsModel::Status::DEFENSE) {
-               offense = "defense";
+               if (s.defensive_formation != 0) {
+                       formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
+               } else {
+                       formation = "Defense";
+               }
        }
 
        snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
-               s.our_score, s.their_score, offense, s.num_passes, s.possession_sec);
+               s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
        if (s.stoppage_sec > 0) {
                char buf2[256];
                snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
@@ -498,6 +545,7 @@ void MainWindow::formation_double_clicked(bool offense, unsigned row)
        } else {
                events->set_formation_at(video->position(), offense, id);
        }
+       update_ui_from_time(video->position());
 }
 
 sqlite3 *open_db(const char *filename)