Skip to content

Commit

Permalink
Avoid cases where empty flights could be created.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAlbert committed Nov 19, 2020
1 parent 216adcc commit 2d56ae1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 12 additions & 1 deletion qt_ui/widgets/combos/QOriginAirfieldSelector.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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))
15 changes: 9 additions & 6 deletions qt_ui/windows/mission/flight/QFlightCreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit 2d56ae1

Please sign in to comment.