Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark model as changed during validation at init to fix timer loop in … #232

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
runs-on: windows-latest
steps:
- name: Python Setup
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.11"
architecture: x64
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Dependencies
run: |
pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/flake8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Python Setup
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.11"
architecture: x64
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
# - name: Collect Workflow Telemetry
# uses: runforesight/workflow-telemetry-action@v1
- name: Python Setup
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.11"
architecture: x64
- name: Checkout Source
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Dependencies
run: |
pip install --upgrade pip
Expand Down
1 change: 1 addition & 0 deletions pywr_editor/assets/assets.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<file alias="plot-data">toolbar/sc_diagramtype.svg</file>
<file alias="run-inspector">toolbar/lc_morecontrols.svg</file>
<file alias="search">toolbar/searchdialog.svg</file>
<file alias="sync-model">toolbar/sync-model.svg</file>
</qresource>
<qresource prefix="file-browser">
<file alias="close">file_browser/x-circle.svg</file>
Expand Down
19 changes: 19 additions & 0 deletions pywr_editor/assets/toolbar/sync-model.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions pywr_editor/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, model_file: str | None = None):
self.empty_model: bool = False
self.prompt_unsaved_changes: bool = True
""" Prompts the user for unsaved model changes """
self.toggle_sync_model_file = False

if model_file is None:
self.logger.debug("No model file was provided")
Expand Down Expand Up @@ -221,6 +222,19 @@ def register_model_actions(self) -> None:
icon=":/toolbar/reload",
tooltip="Reload the JSON file if it was externally edited",
connection=self.reload_model_file,
button_separator=False,
)
)
self.app_actions.add(
Action(
key="sync-model",
name="Sync\n file",
icon=":/toolbar/sync-model",
tooltip="Automatically reload the editor if the JSON file is "
"edited externally. Sync will be paused if there are unsaved "
"changes within the editor",
connection=self.sync_model_file,
is_checked=self.toggle_sync_model_file,
button_separator=True,
)
)
Expand Down Expand Up @@ -588,6 +602,8 @@ def setup_toolbar(self) -> None:
file_panel.add_button(self.app_actions.get("save-model"))
self.app_actions.get("save-model").setDisabled(True)
file_panel.add_button(self.app_actions.get("reload-model"))
file_panel.add_button(self.app_actions.get("sync-model"))

file_panel.add_button(self.app_actions.get("search-in-model"))
file_panel.add_button(self.app_actions.get("open-json-reader"))

Expand Down Expand Up @@ -1106,8 +1122,8 @@ 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():
# do not refresh if the window has not focus or sync is off
if not self.window().isActiveWindow() or not self.toggle_sync_model_file:
return

# noinspection PyBroadException
Expand All @@ -1126,3 +1142,16 @@ def listen_for_changes(self) -> None:
MainWindow(self.model_file)
except Exception:
pass

@Slot()
def sync_model_file(self) -> None:
"""
Enable sync model.
:return: None
"""
self.toggle_sync_model_file = not self.toggle_sync_model_file
self.app_actions.get("sync-model").setChecked(self.toggle_sync_model_file)

status_message = "JSON file synchronisation "
status_message += "enabled" if self.toggle_sync_model_file else "disabled"
self.statusBar().showMessage(status_message)
4 changes: 4 additions & 0 deletions pywr_editor/model/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def check_missing_keys(self) -> None:
for key in ["nodes", "edges", "includes", "scenarios"]:
if key not in self.json:
self.json[key] = []
self.has_changed()

# check for missing keys with dictionary as value
for key in [
Expand All @@ -278,6 +279,7 @@ def check_missing_keys(self) -> None:
]:
if key not in self.json:
self.json[key] = {}
self.has_changed()

if (
Constants.SHAPES_KEY.value
Expand All @@ -286,6 +288,7 @@ def check_missing_keys(self) -> None:
self.json[Constants.EDITOR_CONFIG_KEY.value][
Constants.SHAPES_KEY.value
] = []
self.has_changed()

# check that the metadata dictionary and its key/value pairs are defined
default_metadata = {
Expand All @@ -298,6 +301,7 @@ def check_missing_keys(self) -> None:
self.json["metadata"][key], str
):
self.json["metadata"][key] = value
self.has_changed()

def is_valid(self) -> bool:
"""
Expand Down
Loading
Loading