27
27
List ,
28
28
Callable ,
29
29
Tuple ,
30
+ Literal ,
30
31
)
31
32
32
33
if TYPE_CHECKING :
@@ -272,19 +273,11 @@ def _saveAndCloseApp(self, save_as: bool = False):
272
273
if not success :
273
274
return
274
275
self ._closeApp ()
275
-
276
- def closeAppAfterSaving (self ) -> bool :
276
+
277
+ def _unsavedChangesExist (self ) -> bool :
277
278
"""
278
- Close the app after asking the user whether to save the changes.
279
-
280
- Returns
281
- -------
282
- bool
283
- whether the app is closed
279
+ Check if there are any unsaved changes.
284
280
"""
285
- # first, if the project is open from a file, check the registry dict of the old file
286
- # with that obtained from the current session, if something changed, ask the user
287
- # whether to save the changes
288
281
if self .mainWindow .projectFile is not None :
289
282
registryDict = copy .deepcopy (self .registry .exportDict ())
290
283
registryDictFromFile = copy .deepcopy (
@@ -312,8 +305,36 @@ def closeAppAfterSaving(self) -> bool:
312
305
313
306
else :
314
307
self .mainWindow .unsavedChanges = True
308
+
309
+ return self .mainWindow .unsavedChanges
310
+
311
+ def _saveCheckWithDialog (
312
+ self ,
313
+ ) -> Literal [
314
+ "SAVE_AND_CLOSE" ,
315
+ "CLOSE" ,
316
+ "CANCEL" ,
317
+ ]:
318
+ """
319
+ Before closing the app or moving to a new opened file, check if there
320
+ are any unsaved changes.
315
321
316
- if self .mainWindow .unsavedChanges and self .measDataSet .importFinished :
322
+ Returns
323
+ -------
324
+ Literal[
325
+ "SAVE_AND_CLOSE",
326
+ "CLOSE",
327
+ "CANCEL",
328
+ ]
329
+ The action to take before closing the app.
330
+ """
331
+ # first, if the project is open from a file, check the registry dict of the old file
332
+ # with that obtained from the current session, if something changed, ask the user
333
+ # whether to save the changes
334
+ unsavedChangesExist = self ._unsavedChangesExist ()
335
+
336
+ # if there are unsaved changes, ask the user whether to save the changes
337
+ if unsavedChangesExist and self .measDataSet .importFinished :
317
338
msgBox = QMessageBox ()
318
339
msgBox .setWindowTitle ("qfit" )
319
340
msgBox .setIcon (QMessageBox .Question )
@@ -327,17 +348,34 @@ def closeAppAfterSaving(self) -> bool:
327
348
reply = msgBox .exec_ ()
328
349
329
350
if reply == QMessageBox .Save :
330
- self ._saveAndCloseApp (save_as = self .mainWindow .projectFile is None )
331
- return True
351
+ return "SAVE_AND_CLOSE"
332
352
elif reply == QMessageBox .Discard :
333
- self ._closeApp ()
334
- return True
353
+ return "CLOSE"
335
354
else : # reply == QMessageBox.Cancel
336
- return False
355
+ return "CANCEL"
337
356
338
357
else :
358
+ return "CLOSE"
359
+
360
+ def closeAppAfterSaving (self ) -> bool :
361
+ """
362
+ Close the app after asking the user whether to save the changes.
363
+
364
+ Returns
365
+ -------
366
+ bool
367
+ whether the app is closed
368
+ """
369
+ status = self ._saveCheckWithDialog ()
370
+ if status == "SAVE_AND_CLOSE" :
371
+ self ._saveAndCloseApp (save_as = self .mainWindow .projectFile is None )
372
+ return True
373
+ elif status == "CLOSE" :
339
374
self ._closeApp ()
340
375
return True
376
+ else : # status == "CANCEL"
377
+ return False
378
+
341
379
342
380
# slots ###################################################################
343
381
@Slot ()
@@ -418,9 +456,20 @@ def openFile(
418
456
cause correlated updates to the original HilbertSpace object / other
419
457
HilbertSpace objects.
420
458
"""
421
- if fromMenu and self .menu .isVisible ():
422
- self .menu .toggle ()
423
-
459
+ # check if there are unsaved changes in the current project
460
+ if fromMenu :
461
+ if self .menu .isVisible ():
462
+ self .menu .toggle ()
463
+
464
+ savingStatus = self ._saveCheckWithDialog ()
465
+ print (savingStatus )
466
+ if savingStatus == "SAVE_AND_CLOSE" :
467
+ self ._saveProject ()
468
+ elif savingStatus == "CLOSE" :
469
+ pass
470
+ else : # savingStatus == "CANCEL"
471
+ return
472
+
424
473
# check if file exists
425
474
if fileName is not None :
426
475
if not os .path .isfile (fileName ):
0 commit comments