From 0c19ecdff2ce34b20099cb8428724cd17991ddfd Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Mon, 1 May 2023 18:27:40 +0200 Subject: [PATCH] Make the player buttons actually fetch out the team. --- main.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- players.cpp | 6 ++++++ players.h | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index d3b4ae9..acefe9f 100644 --- a/main.cpp +++ b/main.cpp @@ -131,8 +131,15 @@ void MainWindow::seek(int64_t delta_ms) void MainWindow::insert_event(int button_id) { + uint64_t t = video->position(); + set team = events->get_team_at(t); + if (button_id >= team.size()) { + return; + } + int player_id = *next(team.begin(), button_id - 1); + ui->event_view->selectionModel()->blockSignals(true); - ui->event_view->selectRow(events->insert_event(video->position(), button_id)); + ui->event_view->selectRow(events->insert_event(t, player_id)); ui->event_view->selectionModel()->blockSignals(false); } @@ -193,11 +200,42 @@ void MainWindow::make_substitution() void MainWindow::update_status() { - EventsModel::Status s = events->get_status_at(video->position()); + uint64_t t = video->position(); + EventsModel::Status s = events->get_status_at(t); char buf[256]; snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession", s.our_score, s.their_score, s.offense ? "offense" : "defense", s.num_passes, s.possession_sec); ui->status->setText(buf); + + // FIXME: sort by number, instead of by internal ID + 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->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); + } } sqlite3 *open_db(const char *filename) diff --git a/players.cpp b/players.cpp index fce3f7c..af33df0 100644 --- a/players.cpp +++ b/players.cpp @@ -44,6 +44,12 @@ QVariant PlayersModel::data(const QModelIndex &index, int role) const return QVariant(); } +string PlayersModel::get_player_name_by_id(unsigned player_id) +{ + auto it = find_if(players.begin(), players.end(), [player_id](const Player &p) { return p.player_id == player_id; }); + return it->name; +} + void PlayersModel::load_data() { players.clear(); diff --git a/players.h b/players.h index 24c10b9..bea4562 100644 --- a/players.h +++ b/players.h @@ -25,6 +25,7 @@ public: int get_player_id(unsigned row) const { return players[row].player_id; } + std::string get_player_name_by_id(unsigned player_id); private: struct Player { -- 2.39.2