Skip to content

Commit

Permalink
Reload the editor if the JSON file is changed externally
Browse files Browse the repository at this point in the history
  • Loading branch information
s-simoncelli committed Feb 17, 2024
1 parent f0f4566 commit fc617b0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
2 changes: 0 additions & 2 deletions pywr_editor/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ def app() -> None:
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

# create new model command
if create_new is not None:
Expand Down
35 changes: 34 additions & 1 deletion pywr_editor/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Literal

import PySide6
from PySide6.QtCore import Signal, Slot
from PySide6.QtCore import QTimer, Signal, Slot
from PySide6.QtGui import QAction, QKeySequence, Qt, QUndoStack
from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QSplitter

Expand Down Expand Up @@ -93,6 +93,12 @@ def __init__(self, model_file: str | None = None):
return
self.model_config.model_changed.connect(self.on_model_change)

# listen for file changes
self.timer = QTimer(self)
# noinspection PyUnresolvedReferences
self.timer.timeout.connect(self.listen_for_changes)
self.timer.start(3000)

self.editor_settings = Settings(model_file)
# store recent files
if model_file is not None:
Expand Down Expand Up @@ -1092,3 +1098,30 @@ def reload_model_file(self) -> None:
self.prompt_unsaved_changes = False
MainWindow(self.model_file)
self.close()

@Slot()
def listen_for_changes(self) -> None:
"""
Check whether the file is changed externally and reload the model.
:return: None
"""
# do not refresh if the window has not focus
if not self.window().isActiveWindow():
return

# noinspection PyBroadException
try:
with open(self.model_file, "r") as file:
# only reload the editor if there are not changes made using the editor
if (
file.read() != self.model_config.as_string
and not self.model_config.has_changes
):
self.logger.debug(
"Reloading model because it was changed externally"
)
self.timer.stop()
self.close()
MainWindow(self.model_file)
except Exception:
pass
13 changes: 13 additions & 0 deletions pywr_editor/model/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,19 @@ def reload_file_info(self) -> None:
"""
self.file = ModelFileInfo(self.json_file)

@property
def as_string(self) -> str | bool:
"""
Saves the model as JSON string.
:return: A string containing the model, or False if the model cannot be
converted to a string.
"""
# noinspection PyBroadException
try:
return json.dumps(self.json, indent=2)
except Exception:
return False

def save(self) -> bool | str:
"""
Saves the JSON file.
Expand Down

0 comments on commit fc617b0

Please sign in to comment.