]> git.sesse.net Git - pkanalytics/blob - mainwindow.cpp
We only need one set of soft plus/minus buttons.
[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 void save_video_filename(sqlite3 *db, int match_id, const string &filename)
71 {
72         sqlite3_stmt *stmt;
73
74         int ret = sqlite3_prepare_v2(db, "UPDATE match SET video_filename=? WHERE match=?", -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_text(stmt, 1, filename.data(), filename.size(), SQLITE_STATIC);
81         sqlite3_bind_int64(stmt, 2, match_id);
82
83         ret = sqlite3_step(stmt);
84         if (ret == SQLITE_ROW) {
85                 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
86                 abort();
87         }
88
89         ret = sqlite3_finalize(stmt);
90         if (ret != SQLITE_OK) {
91                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
92                 abort();
93         }
94 }
95
96 MainWindow::MainWindow(EventsModel *events, PlayersModel *players,
97                        FormationsModel *offensive_formations, FormationsModel *defensive_formations,
98                        sqlite3 *db, int match_id)
99         : events(events), players(players), offensive_formations(offensive_formations), defensive_formations(defensive_formations), db(db), match_id(match_id)
100 {
101         ui = new Ui::MainWindow;
102         ui->setupUi(this);
103
104         string filename = get_video_filename(db, match_id);
105         bool need_save_filename = false;
106         for ( ;; ) {
107                 if (!filename.empty() && ui->video->open(filename.c_str())) {
108                         break;
109                 }
110
111                 // TODO: Probably relativize this path, so that we can move the .db
112                 // more easily with the videos.
113                 filename = QFileDialog::getOpenFileName(this, "Open video").toUtf8();
114                 need_save_filename = true;
115         }
116         if (need_save_filename) {
117                 save_video_filename(db, match_id, filename);
118         }
119         ui->video->play();
120
121         ui->event_view->setModel(events);
122         ui->event_view->setColumnWidth(1, 150);
123         ui->event_view->setColumnWidth(2, 150);
124         connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
125                 [this, events](const QModelIndex &current, const QModelIndex &previous) {
126                         uint64_t t = events->get_time(current.row());
127                         if (t != ui->video->get_position()) {
128                                 ui->video->seek_absolute(events->get_time(current.row()));
129                         } else {
130                                 // Selection could have changed, so we still need to update.
131                                 // (Just calling setPosition() would not give us the signal
132                                 // in this case.)
133                                 update_ui_from_time(t);
134                         }
135                 });
136
137         ui->player_view->setModel(players);
138         ui->player_view->setColumnWidth(0, 30);
139         ui->player_view->setColumnWidth(1, 20);
140         ui->player_view->horizontalHeader()->setStretchLastSection(true);
141
142         auto formation_changed = [this](const QModelIndex &current, const QModelIndex &previous) {
143                 QTimer::singleShot(1, [=]{  // The selection is wrong until the callback actually returns.
144                         update_action_buttons(ui->video->get_position());
145                 });
146         };
147         ui->offensive_formation_view->setModel(offensive_formations);
148         ui->defensive_formation_view->setModel(defensive_formations);
149         connect(ui->offensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
150         connect(ui->defensive_formation_view->selectionModel(), &QItemSelectionModel::currentRowChanged, formation_changed);
151         connect(ui->offensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
152                 formation_double_clicked(true, index.row());
153         });
154         connect(ui->defensive_formation_view, &QListView::doubleClicked, [this](const QModelIndex &index) {
155                 formation_double_clicked(false, index.row());
156         });
157
158         connect(ui->video, &VideoWidget::position_changed, [this](uint64_t pos) {
159                 position_changed(pos);
160         });
161
162         // It's not really clear whether PgUp should be forwards or backwards,
163         // but mpv does at least up = forwards, so that's probably standard.
164         QShortcut *pgdown = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
165         connect(pgdown, &QShortcut::activated, [this]() { ui->video->seek(-120000); });
166         QShortcut *pgup = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
167         connect(pgup, &QShortcut::activated, [this]() { ui->video->seek(120000); });
168
169         connect(ui->minus10s, &QPushButton::clicked, [this]() { ui->video->seek(-10000); });
170         connect(ui->plus10s, &QPushButton::clicked, [this]() { ui->video->seek(10000); });
171
172         connect(ui->minus2s, &QPushButton::clicked, [this]() { ui->video->seek(-2000); });
173         connect(ui->plus2s, &QPushButton::clicked, [this]() { ui->video->seek(2000); });
174         connect(ui->video, &VideoWidget::mouse_back_clicked, [this]() { ui->video->seek(-2000); });
175         connect(ui->video, &VideoWidget::mouse_forward_clicked, [this]() { ui->video->seek(2000); });
176
177         connect(ui->minus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(-1); });
178         connect(ui->plus1f, &QPushButton::clicked, [this]() { ui->video->seek_frames(1); });
179
180         connect(ui->play_pause, &QPushButton::clicked, [this]() {
181                 if (playing) {
182                         ui->video->pause();
183                         ui->play_pause->setText("Play (space)");
184                 } else {
185                         ui->video->play();
186                         ui->play_pause->setText("Pause (space)");
187                 }
188                 playing = !playing;
189
190                 // Needs to be set anew when we modify setText(), evidently.
191                 ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
192         });
193
194         connect(ui->player_1, &QPushButton::clicked, [this]() { insert_player_event(0); });
195         connect(ui->player_2, &QPushButton::clicked, [this]() { insert_player_event(1); });
196         connect(ui->player_3, &QPushButton::clicked, [this]() { insert_player_event(2); });
197         connect(ui->player_4, &QPushButton::clicked, [this]() { insert_player_event(3); });
198         connect(ui->player_5, &QPushButton::clicked, [this]() { insert_player_event(4); });
199         connect(ui->player_6, &QPushButton::clicked, [this]() { insert_player_event(5); });
200         connect(ui->player_7, &QPushButton::clicked, [this]() { insert_player_event(6); });
201
202         // Offensive events
203         connect(ui->offense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_offense"); });
204         connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
205         connect(ui->throwaway, &QPushButton::clicked, [this, events]() {
206                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
207                 if (s.attack_state == EventsModel::Status::DEFENSE && s.pull_state == EventsModel::Status::PULL_IN_AIR) {
208                         insert_noplayer_event("pull_oob");
209                 } else {
210                         set_current_event_type("throwaway");
211                 }
212         });
213         connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
214         connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
215         connect(ui->soft_plus, &QPushButton::clicked, [this, events]() {
216                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
217                 if (s.attack_state == EventsModel::Status::OFFENSE) {
218                         set_current_event_type("offensive_soft_plus");
219                 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
220                         set_current_event_type("defensive_soft_plus");
221                 }
222         });
223         connect(ui->soft_minus, &QPushButton::clicked, [this, events]() {
224                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
225                 if (s.attack_state == EventsModel::Status::OFFENSE) {
226                         set_current_event_type("offensive_soft_minus");
227                 } else if (s.attack_state == EventsModel::Status::DEFENSE) {
228                         set_current_event_type("defensive_soft_minus");
229                 }
230         });
231         connect(ui->pull_or_was_d, &QPushButton::clicked, [this, events]() {
232                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
233                 if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
234                         set_current_event_type("pull");
235                 } else if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
236                         insert_noplayer_event("pull_landed");
237                 } else if (s.pull_state == EventsModel::Status::NOT_PULLING) {
238                         set_current_event_type("was_d");
239                 }
240         });
241
242         // Defensive events (TODO add more)
243         connect(ui->interception, &QPushButton::clicked, [this]() { set_current_event_type("interception"); });
244         connect(ui->defense_label, &ClickableLabel::clicked, [this]() { insert_noplayer_event("set_defense"); });
245         connect(ui->their_throwaway, &QPushButton::clicked, [this]() { insert_noplayer_event("their_throwaway"); });
246         connect(ui->their_goal, &QPushButton::clicked, [this]() { insert_noplayer_event("their_goal"); });
247         connect(ui->their_pull, &QPushButton::clicked, [this, events]() {
248                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
249                 if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
250                         insert_noplayer_event("their_pull");
251                 }
252         });
253         connect(ui->our_defense, &QPushButton::clicked, [this]() { set_current_event_type("defense"); });
254
255         connect(ui->offensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/true); });
256         connect(ui->defensive_formation, &QPushButton::clicked, [this]() { insert_or_change_formation(/*offense=*/false); });
257
258         // Misc. events
259         connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); });
260         connect(ui->stoppage, &QPushButton::clicked, [this, events]() {
261                 EventsModel::Status s = events->get_status_at(ui->video->get_position());
262                 if (s.stoppage) {
263                         insert_noplayer_event("restart");
264                 } else {
265                         insert_noplayer_event("stoppage");
266                 }
267         });
268         connect(ui->unknown, &QPushButton::clicked, [this]() { insert_noplayer_event("unknown"); });
269
270         QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
271         connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
272         connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
273 }
274
275 void MainWindow::position_changed(uint64_t pos)
276 {
277         ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
278         if (!playing) {
279                 ui->video->pause();  // We only played to get a picture.
280         }
281         if (playing) {
282                 QModelIndex row = events->get_last_event_qt(ui->video->get_position());
283                 ui->event_view->scrollTo(row, QAbstractItemView::PositionAtCenter);
284         }
285         update_ui_from_time(pos);
286 }
287
288 void MainWindow::insert_player_event(int button_id)
289 {
290         uint64_t t = ui->video->get_position();
291         vector<int> team = events->sort_team(events->get_team_at(t));
292         if (unsigned(button_id) >= team.size()) {
293                 return;
294         }
295         int player_id = team[button_id];
296
297         EventsModel::Status s = events->get_status_at(t);
298
299         ui->event_view->selectionModel()->blockSignals(true);
300         if (s.attack_state == EventsModel::Status::OFFENSE) {
301                 // TODO: Perhaps not if that player already did the last catch?
302                 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt, "catch"));
303         } else {
304                 ui->event_view->selectRow(events->insert_event(t, player_id, nullopt));
305         }
306         ui->event_view->selectionModel()->blockSignals(false);
307
308         update_ui_from_time(t);
309 }
310
311 void MainWindow::insert_noplayer_event(const string &type)
312 {
313         uint64_t t = ui->video->get_position();
314
315         ui->event_view->selectionModel()->blockSignals(true);
316         ui->event_view->selectRow(events->insert_event(t, nullopt, nullopt, type));
317         ui->event_view->selectionModel()->blockSignals(false);
318
319         update_ui_from_time(t);
320 }
321
322 void MainWindow::set_current_event_type(const string &type)
323 {
324         QItemSelectionModel *select = ui->event_view->selectionModel();
325         if (!select->hasSelection()) {
326                 return;
327         }
328         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
329         events->set_event_type(row, type);
330         update_ui_from_time(ui->video->get_position());
331 }
332
333 // Formation buttons either modify the existing formation (if we've selected
334 // a formation change event), or insert a new one (if not).
335 void MainWindow::insert_or_change_formation(bool offense)
336 {
337         FormationsModel *formations = offense ? offensive_formations : defensive_formations;
338         QListView *formation_view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
339         if (!formation_view->selectionModel()->hasSelection()) {
340                 // This shouldn't happen; the button should not have been enabled.
341                 return;
342         }
343         int formation_row = formation_view->selectionModel()->selectedRows().front().row();  // Should only be one, due to our selection behavior.
344         int formation_id = formations->get_formation_id(formation_row);
345         if (formation_id == -1) {
346                 // This also shouldn't happen (“Add new…” selected).
347                 return;
348         }
349
350         QItemSelectionModel *select = ui->event_view->selectionModel();
351         if (select->hasSelection()) {
352                 int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
353                 string expected_type = offense ? "formation_offense" : "formation_defense";
354                 if (events->get_event_type(row) == expected_type) {
355                         events->set_event_formation(row, formation_id);
356                         update_ui_from_time(ui->video->get_position());
357                         return;
358                 }
359         }
360
361         // Insert a new formation event instead (same as double-click on the selected one).
362         events->set_formation_at(ui->video->get_position(), offense, formation_id);
363         update_ui_from_time(ui->video->get_position());
364 }
365
366 void MainWindow::delete_current_event()
367 {
368         QItemSelectionModel *select = ui->event_view->selectionModel();
369         if (!select->hasSelection()) {
370                 return;
371         }
372         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
373         ui->event_view->selectionModel()->blockSignals(true);
374         events->delete_event(row);
375         ui->event_view->selectionModel()->blockSignals(false);
376         update_ui_from_time(ui->video->get_position());
377 }
378
379 void MainWindow::make_substitution()
380 {
381         QItemSelectionModel *select = ui->player_view->selectionModel();
382         set<int> new_team;
383         for (QModelIndex row : select->selectedRows()) {
384                 new_team.insert(players->get_player_id(row.row()));
385         }
386         events->set_team_at(ui->video->get_position(), new_team);
387         update_player_buttons(ui->video->get_position());
388 }
389
390 void MainWindow::update_ui_from_time(uint64_t t)
391 {
392         update_status(t);
393         update_player_buttons(t);
394         update_action_buttons(t);
395 }
396
397 void MainWindow::update_status(uint64_t t)
398 {
399         EventsModel::Status s = events->get_status_at(t);
400         char buf[256];
401         std::string formation = "Not started";
402         if (s.attack_state == EventsModel::Status::OFFENSE) {
403                 if (s.offensive_formation != 0) {
404                         formation = offensive_formations->get_formation_name_by_id(s.offensive_formation);
405                 } else {
406                         formation = "Offense";
407                 }
408         } else if (s.attack_state == EventsModel::Status::DEFENSE) {
409                 if (s.defensive_formation != 0) {
410                         formation = defensive_formations->get_formation_name_by_id(s.defensive_formation);
411                 } else {
412                         formation = "Defense";
413                 }
414         }
415
416         snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
417                 s.our_score, s.their_score, formation.c_str(), s.num_passes, s.possession_sec);
418         if (s.stoppage_sec > 0) {
419                 char buf2[256];
420                 snprintf(buf2, sizeof(buf2), "%s (plus %d sec stoppage)", buf, s.stoppage_sec);
421                 ui->status->setText(buf2);
422         } else {
423                 ui->status->setText(buf);
424         }
425 }
426
427 void MainWindow::update_player_buttons(uint64_t t)
428 {
429         QPushButton *buttons[] = {
430                 ui->player_1,
431                 ui->player_2,
432                 ui->player_3,
433                 ui->player_4,
434                 ui->player_5,
435                 ui->player_6,
436                 ui->player_7
437         };
438         const char shortcuts[] = "qweasdf";
439         int num_players = 0;
440         for (int player_id : events->sort_team(events->get_team_at(t))) {
441                 QPushButton *btn = buttons[num_players];
442                 string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")";
443                 char shortcut[2] = "";
444                 shortcut[0] = toupper(shortcuts[num_players]);
445                 btn->setText(QString::fromUtf8(label));
446                 btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr));
447                 btn->setEnabled(true);
448                 if (++num_players == 7) {
449                         break;
450                 }
451         }
452         for (int i = num_players; i < 7; ++i) {
453                 QPushButton *btn = buttons[i];
454                 btn->setText("No player");
455                 btn->setEnabled(false);
456         }
457 }
458
459 void MainWindow::update_action_buttons(uint64_t t)
460 {
461         {
462                 QItemSelectionModel *select = ui->offensive_formation_view->selectionModel();
463                 if (select->hasSelection()) {
464                         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
465                         ui->offensive_formation->setEnabled(offensive_formations->get_formation_id(row) != -1);
466                 } else {
467                         ui->offensive_formation->setEnabled(false);
468                 }
469         }
470         {
471                 QItemSelectionModel *select = ui->defensive_formation_view->selectionModel();
472                 if (select->hasSelection()) {
473                         int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
474                         ui->defensive_formation->setEnabled(defensive_formations->get_formation_id(row) != -1);
475                 } else {
476                         ui->defensive_formation->setEnabled(false);
477                 }
478         }
479
480         EventsModel::Status s = events->get_status_at(t);
481
482         bool has_selection = false;
483         bool has_selection_with_player = false;
484
485         QItemSelectionModel *select = ui->event_view->selectionModel();
486         if (select->hasSelection()) {
487                 has_selection = true;
488                 int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
489                 has_selection_with_player = events->get_player_id(row).has_value();
490         }
491         ui->delete_->setEnabled(has_selection);
492
493         if (s.stoppage) {
494                 ui->stoppage->setText("Restart (&v)");
495                 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
496                 ui->catch_->setEnabled(false);
497                 ui->throwaway->setEnabled(false);
498                 ui->drop->setEnabled(false);
499                 ui->goal->setEnabled(false);
500                 ui->soft_plus->setEnabled(false);
501                 ui->soft_minus->setEnabled(false);
502                 ui->pull_or_was_d->setEnabled(false);
503                 ui->interception->setEnabled(false);
504                 ui->their_throwaway->setEnabled(false);
505                 ui->our_defense->setEnabled(false);
506                 ui->their_goal->setEnabled(false);
507                 ui->their_pull->setEnabled(false);
508                 return;
509         } else {
510                 ui->stoppage->setText("Stoppage (&v)");
511                 ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr));
512         }
513
514         // Defaults for pull-related buttons.
515         ui->pull_or_was_d->setText("Pull (&p)");
516         ui->their_pull->setText("Their pull (&p)");
517         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
518         ui->their_pull->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
519         ui->throwaway->setText("Throwaway (&t)");
520         ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
521
522         if (s.pull_state == EventsModel::Status::SHOULD_PULL) {
523                 ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
524                 ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE);
525
526                 ui->catch_->setEnabled(false);
527                 ui->throwaway->setEnabled(false);
528                 ui->drop->setEnabled(false);
529                 ui->goal->setEnabled(false);
530                 ui->soft_plus->setEnabled(false);
531                 ui->soft_minus->setEnabled(false);
532                 ui->interception->setEnabled(false);
533                 ui->their_throwaway->setEnabled(false);
534                 ui->our_defense->setEnabled(false);
535                 ui->their_goal->setEnabled(false);
536                 return;
537         }
538         if (s.pull_state == EventsModel::Status::PULL_IN_AIR) {
539                 if (s.attack_state == EventsModel::Status::DEFENSE) {
540                         ui->pull_or_was_d->setText("Pull landed (&p)");
541                         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "P", nullptr));
542                         ui->pull_or_was_d->setEnabled(true);
543
544                         ui->throwaway->setText("Pull OOB (&t)");
545                         ui->throwaway->setShortcut(QCoreApplication::translate("MainWindow", "T", nullptr));
546                         ui->throwaway->setEnabled(true);
547                 } else {
548                         ui->pull_or_was_d->setEnabled(false);
549                         ui->throwaway->setEnabled(false);
550                 }
551                 ui->their_pull->setEnabled(false);  // We don't track their pull landings; only by means of catch etc.
552
553                 ui->catch_->setEnabled(false);
554                 ui->drop->setEnabled(false);
555                 ui->goal->setEnabled(false);
556                 ui->soft_plus->setEnabled(false);
557                 ui->soft_minus->setEnabled(false);
558                 ui->interception->setEnabled(false);
559                 ui->their_throwaway->setEnabled(false);
560                 ui->our_defense->setEnabled(false);
561                 ui->their_goal->setEnabled(false);
562                 return;
563         }
564
565         // Not pulling, so reuse the pull button for got d-ed.
566         ui->pull_or_was_d->setText("Was d-ed (&z)");
567         ui->pull_or_was_d->setShortcut(QCoreApplication::translate("MainWindow", "Z", nullptr));
568         ui->pull_or_was_d->setEnabled(true);
569
570         ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
571         ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
572         ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
573         ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);
574         ui->soft_plus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
575         ui->soft_minus->setEnabled(s.attack_state != EventsModel::Status::NOT_STARTED && has_selection_with_player);
576         ui->pull_or_was_d->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player);  // Was d-ed.
577
578         ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
579         ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
580         ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player);
581         ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE);
582         ui->their_pull->setEnabled(false);
583 }
584
585 void MainWindow::formation_double_clicked(bool offense, unsigned row)
586 {
587         FormationsModel *formations = offense ? offensive_formations : defensive_formations;
588         int id = formations->get_formation_id(row);
589         if (id == -1) {  // “Add new” clicked.
590                 bool ok;
591                 QString new_formation_str = QInputDialog::getText(this, "New formation", "Choose name for new formation:", QLineEdit::Normal, "", &ok);
592                 if (!ok || new_formation_str.isEmpty()) {
593                         return;
594                 }
595
596                 id = formations->insert_new(new_formation_str.toStdString());
597                 QListView *view = offense ? ui->offensive_formation_view : ui->defensive_formation_view;
598                 view->selectionModel()->select(formations->index(formations->get_row_from_id(id), 0), QItemSelectionModel::ClearAndSelect);
599                 events->inserted_new_formation(id, new_formation_str.toStdString());
600         } else {
601                 events->set_formation_at(ui->video->get_position(), offense, id);
602         }
603         update_ui_from_time(ui->video->get_position());
604 }
605