dcs-retribution/qt_ui/windows/QDebriefingWindow.py
Dan Albert 1760532168
Rename UnitType.name what it is: the variant ID.
This property affects safe compat because the ID is what gets preserved
in the save, but it's unfortunately also used as the display name, which
means changing the display name breaks save compat. It also prevents us
from changing display names without breaking faction definitions.

This is the first step in fixing that. The next is adding a separate
display_name property that can be updated without breaking either of
those.
2023-10-07 17:08:33 +02:00

109 lines
3.6 KiB
Python

import logging
from typing import Callable, Dict, TypeVar
from PySide2.QtGui import QIcon, QPixmap, QCloseEvent
from PySide2.QtWidgets import (
QDialog,
QGridLayout,
QGroupBox,
QLabel,
QPushButton,
QScrollArea,
QVBoxLayout,
QWidget,
)
from game.debriefing import Debriefing
from qt_ui.windows.GameUpdateSignal import GameUpdateSignal
T = TypeVar("T")
class LossGrid(QGridLayout):
def __init__(self, debriefing: Debriefing, player: bool) -> None:
super().__init__()
self.add_loss_rows(
debriefing.air_losses.by_type(player), lambda u: u.variant_id
)
self.add_loss_rows(
debriefing.front_line_losses_by_type(player), lambda u: str(u)
)
self.add_loss_rows(
debriefing.convoy_losses_by_type(player), lambda u: f"{u} from convoy"
)
self.add_loss_rows(
debriefing.cargo_ship_losses_by_type(player),
lambda u: f"{u} from cargo ship",
)
self.add_loss_rows(
debriefing.airlift_losses_by_type(player), lambda u: f"{u} from airlift"
)
self.add_loss_rows(debriefing.ground_object_losses_by_type(player), lambda u: u)
self.add_loss_rows(debriefing.scenery_losses_by_type(player), lambda u: u)
# TODO: Display dead ground object units and runways.
def add_loss_rows(self, losses: Dict[T, int], make_name: Callable[[T], str]):
for unit_type, count in losses.items():
row = self.rowCount()
try:
name = make_name(unit_type)
except AttributeError:
logging.exception(f"Could not make unit name for {unit_type}")
name = unit_type.id
self.addWidget(QLabel(name), row, 0)
self.addWidget(QLabel(str(count)), row, 1)
class ScrollingCasualtyReportContainer(QGroupBox):
def __init__(self, debriefing: Debriefing, player: bool) -> None:
country = debriefing.player_country if player else debriefing.enemy_country
super().__init__(f"{country}'s lost units:")
scroll_content = QWidget()
scroll_content.setLayout(LossGrid(debriefing, player))
scroll_area = QScrollArea()
scroll_area.setWidget(scroll_content)
layout = QVBoxLayout()
layout.addWidget(scroll_area)
self.setLayout(layout)
class QDebriefingWindow(QDialog):
def __init__(self, debriefing: Debriefing):
super(QDebriefingWindow, self).__init__()
self.debriefing = debriefing
self.setModal(True)
self.setWindowTitle("Debriefing")
self.setMinimumSize(300, 200)
self.setWindowIcon(QIcon("./resources/icon.png"))
layout = QVBoxLayout()
self.setLayout(layout)
header = QLabel(self)
header.setGeometry(0, 0, 655, 106)
pixmap = QPixmap("./resources/ui/debriefing.png")
header.setPixmap(pixmap)
layout.addWidget(header)
layout.addStretch()
title = QLabel("<b>Casualty report</b>")
layout.addWidget(title)
player_lost_units = ScrollingCasualtyReportContainer(debriefing, player=True)
layout.addWidget(player_lost_units)
enemy_lost_units = ScrollingCasualtyReportContainer(debriefing, player=False)
layout.addWidget(enemy_lost_units)
okay = QPushButton("Okay")
okay.clicked.connect(self.close)
layout.addWidget(okay)
def closeEvent(self, event: QCloseEvent) -> None:
super().closeEvent(event)
state = self.debriefing.game.check_win_loss()
GameUpdateSignal.get_instance().gameStateChanged(state)