]> git.sesse.net Git - pkanalytics/blob - mainwindow.cpp
Start working on some team-wide stats.
[pkanalytics] / mainwindow.cpp
1 #include <QMediaPlayer>
2 #include <QMainWindow>
3 #include <QApplication>
4 #include <QGridLayout>
5 #include <QShortcut>
6 #include <QFileDialog>
7 #include <QInputDialog>
8 #include <QTimer>
9 #include <algorithm>
10 #include <string>
11 #include <map>
12 #include <vector>
13 #include <optional>
14 #include <sqlite3.h>
15 #include "mainwindow.h"
16 #include "ui_mainwindow.h"
17 #include "events.h"
18 #include "players.h"
19 #include "formations.h"
20 #include "json.h"
21 #include "video_widget.h"
22
23 using namespace std;
24
25 string format_timestamp(uint64_t pos)
26 {
27         int ms = pos % 1000;
28         pos /= 1000;
29         int sec = pos % 60;
30         pos /= 60;
31         int min = pos % 60;
32         int hour = pos / 60;
33
34         char buf[256];
35         snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
36         return buf;
37 }
38
39 string get_video_filename(sqlite3 *db, int match_id)
40 {
41         sqlite3_stmt *stmt;
42
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));
46                 abort();
47         }
48
49         sqlite3_bind_int64(stmt, 1, match_id);
50
51         ret = sqlite3_step(stmt);
52         if (ret != SQLITE_ROW) {
53                 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
54                 abort();
55         }
56
57         if (sqlite3_column_type(stmt, 0) != SQLITE_TEXT) {
58                 return "";
59         }
60         string filename = (const char *)sqlite3_column_text(stmt, 0);
61
62         ret = sqlite3_finalize(stmt);
63         if (ret != SQLITE_OK) {
64                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
65                 abort();
66         }
67         return filename;
68 }
69
70 bool get_match_property(sqlite3 *db, int match_id, const string &prop_name)
71 {
72         sqlite3_stmt *stmt;
73
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));
77                 abort();
78         }
79
80         sqlite3_bind_int64(stmt, 1, match_id);
81
82         ret = sqlite3_step(stmt);
83         if (ret != SQLITE_ROW) {
84                 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
85                 abort();
86         }
87
88         if (sqlite3_column_type(stmt, 0) != SQLITE_INTEGER) {
89                 return "";
90         }
91         bool value = sqlite3_column_int(stmt, 0);
92
93         ret = sqlite3_finalize(stmt);
94         if (ret != SQLITE_OK) {
95                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
96                 abort();
97         }
98         return value;
99 }
100
101 void save_video_filename(sqlite3 *db, int match_id, const string &filename)
102 {
103         sqlite3_stmt *stmt;
104
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));
108                 abort();
109         }
110
111         sqlite3_bind_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
112         sqlite3_bind_int64(stmt, 2, match_id);
113
114         ret = sqlite3_step(stmt);
115         if (ret == SQLITE_ROW) {
116                 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
117                 abort();
118         }
119
120         ret = sqlite3_finalize(stmt);
121         if (ret != SQLITE_OK) {
122                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
123                 abort();
124         }
125 }
126
127 void save_match_property(sqlite3 *db, int match_id, const string &prop_name, bool value)
128 {
129         sqlite3_stmt *stmt;
130
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));
134                 abort();
135         }
136
137         sqlite3_bind_int64(stmt, 1, value);
138         sqlite3_bind_int64(stmt, 2, match_id);
139
140         ret = sqlite3_step(stmt);
141         if (ret == SQLITE_ROW) {
142                 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
143                 abort();
144         }
145
146         ret = sqlite3_finalize(stmt);
147         if (ret != SQLITE_OK) {
148                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
149                 abort();
150         }
151 }
152
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)
157 {
158         ui = new Ui::MainWindow;
159         ui->setupUi(this);
160
161         string filename = get_video_filename(db, match_id);
162         bool need_save_filename = false;
163         for ( ;; ) {
164                 if (!filename.empty() && ui->video->open(filename.c_str())) {
165                         break;
166                 }
167
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;
172         }
173         if (need_save_filename) {
174                 save_video_filename(db, match_id, filename);
175         }
176         ui->video->play();
177
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 &current, 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()));
186                         } else {
187                                 // Selection could have changed, so we still need to update.
188                                 // (Just calling setPosition() would not give us the signal
189                                 // in this case.)
190                                 update_ui_from_time(t);
191                         }
192                 });
193
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);
198
199         auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
200                 QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
201                         update_action_buttons(ui->video->get_position());
202                 });
203         };
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());
210         });
211         connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
212                 formation_double_clicked(false, index.row());
213         });
214
215         connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
216                 position_changed(pos);
217         });
218
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); });
225
226         connect(ui->minus10s, &QPushButton::clicked, [this] { ui->video->seek(-10000); });
227         connect(ui->plus10s, &QPushButton::clicked, [this] { ui->video->seek(10000); });
228
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); });
233
234         connect(ui->minus1f, &QPushButton::clicked, [this] { ui->video->seek_frames(-1); });
235         connect(ui->plus1f, &QPushButton::clicked, [this] { ui->video->seek_frames(1); });
236
237         connect(ui->play_pause, &QPushButton::clicked, [this] {
238                 if (playing) {
239                         ui->video->pause();
240                         ui->play_pause->setText("Play (space)");
241                 } else {
242                         ui->video->play();
243                         ui->play_pause->setText("Pause (space)");
244                 }
245                 playing = !playing;
246
247                 // Needs to be set anew when we modify setText(), evidently.
248                 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
249         });
250
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); });
258
259         // Offensive events
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");
266                 } else {
267                         set_current_event_type("throwaway");
268                 }
269         });
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");
279                 }
280         });
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");
287                 }
288         });
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");
298                 }
299         });
300
301         // Defensive events.
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");
310                 }
311         });
312         connect(ui->our_defense, &QPushButton::clicked, [this] { set_current_event_type("defense"); });
313
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); });
316
317         // Misc. events
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());
321                 if (s.stoppage) {
322                         insert_noplayer_event("restart");
323                 } else {
324                         insert_noplayer_event("stoppage");
325                 }
326         });
327         connect(ui->unknown, &QPushButton::clicked, [this] { insert_noplayer_event("unknown"); });
328
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(); });
332
333         // Menus.
334         connect(ui->action_exit, &QAction::triggered, [this] { close(); });
335         connect(ui->action_export_json, &QAction::triggered, [db] { export_to_json(db, "ultimate.json"); });
336
337         ui->action_gender_rule_a->setChecked(get_match_property(db, match_id, "gender_rule_a"));
338         ui->action_gender_pull_rule->setChecked(get_match_property(db, match_id, "gender_pull_rule"));
339         connect(ui->action_gender_rule_a, &QAction::toggled, [this, db, match_id] {
340                 save_match_property(db, match_id, "gender_rule_a", ui->action_gender_rule_a->isChecked());
341         });
342         connect(ui->action_gender_pull_rule, &QAction::toggled, [this, db, match_id] {
343                 save_match_property(db, match_id, "gender_pull_rule", ui->action_gender_pull_rule->isChecked());
344         });
345 }
346
347 void MainWindow::position_changed(uint64_t pos)
348 {
349         ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
350         if (!playing) {
351                 ui->video->pause();  // We only played to get a picture.
352         }
353         if (playing) {
354                 QModelIndex row = events->get_last_event_qt(ui->video->get_position());
355                 ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
356         }
357         update_ui_from_time(pos);
358 }
359
360 void MainWindow::insert_player_event(int button_id)
361 {
362         uint64_t t = ui->video->get_position();
363         vector<int> team = events->sort_team(events->get_team_at(t));
364         if (unsigned(button_id) >= team.size()) {
365                 return;
366         }
367         int player_id = team[button_id];
368
369         EventsModel::Status s = events->get_status_at(t);
370
371         ui->event_view->selectionModel()->blockSignals(true);
372         if (s.attack_state == EventsModel::Status::OFFENSE) {
373                 // TODO: Perhaps not if that player already did the last catch?
374                 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
375         } else {
376                 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
377         }
378         ui->event_view->selectionModel()->blockSignals(false);
379
380         update_ui_from_time(t);
381 }
382
383 void MainWindow::insert_noplayer_event(const string &type)
384 {
385         uint64_t t = ui->video->get_position();
386
387         ui->event_view->selectionModel()->blockSignals(true);
388         ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
389         ui->event_view->selectionModel()->blockSignals(false);
390
391         update_ui_from_time(t);
392 }
393
394 void MainWindow::set_current_event_type(const string &type)
395 {
396         QItemSelectionModel *select = ui->event_view->selectionModel();
397         if (!select->hasSelection()) {
398                 return;
399         }
400         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
401         events->set_event_type(row, type);
402         update_ui_from_time(ui->video->get_position());
403 }
404
405 // Formation buttons either modify the existing formation (if we've selected
406 // a formation change event), or insert a new one (if not).
407 void MainWindow::insert_or_change_formation(bool offense)
408 {
409         FormationsModel *formations = offense ? offensive_formations : defensive_formations;
410         QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
411         if (!formation_view->selectionModel()->hasSelection()) {
412                 // This shouldn't happen; the button should not have been enabled.
413                 return;
414         }
415         int formation_row = formation_view->selectionModel()->selectedRows().front().row();  // Should only be one, due to our selection behavior.
416         int formation_id = formations->get_formation_id(formation_row);
417         if (formation_id == -1) {
418                 // This also shouldn't happen (“Add new…” selected).
419                 return;
420         }
421
422         QItemSelectionModel *select = ui->event_view->selectionModel();
423         if (select->hasSelection()) {
424                 int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
425                 EventType expected_type = offense ? EventType::FORMATION_OFFENSE : EventType::FORMATION_DEFENSE;
426                 if (events->get_event_type(row) == expected_type) {
427                         events->set_event_formation(row, formation_id);
428                         update_ui_from_time(ui->video->get_position());
429                         return;
430                 }
431         }
432
433         // Insert a new formation event instead (same as double-click on the selected one).
434         events->set_formation_at(ui->video->get_position(), offense, formation_id);
435         update_ui_from_time(ui->video->get_position());
436 }
437
438 void MainWindow::delete_current_event()
439 {
440         QItemSelectionModel *select = ui->event_view->selectionModel();
441         if (!select->hasSelection()) {
442                 return;
443         }
444         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
445         ui->event_view->selectionModel()->blockSignals(true);
446         events->delete_event(row);
447         ui->event_view->selectionModel()->blockSignals(false);
448         update_ui_from_time(ui->video->get_position());
449 }
450
451 void MainWindow::make_substitution()
452 {
453         QItemSelectionModel *select = ui->player_view->selectionModel();
454         set<int> new_team;
455         for (QModelIndex row : select->selectedRows()) {
456                 new_team.insert(players->get_player_id(row.row()));
457         }
458         events->set_team_at(ui->video->get_position(), new_team);
459         update_player_buttons(ui->video->get_position());
460 }
461
462 void MainWindow::update_ui_from_time(uint64_t t)
463 {
464         update_status(t);
465         update_player_buttons(t);
466         update_action_buttons(t);
467 }
468
469 void MainWindow::update_status(uint64_t t)
470 {
471         EventsModel::Status s = events->get_status_at(t);
472         char buf[256];
473         std::string formation = "Not started";
474         if (s.attack_state == EventsModel::Status::OFFENSE) {
475                 if (s.offensive_formation != 0) {
476                         formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
477                 } else {
478                         formation = "Offense";
479                 }
480         } else if (s.attack_state == EventsModel::Status::DEFENSE) {
481                 if (s.defensive_formation != 0) {
482                         formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
483                 } else {
484                         formation = "Defense";
485                 }
486         }
487
488         snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
489                 s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
490         if (s.stoppage_sec > 0) {
491                 char buf2[512];
492                 snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
493                 ui->status->setText(buf2);
494         } else {
495                 ui->status->setText(buf);
496         }
497 }
498
499 void MainWindow::update_player_buttons(uint64_t t)
500 {
501         QPushButton *buttons[] = {
502                 ui->player_1,
503                 ui->player_2,
504                 ui->player_3,
505                 ui->player_4,
506                 ui->player_5,
507                 ui->player_6,
508                 ui->player_7
509         };
510         const char shortcuts[] = "qweasdf";
511         int num_players = 0;
512         for (int player_id : events->sort_team(events->get_team_at(t))) {
513                 QPushButton *btn = buttons[num_players];
514                 string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
515                 char shortcut[2] = "";
516                 shortcut[0] = toupper(shortcuts[num_players]);
517                 btn->setText(QString::fromUtf8(label));
518                 btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
519                 btn->setEnabled(true);
520                 if (++num_players == 7) {
521                         break;
522                 }
523         }
524         for (int i = num_players; i < 7; ++i) {
525                 QPushButton *btn = buttons[i];
526                 btn->setText("No player");
527                 btn->setEnabled(false);
528         }
529 }
530
531 void MainWindow::update_action_buttons(uint64_t t)
532 {
533         {
534                 QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
535                 if (select->hasSelection()) {
536                         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
537                         ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
538                 } else {
539                         ui->offensive_formation->setEnabled(false);
540                 }
541         }
542         {
543                 QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
544                 if (select->hasSelection()) {
545                         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
546                         ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
547                 } else {
548                         ui->defensive_formation->setEnabled(false);
549                 }
550         }
551
552         EventsModel::Status s = events->get_status_at(t);
553
554         bool has_selection = false;
555         bool has_selection_with_player = false;
556
557         QItemSelectionModel *select = ui->event_view->selectionModel();
558         if (select->hasSelection()) {
559                 has_selection = true;
560                 int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
561                 has_selection_with_player = events->get_player_id(row).has_value();
562         }
563         ui->delete_->setEnabled(has_selection);
564
565         if (s.stoppage) {
566                 ui->stoppage->setText("Restart (&v)");
567                 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
568                 ui->catch_->setEnabled(false);
569                 ui->throwaway->setEnabled(false);
570                 ui->drop->setEnabled(false);
571                 ui->goal->setEnabled(false);
572                 ui->stallout->setEnabled(false);
573                 ui->soft_plus->setEnabled(false);
574                 ui->soft_minus->setEnabled(false);
575                 ui->pull_or_was_d->setEnabled(false);
576                 ui->interception->setEnabled(false);
577                 ui->their_throwaway->setEnabled(false);
578                 ui->our_defense->setEnabled(false);
579                 ui->their_goal->setEnabled(false);
580                 ui->their_pull->setEnabled(false);
581                 return;
582         } else {
583                 ui->stoppage->setText("Stoppage (&v)");
584                 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
585         }
586
587         // Defaults for pull-related buttons.
588         ui->pull_or_was_d->setText("Pull (&p)");
589         ui->their_pull->setText("Their pull (&p)");
590         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
591         ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
592         ui->throwaway->setText("Throwaway (&t)");
593         ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
594
595         if (s.pull_state == EventsModel::Status::SHOULD_PULL ||
596             (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.
597                 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
598                 ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE);
599
600                 ui->catch_->setEnabled(false);
601                 ui->throwaway->setEnabled(false);
602                 ui->drop->setEnabled(false);
603                 ui->goal->setEnabled(false);
604                 ui->stallout->setEnabled(false);
605                 ui->soft_plus->setEnabled(false);
606                 ui->soft_minus->setEnabled(false);
607                 ui->interception->setEnabled(false);
608                 ui->their_throwaway->setEnabled(false);
609                 ui->our_defense->setEnabled(false);
610                 ui->their_goal->setEnabled(false);
611                 return;
612         }
613         if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
614                 if (s.attack_state == EventsModel::Status::DEFENSE) {
615                         ui->pull_or_was_d->setText("Pull landed (&p)");
616                         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
617                         ui->pull_or_was_d->setEnabled(true);
618
619                         ui->throwaway->setText("Pull OOB (&t)");
620                         ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
621                         ui->throwaway->setEnabled(true);
622                 } else {
623                         ui->pull_or_was_d->setEnabled(false);
624                         ui->throwaway->setEnabled(false);
625                 }
626                 ui->their_pull->setEnabled(false);  // We don't track their pull landings; only by means of catch etc.
627
628                 ui->catch_->setEnabled(false);
629                 ui->drop->setEnabled(false);
630                 ui->goal->setEnabled(false);
631                 ui->stallout->setEnabled(false);
632                 ui->soft_plus->setEnabled(false);
633                 ui->soft_minus->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                 return;
639         }
640
641         // Not pulling, so reuse the pull button for got d-ed.
642         ui->pull_or_was_d->setText("Was d-ed (&z)");
643         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
644         ui->pull_or_was_d->setEnabled(true);
645
646         ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
647         ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
648         ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
649         ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
650         ui->stallout->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
651         ui->soft_plus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
652         ui->soft_minus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
653         ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);  // Was d-ed.
654
655         ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
656         ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
657         ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
658         ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
659         ui->their_pull->setEnabled(false);
660 }
661
662 void MainWindow::formation_double_clicked(bool offense, unsigned row)
663 {
664         FormationsModel *formations = offense ? offensive_formations : defensive_formations;
665         int id = formations->get_formation_id(row);
666         if (id == -1) {  // “Add new” clicked.
667                 bool ok;
668                 QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
669                 if (!ok || new_formation_str.isEmpty()) {
670                         return;
671                 }
672
673                 id = formations->insert_new(new_formation_str.toStdString());
674                 QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
675                 view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
676                 events->inserted_new_formation(id, new_formation_str.toStdString());
677         } else {
678                 events->set_formation_at(ui->video->get_position(), offense, id);
679         }
680         update_ui_from_time(ui->video->get_position());
681 }
682