dcs-retribution/qt_ui/windows/mission/QPackageDialog.py
Dan Albert ff083942e8 Replace mission planning UI.
Mission planning has been completely redone. Missions are now planned
by right clicking the target area and choosing "New package".

A package can include multiple flights for the same objective. Right
now the automatic flight planner is only fragging single-flight
packages in the same manner that it used to, but that can be improved
now.

The air tasking order (ATO) is now the left bar of the main UI. This
shows every fragged package, and the flights in the selected package.
The info bar that was previously on the left is now a smaller bar at
the bottom of the screen. The old "Mission Planning" button is now
just the "Take Off" button.

The flight plan display no longer shows enemy flight plans. That could
be re-added if needed, probably with a difficulty/cheat option.

Aircraft inventories have been disassociated from the Planner class.
Aircraft inventories are now stored globally in the Game object.

Save games made prior to this update will not be compatible do to the
changes in how aircraft inventories and planned flights are stored.
2020-09-27 13:44:58 -07:00

199 lines
7.2 KiB
Python

"""Dialogs for creating and editing ATO packages."""
import logging
from typing import Optional
from PySide2.QtCore import QItemSelection, Signal
from PySide2.QtWidgets import (
QDialog,
QHBoxLayout,
QLabel,
QPushButton,
QVBoxLayout,
)
from game.game import Game
from gen.ato import Package
from gen.flights.flight import Flight
from qt_ui.models import AtoModel, PackageModel
from qt_ui.uiconstants import EVENT_ICONS
from qt_ui.widgets.ato import QFlightList
from qt_ui.windows.mission.flight.QFlightCreator import QFlightCreator
from theater.missiontarget import MissionTarget
class QPackageDialog(QDialog):
"""Base package management dialog.
The dialogs for creating a new package and editing an existing dialog are
very similar, and this implements the shared behavior.
"""
#: Emitted when a change is made to the package.
package_changed = Signal()
#: Emitted when a flight is added to the package.
flight_added = Signal(Flight)
#: Emitted when a flight is removed from the package.
flight_removed = Signal(Flight)
def __init__(self, game: Game, model: PackageModel) -> None:
super().__init__()
self.game = game
self.package_model = model
self.add_flight_dialog: Optional[QFlightCreator] = None
self.setMinimumSize(1000, 440)
self.setWindowTitle(
f"Mission Package: {self.package_model.mission_target.name}"
)
self.setWindowIcon(EVENT_ICONS["strike"])
self.layout = QVBoxLayout()
self.summary_row = QHBoxLayout()
self.layout.addLayout(self.summary_row)
self.package_type_label = QLabel("Package Type:")
self.package_type_text = QLabel(self.package_model.description)
# noinspection PyUnresolvedReferences
self.package_changed.connect(lambda: self.package_type_text.setText(
self.package_model.description
))
self.summary_row.addWidget(self.package_type_label)
self.summary_row.addWidget(self.package_type_text)
self.package_view = QFlightList(self.package_model)
self.package_view.selectionModel().selectionChanged.connect(
self.on_selection_changed
)
self.layout.addWidget(self.package_view)
self.button_layout = QHBoxLayout()
self.layout.addLayout(self.button_layout)
self.add_flight_button = QPushButton("Add Flight")
self.add_flight_button.clicked.connect(self.on_add_flight)
self.button_layout.addWidget(self.add_flight_button)
self.delete_flight_button = QPushButton("Delete Selected")
self.delete_flight_button.setProperty("style", "btn-danger")
self.delete_flight_button.clicked.connect(self.on_delete_flight)
self.delete_flight_button.setEnabled(False)
self.button_layout.addWidget(self.delete_flight_button)
self.button_layout.addStretch()
self.setLayout(self.layout)
def on_selection_changed(self, selected: QItemSelection,
_deselected: QItemSelection) -> None:
"""Updates the state of the delete button."""
self.delete_flight_button.setEnabled(not selected.empty())
def on_add_flight(self) -> None:
"""Opens the new flight dialog."""
self.add_flight_dialog = QFlightCreator(
self.game, self.package_model.package
)
self.add_flight_dialog.created.connect(self.add_flight)
self.add_flight_dialog.show()
def add_flight(self, flight: Flight) -> None:
"""Adds the new flight to the package."""
self.package_model.add_flight(flight)
# noinspection PyUnresolvedReferences
self.package_changed.emit()
# noinspection PyUnresolvedReferences
self.flight_added.emit(flight)
def on_delete_flight(self) -> None:
"""Removes the selected flight from the package."""
flight = self.package_view.selected_item
if flight is None:
logging.error(f"Cannot delete flight when no flight is selected.")
return
self.package_model.delete_flight(flight)
# noinspection PyUnresolvedReferences
self.package_changed.emit()
# noinspection PyUnresolvedReferences
self.flight_removed.emit(flight)
class QNewPackageDialog(QPackageDialog):
"""Dialog window for creating a new package.
New packages do not affect the ATO model until they are saved.
"""
def __init__(self, game: Game, model: AtoModel,
target: MissionTarget) -> None:
super().__init__(game, PackageModel(Package(target)))
self.ato_model = model
self.save_button = QPushButton("Save")
self.save_button.setProperty("style", "start-button")
self.save_button.clicked.connect(self.on_save)
self.button_layout.addWidget(self.save_button)
self.delete_flight_button.clicked.connect(self.on_delete_flight)
def on_save(self) -> None:
"""Saves the created package.
Empty packages may be created. They can be modified later, and will have
no effect if empty when the mission is generated.
"""
self.ato_model.add_package(self.package_model.package)
for flight in self.package_model.package.flights:
self.game.aircraft_inventory.claim_for_flight(flight)
self.close()
class QEditPackageDialog(QPackageDialog):
"""Dialog window for editing an existing package.
Changes to existing packages occur immediately.
"""
def __init__(self, game: Game, model: AtoModel,
package: PackageModel) -> None:
super().__init__(game, package)
self.ato_model = model
self.delete_button = QPushButton("Delete package")
self.delete_button.setProperty("style", "btn-danger")
self.delete_button.clicked.connect(self.on_delete)
self.button_layout.addWidget(self.delete_button)
self.done_button = QPushButton("Done")
self.done_button.setProperty("style", "start-button")
self.done_button.clicked.connect(self.on_done)
self.button_layout.addWidget(self.done_button)
# noinspection PyUnresolvedReferences
self.flight_added.connect(self.on_flight_added)
# noinspection PyUnresolvedReferences
self.flight_removed.connect(self.on_flight_removed)
# TODO: Make the new package dialog do this too, return on cancel.
# Not claiming the aircraft when they are added to the planner means that
# inventory counts are not updated until after the new package is updated,
# so you can add an infinite number of aircraft to a new package in the UI,
# which will crash when the flight package is saved.
def on_flight_added(self, flight: Flight) -> None:
self.game.aircraft_inventory.claim_for_flight(flight)
def on_flight_removed(self, flight: Flight) -> None:
self.game.aircraft_inventory.return_from_flight(flight)
def on_done(self) -> None:
"""Closes the window."""
self.close()
def on_delete(self) -> None:
"""Removes the viewed package from the ATO."""
# The ATO model returns inventory for us when deleting a package.
self.ato_model.delete_package(self.package_model.package)
self.close()