Add unsaved changes prompts

This commit is contained in:
2026-06-29 18:24:59 +08:00
parent cc31574830
commit 9d57117094
5 changed files with 68 additions and 7 deletions
+48 -5
View File
@@ -90,6 +90,9 @@ class MainWindow(QMainWindow):
self._update_status()
def closeEvent(self, event) -> None:
if not self._confirm_close_all_sessions():
event.ignore()
return
self.settings.setValue("windowGeometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
super().closeEvent(event)
@@ -166,6 +169,8 @@ class MainWindow(QMainWindow):
if not (0 <= index < len(self.sessions)):
return
session = self.sessions[index]
if not self._confirm_close_session(index):
return
session.document.close()
self.tab_widget.removeTab(index)
self.sessions.pop(index)
@@ -178,8 +183,39 @@ class MainWindow(QMainWindow):
index = self.tab_widget.currentIndex()
if 0 <= index < len(self.sessions):
title = self.document.title if self.document.is_open else "未打开"
if self._session_has_unsaved_changes(self.sessions[index]):
title = f"*{title}"
self.tab_widget.setTabText(index, title)
def _session_has_unsaved_changes(self, session: DocumentSession) -> bool:
return session.document.is_dirty or bool(session.session_raster_objects)
def _confirm_close_session(self, index: int) -> bool:
session = self.sessions[index]
if not self._session_has_unsaved_changes(session):
return True
title = session.document.title if session.document.is_open else "未打开"
reply = QMessageBox.question(
self,
"未保存的更改",
f"{title} 有未保存的更改,是否保存?",
QMessageBox.StandardButton.Save
| QMessageBox.StandardButton.Discard
| QMessageBox.StandardButton.Cancel,
)
if reply == QMessageBox.StandardButton.Cancel:
return False
if reply == QMessageBox.StandardButton.Discard:
return True
self._activate_session(index)
return self.save_file()
def _confirm_close_all_sessions(self) -> bool:
for index in reversed(range(len(self.sessions))):
if not self._confirm_close_session(index):
return False
return True
def offer_restore_last_file(self) -> None:
last_file = self.settings.value("lastOpenFile", "", str)
if not last_file or self.document.is_open or not Path(last_file).exists():
@@ -235,25 +271,27 @@ class MainWindow(QMainWindow):
except Exception as exc:
QMessageBox.critical(self, "打开失败", f"无法打开 PDF:\n{exc}")
def save_file(self) -> None:
def save_file(self) -> bool:
if not self._ensure_document():
return
return False
try:
self._flush_session_raster_objects()
self.document.save()
self._update_current_tab_title()
self.statusBar().showMessage("已保存", 3000)
return True
except Exception as exc:
QMessageBox.critical(self, "保存失败", f"无法保存文件:\n{exc}")
return False
def save_as_file(self) -> None:
def save_as_file(self) -> bool:
if not self._ensure_document():
return
return False
selected, _ = QFileDialog.getSaveFileName(
self, "另存为", self.document.title, "PDF 文件 (*.pdf)"
)
if not selected:
return
return False
if not selected.lower().endswith(".pdf"):
selected += ".pdf"
try:
@@ -263,8 +301,10 @@ class MainWindow(QMainWindow):
self._update_current_tab_title()
self._update_title()
self.statusBar().showMessage("已另存为", 3000)
return True
except Exception as exc:
QMessageBox.critical(self, "另存为失败", f"无法保存文件:\n{exc}")
return False
def save_encrypted_file(self) -> None:
if not self._ensure_document():
@@ -1391,6 +1431,7 @@ class MainWindow(QMainWindow):
def _refresh_session_raster_overlays(self) -> None:
self.viewer.set_overlay_images(self.session_raster_objects)
self._update_current_tab_title()
def _clear_session_raster_objects(self) -> None:
self.session_raster_objects.clear()
@@ -1409,6 +1450,7 @@ class MainWindow(QMainWindow):
else:
self.document.insert_image_rect(int(item["page_index"]), str(item["path"]), rect)
self._clear_session_raster_objects()
self._update_current_tab_title()
def _start_placement(self, payload: dict[str, object], message: str) -> None:
self.pending_placement = payload
@@ -1701,6 +1743,7 @@ class MainWindow(QMainWindow):
self.setWindowTitle(f"{self.document.title} - PDFReaderLite")
def _update_status(self) -> None:
self._update_current_tab_title()
if not self.document.is_open:
self.statusBar().showMessage("就绪")
return