|
18 | 18 | import webbrowser |
19 | 19 | from logging import getLogger |
20 | 20 | from pathlib import Path |
| 21 | +from tempfile import NamedTemporaryFile |
21 | 22 |
|
22 | 23 | from scenedetect import AVAILABLE_BACKENDS, FrameTimecode, open_video |
23 | 24 |
|
|
26 | 27 | from dvr_scan.app.region_editor import RegionEditor |
27 | 28 | from dvr_scan.app.scan_window import ScanWindow |
28 | 29 | from dvr_scan.app.widgets import ColorPicker, Spinbox, TimecodeEntry |
29 | | -from dvr_scan.config import CHOICE_MAP, CONFIG_MAP, ConfigLoadFailure, ConfigRegistry |
| 30 | +from dvr_scan.config import ( |
| 31 | + CHOICE_MAP, |
| 32 | + CONFIG_MAP, |
| 33 | + USER_CONFIG_FILE_PATH, |
| 34 | + ConfigLoadFailure, |
| 35 | + ConfigRegistry, |
| 36 | +) |
30 | 37 | from dvr_scan.scanner import OutputMode, Point |
31 | 38 | from dvr_scan.shared import ScanSettings |
32 | 39 | from dvr_scan.subtractor import SubtractorCudaMOG2 |
@@ -1182,6 +1189,7 @@ def save(self, settings: ScanSettings) -> ScanSettings: |
1182 | 1189 | settings.set("output-mode", self._output_mode) |
1183 | 1190 | if self._output_dir: |
1184 | 1191 | settings.set("output-dir", self._output_dir) |
| 1192 | + # TODO: Should we save all these settings instead of being dependent on output mode? |
1185 | 1193 | if self._output_mode == OutputMode.FFMPEG: |
1186 | 1194 | settings.set("ffmpeg-input-args", self._ffmpeg_input_args.get()) |
1187 | 1195 | settings.set("ffmpeg-output-args", self._ffmpeg_output_args.get()) |
@@ -1226,6 +1234,7 @@ def __init__(self, root: tk.Tk, frame: tk.Widget): |
1226 | 1234 | pady=(0, PADDING), |
1227 | 1235 | ) |
1228 | 1236 | self._scan_only = tk.BooleanVar(frame, value=False) |
| 1237 | + # TODO: This should be merged into output-mode to match the config file option. |
1229 | 1238 | self._scan_only_button = ttk.Checkbutton( |
1230 | 1239 | frame, |
1231 | 1240 | text="Scan Only", |
@@ -1346,7 +1355,9 @@ def _create_menubar(self): |
1346 | 1355 | ) |
1347 | 1356 | # TODO: Add functionality to save settings to a config file. |
1348 | 1357 | settings_menu.add_command(label="Save...", underline=0, command=self._on_save_config) |
1349 | | - settings_menu.add_command(label="Save As User Default", underline=2, state=tk.DISABLED) |
| 1358 | + settings_menu.add_command( |
| 1359 | + label="Save As User Default", underline=2, command=self._on_save_config_as_user_default |
| 1360 | + ) |
1350 | 1361 | settings_menu.add_separator() |
1351 | 1362 | settings_menu.add_command( |
1352 | 1363 | label="Reset To User Default", underline=12, command=self._reset_config |
@@ -1503,9 +1514,25 @@ def _on_save_config(self): |
1503 | 1514 | if not save_path: |
1504 | 1515 | return |
1505 | 1516 | settings = self._get_config_settings() |
| 1517 | + logger.debug(f"saving config to {save_path}") |
1506 | 1518 | with open(save_path, "w") as file: |
1507 | 1519 | settings.write_to_file(file) |
1508 | 1520 |
|
| 1521 | + def _on_save_config_as_user_default(self): |
| 1522 | + if not tkinter.messagebox.askyesno( |
| 1523 | + title="Save As User Default", |
| 1524 | + message="Existing user config will be overwritten. Do you want to continue?", |
| 1525 | + icon=tkinter.messagebox.WARNING, |
| 1526 | + ): |
| 1527 | + return |
| 1528 | + |
| 1529 | + settings = self._get_config_settings() |
| 1530 | + logger.debug(f"saving config to {USER_CONFIG_FILE_PATH}") |
| 1531 | + with NamedTemporaryFile(mode="w", delete_on_close=False) as file: |
| 1532 | + settings.write_to_file(file) |
| 1533 | + file.close() |
| 1534 | + Path(file.name).replace(USER_CONFIG_FILE_PATH) |
| 1535 | + |
1509 | 1536 | def _get_config_settings(self) -> ScanSettings: |
1510 | 1537 | """Get current UI state for writing a config file.""" |
1511 | 1538 | settings = ScanSettings(args=None, config=ConfigRegistry()) |
|
0 commit comments