From 4cf406aefae4f094d41f7cb901a1f03d51b570ea Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Sat, 7 Nov 2020 12:35:23 -0800 Subject: [PATCH] Defer game load to after UI initialization. This removes both the double load that happens on startup and also makes it possible to get the UI to create a new game if the existing default.liberation is not compatible. --- qt_ui/widgets/map/QLiberationMap.py | 2 +- qt_ui/windows/QLiberationWindow.py | 49 +++++++++++++++---------- resources/stylesheets/style-dcs.css | 8 ++++ resources/stylesheets/windows-style.css | 8 ++++ 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/qt_ui/widgets/map/QLiberationMap.py b/qt_ui/widgets/map/QLiberationMap.py index 1017619b..c03290f4 100644 --- a/qt_ui/widgets/map/QLiberationMap.py +++ b/qt_ui/widgets/map/QLiberationMap.py @@ -121,8 +121,8 @@ class QLiberationMap(QGraphicsView): def setGame(self, game: Optional[Game]): self.game = game - logging.debug("Reloading Map Canvas") if self.game is not None: + logging.debug("Reloading Map Canvas") self.reload_scene() """ diff --git a/qt_ui/windows/QLiberationWindow.py b/qt_ui/windows/QLiberationWindow.py index 45520c5c..67dba6f8 100644 --- a/qt_ui/windows/QLiberationWindow.py +++ b/qt_ui/windows/QLiberationWindow.py @@ -1,4 +1,5 @@ import logging +import traceback import webbrowser from typing import Optional @@ -16,7 +17,7 @@ from PySide2.QtWidgets import ( ) import qt_ui.uiconstants as CONST -from game import Game, persistency, VERSION +from game import Game, VERSION, persistency from qt_ui.dialogs import Dialog from qt_ui.displayoptions import DisplayGroup, DisplayOptions, DisplayRule from qt_ui.models import GameModel @@ -40,10 +41,9 @@ class QLiberationWindow(QMainWindow): self.game: Optional[Game] = None self.game_model = GameModel() Dialog.set_game(self.game_model) - self.ato_panel = None - self.info_panel = None - self.liberation_map = None - self.setGame(persistency.restore_game()) + self.ato_panel = QAirTaskingOrderPanel(self.game_model) + self.info_panel = QInfoPanel(self.game) + self.liberation_map = QLiberationMap(self.game_model) self.setGeometry(300, 100, 270, 100) self.setWindowTitle(f"DCS Liberation - v{VERSION}") @@ -55,17 +55,14 @@ class QLiberationWindow(QMainWindow): self.initMenuBar() self.initToolbar() self.connectSignals() - self.onGameGenerated(self.game) screen = QDesktopWidget().screenGeometry() self.setGeometry(0, 0, screen.width(), screen.height()) self.setWindowState(Qt.WindowMaximized) - def initUi(self): - self.ato_panel = QAirTaskingOrderPanel(self.game_model) - self.liberation_map = QLiberationMap(self.game_model) - self.info_panel = QInfoPanel(self.game) + self.onGameGenerated(persistency.restore_game()) + def initUi(self): hbox = QSplitter(Qt.Horizontal) vbox = QSplitter(Qt.Vertical) hbox.addWidget(self.ato_panel) @@ -193,8 +190,7 @@ class QLiberationWindow(QMainWindow): filter="*.liberation") if file is not None: game = persistency.load_game(file[0]) - self.setGame(game) - GameUpdateSignal.get_instance().updateGame(self.game) + GameUpdateSignal.get_instance().updateGame(game) def saveGame(self): logging.info("Saving game") @@ -217,14 +213,27 @@ class QLiberationWindow(QMainWindow): GameUpdateSignal.get_instance().updateGame(self.game) def setGame(self, game: Optional[Game]): - if game is not None: - game.on_load() - self.game = game - if self.info_panel is not None: - self.info_panel.setGame(game) - self.game_model.set(self.game) - if self.liberation_map is not None: - self.liberation_map.setGame(game) + try: + if game is not None: + game.on_load() + self.game = game + if self.info_panel is not None: + self.info_panel.setGame(game) + self.game_model.set(self.game) + if self.liberation_map is not None: + self.liberation_map.setGame(game) + except AttributeError: + logging.exception("Incompatible save game") + QMessageBox.critical( + self, + "Could not load save game", + "The save game you have loaded is incompatible with this " + "version of DCS Liberation.\n" + "\n" + f"{traceback.format_exc()}", + QMessageBox.Ok + ) + GameUpdateSignal.get_instance().updateGame(None) def showAboutDialog(self): text = "

DCS Liberation " + VERSION + "

" + \ diff --git a/resources/stylesheets/style-dcs.css b/resources/stylesheets/style-dcs.css index bd866559..dab1b553 100644 --- a/resources/stylesheets/style-dcs.css +++ b/resources/stylesheets/style-dcs.css @@ -19,6 +19,14 @@ grey text -------------------- #B7C0C6 */ +/* + * Makes all message box text selectable. + * https://stackoverflow.com/a/32595502/632035 + */ +QMessageBox { + messagebox-text-interaction-flags: 5; +} + /*QMenuBar*/ QMenuBar { spacing: 2px; /* spacing between menu bar items */ diff --git a/resources/stylesheets/windows-style.css b/resources/stylesheets/windows-style.css index cbf5010f..5d6c99ea 100644 --- a/resources/stylesheets/windows-style.css +++ b/resources/stylesheets/windows-style.css @@ -1,3 +1,11 @@ /* windows basis styles */ + +/* + * Makes all message box text selectable. + * https://stackoverflow.com/a/32595502/632035 + */ +QMessageBox { + messagebox-text-interaction-flags: 5; +} \ No newline at end of file