-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_operations.py
More file actions
183 lines (165 loc) · 6.49 KB
/
Copy pathfile_operations.py
File metadata and controls
183 lines (165 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""File operation helpers for RapNote main window."""
from __future__ import annotations
from typing import TYPE_CHECKING
from PyQt6.QtWidgets import QFileDialog, QMessageBox
from logic import export_txt, import_txt
from model import Project, load_project, save_autosave, save_project
if TYPE_CHECKING:
from ui import MainWindow
class FileOperations:
"""Handle project file lifecycle for the main window."""
def __init__(self, window: "MainWindow") -> None:
self._window = window
def confirm_discard_if_modified(self) -> bool:
if self._window.is_modified:
res = QMessageBox.question(
self._window,
self._window.i18n.t("UNSAVED_TITLE"),
self._window.i18n.t("UNSAVED_Q"),
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
)
return res == QMessageBox.StandardButton.Yes
return True
def file_new(self) -> None:
if self._window.is_modified:
save_autosave(self._window.project)
if not self.confirm_discard_if_modified():
return
self._window.project = Project.new_default()
self._window.i18n.lang = self._window.project.lang
self._window.current_path = None
self._window.active_block_id = self._window.project.blocks[0].id
self._window.apply_language_to_ui()
self._window.apply_project_to_controls()
self._window.rebuild_all()
self._window.mark_modified(False)
def file_open(self) -> None:
if self._window.is_modified:
save_autosave(self._window.project)
if not self.confirm_discard_if_modified():
return
path, _ = QFileDialog.getOpenFileName(
self._window,
self._window.i18n.t("OPEN_PROJECT"),
"",
self._window.i18n.t("FILTER_RAPNOTE"),
)
if not path:
return
try:
project = load_project(path)
except Exception as exc: # pylint: disable=broad-except
QMessageBox.critical(self._window, "Open failed", str(exc))
return
self._window.project = project
self._window.current_path = path
self._window.active_block_id = self._window.project.blocks[0].id if self._window.project.blocks else None
self._window.i18n.lang = self._window.project.lang
self._window.apply_language_to_ui()
self._window.apply_project_to_controls()
self._window.rebuild_all()
self._window.mark_modified(False)
self._window.statusBar().showMessage(self._window.i18n.t("OPENED"), 2000)
def file_save(self) -> None:
if not self._window.current_path:
self.file_save_as()
return
try:
save_project(self._window.project, self._window.current_path)
except Exception as exc: # pylint: disable=broad-except
QMessageBox.critical(self._window, "Save failed", str(exc))
return
self._window.mark_modified(False)
self._window.statusBar().showMessage(self._window.i18n.t("SAVED"), 2000)
def file_save_as(self) -> None:
path, _ = QFileDialog.getSaveFileName(
self._window,
self._window.i18n.t("SAVE_PROJECT"),
"",
self._window.i18n.t("FILTER_RAPNOTE_ONLY"),
)
if not path:
return
if not path.lower().endswith(".rapnote"):
path += ".rapnote"
self._window.current_path = path
self.file_save()
def file_import_txt(self) -> None:
if self._window.is_modified:
save_autosave(self._window.project)
if not self.confirm_discard_if_modified():
return
path, _ = QFileDialog.getOpenFileName(
self._window,
self._window.i18n.t("IMPORT_TXT_FILE"),
"",
self._window.i18n.t("FILTER_TXT"),
)
if not path:
return
try:
blocks = import_txt(path)
except Exception as exc: # pylint: disable=broad-except
QMessageBox.critical(self._window, "Import failed", str(exc))
return
if not blocks:
QMessageBox.information(
self._window,
self._window.i18n.t("IMPORT_TXT_EMPTY_TITLE"),
self._window.i18n.t("IMPORT_TXT_EMPTY_MESSAGE"),
)
return
self._window.project.blocks = blocks
self._window.current_path = None
self._window.active_block_id = blocks[0].id
self._window.apply_project_to_controls()
self._window.rebuild_all()
self._window.mark_modified(True)
self._window.statusBar().showMessage(self._window.i18n.t("IMPORTED_TXT"), 2000)
def file_export(self) -> None:
path, _ = QFileDialog.getSaveFileName(
self._window,
self._window.i18n.t("EXPORT_FILE"),
"",
self._window.i18n.t("FILTER_TXT"),
)
if not path:
return
if not path.lower().endswith(".txt"):
path += ".txt"
try:
export_txt(self._window.project, path)
except Exception as exc: # pylint: disable=broad-except
QMessageBox.critical(self._window, "Export failed", str(exc))
return
self._window.statusBar().showMessage(self._window.i18n.t("EXPORTED"), 2000)
def file_export_json(self) -> None:
path, _ = QFileDialog.getSaveFileName(
self._window,
self._window.i18n.t("EXPORT_FILE"),
"",
self._window.i18n.t("FILTER_JSON_ONLY"),
)
if not path:
return
if not path.lower().endswith(".json"):
path += ".json"
try:
save_project(self._window.project, path)
except Exception as exc: # pylint: disable=broad-except
QMessageBox.critical(self._window, "Export failed", str(exc))
return
self._window.statusBar().showMessage(self._window.i18n.t("EXPORTED"), 2000)
def mark_modified(self, modified: bool) -> None:
self._window.is_modified = modified
title = self._window.i18n.t("APP_TITLE")
if self._window.current_path:
title += f" — {self._window.current_path}"
if modified:
title += " *"
self._window.setWindowTitle(title)
def close_event(self, event) -> None:
if not self.confirm_discard_if_modified():
event.ignore()
return
event.accept()