diff --git a/qt_ui/widgets/combos/QOriginAirfieldSelector.py b/qt_ui/widgets/combos/QOriginAirfieldSelector.py index b0530efc4..eab48e3a6 100644 --- a/qt_ui/widgets/combos/QOriginAirfieldSelector.py +++ b/qt_ui/widgets/combos/QOriginAirfieldSelector.py @@ -1,9 +1,10 @@ """Combo box for selecting a departure airfield.""" from typing import Iterable +from PySide2.QtCore import Signal from PySide2.QtWidgets import QComboBox - from dcs.planes import PlaneType + from game.inventory import GlobalAircraftInventory from theater.controlpoint import ControlPoint @@ -15,6 +16,8 @@ class QOriginAirfieldSelector(QComboBox): that have unassigned inventory of the given aircraft type. """ + availability_changed = Signal(int) + def __init__(self, global_inventory: GlobalAircraftInventory, origins: Iterable[ControlPoint], aircraft: PlaneType) -> None: @@ -23,6 +26,7 @@ def __init__(self, global_inventory: GlobalAircraftInventory, self.origins = list(origins) self.aircraft = aircraft self.rebuild_selector() + self.currentIndexChanged.connect(self.index_changed) def change_aircraft(self, aircraft: PlaneType) -> None: if self.aircraft == aircraft: @@ -47,3 +51,10 @@ def available(self) -> int: return 0 inventory = self.global_inventory.for_control_point(origin) return inventory.available(self.aircraft) + + def index_changed(self, index: int) -> None: + origin = self.itemData(index) + if origin is None: + return + inventory = self.global_inventory.for_control_point(origin) + self.availability_changed.emit(inventory.available(self.aircraft)) diff --git a/qt_ui/windows/mission/flight/QFlightCreator.py b/qt_ui/windows/mission/flight/QFlightCreator.py index ee4b33543..f4fe60419 100644 --- a/qt_ui/windows/mission/flight/QFlightCreator.py +++ b/qt_ui/windows/mission/flight/QFlightCreator.py @@ -54,11 +54,11 @@ def __init__(self, game: Game, package: Package, parent=None) -> None: [cp for cp in game.theater.controlpoints if cp.captured], self.aircraft_selector.currentData() ) - self.airfield_selector.currentIndexChanged.connect(self.update_max_size) + self.airfield_selector.availability_changed.connect(self.update_max_size) layout.addLayout(QLabeledWidget("Airfield:", self.airfield_selector)) self.flight_size_spinner = QFlightSizeSpinner() - self.update_max_size() + self.update_max_size(self.airfield_selector.available) layout.addLayout(QLabeledWidget("Size:", self.flight_size_spinner)) self.client_slots_spinner = QFlightSizeSpinner( @@ -91,6 +91,8 @@ def verify_form(self) -> Optional[str]: return f"{origin.name} has no {aircraft.id} available." if size > available: return f"{origin.name} has only {available} {aircraft.id} available." + if size <= 0: + return f"Flight must have at least one aircraft." return None def create_flight(self) -> None: @@ -120,7 +122,8 @@ def on_aircraft_changed(self, index: int) -> None: new_aircraft = self.aircraft_selector.itemData(index) self.airfield_selector.change_aircraft(new_aircraft) - def update_max_size(self) -> None: - self.flight_size_spinner.setMaximum( - min(self.airfield_selector.available, 4) - ) + def update_max_size(self, available: int) -> None: + self.flight_size_spinner.setMaximum(min(available, 4)) + if self.flight_size_spinner.maximum() >= 2: + if self.flight_size_spinner.value() < 2: + self.flight_size_spinner.setValue(2)