mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Add locking to some UI actions.
This is by no means complete. The bugs that this solves were already in 6.x, but we'd hidden the speed controls for the sim in that release, and have always said that anything done after pressing "go" the first time is undefined behavior. This is the first step on making those mid-sim actions behave correctly. UI actions such as creating a new package need to be executed between ticks of the sim. We can either do this synchronously by blocking the UI until the tick is done executing, acquiring a lock on the sim, executing the action, then releasing the lock; or asynchronously by queueing events and letting the sim execute them when it completes the current tick (or instantly if the sim is paused). Anything that comes from the new UI (currently just the map) must be asynchronous because it goes through the REST API, but for the old UI it's simpler (and because the lock will only be acquired as quickly as the user can act, shouldn't slow anything down) to do this synchronously, since it's difficult to use coroutines in Qt. https://github.com/dcs-liberation/dcs_liberation/issues/1680
This commit is contained in:
108
qt_ui/models.py
108
qt_ui/models.py
@@ -158,54 +158,60 @@ class PackageModel(QAbstractListModel):
|
||||
|
||||
def add_flight(self, flight: Flight) -> None:
|
||||
"""Adds the given flight to the package."""
|
||||
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
|
||||
self.package.add_flight(flight)
|
||||
# update_tot is not called here because the new flight does not have a
|
||||
# flight plan yet. Will be called manually by the caller.
|
||||
self.endInsertRows()
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
|
||||
self.package.add_flight(flight)
|
||||
# update_tot is not called here because the new flight does not have a
|
||||
# flight plan yet. Will be called manually by the caller.
|
||||
self.endInsertRows()
|
||||
|
||||
def cancel_or_abort_flight_at_index(self, index: QModelIndex) -> None:
|
||||
"""Removes the flight at the given index from the package."""
|
||||
self.cancel_or_abort_flight(self.flight_at_index(index))
|
||||
|
||||
def cancel_or_abort_flight(self, flight: Flight) -> None:
|
||||
if flight.state.cancelable:
|
||||
self.delete_flight(flight)
|
||||
EventStream.put_nowait(GameUpdateEvents().delete_flight(flight))
|
||||
else:
|
||||
flight.abort()
|
||||
EventStream.put_nowait(GameUpdateEvents().update_flight(flight))
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
if flight.state.cancelable:
|
||||
self.delete_flight(flight)
|
||||
EventStream.put_nowait(GameUpdateEvents().delete_flight(flight))
|
||||
else:
|
||||
flight.abort()
|
||||
EventStream.put_nowait(GameUpdateEvents().update_flight(flight))
|
||||
|
||||
def delete_flight(self, flight: Flight) -> None:
|
||||
"""Removes the given flight from the package."""
|
||||
index = self.package.flights.index(flight)
|
||||
self.beginRemoveRows(QModelIndex(), index, index)
|
||||
self.package.remove_flight(flight)
|
||||
self.endRemoveRows()
|
||||
self.update_tot()
|
||||
self.game_model.release_freq(flight.frequency)
|
||||
self.game_model.release_tacan(flight.tacan)
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
index = self.package.flights.index(flight)
|
||||
self.beginRemoveRows(QModelIndex(), index, index)
|
||||
self.package.remove_flight(flight)
|
||||
self.endRemoveRows()
|
||||
self.update_tot()
|
||||
self.game_model.release_freq(flight.frequency)
|
||||
self.game_model.release_tacan(flight.tacan)
|
||||
|
||||
def flight_at_index(self, index: QModelIndex) -> Flight:
|
||||
"""Returns the flight located at the given index."""
|
||||
return self.package.flights[index.row()]
|
||||
|
||||
def set_tot(self, tot: datetime.datetime) -> None:
|
||||
self.package.time_over_target = tot
|
||||
self.update_tot()
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
self.package.time_over_target = tot
|
||||
self.update_tot()
|
||||
|
||||
def set_asap(self, asap: bool) -> None:
|
||||
self.package.auto_asap = asap
|
||||
self.update_tot()
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
self.package.auto_asap = asap
|
||||
self.update_tot()
|
||||
|
||||
def update_tot(self) -> None:
|
||||
if self.package.auto_asap:
|
||||
self.package.set_tot_asap(
|
||||
self.game_model.sim_controller.current_time_in_sim
|
||||
)
|
||||
self.tot_changed.emit()
|
||||
# For some reason this is needed to make the UI update quickly.
|
||||
self.layoutChanged.emit()
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
if self.package.auto_asap:
|
||||
self.package.set_tot_asap(
|
||||
self.game_model.sim_controller.current_time_in_sim
|
||||
)
|
||||
self.tot_changed.emit()
|
||||
# For some reason this is needed to make the UI update quickly.
|
||||
self.layoutChanged.emit()
|
||||
|
||||
@property
|
||||
def mission_target(self) -> MissionTarget:
|
||||
@@ -263,34 +269,36 @@ class AtoModel(QAbstractListModel):
|
||||
|
||||
def add_package(self, package: Package) -> None:
|
||||
"""Adds a package to the ATO."""
|
||||
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
|
||||
self.ato.add_package(package)
|
||||
# We do not need to send events for new flights in the package here. Events were
|
||||
# already sent when the flights were added to the in-progress package.
|
||||
self.endInsertRows()
|
||||
# noinspection PyUnresolvedReferences
|
||||
self.client_slots_changed.emit()
|
||||
self.on_packages_changed()
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount())
|
||||
self.ato.add_package(package)
|
||||
# We do not need to send events for new flights in the package here. Events
|
||||
# were already sent when the flights were added to the in-progress package.
|
||||
self.endInsertRows()
|
||||
# noinspection PyUnresolvedReferences
|
||||
self.client_slots_changed.emit()
|
||||
self.on_packages_changed()
|
||||
|
||||
def cancel_or_abort_package_at_index(self, index: QModelIndex) -> None:
|
||||
"""Removes the package at the given index from the ATO."""
|
||||
self.cancel_or_abort_package(self.package_at_index(index))
|
||||
|
||||
def cancel_or_abort_package(self, package: Package) -> None:
|
||||
with EventStream.event_context() as events:
|
||||
if all(f.state.cancelable for f in package.flights):
|
||||
events.delete_flights_in_package(package)
|
||||
self._delete_package(package)
|
||||
return
|
||||
with self.game_model.sim_controller.paused_sim():
|
||||
with EventStream.event_context() as events:
|
||||
if all(f.state.cancelable for f in package.flights):
|
||||
events.delete_flights_in_package(package)
|
||||
self._delete_package(package)
|
||||
return
|
||||
|
||||
package_model = self.find_matching_package_model(package)
|
||||
for flight in package.flights:
|
||||
if flight.state.cancelable:
|
||||
package_model.delete_flight(flight)
|
||||
events.delete_flight(flight)
|
||||
else:
|
||||
flight.abort()
|
||||
events.update_flight(flight)
|
||||
package_model = self.find_matching_package_model(package)
|
||||
for flight in package.flights:
|
||||
if flight.state.cancelable:
|
||||
package_model.delete_flight(flight)
|
||||
events.delete_flight(flight)
|
||||
else:
|
||||
flight.abort()
|
||||
events.update_flight(flight)
|
||||
|
||||
def _delete_package(self, package: Package) -> None:
|
||||
"""Removes the given package from the ATO."""
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Callable, Optional, TYPE_CHECKING
|
||||
@@ -76,6 +78,11 @@ class SimController(QObject):
|
||||
self.started = True
|
||||
self.game_loop.set_simulation_speed(simulation_speed)
|
||||
|
||||
@contextmanager
|
||||
def paused_sim(self) -> Iterator[None]:
|
||||
with self.game_loop.paused_sim():
|
||||
yield
|
||||
|
||||
def run_to_first_contact(self) -> None:
|
||||
self.game_loop.run_to_first_contact()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user