Skip to content

Commit

Permalink
Merge pull request #936 from jbellister-slac/fix_stylesheet
Browse files Browse the repository at this point in the history
Fix style sheet overwriting specific rules with global ones
  • Loading branch information
YektaY authored Nov 2, 2022
2 parents deb3608 + bc7f7dd commit 2294642
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
15 changes: 4 additions & 11 deletions pydm/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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)
18 changes: 8 additions & 10 deletions pydm/tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
2 changes: 2 additions & 0 deletions pydm/utilities/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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.
Expand Down Expand Up @@ -116,5 +117,6 @@ def _get_style_data(stylesheet_file_path=None):
str(ex)))
return __style_data


def global_style():
return _get_style_data()

0 comments on commit 2294642

Please sign in to comment.