2 #include <QApplication>
6 #include <QInputDialog>
14 #include "mainwindow.h"
15 #include "edit_player_dialog.h"
16 #include "ui_mainwindow.h"
19 #include "formations.h"
21 #include "video_widget.h"
25 string format_timestamp(uint64_t pos)
35 snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
39 string get_video_filename(sqlite3 *db, int match_id)
43 int ret = sqlite3_prepare_v2(db, "SELECT video_filename FROM match WHERE match=?", -1, &stmt, 0);
44 if (ret != SQLITE_OK) {
45 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
49 sqlite3_bind_int64(stmt, 1, match_id);
51 ret = sqlite3_step(stmt);
52 if (ret != SQLITE_ROW) {
53 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
57 if (sqlite3_column_type(stmt, 0) != SQLITE_TEXT) {
60 string filename = (const char *)sqlite3_column_text(stmt, 0);
62 ret = sqlite3_finalize(stmt);
63 if (ret != SQLITE_OK) {
64 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
70 bool get_match_property(sqlite3 *db, int match_id, const string &prop_name)
74 int ret = sqlite3_prepare_v2(db, ("SELECT " + prop_name + " FROM match WHERE match=?").c_str(), -1, &stmt, 0);
75 if (ret != SQLITE_OK) {
76 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
80 sqlite3_bind_int64(stmt, 1, match_id);
82 ret = sqlite3_step(stmt);
83 if (ret != SQLITE_ROW) {
84 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
88 if (sqlite3_column_type(stmt, 0) != SQLITE_INTEGER) {
91 bool value = sqlite3_column_int(stmt, 0);
93 ret = sqlite3_finalize(stmt);
94 if (ret != SQLITE_OK) {
95 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
101 void save_video_filename(sqlite3 *db, int match_id, const string &filename)
105 int ret = sqlite3_prepare_v2(db, "UPDATE match SET video_filename=? WHERE match=?", -1, &stmt, 0);
106 if (ret != SQLITE_OK) {
107 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
111 sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
112 sqlite3_bind_int64(stmt, 2, match_id);
114 ret = sqlite3_step(stmt);
115 if (ret == SQLITE_ROW) {
116 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
120 ret = sqlite3_finalize(stmt);
121 if (ret != SQLITE_OK) {
122 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
127 void save_match_property(sqlite3 *db, int match_id, const string &prop_name, bool value)
131 int ret = sqlite3_prepare_v2(db, ("UPDATE match SET " + prop_name + "=? WHERE match=?").c_str(), -1, &stmt, 0);
132 if (ret != SQLITE_OK) {
133 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
137 sqlite3_bind_int64(stmt, 1, value);
138 sqlite3_bind_int64(stmt, 2, match_id);
140 ret = sqlite3_step(stmt);
141 if (ret == SQLITE_ROW) {
142 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
146 ret = sqlite3_finalize(stmt);
147 if (ret != SQLITE_OK) {
148 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
153 MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
154 FormationsModel *offensive_formations, FormationsModel *defensive_formations,
155 sqlite3 *db, int match_id)
156 : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations), db(db), match_id(match_id)
158 ui = new Ui::MainWindow;
161 string filename = get_video_filename(db, match_id);
162 bool need_save_filename = false;
164 if (!filename.empty() && ui->video->open(filename.c_str())) {
168 // TODO: Probably relativize this path, so that we can move the .db
169 // more easily with the videos.
170 filename = QFileDialog::getOpenFileName(this, "Open video").toUtf8();
171 need_save_filename = true;
173 if (need_save_filename) {
174 save_video_filename(db, match_id, filename);
178 ui->event_view->setModel(events);
179 ui->event_view->setColumnWidth(1, 150);
180 ui->event_view->setColumnWidth(2, 150);
181 connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
182 [this, events](const QModelIndex ¤t, const QModelIndex &previous) {
183 uint64_t t = events->get_time(current.row());
184 if (t != ui->video->get_position()) {
185 ui->video->seek_absolute(events->get_time(current.row()));
187 // Selection could have changed, so we still need to update.
188 // (Just calling setPosition() would not give us the signal
190 update_ui_from_time(t);
194 ui->player_view->setModel(players);
195 ui->player_view->setColumnWidth(0, 30);
196 ui->player_view->setColumnWidth(1, 20);
197 ui->player_view->horizontalHeader()->setStretchLastSection(true);
199 auto formation_changed = [this](const QModelIndex ¤t, const QModelIndex &previous) {
200 QTimer::singleShot(1, [=]{ // The selection is wrong until the callback actually returns.
201 update_action_buttons(ui->video->get_position());
204 ui->offensive_formation_view->setModel(offensive_formations);
205 ui->defensive_formation_view->setModel(defensive_formations);
206 connect(ui->offensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
207 connect(ui->defensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
208 connect(ui->offensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
209 formation_double_clicked(true, index.row());
211 connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
212 formation_double_clicked(false, index.row());
215 connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
216 position_changed(pos);
219 // It's not really clear whether PgUp should be forwards or backwards,
220 // but mpv does at least up = forwards, so that's probably standard.
221 QShortcut *pgdown = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
222 connect(pgdown, &QShortcut::activated, [this] { ui->video->seek(-120000); });
223 QShortcut *pgup = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
224 connect(pgup, &QShortcut::activated, [this] { ui->video->seek(120000); });
226 connect(ui->minus10s, &QPushButton::clicked, [this] { ui->video->seek(-10000); });
227 connect(ui->plus10s, &QPushButton::clicked, [this] { ui->video->seek(10000); });
229 connect(ui->minus2s, &QPushButton::clicked, [this] { ui->video->seek(-2000); });
230 connect(ui->plus2s, &QPushButton::clicked, [this] { ui->video->seek(2000); });
231 connect(ui->video, &VideoWidget::mouse_back_clicked, [this] { ui->video->seek(-2000); });
232 connect(ui->video, &VideoWidget::mouse_forward_clicked, [this] { ui->video->seek(2000); });
234 connect(ui->minus1f, &QPushButton::clicked, [this] { ui->video->seek_frames(-1); });
235 connect(ui->plus1f, &QPushButton::clicked, [this] { ui->video->seek_frames(1); });
237 connect(ui->play_pause, &QPushButton::clicked, [this] {
240 ui->play_pause->setText("Play (space)");
243 ui->play_pause->setText("Pause (space)");
247 // Needs to be set anew when we modify setText(), evidently.
248 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
251 connect(ui->player_1, &QPushButton::clicked, [this] { insert_player_event(0); });
252 connect(ui->player_2, &QPushButton::clicked, [this] { insert_player_event(1); });
253 connect(ui->player_3, &QPushButton::clicked, [this] { insert_player_event(2); });
254 connect(ui->player_4, &QPushButton::clicked, [this] { insert_player_event(3); });
255 connect(ui->player_5, &QPushButton::clicked, [this] { insert_player_event(4); });
256 connect(ui->player_6, &QPushButton::clicked, [this] { insert_player_event(5); });
257 connect(ui->player_7, &QPushButton::clicked, [this] { insert_player_event(6); });
260 connect(ui->offense_label, &ClickableLabel::clicked, [this] { insert_noplayer_event("set_offense"); });
261 connect(ui->catch_, &QPushButton::clicked, [this] { set_current_event_type("catch"); });
262 connect(ui->throwaway, &QPushButton::clicked, [this, events] {
263 EventsModel::Status s = events->get_status_at(ui->video->get_position());
264 if (s.attack_state == EventsModel::Status::DEFENSE && s.pull_state == EventsModel::Status::PULL_IN_AIR) {
265 insert_noplayer_event("pull_oob");
270 connect(ui->drop, &QPushButton::clicked, [this] { set_current_event_type("drop"); });
271 connect(ui->goal, &QPushButton::clicked, [this] { set_current_event_type("goal"); });
272 connect(ui->stallout, &QPushButton::clicked, [this] { set_current_event_type("stallout"); });
273 connect(ui->soft_plus, &QPushButton::clicked, [this, events] {
274 EventsModel::Status s = events->get_status_at(ui->video->get_position());
275 if (s.attack_state == EventsModel::Status::OFFENSE) {
276 set_current_event_type("offensive_soft_plus");
277 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
278 set_current_event_type("defensive_soft_plus");
281 connect(ui->soft_minus, &QPushButton::clicked, [this, events] {
282 EventsModel::Status s = events->get_status_at(ui->video->get_position());
283 if (s.attack_state == EventsModel::Status::OFFENSE) {
284 set_current_event_type("offensive_soft_minus");
285 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
286 set_current_event_type("defensive_soft_minus");
289 connect(ui->pull_or_was_d, &QPushButton::clicked, [this, events] {
290 EventsModel::Status s = events->get_status_at(ui->video->get_position());
291 if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
292 events->get_status_at(ui->video->get_position() - 1).pull_state == EventsModel::Status::SHOULD_PULL) {
293 set_current_event_type("pull");
294 } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
295 insert_noplayer_event("pull_landed");
296 } else if (s.pull_state == EventsModel::Status::NOT_PULLING) {
297 set_current_event_type("was_d");
302 connect(ui->interception, &QPushButton::clicked, [this] { set_current_event_type("interception"); });
303 connect(ui->defense_label, &ClickableLabel::clicked, [this] { insert_noplayer_event("set_defense"); });
304 connect(ui->their_throwaway, &QPushButton::clicked, [this] { insert_noplayer_event("their_throwaway"); });
305 connect(ui->their_goal, &QPushButton::clicked, [this] { insert_noplayer_event("their_goal"); });
306 connect(ui->their_pull, &QPushButton::clicked, [this, events] {
307 EventsModel::Status s = events->get_status_at(ui->video->get_position());
308 if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
309 insert_noplayer_event("their_pull");
312 connect(ui->our_defense, &QPushButton::clicked, [this] { set_current_event_type("defense"); });
314 connect(ui->offensive_formation, &QPushButton::clicked, [this] { insert_or_change_formation(/*offense=*/true); });
315 connect(ui->defensive_formation, &QPushButton::clicked, [this] { insert_or_change_formation(/*offense=*/false); });
318 connect(ui->substitution, &QPushButton::clicked, [this] { make_substitution(); });
319 connect(ui->stoppage, &QPushButton::clicked, [this, events] {
320 EventsModel::Status s = events->get_status_at(ui->video->get_position());
322 insert_noplayer_event("restart");
324 insert_noplayer_event("stoppage");
327 connect(ui->unknown, &QPushButton::clicked, [this] { insert_noplayer_event("unknown"); });
329 QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
330 connect(key_delete, &QShortcut::activated, [this] { ui->delete_->animateClick(); });
331 connect(ui->delete_, &QPushButton::clicked, [this] { delete_current_event(); });
333 // Player list shortcuts.
334 connect(ui->get_current_players, &QPushButton::clicked, [this, players, events] {
335 uint64_t t = ui->video->get_position();
336 QItemSelection selection;
337 set<int> team = events->get_team_at(t);
338 for (int row = 0; row < players->rowCount(QModelIndex()); ++row) {
339 if (team.count(players->get_player_id(row))) {
340 selection.select(players->get_row_start_qt(row), players->get_row_end_qt(row));
343 ui->player_view->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
345 connect(ui->clear_player_list, &QPushButton::clicked, [this] {
346 ui->player_view->selectionModel()->clear();
348 connect(ui->player_view, &QTableView::doubleClicked, [this](const QModelIndex &index) {
349 open_edit_player_dialog(index.row());
351 connect(ui->player_view->selectionModel(), &QItemSelectionModel::selectionChanged, [this] {
352 update_gender_ratio(ui->video->get_position());
354 update_gender_ratio(0);
356 // The shortcuts take up so much space that we really need, so we sacrifice the header.
357 ui->player_view->horizontalHeader()->hide();
360 connect(ui->action_exit, &QAction::triggered, [this] { close(); });
361 connect(ui->action_export_json, &QAction::triggered, [db] { export_to_json(db, "ultimate.json"); });
363 ui->action_gender_rule_a->setChecked(get_match_property(db, match_id, "gender_rule_a"));
364 ui->action_gender_pull_rule->setChecked(get_match_property(db, match_id, "gender_pull_rule"));
365 connect(ui->action_gender_rule_a, &QAction::toggled, [this, db, match_id] {
366 save_match_property(db, match_id, "gender_rule_a", ui->action_gender_rule_a->isChecked());
368 connect(ui->action_gender_pull_rule, &QAction::toggled, [this, db, match_id] {
369 save_match_property(db, match_id, "gender_pull_rule", ui->action_gender_pull_rule->isChecked());
370 update_gender_ratio(ui->video->get_position());
374 void MainWindow::position_changed(uint64_t pos)
376 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
378 ui->video->pause(); // We only played to get a picture.
381 QModelIndex row = events->get_last_event_qt(ui->video->get_position());
382 ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
384 update_ui_from_time(pos);
387 void MainWindow::insert_player_event(int button_id)
389 uint64_t t = ui->video->get_position();
390 vector<int> team = events->sort_team(events->get_team_at(t));
391 if (unsigned(button_id) >= team.size()) {
394 int player_id = team[button_id];
396 EventsModel::Status s = events->get_status_at(t);
398 ui->event_view->selectionModel()->blockSignals(true);
399 if (s.attack_state == EventsModel::Status::OFFENSE) {
400 // TODO: Perhaps not if that player already did the last catch?
401 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
403 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
405 ui->event_view->selectionModel()->blockSignals(false);
407 update_ui_from_time(t);
410 void MainWindow::insert_noplayer_event(const string &type)
412 uint64_t t = ui->video->get_position();
414 ui->event_view->selectionModel()->blockSignals(true);
415 ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
416 ui->event_view->selectionModel()->blockSignals(false);
418 update_ui_from_time(t);
421 void MainWindow::set_current_event_type(const string &type)
423 QItemSelectionModel *select = ui->event_view->selectionModel();
424 if (!select->hasSelection()) {
427 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
428 events->set_event_type(row, type);
429 update_ui_from_time(ui->video->get_position());
432 void MainWindow::insert_throwaway()
434 uint64_t t = ui->video->get_position();
436 QItemSelectionModel *select = ui->event_view->selectionModel();
437 if (select->hasSelection()) {
438 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
439 // We could want to modify this catch event into a throwaway. See if that is the case.
440 int last_catching_player = events->get_status_at(events->get_time(row) - 1).last_catching_player;
441 if (last_catching_player != -1 && last_catching_player == events->get_player_id(row)) {
442 // Last event was that this player caught the disc, so yes, make this a throwaway.
443 events->set_event_type(row, "throwaway");
444 update_ui_from_time(t);
447 // It doesn't make sense that the player throws it away without holding the disc first,
448 // so we insert a new event where the person holding the disc throws it away.
449 // (In other words, fall back to inserting a new one based on time.)
452 int last_catching_player = events->get_status_at(t - 1).last_catching_player;
453 if (last_catching_player == -1) {
457 ui->event_view->selectionModel()->blockSignals(true);
458 ui->event_view->selectRow(events->insert_event(t, last_catching_player, nullopt, "throwaway"));
459 ui->event_view->selectionModel()->blockSignals(false);
462 // Formation buttons either modify the existing formation (if we've selected
463 // a formation change event), or insert a new one (if not).
464 void MainWindow::insert_or_change_formation(bool offense)
466 FormationsModel *formations = offense ? offensive_formations : defensive_formations;
467 QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
468 if (!formation_view->selectionModel()->hasSelection()) {
469 // This shouldn't happen; the button should not have been enabled.
472 int formation_row = formation_view->selectionModel()->selectedRows().front().row(); // Should only be one, due to our selection behavior.
473 int formation_id = formations->get_formation_id(formation_row);
474 if (formation_id == -1) {
475 // This also shouldn't happen (“Add new…” selected).
479 QItemSelectionModel *select = ui->event_view->selectionModel();
480 if (select->hasSelection()) {
481 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
482 EventType expected_type = offense ? EventType::FORMATION_OFFENSE : EventType::FORMATION_DEFENSE;
483 if (events->get_event_type(row) == expected_type) {
484 events->set_event_formation(row, formation_id);
485 update_ui_from_time(ui->video->get_position());
490 // Insert a new formation event instead (same as double-click on the selected one).
491 events->set_formation_at(ui->video->get_position(), offense, formation_id);
492 update_ui_from_time(ui->video->get_position());
495 void MainWindow::delete_current_event()
497 QItemSelectionModel *select = ui->event_view->selectionModel();
498 if (!select->hasSelection()) {
501 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
502 ui->event_view->selectionModel()->blockSignals(true);
503 events->delete_event(row);
504 ui->event_view->selectionModel()->blockSignals(false);
505 update_ui_from_time(ui->video->get_position());
508 void MainWindow::make_substitution()
510 QItemSelectionModel *select = ui->player_view->selectionModel();
512 for (QModelIndex row : select->selectedRows()) {
513 new_team.insert(players->get_player_id(row.row()));
515 events->set_team_at(ui->video->get_position(), new_team);
516 update_player_buttons(ui->video->get_position());
519 void MainWindow::update_ui_from_time(uint64_t t)
522 update_player_buttons(t);
523 update_action_buttons(t);
524 update_gender_ratio(t);
527 void MainWindow::update_status(uint64_t t)
529 EventsModel::Status s = events->get_status_at(t);
531 std::string formation = "Not started";
532 if (s.attack_state == EventsModel::Status::OFFENSE) {
533 if (s.offensive_formation != 0) {
534 formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
536 formation = "Offense";
538 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
539 if (s.defensive_formation != 0) {
540 formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
542 formation = "Defense";
546 snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
547 s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
548 if (s.stoppage_sec > 0) {
550 snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
551 ui->status->setText(buf2);
553 ui->status->setText(buf);
557 void MainWindow::update_player_buttons(uint64_t t)
559 QPushButton *buttons[] = {
568 const char shortcuts[] = "qweasdf";
570 for (int player_id : events->sort_team(events->get_team_at(t))) {
571 QPushButton *btn = buttons[num_players];
572 string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
573 char shortcut[2] = "";
574 shortcut[0] = toupper(shortcuts[num_players]);
575 btn->setText(QString::fromUtf8(label));
576 btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
577 btn->setEnabled(true);
578 if (++num_players == 7) {
582 for (int i = num_players; i < 7; ++i) {
583 QPushButton *btn = buttons[i];
584 btn->setText("No player");
585 btn->setEnabled(false);
589 void MainWindow::update_action_buttons(uint64_t t)
592 QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
593 if (select->hasSelection()) {
594 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
595 ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
597 ui->offensive_formation->setEnabled(false);
601 QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
602 if (select->hasSelection()) {
603 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
604 ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
606 ui->defensive_formation->setEnabled(false);
610 EventsModel::Status s = events->get_status_at(t);
612 bool has_selection = false;
613 bool has_selection_with_player = false;
615 QItemSelectionModel *select = ui->event_view->selectionModel();
616 if (select->hasSelection()) {
617 has_selection = true;
618 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
619 has_selection_with_player = events->get_player_id(row).has_value();
621 ui->delete_->setEnabled(has_selection);
624 ui->stoppage->setText("Restart (&v)");
625 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
626 ui->catch_->setEnabled(false);
627 ui->throwaway->setEnabled(false);
628 ui->drop->setEnabled(false);
629 ui->goal->setEnabled(false);
630 ui->stallout->setEnabled(false);
631 ui->soft_plus->setEnabled(false);
632 ui->soft_minus->setEnabled(false);
633 ui->pull_or_was_d->setEnabled(false);
634 ui->interception->setEnabled(false);
635 ui->their_throwaway->setEnabled(false);
636 ui->our_defense->setEnabled(false);
637 ui->their_goal->setEnabled(false);
638 ui->their_pull->setEnabled(false);
641 ui->stoppage->setText("Stoppage (&v)");
642 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
645 // Defaults for pull-related buttons.
646 ui->pull_or_was_d->setText("Pull (&p)");
647 ui->their_pull->setText("Their pull (&p)");
648 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
649 ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
650 ui->throwaway->setText("Throwaway (&t)");
651 ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
653 if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
654 (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.
655 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
656 ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE);
658 ui->catch_->setEnabled(false);
659 ui->throwaway->setEnabled(false);
660 ui->drop->setEnabled(false);
661 ui->goal->setEnabled(false);
662 ui->stallout->setEnabled(false);
663 ui->soft_plus->setEnabled(false);
664 ui->soft_minus->setEnabled(false);
665 ui->interception->setEnabled(false);
666 ui->their_throwaway->setEnabled(false);
667 ui->our_defense->setEnabled(false);
668 ui->their_goal->setEnabled(false);
671 if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
672 if (s.attack_state == EventsModel::Status::DEFENSE) {
673 ui->pull_or_was_d->setText("Pull landed (&p)");
674 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
675 ui->pull_or_was_d->setEnabled(true);
677 ui->throwaway->setText("Pull OOB (&t)");
678 ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
679 ui->throwaway->setEnabled(true);
681 ui->pull_or_was_d->setEnabled(false);
682 ui->throwaway->setEnabled(false);
684 ui->their_pull->setEnabled(false); // We don't track their pull landings; only by means of catch etc.
686 ui->catch_->setEnabled(false);
687 ui->drop->setEnabled(false);
688 ui->goal->setEnabled(false);
689 ui->stallout->setEnabled(false);
690 ui->soft_plus->setEnabled(false);
691 ui->soft_minus->setEnabled(false);
692 ui->interception->setEnabled(false);
693 ui->their_throwaway->setEnabled(false);
694 ui->our_defense->setEnabled(false);
695 ui->their_goal->setEnabled(false);
699 // Not pulling, so reuse the pull button for got d-ed.
700 ui->pull_or_was_d->setText("Was d-ed (&z)");
701 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
702 ui->pull_or_was_d->setEnabled(true);
704 ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
705 ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && s.last_catching_player != -1);
706 ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
707 ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
708 ui->stallout->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
709 ui->soft_plus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
710 ui->soft_minus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
711 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); // Was d-ed.
713 ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
714 ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
715 ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
716 ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
717 ui->their_pull->setEnabled(false);
720 vector<pair<string, int>> sort_gender(const map<string, int> &gender_count)
722 vector<pair<string, int>> sorted_gender;
723 for (const auto &[gender, count] : gender_count) {
724 sorted_gender.emplace_back(gender, count);
726 sort(sorted_gender.begin(), sorted_gender.end(), [](const pair<string, int> &a, const pair<string, int> &b) {
727 if (a.second != b.second) {
728 return b.second < a.second;
730 return a.first < b.first;
732 return sorted_gender;
735 string format_gender_counts(const map<string, int> &gender_count)
737 vector<pair<string, int>> sorted_gender = sort_gender(gender_count);
740 for (const auto &[gender, count] : sorted_gender) {
745 snprintf(buf, sizeof(buf), "%d ", count);
752 string format_gender_counts(const map<string, int> &gender_count, const map<string, int> &reference_gender_count)
754 vector<pair<string, int>> sorted_gender = sort_gender(reference_gender_count); // Less swapping around this way.
757 for (const auto &[gender, count] : sorted_gender) {
762 snprintf(buf, sizeof(buf), "%d/%d ", gender_count.find(gender)->second, count);
769 void MainWindow::update_gender_ratio(uint64_t t)
774 // Count the gender ratio in the current selection.
775 map<string, int> gender_count;
776 for (int i = 0; i < players->rowCount(QModelIndex()); ++i) {
777 string gender = players->get_player_gender(i);
778 gender_count[gender] = 0;
781 QItemSelectionModel *select = ui->player_view->selectionModel();
782 for (QModelIndex row : select->selectedRows()) {
783 string gender = players->get_player_gender(row.row());
784 ++gender_count[gender];
788 const bool gender_rule_a = ui->action_gender_rule_a->isChecked();
790 // This is tricky. We don't want to hard-code assumptions about gender,
791 // since there are so many possible variations (e.g. 5 players for indoors,
792 // or loose mixed). We can't get everything right, but our general
795 // - We assume ABBA pattern is followed throughout, ie., we switch
796 // on odd-numbered points. We use goals as reference for points.
797 // - We always use the two or three latest points as reference;
798 // this means an issue with the first point won't persist forever.
799 // - When we don't switch, we expect the identical number as last time.
800 // - When we _do_ switch, we copy the variation from two points ago
801 // if we have it; if not, we simply expect it to be different.
802 // - We should always have the same number of people as last point.
804 // The viewer's warnings also checks that things are correct across
805 // stoppages, which we don't yet.
806 map<string, int> current_gender_count;
807 for (int i = 0; i < players->rowCount(QModelIndex()); ++i) {
808 string gender = players->get_player_gender(i);
809 current_gender_count[gender] = 0;
811 vector<map<string, int>> historical_gender_counts;
812 for (int row = 0; row < events->rowCount(QModelIndex()); ++row) {
813 if (events->get_time(row) > t) {
816 EventType type = events->get_event_type(row);
817 if (type == EventType::GOAL || type == EventType::THEIR_GOAL) {
818 historical_gender_counts.push_back(current_gender_count);
819 } else if (type == EventType::SWAP_IN) {
820 string gender = players->get_player_gender_by_id(*events->get_player_id(row));
821 ++current_gender_count[gender];
822 } else if (type == EventType::SWAP_OUT) {
823 string gender = players->get_player_gender_by_id(*events->get_player_id(row));
824 --current_gender_count[gender];
829 if (historical_gender_counts.empty()) {
830 // We don't have any points yet. Just output the ratio.
831 str = format_gender_counts(gender_count);
832 } else if (historical_gender_counts.size() == 1) {
833 // We have one, so this one must be different, but we don't know what it must be.
834 // It must have the same number of players, though.
835 str = format_gender_counts(gender_count);
836 ok = (gender_count != historical_gender_counts.back());
837 int old_sum = 0, new_sum = 0;
838 for (const auto &[gender, count] : historical_gender_counts.back()) {
841 for (const auto &[gender, count] : gender_count) {
844 if (old_sum != new_sum) {
847 } else if (historical_gender_counts.size() % 2 == 0) {
848 // Must be same as previous.
849 str = format_gender_counts(gender_count, historical_gender_counts.back());
850 ok = (gender_count == historical_gender_counts.back());
852 // Must be same as two points ago.
853 const auto &ref = historical_gender_counts[historical_gender_counts.size() - 3];
854 str = format_gender_counts(gender_count, ref);
855 ok = (gender_count == ref);
857 } else if (gender_count.size() == 1) {
858 // Everybody is either of the same gender or nobody has gender noted,
859 // so just count the number of players. We don't make red here.
861 snprintf(buf, sizeof(buf), "%d selected", num_players);
864 // We don't have gender rule A, but we have gender counts,
865 // so show that. We don't make red here.
866 string str = format_gender_counts(gender_count);
869 // Seemingly this setting this every frame is very costly, so we diff.
870 if (QString::fromUtf8(str) != ui->selected_gender_ratio->text()) {
871 ui->selected_gender_ratio->setText(QString::fromUtf8(str));
873 bool current_ok = ui->selected_gender_ratio->styleSheet().isEmpty();
874 if (ok && !current_ok) {
875 ui->selected_gender_ratio->setStyleSheet("");
876 } else if (!ok && current_ok) {
877 ui->selected_gender_ratio->setStyleSheet("QLabel { color: red }");
881 void MainWindow::formation_double_clicked(bool offense, unsigned row)
883 FormationsModel *formations = offense ? offensive_formations : defensive_formations;
884 int id = formations->get_formation_id(row);
885 if (id == -1) { // “Add new” clicked.
887 QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
888 if (!ok || new_formation_str.isEmpty()) {
892 id = formations->insert_new(new_formation_str.toStdString());
893 QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
894 view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
895 events->inserted_new_formation(id, new_formation_str.toStdString());
897 events->set_formation_at(ui->video->get_position(), offense, id);
899 update_ui_from_time(ui->video->get_position());
902 void MainWindow::open_edit_player_dialog(unsigned row)
904 int player_id = players->get_player_id(row);
905 string number = players->get_player_number(row);
906 string gender = players->get_player_gender(row);
907 string name = players->get_player_name(row);
909 EditPlayerDialog *dialog = new EditPlayerDialog(number, gender, name);
910 connect(dialog, &QDialog::finished, [this, dialog, player_id](int result) {
911 if (result == QDialog::Accepted) {
912 players->edit_player(player_id, dialog->number(), dialog->gender(), dialog->name());
913 update_ui_from_time(ui->video->get_position());