]> git.sesse.net Git - pkanalytics/commitdiff
Move MainWindow into its own class.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 20:22:46 +0000 (22:22 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jul 2023 20:22:46 +0000 (22:22 +0200)
main.cpp
mainwindow.cpp [new file with mode: 0644]
meson.build

index 4caeac38c4d839450dd1db7dd16f28677637943d..4654a928e4d84117148bd6d6ad704cf7caffcdb4 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include <optional>
 #include <sqlite3.h>
 #include "mainwindow.h"
-#include "ui_mainwindow.h"
 #include "events.h"
 #include "players.h"
 #include "formations.h"
 #include "json.h"
-#include "video_widget.h"
 
 using namespace std;
 
-string format_timestamp(uint64_t pos)
-{
-       int ms = pos % 1000;
-       pos /= 1000;
-       int sec = pos % 60;
-       pos /= 60;
-       int min = pos % 60;
-       int hour = pos / 60;
-
-       char buf[256];
-       snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
-       return buf;
-}
-
-MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
-                       FormationsModel *offensive_formations, FormationsModel *defensive_formations)
-       : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations)
-{
-       ui = new Ui::MainWindow;
-       ui->setupUi(this);
-
-       ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv");
-       ui->video->play();
-
-       ui->event_view->setModel(events);
-       ui->event_view->setColumnWidth(1, 150);
-       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());
-                       if (t != ui->video->get_position()) {
-                               ui->video->seek_absolute(events->get_time(current.row()));
-                       } else {
-                               // Selection could have changed, so we still need to update.
-                               // (Just calling setPosition() would not give us the signal
-                               // in this case.)
-                               update_ui_from_time(t);
-                       }
-               });
-
-       ui->player_view->setModel(players);
-       ui->player_view->setColumnWidth(0, 30);
-       ui->player_view->setColumnWidth(1, 20);
-       ui->player_view->horizontalHeader()->setStretchLastSection(true);
-
-       auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
-               QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
-                       update_action_buttons(ui->video->get_position());
-               });
-       };
-       ui->offensive_formation_view->setModel(offensive_formations);
-       ui->defensive_formation_view->setModel(defensive_formations);
-       connect(ui->offensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
-       connect(ui->defensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
-       connect(ui->offensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
-               formation_double_clicked(true, index.row());
-       });
-       connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
-               formation_double_clicked(false, index.row());
-       });
-
-       connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
-               position_changed(pos);
-       });
-
-       // It's not really clear whether PgUp should be forwards or backwards,
-       // but mpv does at least up = forwards, so that's probably standard.
-       QShortcut *pgdown = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
-       connect(pgdown, &QShortcut::activated, [this]() { ui->video->seek(-120000); });
-       QShortcut *pgup = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
-       connect(pgup, &QShortcut::activated, [this]() { ui->video->seek(120000); });
-
-       connect(ui->minus10s, &QPushButton::clicked, [this]() { ui->video->seek(-10000); });
-       connect(ui->plus10s, &QPushButton::clicked, [this]() { ui->video->seek(10000); });
-
-       connect(ui->minus2s, &QPushButton::clicked, [this]() { ui->video->seek(-2000); });
-       connect(ui->plus2s, &QPushButton::clicked, [this]() { ui->video->seek(2000); });
-       connect(ui->video, &VideoWidget::mouse_back_clicked, [this]() { ui->video->seek(-2000); });
-       connect(ui->video, &VideoWidget::mouse_forward_clicked, [this]() { ui->video->seek(2000); });
-
-       connect(ui->minus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(-1); });
-       connect(ui->plus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(1); });
-
-       connect(ui->play_pause, &QPushButton::clicked, [this]() {
-               if (playing) {
-                       ui->video->pause();
-                       ui->play_pause->setText("Play (space)");
-               } else {
-                       ui->video->play();
-                       ui->play_pause->setText("Pause (space)");
-               }
-               playing = !playing;
-
-               // Needs to be set anew when we modify setText(), evidently.
-               ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
-       });
-
-       connect(ui->player_1, &QPushButton::clicked, [this]() { insert_player_event(0); });
-       connect(ui->player_2, &QPushButton::clicked, [this]() { insert_player_event(1); });
-       connect(ui->player_3, &QPushButton::clicked, [this]() { insert_player_event(2); });
-       connect(ui->player_4, &QPushButton::clicked, [this]() { insert_player_event(3); });
-       connect(ui->player_5, &QPushButton::clicked, [this]() { insert_player_event(4); });
-       connect(ui->player_6, &QPushButton::clicked, [this]() { insert_player_event(5); });
-       connect(ui->player_7, &QPushButton::clicked, [this]() { insert_player_event(6); });
-
-       // Offensive events
-       connect(ui->offense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_offense"); });
-       connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
-       connect(ui->throwaway, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(ui->video->get_position());
-               if (s.attack_state == EventsModel::Status::DEFENSE && s.pull_state == EventsModel::Status::PULL_IN_AIR) {
-                       insert_noplayer_event("pull_oob");
-               } else {
-                       set_current_event_type("throwaway");
-               }
-       });
-       connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
-       connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
-       connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
-       connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
-       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) {
-                       set_current_event_type("pull");
-               } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
-                       insert_noplayer_event("pull_landed");
-               } else if (s.pull_state == EventsModel::Status::NOT_PULLING) {
-                       set_current_event_type("was_d");
-               }
-       });
-
-       // Defensive events (TODO add more)
-       connect(ui->interception, &QPushButton::clicked, [this]() { set_current_event_type("interception"); });
-       connect(ui->defense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_defense"); });
-       connect(ui->their_throwaway, &QPushButton::clicked, [this]() { insert_noplayer_event("their_throwaway"); });
-       connect(ui->their_goal, &QPushButton::clicked, [this]() { insert_noplayer_event("their_goal"); });
-       connect(ui->their_pull, &QPushButton::clicked, [this, events]() {
-               EventsModel::Status s = events->get_status_at(ui->video->get_position());
-               if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
-                       insert_noplayer_event("their_pull");
-               }
-       });
-       connect(ui->our_defense, &QPushButton::clicked, [this]() { set_current_event_type("defense"); });
-       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]() {
-               EventsModel::Status s = events->get_status_at(ui->video->get_position());
-               if (s.stoppage) {
-                       insert_noplayer_event("restart");
-               } else {
-                       insert_noplayer_event("stoppage");
-               }
-       });
-       connect(ui->unknown, &QPushButton::clicked, [this]() { insert_noplayer_event("unknown"); });
-
-       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(); });
-}
-
-void MainWindow::position_changed(uint64_t pos)
-{
-       ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
-       if (!playing) {
-               ui->video->pause();  // We only played to get a picture.
-       }
-       if (playing) {
-               QModelIndex row = events->get_last_event_qt(ui->video->get_position());
-               ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
-       }
-       update_ui_from_time(pos);
-}
-
-void MainWindow::insert_player_event(int button_id)
-{
-       uint64_t t = ui->video->get_position();
-       vector<int> team = events->sort_team(events->get_team_at(t));
-       if (unsigned(button_id) >= team.size()) {
-               return;
-       }
-       int player_id = team[button_id];
-
-       EventsModel::Status s = events->get_status_at(t);
-
-       ui->event_view->selectionModel()->blockSignals(true);
-       if (s.attack_state == EventsModel::Status::OFFENSE) {
-               // TODO: Perhaps not if that player already did the last catch?
-               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
-       } else {
-               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
-       }
-       ui->event_view->selectionModel()->blockSignals(false);
-
-       update_ui_from_time(t);
-}
-
-void MainWindow::insert_noplayer_event(const string &type)
-{
-       uint64_t t = ui->video->get_position();
-
-       ui->event_view->selectionModel()->blockSignals(true);
-       ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
-       ui->event_view->selectionModel()->blockSignals(false);
-
-       update_ui_from_time(t);
-}
-
-void MainWindow::set_current_event_type(const string &type)
-{
-       QItemSelectionModel *select = ui->event_view->selectionModel();
-       if (!select->hasSelection()) {
-               return;
-       }
-       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-       events->set_event_type(row, type);
-       update_ui_from_time(ui->video->get_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(ui->video->get_position());
-                       return;
-               }
-       }
-
-       // Insert a new formation event instead (same as double-click on the selected one).
-       events->set_formation_at(ui->video->get_position(), offense, formation_id);
-       update_ui_from_time(ui->video->get_position());
-}
-
-void MainWindow::delete_current_event()
-{
-       QItemSelectionModel *select = ui->event_view->selectionModel();
-       if (!select->hasSelection()) {
-               return;
-       }
-       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-       ui->event_view->selectionModel()->blockSignals(true);
-       events->delete_event(row);
-       ui->event_view->selectionModel()->blockSignals(false);
-       update_ui_from_time(ui->video->get_position());
-}
-
-void MainWindow::make_substitution()
-{
-       QItemSelectionModel *select = ui->player_view->selectionModel();
-       set<int> new_team;
-       for (QModelIndex row : select->selectedRows()) {
-               new_team.insert(players->get_player_id(row.row()));
-       }
-       events->set_team_at(ui->video->get_position(), new_team);
-       update_player_buttons(ui->video->get_position());
-}
-
-void MainWindow::update_ui_from_time(uint64_t t)
-{
-       update_status(t);
-       update_player_buttons(t);
-       update_action_buttons(t);
-}
-
-void MainWindow::update_status(uint64_t t)
-{
-       EventsModel::Status s = events->get_status_at(t);
-       char buf[256];
-       std::string formation = "Not started";
-       if (s.attack_state == EventsModel::Status::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) {
-               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, 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);
-               ui->status->setText(buf2);
-       } else {
-               ui->status->setText(buf);
-       }
-}
-
-void MainWindow::update_player_buttons(uint64_t t)
-{
-       QPushButton *buttons[] = {
-               ui->player_1,
-               ui->player_2,
-               ui->player_3,
-               ui->player_4,
-               ui->player_5,
-               ui->player_6,
-               ui->player_7
-       };
-       const char shortcuts[] = "qweasdf";
-       int num_players = 0;
-       for (int player_id : events->sort_team(events->get_team_at(t))) {
-               QPushButton *btn = buttons[num_players];
-               string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
-               char shortcut[2] = "";
-               shortcut[0] = toupper(shortcuts[num_players]);
-               btn->setText(QString::fromUtf8(label));
-               btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
-               btn->setEnabled(true);
-               if (++num_players == 7) {
-                       break;
-               }
-       }
-       for (int i = num_players; i < 7; ++i) {
-               QPushButton *btn = buttons[i];
-               btn->setText("No player");
-               btn->setEnabled(false);
-       }
-}
-
-void MainWindow::update_action_buttons(uint64_t t)
-{
-       {
-               QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
-               if (select->hasSelection()) {
-                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-                       ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
-               } else {
-                       ui->offensive_formation->setEnabled(false);
-               }
-       }
-       {
-               QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
-               if (select->hasSelection()) {
-                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-                       ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
-               } else {
-                       ui->defensive_formation->setEnabled(false);
-               }
-       }
-
-       EventsModel::Status s = events->get_status_at(t);
-
-       bool has_selection = false;
-       bool has_selection_with_player = false;
-
-       QItemSelectionModel *select = ui->event_view->selectionModel();
-       if (select->hasSelection()) {
-               has_selection = true;
-               int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-               has_selection_with_player = events->get_player_id(row).has_value();
-       }
-       ui->delete_->setEnabled(has_selection);
-
-       if (s.stoppage) {
-               ui->stoppage->setText("Restart (&v)");
-               ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
-               ui->catch_->setEnabled(false);
-               ui->throwaway->setEnabled(false);
-               ui->drop->setEnabled(false);
-               ui->goal->setEnabled(false);
-               ui->offensive_soft_plus->setEnabled(false);
-               ui->offensive_soft_minus->setEnabled(false);
-               ui->pull_or_was_d->setEnabled(false);
-               ui->interception->setEnabled(false);
-               ui->their_throwaway->setEnabled(false);
-               ui->our_defense->setEnabled(false);
-               ui->their_goal->setEnabled(false);
-               ui->defensive_soft_plus->setEnabled(false);
-               ui->defensive_soft_minus->setEnabled(false);
-               ui->their_pull->setEnabled(false);
-               return;
-       } else {
-               ui->stoppage->setText("Stoppage (&v)");
-               ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
-       }
-
-       // Defaults for pull-related buttons.
-       ui->pull_or_was_d->setText("Pull (&p)");
-       ui->their_pull->setText("Their pull (&p)");
-       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
-       ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
-       ui->throwaway->setText("Throwaway (&t)");
-       ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
-
-       if (s.pull_state == EventsModel::Status::SHOULD_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);
-
-               ui->catch_->setEnabled(false);
-               ui->throwaway->setEnabled(false);
-               ui->drop->setEnabled(false);
-               ui->goal->setEnabled(false);
-               ui->offensive_soft_plus->setEnabled(false);
-               ui->offensive_soft_minus->setEnabled(false);
-               ui->interception->setEnabled(false);
-               ui->their_throwaway->setEnabled(false);
-               ui->our_defense->setEnabled(false);
-               ui->their_goal->setEnabled(false);
-               ui->defensive_soft_plus->setEnabled(false);
-               ui->defensive_soft_minus->setEnabled(false);
-               return;
-       }
-       if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
-               if (s.attack_state == EventsModel::Status::DEFENSE) {
-                       ui->pull_or_was_d->setText("Pull landed (&p)");
-                       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
-                       ui->pull_or_was_d->setEnabled(true);
-
-                       ui->throwaway->setText("Pull OOB (&t)");
-                       ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
-                       ui->throwaway->setEnabled(true);
-               } else {
-                       ui->pull_or_was_d->setEnabled(false);
-                       ui->throwaway->setEnabled(false);
-               }
-               ui->their_pull->setEnabled(false);  // We don't track their pull landings; only by means of catch etc.
-
-               ui->catch_->setEnabled(false);
-               ui->drop->setEnabled(false);
-               ui->goal->setEnabled(false);
-               ui->offensive_soft_plus->setEnabled(false);
-               ui->offensive_soft_minus->setEnabled(false);
-               ui->interception->setEnabled(false);
-               ui->their_throwaway->setEnabled(false);
-               ui->our_defense->setEnabled(false);
-               ui->their_goal->setEnabled(false);
-               ui->defensive_soft_plus->setEnabled(false);
-               ui->defensive_soft_minus->setEnabled(false);
-               return;
-       }
-
-       // Not pulling, so reuse the pull button for got d-ed.
-       ui->pull_or_was_d->setText("Was d-ed (&z)");
-       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
-       ui->pull_or_was_d->setEnabled(true);
-
-       ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
-       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->offensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
-       ui->offensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
-       ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);  // Was d-ed.
-
-       ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
-       ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
-       ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
-       ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
-       ui->defensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
-       ui->defensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
-       ui->their_pull->setEnabled(false);
-}
-
-void MainWindow::formation_double_clicked(bool offense, unsigned row)
-{
-       FormationsModel *formations = offense ? offensive_formations : defensive_formations;
-       int id = formations->get_formation_id(row);
-       if (id == -1) {  // “Add new” clicked.
-               bool ok;
-               QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
-               if (!ok || new_formation_str.isEmpty()) {
-                       return;
-               }
-
-               id = formations->insert_new(new_formation_str.toStdString());
-               QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
-               view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
-               events->inserted_new_formation(id, new_formation_str.toStdString());
-       } else {
-               events->set_formation_at(ui->video->get_position(), offense, id);
-       }
-       update_ui_from_time(ui->video->get_position());
-}
-
 sqlite3 *open_db(const char *filename)
 {
        sqlite3 *db;
@@ -556,7 +49,6 @@ sqlite3 *open_db(const char *filename)
        sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);  // Ignore errors.
        return db;
 }
-
 int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
 {
        QStringList items;
diff --git a/mainwindow.cpp b/mainwindow.cpp
new file mode 100644 (file)
index 0000000..7eec2cb
--- /dev/null
@@ -0,0 +1,528 @@
+#include <QMediaPlayer>
+#include <QMainWindow>
+#include <QApplication>
+#include <QGridLayout>
+#include <QShortcut>
+#include <QInputDialog>
+#include <QTimer>
+#include <algorithm>
+#include <string>
+#include <map>
+#include <vector>
+#include <optional>
+#include <sqlite3.h>
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include "events.h"
+#include "players.h"
+#include "formations.h"
+#include "json.h"
+#include "video_widget.h"
+
+using namespace std;
+
+string format_timestamp(uint64_t pos)
+{
+       int ms = pos % 1000;
+       pos /= 1000;
+       int sec = pos % 60;
+       pos /= 60;
+       int min = pos % 60;
+       int hour = pos / 60;
+
+       char buf[256];
+       snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
+       return buf;
+}
+
+MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
+                       FormationsModel *offensive_formations, FormationsModel *defensive_formations)
+       : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations)
+{
+       ui = new Ui::MainWindow;
+       ui->setupUi(this);
+
+       ui->video->open("/home/sesse/dev/stats/ultimate-prores.mkv");
+       ui->video->play();
+
+       ui->event_view->setModel(events);
+       ui->event_view->setColumnWidth(1, 150);
+       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());
+                       if (t != ui->video->get_position()) {
+                               ui->video->seek_absolute(events->get_time(current.row()));
+                       } else {
+                               // Selection could have changed, so we still need to update.
+                               // (Just calling setPosition() would not give us the signal
+                               // in this case.)
+                               update_ui_from_time(t);
+                       }
+               });
+
+       ui->player_view->setModel(players);
+       ui->player_view->setColumnWidth(0, 30);
+       ui->player_view->setColumnWidth(1, 20);
+       ui->player_view->horizontalHeader()->setStretchLastSection(true);
+
+       auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
+               QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
+                       update_action_buttons(ui->video->get_position());
+               });
+       };
+       ui->offensive_formation_view->setModel(offensive_formations);
+       ui->defensive_formation_view->setModel(defensive_formations);
+       connect(ui->offensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
+       connect(ui->defensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
+       connect(ui->offensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
+               formation_double_clicked(true, index.row());
+       });
+       connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
+               formation_double_clicked(false, index.row());
+       });
+
+       connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
+               position_changed(pos);
+       });
+
+       // It's not really clear whether PgUp should be forwards or backwards,
+       // but mpv does at least up = forwards, so that's probably standard.
+       QShortcut *pgdown = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
+       connect(pgdown, &QShortcut::activated, [this]() { ui->video->seek(-120000); });
+       QShortcut *pgup = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
+       connect(pgup, &QShortcut::activated, [this]() { ui->video->seek(120000); });
+
+       connect(ui->minus10s, &QPushButton::clicked, [this]() { ui->video->seek(-10000); });
+       connect(ui->plus10s, &QPushButton::clicked, [this]() { ui->video->seek(10000); });
+
+       connect(ui->minus2s, &QPushButton::clicked, [this]() { ui->video->seek(-2000); });
+       connect(ui->plus2s, &QPushButton::clicked, [this]() { ui->video->seek(2000); });
+       connect(ui->video, &VideoWidget::mouse_back_clicked, [this]() { ui->video->seek(-2000); });
+       connect(ui->video, &VideoWidget::mouse_forward_clicked, [this]() { ui->video->seek(2000); });
+
+       connect(ui->minus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(-1); });
+       connect(ui->plus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(1); });
+
+       connect(ui->play_pause, &QPushButton::clicked, [this]() {
+               if (playing) {
+                       ui->video->pause();
+                       ui->play_pause->setText("Play (space)");
+               } else {
+                       ui->video->play();
+                       ui->play_pause->setText("Pause (space)");
+               }
+               playing = !playing;
+
+               // Needs to be set anew when we modify setText(), evidently.
+               ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
+       });
+
+       connect(ui->player_1, &QPushButton::clicked, [this]() { insert_player_event(0); });
+       connect(ui->player_2, &QPushButton::clicked, [this]() { insert_player_event(1); });
+       connect(ui->player_3, &QPushButton::clicked, [this]() { insert_player_event(2); });
+       connect(ui->player_4, &QPushButton::clicked, [this]() { insert_player_event(3); });
+       connect(ui->player_5, &QPushButton::clicked, [this]() { insert_player_event(4); });
+       connect(ui->player_6, &QPushButton::clicked, [this]() { insert_player_event(5); });
+       connect(ui->player_7, &QPushButton::clicked, [this]() { insert_player_event(6); });
+
+       // Offensive events
+       connect(ui->offense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_offense"); });
+       connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
+       connect(ui->throwaway, &QPushButton::clicked, [this, events]() {
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
+               if (s.attack_state == EventsModel::Status::DEFENSE && s.pull_state == EventsModel::Status::PULL_IN_AIR) {
+                       insert_noplayer_event("pull_oob");
+               } else {
+                       set_current_event_type("throwaway");
+               }
+       });
+       connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
+       connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
+       connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
+       connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
+       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) {
+                       set_current_event_type("pull");
+               } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
+                       insert_noplayer_event("pull_landed");
+               } else if (s.pull_state == EventsModel::Status::NOT_PULLING) {
+                       set_current_event_type("was_d");
+               }
+       });
+
+       // Defensive events (TODO add more)
+       connect(ui->interception, &QPushButton::clicked, [this]() { set_current_event_type("interception"); });
+       connect(ui->defense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_defense"); });
+       connect(ui->their_throwaway, &QPushButton::clicked, [this]() { insert_noplayer_event("their_throwaway"); });
+       connect(ui->their_goal, &QPushButton::clicked, [this]() { insert_noplayer_event("their_goal"); });
+       connect(ui->their_pull, &QPushButton::clicked, [this, events]() {
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
+               if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
+                       insert_noplayer_event("their_pull");
+               }
+       });
+       connect(ui->our_defense, &QPushButton::clicked, [this]() { set_current_event_type("defense"); });
+       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]() {
+               EventsModel::Status s = events->get_status_at(ui->video->get_position());
+               if (s.stoppage) {
+                       insert_noplayer_event("restart");
+               } else {
+                       insert_noplayer_event("stoppage");
+               }
+       });
+       connect(ui->unknown, &QPushButton::clicked, [this]() { insert_noplayer_event("unknown"); });
+
+       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(); });
+}
+
+void MainWindow::position_changed(uint64_t pos)
+{
+       ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
+       if (!playing) {
+               ui->video->pause();  // We only played to get a picture.
+       }
+       if (playing) {
+               QModelIndex row = events->get_last_event_qt(ui->video->get_position());
+               ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
+       }
+       update_ui_from_time(pos);
+}
+
+void MainWindow::insert_player_event(int button_id)
+{
+       uint64_t t = ui->video->get_position();
+       vector<int> team = events->sort_team(events->get_team_at(t));
+       if (unsigned(button_id) >= team.size()) {
+               return;
+       }
+       int player_id = team[button_id];
+
+       EventsModel::Status s = events->get_status_at(t);
+
+       ui->event_view->selectionModel()->blockSignals(true);
+       if (s.attack_state == EventsModel::Status::OFFENSE) {
+               // TODO: Perhaps not if that player already did the last catch?
+               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
+       } else {
+               ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
+       }
+       ui->event_view->selectionModel()->blockSignals(false);
+
+       update_ui_from_time(t);
+}
+
+void MainWindow::insert_noplayer_event(const string &type)
+{
+       uint64_t t = ui->video->get_position();
+
+       ui->event_view->selectionModel()->blockSignals(true);
+       ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
+       ui->event_view->selectionModel()->blockSignals(false);
+
+       update_ui_from_time(t);
+}
+
+void MainWindow::set_current_event_type(const string &type)
+{
+       QItemSelectionModel *select = ui->event_view->selectionModel();
+       if (!select->hasSelection()) {
+               return;
+       }
+       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+       events->set_event_type(row, type);
+       update_ui_from_time(ui->video->get_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(ui->video->get_position());
+                       return;
+               }
+       }
+
+       // Insert a new formation event instead (same as double-click on the selected one).
+       events->set_formation_at(ui->video->get_position(), offense, formation_id);
+       update_ui_from_time(ui->video->get_position());
+}
+
+void MainWindow::delete_current_event()
+{
+       QItemSelectionModel *select = ui->event_view->selectionModel();
+       if (!select->hasSelection()) {
+               return;
+       }
+       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+       ui->event_view->selectionModel()->blockSignals(true);
+       events->delete_event(row);
+       ui->event_view->selectionModel()->blockSignals(false);
+       update_ui_from_time(ui->video->get_position());
+}
+
+void MainWindow::make_substitution()
+{
+       QItemSelectionModel *select = ui->player_view->selectionModel();
+       set<int> new_team;
+       for (QModelIndex row : select->selectedRows()) {
+               new_team.insert(players->get_player_id(row.row()));
+       }
+       events->set_team_at(ui->video->get_position(), new_team);
+       update_player_buttons(ui->video->get_position());
+}
+
+void MainWindow::update_ui_from_time(uint64_t t)
+{
+       update_status(t);
+       update_player_buttons(t);
+       update_action_buttons(t);
+}
+
+void MainWindow::update_status(uint64_t t)
+{
+       EventsModel::Status s = events->get_status_at(t);
+       char buf[256];
+       std::string formation = "Not started";
+       if (s.attack_state == EventsModel::Status::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) {
+               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, 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);
+               ui->status->setText(buf2);
+       } else {
+               ui->status->setText(buf);
+       }
+}
+
+void MainWindow::update_player_buttons(uint64_t t)
+{
+       QPushButton *buttons[] = {
+               ui->player_1,
+               ui->player_2,
+               ui->player_3,
+               ui->player_4,
+               ui->player_5,
+               ui->player_6,
+               ui->player_7
+       };
+       const char shortcuts[] = "qweasdf";
+       int num_players = 0;
+       for (int player_id : events->sort_team(events->get_team_at(t))) {
+               QPushButton *btn = buttons[num_players];
+               string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
+               char shortcut[2] = "";
+               shortcut[0] = toupper(shortcuts[num_players]);
+               btn->setText(QString::fromUtf8(label));
+               btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
+               btn->setEnabled(true);
+               if (++num_players == 7) {
+                       break;
+               }
+       }
+       for (int i = num_players; i < 7; ++i) {
+               QPushButton *btn = buttons[i];
+               btn->setText("No player");
+               btn->setEnabled(false);
+       }
+}
+
+void MainWindow::update_action_buttons(uint64_t t)
+{
+       {
+               QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
+               if (select->hasSelection()) {
+                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+                       ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
+               } else {
+                       ui->offensive_formation->setEnabled(false);
+               }
+       }
+       {
+               QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
+               if (select->hasSelection()) {
+                       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+                       ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
+               } else {
+                       ui->defensive_formation->setEnabled(false);
+               }
+       }
+
+       EventsModel::Status s = events->get_status_at(t);
+
+       bool has_selection = false;
+       bool has_selection_with_player = false;
+
+       QItemSelectionModel *select = ui->event_view->selectionModel();
+       if (select->hasSelection()) {
+               has_selection = true;
+               int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
+               has_selection_with_player = events->get_player_id(row).has_value();
+       }
+       ui->delete_->setEnabled(has_selection);
+
+       if (s.stoppage) {
+               ui->stoppage->setText("Restart (&v)");
+               ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
+               ui->catch_->setEnabled(false);
+               ui->throwaway->setEnabled(false);
+               ui->drop->setEnabled(false);
+               ui->goal->setEnabled(false);
+               ui->offensive_soft_plus->setEnabled(false);
+               ui->offensive_soft_minus->setEnabled(false);
+               ui->pull_or_was_d->setEnabled(false);
+               ui->interception->setEnabled(false);
+               ui->their_throwaway->setEnabled(false);
+               ui->our_defense->setEnabled(false);
+               ui->their_goal->setEnabled(false);
+               ui->defensive_soft_plus->setEnabled(false);
+               ui->defensive_soft_minus->setEnabled(false);
+               ui->their_pull->setEnabled(false);
+               return;
+       } else {
+               ui->stoppage->setText("Stoppage (&v)");
+               ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
+       }
+
+       // Defaults for pull-related buttons.
+       ui->pull_or_was_d->setText("Pull (&p)");
+       ui->their_pull->setText("Their pull (&p)");
+       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
+       ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
+       ui->throwaway->setText("Throwaway (&t)");
+       ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
+
+       if (s.pull_state == EventsModel::Status::SHOULD_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);
+
+               ui->catch_->setEnabled(false);
+               ui->throwaway->setEnabled(false);
+               ui->drop->setEnabled(false);
+               ui->goal->setEnabled(false);
+               ui->offensive_soft_plus->setEnabled(false);
+               ui->offensive_soft_minus->setEnabled(false);
+               ui->interception->setEnabled(false);
+               ui->their_throwaway->setEnabled(false);
+               ui->our_defense->setEnabled(false);
+               ui->their_goal->setEnabled(false);
+               ui->defensive_soft_plus->setEnabled(false);
+               ui->defensive_soft_minus->setEnabled(false);
+               return;
+       }
+       if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
+               if (s.attack_state == EventsModel::Status::DEFENSE) {
+                       ui->pull_or_was_d->setText("Pull landed (&p)");
+                       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
+                       ui->pull_or_was_d->setEnabled(true);
+
+                       ui->throwaway->setText("Pull OOB (&t)");
+                       ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
+                       ui->throwaway->setEnabled(true);
+               } else {
+                       ui->pull_or_was_d->setEnabled(false);
+                       ui->throwaway->setEnabled(false);
+               }
+               ui->their_pull->setEnabled(false);  // We don't track their pull landings; only by means of catch etc.
+
+               ui->catch_->setEnabled(false);
+               ui->drop->setEnabled(false);
+               ui->goal->setEnabled(false);
+               ui->offensive_soft_plus->setEnabled(false);
+               ui->offensive_soft_minus->setEnabled(false);
+               ui->interception->setEnabled(false);
+               ui->their_throwaway->setEnabled(false);
+               ui->our_defense->setEnabled(false);
+               ui->their_goal->setEnabled(false);
+               ui->defensive_soft_plus->setEnabled(false);
+               ui->defensive_soft_minus->setEnabled(false);
+               return;
+       }
+
+       // Not pulling, so reuse the pull button for got d-ed.
+       ui->pull_or_was_d->setText("Was d-ed (&z)");
+       ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
+       ui->pull_or_was_d->setEnabled(true);
+
+       ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
+       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->offensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
+       ui->offensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
+       ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);  // Was d-ed.
+
+       ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
+       ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
+       ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
+       ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
+       ui->defensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
+       ui->defensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
+       ui->their_pull->setEnabled(false);
+}
+
+void MainWindow::formation_double_clicked(bool offense, unsigned row)
+{
+       FormationsModel *formations = offense ? offensive_formations : defensive_formations;
+       int id = formations->get_formation_id(row);
+       if (id == -1) {  // “Add new” clicked.
+               bool ok;
+               QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
+               if (!ok || new_formation_str.isEmpty()) {
+                       return;
+               }
+
+               id = formations->insert_new(new_formation_str.toStdString());
+               QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
+               view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
+               events->inserted_new_formation(id, new_formation_str.toStdString());
+       } else {
+               events->set_formation_at(ui->video->get_position(), offense, id);
+       }
+       update_ui_from_time(ui->video->get_position());
+}
+
index d38e79761026877ad7d9276e252082a8edb6063b..51a9e237bd436939e936dfbbb98d73e8f101e2e8 100644 (file)
@@ -15,6 +15,6 @@ qt_files = qt6.preprocess(
         dependencies: qt6deps)
 
 executable('stats',
-           ['main.cpp', 'events.cpp', 'players.cpp', 'formations.cpp', 'json.cpp', 'video_widget.cpp', 'ffmpeg_raii.cpp'],
+           ['main.cpp', 'mainwindow.cpp', 'events.cpp', 'players.cpp', 'formations.cpp', 'json.cpp', 'video_widget.cpp', 'ffmpeg_raii.cpp'],
            qt_files,
            dependencies: [qt6deps, sqlite3dep, libavcodecdep, libavformatdep, libavutildep, libswscaledep, gldep])