Document handling
New/Open/Save/Save as and Undo/Redo
This commit is contained in:
67
src/bedit_gui/views/dialogs/document_dialogs.py
Normal file
67
src/bedit_gui/views/dialogs/document_dialogs.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum, auto
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget
|
||||
|
||||
|
||||
class SaveChangesChoice(Enum):
|
||||
SAVE = auto()
|
||||
DISCARD = auto()
|
||||
CANCEL = auto()
|
||||
|
||||
|
||||
class DocumentDialogs:
|
||||
"""All modal dialogs used by the document workflow."""
|
||||
|
||||
FILE_FILTER = "BEdit documents (*.beb *.json);;BEdit binary (*.beb);;JSON (*.json)"
|
||||
|
||||
def __init__(self, parent: QWidget) -> None:
|
||||
self._parent = parent
|
||||
|
||||
def choose_open_path(self, current_path: Path | None) -> Path | None:
|
||||
directory = str(current_path.parent) if current_path else ""
|
||||
file_name, _ = QFileDialog.getOpenFileName(
|
||||
self._parent,
|
||||
"Open BEdit Document",
|
||||
directory,
|
||||
self.FILE_FILTER,
|
||||
)
|
||||
return Path(file_name) if file_name else None
|
||||
|
||||
def choose_save_path(self, current_path: Path | None) -> Path | None:
|
||||
suggested = str(current_path) if current_path else "Untitled.beb"
|
||||
file_name, selected_filter = QFileDialog.getSaveFileName(
|
||||
self._parent,
|
||||
"Save BEdit Document",
|
||||
suggested,
|
||||
self.FILE_FILTER,
|
||||
)
|
||||
if not file_name:
|
||||
return None
|
||||
|
||||
path = Path(file_name)
|
||||
if not path.suffix:
|
||||
suffix = ".json" if "JSON" in selected_filter else ".beb"
|
||||
path = path.with_suffix(suffix)
|
||||
return path
|
||||
|
||||
def ask_save_changes(self) -> SaveChangesChoice:
|
||||
answer = QMessageBox.warning(
|
||||
self._parent,
|
||||
"Unsaved Changes",
|
||||
"The current document has unsaved changes.",
|
||||
QMessageBox.StandardButton.Save
|
||||
| QMessageBox.StandardButton.Discard
|
||||
| QMessageBox.StandardButton.Cancel,
|
||||
QMessageBox.StandardButton.Save,
|
||||
)
|
||||
if answer == QMessageBox.StandardButton.Save:
|
||||
return SaveChangesChoice.SAVE
|
||||
if answer == QMessageBox.StandardButton.Discard:
|
||||
return SaveChangesChoice.DISCARD
|
||||
return SaveChangesChoice.CANCEL
|
||||
|
||||
def show_file_error(self, title: str, error: Exception) -> None:
|
||||
QMessageBox.critical(self._parent, title, str(error))
|
||||
Reference in New Issue
Block a user