1 #include <QMediaPlayer>
3 #include <QApplication>
7 #include <QInputDialog>
15 #include "mainwindow.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");
267 set_current_event_type("throwaway");
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->selectionModel(), &QItemSelectionModel::selectionChanged, [this] {
349 update_gender_ratio(ui->video->get_position());
351 update_gender_ratio(0);
353 // The shortcuts take up so much space that we really need, so we sacrifice the header.
354 ui->player_view->horizontalHeader()->hide();
357 connect(ui->action_exit, &QAction::triggered, [this] { close(); });
358 connect(ui->action_export_json, &QAction::triggered, [db] { export_to_json(db, "ultimate.json"); });
360 ui->action_gender_rule_a->setChecked(get_match_property(db, match_id, "gender_rule_a"));
361 ui->action_gender_pull_rule->setChecked(get_match_property(db, match_id, "gender_pull_rule"));
362 connect(ui->action_gender_rule_a, &QAction::toggled, [this, db, match_id] {
363 save_match_property(db, match_id, "gender_rule_a", ui->action_gender_rule_a->isChecked());
365 connect(ui->action_gender_pull_rule, &QAction::toggled, [this, db, match_id] {
366 save_match_property(db, match_id, "gender_pull_rule", ui->action_gender_pull_rule->isChecked());
367 update_gender_ratio(ui->video->get_position());
371 void MainWindow::position_changed(uint64_t pos)
373 ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
375 ui->video->pause(); // We only played to get a picture.
378 QModelIndex row = events->get_last_event_qt(ui->video->get_position());
379 ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
381 update_ui_from_time(pos);
384 void MainWindow::insert_player_event(int button_id)
386 uint64_t t = ui->video->get_position();
387 vector<int> team = events->sort_team(events->get_team_at(t));
388 if (unsigned(button_id) >= team.size()) {
391 int player_id = team[button_id];
393 EventsModel::Status s = events->get_status_at(t);
395 ui->event_view->selectionModel()->blockSignals(true);
396 if (s.attack_state == EventsModel::Status::OFFENSE) {
397 // TODO: Perhaps not if that player already did the last catch?
398 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
400 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
402 ui->event_view->selectionModel()->blockSignals(false);
404 update_ui_from_time(t);
407 void MainWindow::insert_noplayer_event(const string &type)
409 uint64_t t = ui->video->get_position();
411 ui->event_view->selectionModel()->blockSignals(true);
412 ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
413 ui->event_view->selectionModel()->blockSignals(false);
415 update_ui_from_time(t);
418 void MainWindow::set_current_event_type(const string &type)
420 QItemSelectionModel *select = ui->event_view->selectionModel();
421 if (!select->hasSelection()) {
424 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
425 events->set_event_type(row, type);
426 update_ui_from_time(ui->video->get_position());
429 // Formation buttons either modify the existing formation (if we've selected
430 // a formation change event), or insert a new one (if not).
431 void MainWindow::insert_or_change_formation(bool offense)
433 FormationsModel *formations = offense ? offensive_formations : defensive_formations;
434 QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
435 if (!formation_view->selectionModel()->hasSelection()) {
436 // This shouldn't happen; the button should not have been enabled.
439 int formation_row = formation_view->selectionModel()->selectedRows().front().row(); // Should only be one, due to our selection behavior.
440 int formation_id = formations->get_formation_id(formation_row);
441 if (formation_id == -1) {
442 // This also shouldn't happen (“Add new…” selected).
446 QItemSelectionModel *select = ui->event_view->selectionModel();
447 if (select->hasSelection()) {
448 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
449 EventType expected_type = offense ? EventType::FORMATION_OFFENSE : EventType::FORMATION_DEFENSE;
450 if (events->get_event_type(row) == expected_type) {
451 events->set_event_formation(row, formation_id);
452 update_ui_from_time(ui->video->get_position());
457 // Insert a new formation event instead (same as double-click on the selected one).
458 events->set_formation_at(ui->video->get_position(), offense, formation_id);
459 update_ui_from_time(ui->video->get_position());
462 void MainWindow::delete_current_event()
464 QItemSelectionModel *select = ui->event_view->selectionModel();
465 if (!select->hasSelection()) {
468 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
469 ui->event_view->selectionModel()->blockSignals(true);
470 events->delete_event(row);
471 ui->event_view->selectionModel()->blockSignals(false);
472 update_ui_from_time(ui->video->get_position());
475 void MainWindow::make_substitution()
477 QItemSelectionModel *select = ui->player_view->selectionModel();
479 for (QModelIndex row : select->selectedRows()) {
480 new_team.insert(players->get_player_id(row.row()));
482 events->set_team_at(ui->video->get_position(), new_team);
483 update_player_buttons(ui->video->get_position());
486 void MainWindow::update_ui_from_time(uint64_t t)
489 update_player_buttons(t);
490 update_action_buttons(t);
491 update_gender_ratio(t);
494 void MainWindow::update_status(uint64_t t)
496 EventsModel::Status s = events->get_status_at(t);
498 std::string formation = "Not started";
499 if (s.attack_state == EventsModel::Status::OFFENSE) {
500 if (s.offensive_formation != 0) {
501 formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
503 formation = "Offense";
505 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
506 if (s.defensive_formation != 0) {
507 formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
509 formation = "Defense";
513 snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
514 s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
515 if (s.stoppage_sec > 0) {
517 snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
518 ui->status->setText(buf2);
520 ui->status->setText(buf);
524 void MainWindow::update_player_buttons(uint64_t t)
526 QPushButton *buttons[] = {
535 const char shortcuts[] = "qweasdf";
537 for (int player_id : events->sort_team(events->get_team_at(t))) {
538 QPushButton *btn = buttons[num_players];
539 string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
540 char shortcut[2] = "";
541 shortcut[0] = toupper(shortcuts[num_players]);
542 btn->setText(QString::fromUtf8(label));
543 btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
544 btn->setEnabled(true);
545 if (++num_players == 7) {
549 for (int i = num_players; i < 7; ++i) {
550 QPushButton *btn = buttons[i];
551 btn->setText("No player");
552 btn->setEnabled(false);
556 void MainWindow::update_action_buttons(uint64_t t)
559 QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
560 if (select->hasSelection()) {
561 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
562 ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
564 ui->offensive_formation->setEnabled(false);
568 QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
569 if (select->hasSelection()) {
570 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
571 ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
573 ui->defensive_formation->setEnabled(false);
577 EventsModel::Status s = events->get_status_at(t);
579 bool has_selection = false;
580 bool has_selection_with_player = false;
582 QItemSelectionModel *select = ui->event_view->selectionModel();
583 if (select->hasSelection()) {
584 has_selection = true;
585 int row = select->selectedRows().front().row(); // Should only be one, due to our selection behavior.
586 has_selection_with_player = events->get_player_id(row).has_value();
588 ui->delete_->setEnabled(has_selection);
591 ui->stoppage->setText("Restart (&v)");
592 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
593 ui->catch_->setEnabled(false);
594 ui->throwaway->setEnabled(false);
595 ui->drop->setEnabled(false);
596 ui->goal->setEnabled(false);
597 ui->stallout->setEnabled(false);
598 ui->soft_plus->setEnabled(false);
599 ui->soft_minus->setEnabled(false);
600 ui->pull_or_was_d->setEnabled(false);
601 ui->interception->setEnabled(false);
602 ui->their_throwaway->setEnabled(false);
603 ui->our_defense->setEnabled(false);
604 ui->their_goal->setEnabled(false);
605 ui->their_pull->setEnabled(false);
608 ui->stoppage->setText("Stoppage (&v)");
609 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
612 // Defaults for pull-related buttons.
613 ui->pull_or_was_d->setText("Pull (&p)");
614 ui->their_pull->setText("Their pull (&p)");
615 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
616 ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
617 ui->throwaway->setText("Throwaway (&t)");
618 ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
620 if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
621 (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.
622 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
623 ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE);
625 ui->catch_->setEnabled(false);
626 ui->throwaway->setEnabled(false);
627 ui->drop->setEnabled(false);
628 ui->goal->setEnabled(false);
629 ui->stallout->setEnabled(false);
630 ui->soft_plus->setEnabled(false);
631 ui->soft_minus->setEnabled(false);
632 ui->interception->setEnabled(false);
633 ui->their_throwaway->setEnabled(false);
634 ui->our_defense->setEnabled(false);
635 ui->their_goal->setEnabled(false);
638 if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
639 if (s.attack_state == EventsModel::Status::DEFENSE) {
640 ui->pull_or_was_d->setText("Pull landed (&p)");
641 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
642 ui->pull_or_was_d->setEnabled(true);
644 ui->throwaway->setText("Pull OOB (&t)");
645 ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
646 ui->throwaway->setEnabled(true);
648 ui->pull_or_was_d->setEnabled(false);
649 ui->throwaway->setEnabled(false);
651 ui->their_pull->setEnabled(false); // We don't track their pull landings; only by means of catch etc.
653 ui->catch_->setEnabled(false);
654 ui->drop->setEnabled(false);
655 ui->goal->setEnabled(false);
656 ui->stallout->setEnabled(false);
657 ui->soft_plus->setEnabled(false);
658 ui->soft_minus->setEnabled(false);
659 ui->interception->setEnabled(false);
660 ui->their_throwaway->setEnabled(false);
661 ui->our_defense->setEnabled(false);
662 ui->their_goal->setEnabled(false);
666 // Not pulling, so reuse the pull button for got d-ed.
667 ui->pull_or_was_d->setText("Was d-ed (&z)");
668 ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
669 ui->pull_or_was_d->setEnabled(true);
671 ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
672 ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
673 ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
674 ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
675 ui->stallout->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
676 ui->soft_plus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
677 ui->soft_minus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
678 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); // Was d-ed.
680 ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
681 ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
682 ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
683 ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
684 ui->their_pull->setEnabled(false);
687 vector<pair<string, int>> sort_gender(const map<string, int> &gender_count)
689 vector<pair<string, int>> sorted_gender;
690 for (const auto &[gender, count] : gender_count) {
691 sorted_gender.emplace_back(gender, count);
693 sort(sorted_gender.begin(), sorted_gender.end(), [](const pair<string, int> &a, const pair<string, int> &b) {
694 if (a.second != b.second) {
695 return b.second < a.second;
697 return a.first < b.first;
699 return sorted_gender;
702 string format_gender_counts(const map<string, int> &gender_count)
704 vector<pair<string, int>> sorted_gender = sort_gender(gender_count);
707 for (const auto &[gender, count] : sorted_gender) {
712 snprintf(buf, sizeof(buf), "%d ", count);
719 string format_gender_counts(const map<string, int> &gender_count, const map<string, int> &reference_gender_count)
721 vector<pair<string, int>> sorted_gender = sort_gender(reference_gender_count); // Less swapping around this way.
724 for (const auto &[gender, count] : sorted_gender) {
729 snprintf(buf, sizeof(buf), "%d/%d ", gender_count.find(gender)->second, count);
736 void MainWindow::update_gender_ratio(uint64_t t)
741 // Count the gender ratio in the current selection.
742 map<string, int> gender_count;
743 for (int i = 0; i < players->rowCount(QModelIndex()); ++i) {
744 string gender = players->get_player_gender(i);
745 gender_count[gender] = 0;
748 QItemSelectionModel *select = ui->player_view->selectionModel();
749 for (QModelIndex row : select->selectedRows()) {
750 string gender = players->get_player_gender(row.row());
751 ++gender_count[gender];
755 const bool gender_rule_a = ui->action_gender_rule_a->isChecked();
757 // This is tricky. We don't want to hard-code assumptions about gender,
758 // since there are so many possible variations (e.g. 5 players for indoors,
759 // or loose mixed). We can't get everything right, but our general
762 // - We assume ABBA pattern is followed throughout, ie., we switch
763 // on odd-numbered points. We use goals as reference for points.
764 // - We always use the two or three latest points as reference;
765 // this means an issue with the first point won't persist forever.
766 // - When we don't switch, we expect the identical number as last time.
767 // - When we _do_ switch, we copy the variation from two points ago
768 // if we have it; if not, we simply expect it to be different.
769 // - We should always have the same number of people as last point.
771 // The viewer's warnings also checks that things are correct across
772 // stoppages, which we don't yet.
773 map<string, int> current_gender_count;
774 for (int i = 0; i < players->rowCount(QModelIndex()); ++i) {
775 string gender = players->get_player_gender(i);
776 current_gender_count[gender] = 0;
778 vector<map<string, int>> historical_gender_counts;
779 for (int row = 0; row < events->rowCount(QModelIndex()); ++row) {
780 if (events->get_time(row) > t) {
783 EventType type = events->get_event_type(row);
784 if (type == EventType::GOAL || type == EventType::THEIR_GOAL) {
785 historical_gender_counts.push_back(current_gender_count);
786 } else if (type == EventType::IN) {
787 string gender = players->get_player_gender_by_id(*events->get_player_id(row));
788 ++current_gender_count[gender];
789 } else if (type == EventType::OUT) {
790 string gender = players->get_player_gender_by_id(*events->get_player_id(row));
791 --current_gender_count[gender];
796 if (historical_gender_counts.empty()) {
797 // We don't have any points yet. Just output the ratio.
798 str = format_gender_counts(gender_count);
799 } else if (historical_gender_counts.size() == 1) {
800 // We have one, so this one must be different, but we don't know what it must be.
801 // It must have the same number of players, though.
802 str = format_gender_counts(gender_count);
803 ok = (gender_count != historical_gender_counts.back());
804 int old_sum = 0, new_sum = 0;
805 for (const auto &[gender, count] : historical_gender_counts.back()) {
808 for (const auto &[gender, count] : gender_count) {
811 if (old_sum != new_sum) {
814 } else if (historical_gender_counts.size() % 2 == 0) {
815 // Must be same as previous.
816 str = format_gender_counts(gender_count, historical_gender_counts.back());
817 ok = (gender_count == historical_gender_counts.back());
819 // Must be same as two points ago.
820 const auto &ref = historical_gender_counts[historical_gender_counts.size() - 3];
821 str = format_gender_counts(gender_count, ref);
822 ok = (gender_count == ref);
824 } else if (gender_count.size() == 1) {
825 // Everybody is either of the same gender or nobody has gender noted,
826 // so just count the number of players. We don't make red here.
828 snprintf(buf, sizeof(buf), "%d selected", num_players);
831 // We don't have gender rule A, but we have gender counts,
832 // so show that. We don't make red here.
833 string str = format_gender_counts(gender_count);
836 // Seemingly this setting this every frame is very costly, so we diff.
837 if (QString::fromUtf8(str) != ui->selected_gender_ratio->text()) {
838 ui->selected_gender_ratio->setText(QString::fromUtf8(str));
840 bool current_ok = ui->selected_gender_ratio->styleSheet().isEmpty();
841 if (ok && !current_ok) {
842 ui->selected_gender_ratio->setStyleSheet("");
843 } else if (!ok && current_ok) {
844 ui->selected_gender_ratio->setStyleSheet("QLabel { color: red }");
848 void MainWindow::formation_double_clicked(bool offense, unsigned row)
850 FormationsModel *formations = offense ? offensive_formations : defensive_formations;
851 int id = formations->get_formation_id(row);
852 if (id == -1) { // “Add new” clicked.
854 QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
855 if (!ok || new_formation_str.isEmpty()) {
859 id = formations->insert_new(new_formation_str.toStdString());
860 QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
861 view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
862 events->inserted_new_formation(id, new_formation_str.toStdString());
864 events->set_formation_at(ui->video->get_position(), offense, id);
866 update_ui_from_time(ui->video->get_position());