From 77eb3b0bb3ffe904dd41d72d07d61a0ae53a5e90 Mon Sep 17 00:00:00 2001 From: jbellister-slac Date: Fri, 21 Oct 2022 16:13:39 -0700 Subject: [PATCH 1/2] Don't merge the global and local style sheets together when setting a display's style sheet --- pydm/display.py | 15 ++++----------- pydm/tests/test_display.py | 18 ++++++++---------- pydm/utilities/stylesheet.py | 8 ++------ 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/pydm/display.py b/pydm/display.py index ba58bf87e..d2f1fe03d 100644 --- a/pydm/display.py +++ b/pydm/display.py @@ -12,7 +12,6 @@ from qtpy.QtWidgets import QApplication, QWidget from .utilities import import_module_by_filename, is_pydm_app, macro -from .utilities.stylesheet import merge_widget_stylesheet, global_style class ScreenTarget: @@ -310,10 +309,10 @@ def setStyleSheet(self, new_stylesheet): logger.debug("Calling Display.setStyleSheet, new_stylesheet is %s", possible_stylesheet_filename) stylesheet_filename = None try: - #First, check if the file is already an absolute path. + # First, check if the file is already an absolute path. if os.path.isfile(possible_stylesheet_filename): stylesheet_filename = possible_stylesheet_filename - #Second, check if the css file is specified relative to the display file. + # Second, check if the css file is specified relative to the display file. else: rel_path = os.path.join(os.path.dirname(os.path.abspath(self._loaded_file)), possible_stylesheet_filename) if os.path.isfile(rel_path): @@ -326,11 +325,5 @@ def setStyleSheet(self, new_stylesheet): logger.debug("styleSheet property contains a filename, loading %s", stylesheet_filename) with open(stylesheet_filename) as f: self._local_style = f.read() - style = global_style() + self._local_style - logger.debug("Setting stylesheet to: %s", style) - super(Display, self).setStyleSheet(style) - - def styleSheet(self): - logger.debug("local styleSheet is: %s", self._local_style) - logger.debug("real styleSheet is: %s", super(Display, self).styleSheet()) - return self._local_style + logger.debug("Setting stylesheet to: %s", self._local_style) + super(Display, self).setStyleSheet(self._local_style) diff --git a/pydm/tests/test_display.py b/pydm/tests/test_display.py index 8c0ee33e6..854685b85 100644 --- a/pydm/tests/test_display.py +++ b/pydm/tests/test_display.py @@ -83,6 +83,7 @@ def test_load_python_file_with_macros(qtbot): assert display.ui_filename() == 'test.ui' assert display.macros() == {'MACRO_1': 7, 'MACRO_2': 'test_string'} + def test_file_path_in_stylesheet_property(qtbot): """If you supply a valid filename argument, you shouldn't get any exceptions.""" my_display = Display(parent=None, ui_filename=test_ui_path) @@ -93,15 +94,12 @@ def test_file_path_in_stylesheet_property(qtbot): css = css_file.read() # Assert that the stylesheet property is populated with the contents of the file. assert my_display.styleSheet() == css - # Assert that the stylesheet property "hides" the global stylesheet info. - assert QWidget.styleSheet(my_display) == pydm.utilities.stylesheet.global_style() + css + def test_stylesheet_property_without_path(qtbot): - """If you supply a valid filename argument, you shouldn't get any exceptions.""" - my_display = Display(parent=None, ui_filename=test_ui_path) - qtbot.addWidget(my_display) - css = "PyDMLabel { font-weight: bold; }" - my_display.setStyleSheet(css) - assert my_display.styleSheet() == css - # Assert that the stylesheet property "hides" the global stylesheet info. - assert QWidget.styleSheet(my_display) == pydm.utilities.stylesheet.global_style() + css + """If you supply a valid filename argument, you shouldn't get any exceptions.""" + my_display = Display(parent=None, ui_filename=test_ui_path) + qtbot.addWidget(my_display) + css = "PyDMLabel { font-weight: bold; }" + my_display.setStyleSheet(css) + assert my_display.styleSheet() == css diff --git a/pydm/utilities/stylesheet.py b/pydm/utilities/stylesheet.py index f721f57c3..35edfe1d6 100644 --- a/pydm/utilities/stylesheet.py +++ b/pydm/utilities/stylesheet.py @@ -28,12 +28,6 @@ def clear_cache(): __style_data = None -def merge_widget_stylesheet(widget, stylesheet_file_path=None): - curr_style = widget.styleSheet() or "" - env_style = _get_style_data(stylesheet_file_path) or "" - widget.setStyleSheet(env_style + curr_style) - - def apply_stylesheet(stylesheet_file_path=None, widget=None): """ Apply a stylesheet to the current Qt Designer form (a .ui file) or to a @@ -59,6 +53,7 @@ def apply_stylesheet(stylesheet_file_path=None, widget=None): widget.setStyleSheet(style) + def _get_style_data(stylesheet_file_path=None): """ Read the global stylesheet file and provide the style data as a str. @@ -116,5 +111,6 @@ def _get_style_data(stylesheet_file_path=None): str(ex))) return __style_data + def global_style(): return _get_style_data() From bc7f7dda3170baed9f861f90f7d5b5b499ab29d8 Mon Sep 17 00:00:00 2001 From: jbellister-slac Date: Mon, 24 Oct 2022 09:46:59 -0700 Subject: [PATCH 2/2] Putting back the method for merging stylesheets in case it is used outside the main codebase --- pydm/utilities/stylesheet.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pydm/utilities/stylesheet.py b/pydm/utilities/stylesheet.py index 35edfe1d6..72c7968b8 100644 --- a/pydm/utilities/stylesheet.py +++ b/pydm/utilities/stylesheet.py @@ -28,6 +28,12 @@ def clear_cache(): __style_data = None +def merge_widget_stylesheet(widget, stylesheet_file_path=None): + curr_style = widget.styleSheet() or "" + env_style = _get_style_data(stylesheet_file_path) or "" + widget.setStyleSheet(env_style + curr_style) + + def apply_stylesheet(stylesheet_file_path=None, widget=None): """ Apply a stylesheet to the current Qt Designer form (a .ui file) or to a