Add unsaved changes prompts
This commit is contained in:
@@ -41,6 +41,7 @@ class PdfDocument:
|
||||
self.path: Path | None = None
|
||||
self._doc: fitz.Document | None = None
|
||||
self._revision = 0
|
||||
self._dirty = False
|
||||
self._undo_stack: list[bytes] = []
|
||||
self._redo_stack: list[bytes] = []
|
||||
self._render_cache: OrderedDict[tuple[int, int, float], QImage] = OrderedDict()
|
||||
@@ -60,6 +61,10 @@ class PdfDocument:
|
||||
return "未打开文档"
|
||||
return self.path.name
|
||||
|
||||
@property
|
||||
def is_dirty(self) -> bool:
|
||||
return self._dirty
|
||||
|
||||
def open(self, path: str | Path, password: str | None = None) -> None:
|
||||
self.close()
|
||||
self.path = Path(path)
|
||||
@@ -68,6 +73,7 @@ class PdfDocument:
|
||||
if not password or not self._doc.authenticate(password):
|
||||
self.close()
|
||||
raise EncryptedPdfError("该 PDF 需要密码。")
|
||||
self._dirty = False
|
||||
self.clear_history()
|
||||
|
||||
def close(self) -> None:
|
||||
@@ -75,6 +81,7 @@ class PdfDocument:
|
||||
self._doc.close()
|
||||
self._doc = None
|
||||
self.path = None
|
||||
self._dirty = False
|
||||
self.clear_history()
|
||||
self.clear_cache()
|
||||
|
||||
@@ -653,7 +660,7 @@ class PdfDocument:
|
||||
self._doc.close()
|
||||
self.path = target
|
||||
self._doc = fitz.open(target)
|
||||
self.mark_changed()
|
||||
self.mark_saved()
|
||||
return
|
||||
|
||||
with NamedTemporaryFile(delete=False, suffix=".pdf", dir=target.parent) as tmp:
|
||||
@@ -666,7 +673,7 @@ class PdfDocument:
|
||||
if self._doc:
|
||||
self._doc.close()
|
||||
self._doc = fitz.open(target)
|
||||
self.mark_changed()
|
||||
self.mark_saved()
|
||||
finally:
|
||||
if temp_path.exists():
|
||||
temp_path.unlink()
|
||||
@@ -721,6 +728,12 @@ class PdfDocument:
|
||||
|
||||
def mark_changed(self) -> None:
|
||||
self._revision += 1
|
||||
self._dirty = True
|
||||
self.clear_cache()
|
||||
|
||||
def mark_saved(self) -> None:
|
||||
self._revision += 1
|
||||
self._dirty = False
|
||||
self.clear_cache()
|
||||
|
||||
def snapshot(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user