Skip to content

Commit

Permalink
Merge pull request #1060 from jbellister-slac/fix_reload
Browse files Browse the repository at this point in the history
FIX: Pick up changes to UI files when the main window is reloaded
  • Loading branch information
YektaY authored Feb 12, 2024
2 parents aabe637 + 0898fe6 commit 8587254
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pydm/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def _load_ui_into_display(uifile, display):
display.ui = display


def clear_compiled_ui_file_cache() -> None:
"""
Clears the cache of compiled ui files. Needed if changes to the underlying ui files have been made on disk and
need to be picked up, such as the user choosing to reload the display.
"""
_compile_ui_file.cache_clear()


def _load_compiled_ui_into_display(
code_string: str, class_name: str, display: Display, macros: Optional[Dict[str, str]] = None
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion pydm/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from qtpy.QtGui import QKeySequence
from .utilities import IconFont, find_file, establish_widget_connections, close_widget_connections
from .pydm_ui import Ui_MainWindow
from .display import Display, ScreenTarget, load_file
from .display import Display, ScreenTarget, load_file, clear_compiled_ui_file_cache
from .connection_inspector import ConnectionInspector
from .about_pydm import AboutWindow
from .show_macros import MacroWindow
Expand Down Expand Up @@ -442,6 +442,7 @@ def reload_display(self, checked):
loaded_file = curr_display.loaded_file()

self.statusBar().showMessage("Reloading '{0}'...".format(self.current_file()), 5000)
clear_compiled_ui_file_cache()
new_widget = self.open(loaded_file, macros=macros, args=args)
new_widget.previous_display = prev_display
new_widget.next_display = next_display
Expand Down
30 changes: 30 additions & 0 deletions pydm/tests/test_main_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

from pydm import PyDMApplication
from pydm.display import Display, clear_compiled_ui_file_cache
from qtpy import uic
from unittest.mock import MagicMock, patch

# The path to the .ui file for creating a main window
test_ui_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data", "test.ui")


@patch("qtpy.uic.compileUi", wraps=uic.compileUi)
def test_reload_display(wrapped_compile_ui: MagicMock, qapp: PyDMApplication) -> None:
"""Verify that when a user reloads a PyDM window the underling display's ui file is actually reloaded"""
clear_compiled_ui_file_cache() # Ensure other tests have not already compiled our test file before we start

try:
display = Display(parent=None, ui_filename=test_ui_path)

qapp.make_main_window()
qapp.main_window.set_display_widget(display)

# When the display is first created and loaded the underlying ui file gets compiled
wrapped_compile_ui.assert_called_once()

# Reloading should force a re-compile of the ui file to ensure any changes are picked up
qapp.main_window.reload_display(True)
assert wrapped_compile_ui.call_count == 2
finally:
clear_compiled_ui_file_cache()

0 comments on commit 8587254

Please sign in to comment.