11from pathlib import Path
2+ from typing import TYPE_CHECKING , cast
23
34from qtpy .QtCore import QFile , QFileInfo , QSaveFile , Qt , QTextStream
45from qtpy .QtWidgets import QApplication , QFileDialog , QMessageBox , QTextEdit
78from app_model .backends .qt import QModelMainWindow
89from app_model .expressions import create_context
910
11+ if TYPE_CHECKING :
12+ from app_model .backends .qt ._qmenu import QModelMenuBar
13+
1014
1115class MainWindow (QModelMainWindow ):
1216 def __init__ (self , app : Application ) -> None :
@@ -21,7 +25,8 @@ def __init__(self, app: Application) -> None:
2125 self .addModelToolBar (MenuId .FILE , exclude = {CommandId .SAVE_AS , CommandId .EXIT })
2226 self .addModelToolBar (MenuId .EDIT )
2327 self .addModelToolBar (MenuId .HELP )
24- self .statusBar ().showMessage ("Ready" )
28+ if sb := self .statusBar ():
29+ sb .showMessage ("Ready" )
2530
2631 self .set_current_file ("" )
2732
@@ -33,11 +38,13 @@ def _update_context(self, available: bool) -> None:
3338 self ._ctx ["copyAvailable" ] = available
3439
3540 def _on_context_changed (self ) -> None :
36- self .menuBar ().update_from_context (self ._ctx )
41+ mb = cast ("QModelMenuBar" , self .menuBar ())
42+ mb .update_from_context (self ._ctx )
3743
3844 def set_current_file (self , fileName : str ) -> None :
3945 self ._cur_file = fileName
40- self ._text_edit .document ().setModified (False )
46+ if doc := self ._text_edit .document ():
47+ doc .setModified (False )
4148 self .setWindowModified (False )
4249
4350 if self ._cur_file :
@@ -58,11 +65,11 @@ def save_as(self) -> bool:
5865
5966 def save_file (self , fileName : str ) -> bool :
6067 error = None
61- QApplication .setOverrideCursor (Qt .WaitCursor )
68+ QApplication .setOverrideCursor (Qt .CursorShape . WaitCursor )
6269 file = QSaveFile (fileName )
6370 if file .open (QFile .OpenModeFlag .WriteOnly | QFile .OpenModeFlag .Text ): # type: ignore
6471 outf = QTextStream (file )
65- outf << self ._text_edit .toPlainText ()
72+ outf << self ._text_edit .toPlainText () # pyright: ignore
6673 if not file .commit ():
6774 reason = file .errorString ()
6875 error = f"Cannot write file { fileName } :\n { reason } ."
@@ -77,7 +84,7 @@ def save_file(self, fileName: str) -> bool:
7784 return True
7885
7986 def maybe_save (self ) -> bool :
80- if self ._text_edit .document ().isModified ():
87+ if ( doc := self ._text_edit .document ()) and doc .isModified ():
8188 ret = QMessageBox .warning (
8289 self ,
8390 "Application" ,
@@ -113,12 +120,13 @@ def load_file(self, fileName: str) -> None:
113120 return
114121
115122 inf = QTextStream (file )
116- QApplication .setOverrideCursor (Qt .WaitCursor )
123+ QApplication .setOverrideCursor (Qt .CursorShape . WaitCursor )
117124 self ._text_edit .setPlainText (inf .readAll ())
118125 QApplication .restoreOverrideCursor ()
119126
120127 self .set_current_file (fileName )
121- self .statusBar ().showMessage ("File loaded" , 2000 )
128+ if sb := self .statusBar ():
129+ sb .showMessage ("File loaded" , 2000 )
122130
123131 def about (self ) -> None :
124132 QMessageBox .about (
@@ -255,4 +263,4 @@ class CommandId:
255263
256264 app .injection_store .register_provider (lambda : main_win , MainWindow )
257265 main_win .show ()
258- qapp .exec_ ()
266+ qapp .exec ()
0 commit comments