From b0bcf8605c4a761c7add2b696aa67dba0215133b Mon Sep 17 00:00:00 2001
From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com>
Date: Fri, 8 Nov 2024 17:30:54 +0100
Subject: [PATCH] Fix closing app with no project bug
---
novelwriter/guimain.py | 36 +++++++++++++++---------------
tests/test_base/test_base_init.py | 8 +++++++
tests/test_gui/test_gui_guimain.py | 7 ++++++
3 files changed, 33 insertions(+), 18 deletions(-)
diff --git a/novelwriter/guimain.py b/novelwriter/guimain.py
index a0d28901..1bb744f6 100644
--- a/novelwriter/guimain.py
+++ b/novelwriter/guimain.py
@@ -845,33 +845,33 @@ class GuiMain(QMainWindow):
def closeMain(self) -> bool:
"""Save everything, and close novelWriter."""
- if SHARED.hasProject and SHARED.question("%s
%s" % (
+ if SHARED.hasProject and not SHARED.question("%s
%s" % (
self.tr("Do you want to exit novelWriter?"),
self.tr("Changes are saved automatically.")
)):
- logger.info("Exiting novelWriter")
+ return False
- if not SHARED.focusMode:
- CONFIG.setMainPanePos(self.splitMain.sizes())
- CONFIG.setOutlinePanePos(self.outlineView.splitSizes())
- if self.docViewerPanel.isVisible():
- CONFIG.setViewPanePos(self.splitView.sizes())
+ logger.info("Exiting novelWriter")
- CONFIG.showViewerPanel = self.docViewerPanel.isVisible()
- wFull = Qt.WindowState.WindowFullScreen
- if self.windowState() & wFull != wFull:
- # Ignore window size if in full screen mode
- CONFIG.setMainWinSize(self.width(), self.height())
+ if not SHARED.focusMode:
+ CONFIG.setMainPanePos(self.splitMain.sizes())
+ CONFIG.setOutlinePanePos(self.outlineView.splitSizes())
+ if self.docViewerPanel.isVisible():
+ CONFIG.setViewPanePos(self.splitView.sizes())
- if SHARED.hasProject:
- self.closeProject(True)
- CONFIG.saveConfig()
+ CONFIG.showViewerPanel = self.docViewerPanel.isVisible()
+ wFull = Qt.WindowState.WindowFullScreen
+ if self.windowState() & wFull != wFull:
+ # Ignore window size if in full screen mode
+ CONFIG.setMainWinSize(self.width(), self.height())
- QApplication.quit()
+ if SHARED.hasProject:
+ self.closeProject(True)
+ CONFIG.saveConfig()
- return True
+ QApplication.quit()
- return False
+ return True
def closeViewerPanel(self, byUser: bool = True) -> bool:
"""Close the document view panel."""
diff --git a/tests/test_base/test_base_init.py b/tests/test_base/test_base_init.py
index 0ff69505..6fb691a0 100644
--- a/tests/test_base/test_base_init.py
+++ b/tests/test_base/test_base_init.py
@@ -85,6 +85,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
# Defaults w/None Args
nwGUI = main()
+ assert nwGUI is not None
assert logger.getEffectiveLevel() == logging.WARNING
assert nwGUI.closeMain() == "closeMain"
@@ -92,6 +93,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", f"--config={fncPath}", f"--data={fncPath}", "--style=Fusion"]
)
+ assert nwGUI is not None
assert logger.getEffectiveLevel() == logging.WARNING
assert nwGUI.closeMain() == "closeMain"
@@ -99,12 +101,14 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", "--info", f"--config={fncPath}", f"--data={fncPath}"]
)
+ assert nwGUI is not None
assert logger.getEffectiveLevel() == logging.INFO
assert nwGUI.closeMain() == "closeMain"
nwGUI = main(
["--testmode", "--debug", f"--config={fncPath}", f"--data={fncPath}"]
)
+ assert nwGUI is not None
assert logger.getEffectiveLevel() == logging.DEBUG
assert nwGUI.closeMain() == "closeMain"
@@ -113,6 +117,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", "--help", f"--config={fncPath}", f"--data={fncPath}"]
)
+ assert nwGUI is not None
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 0
@@ -120,6 +125,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", "--version", f"--config={fncPath}", f"--data={fncPath}"]
)
+ assert nwGUI is not None
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 0
@@ -128,6 +134,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", "--invalid", f"--config={fncPath}", f"--data={fncPath}"]
)
+ assert nwGUI is not None
assert nwGUI.closeMain() == "closeMain"
assert ex.value.code == 2
@@ -135,6 +142,7 @@ def testBaseInit_Options(monkeypatch, fncPath):
nwGUI = main(
["--testmode", f"--config={fncPath}", f"--data={fncPath}", "sample/"]
)
+ assert nwGUI is not None
assert nwGUI.closeMain() == "closeMain"
diff --git a/tests/test_gui/test_gui_guimain.py b/tests/test_gui/test_gui_guimain.py
index 6d151b81..8bdbc45e 100644
--- a/tests/test_gui/test_gui_guimain.py
+++ b/tests/test_gui/test_gui_guimain.py
@@ -100,6 +100,13 @@ def testGuiMain_Launch(qtbot, monkeypatch, nwGUI, projPath):
assert nwGUI.openProject(projPath) is True
nwGUI.closeProject()
+ # Check that closes can be blocked
+ with monkeypatch.context() as mp:
+ mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.StandardButton.No)
+ assert nwGUI.openProject(projPath) is True
+ assert nwGUI.closeMain() is False
+ nwGUI.closeProject()
+
# Check that latest release info updated
assert CONFIG.lastNotes != "0x0"