Source code for schrodinger.ui.qt.style
"""
Module to manage Schrodinger-wide Qt style sheet. The function
apply_style_sheet() will read the
Copyright Schrodinger, LLC. All rights reserved.
"""
#Contributors: Quentin McDonald
import os
import warnings
from schrodinger import get_maestro
from schrodinger.application.utils import find_product_package_dir
from schrodinger.Qt import QtWidgets
from schrodinger.ui.qt.schrodingerstyle import apply_schrodingerstyle
from schrodinger.ui.qt.schrodingerstyle import \
    get_legacy_spacing_style_sheet_file
from schrodinger.ui.qt.schrodingerstyle import get_schrodinger_style_sheet_file
from schrodinger.ui.qt.schrodingerstyle import read_style_sheet_file
from schrodinger.utils.imputils import import_module_from_file
maestro = get_maestro()
if not maestro:
    try:
        _d = find_product_package_dir('maestro', 'schrodinger_maestro')
        import_module_from_file(os.path.join(_d, "maestro_rc.py"))
        del _d
    except (ImportError, IOError):
        warnings.warn(
            "Maestro resources are not found, some images may be missing.",
            RuntimeWarning)
_style_sheet_read = False
_styles_applied = False
[docs]def apply_styles():
    global _styles_applied
    if _styles_applied:
        return
    apply_schrodingerstyle()
    apply_style_sheet(inmodule=True)
    _styles_applied = True 
[docs]def apply_style_sheet(inmodule=False):
    """
    Read the schrodinger-wide style sheet and set that as the
    Application-level style sheet. If running in Maestro then we don't
    bother doing that as Maestro will have already performed this operation.
    :param immodule: True when called from inside style.py. This is the only
        valid way to call this function
    :type inmodule: bool
    """
    global _style_sheet_read
    if not inmodule:
        msg = "Calling apply_style_sheet directly can result in a runtime "
        msg += "crash. Call schrodinger.ui.qt.style.apply_styles()."
        warnings.warn(msg, RuntimeWarning)
    if maestro:
        # In Maestro, don't need to do anything as style sheet is already
        # applied.
        return
    if _style_sheet_read:
        # Already done it, don't need to do it again
        return
    _style_sheet_read = True
    file_name = get_schrodinger_style_sheet_file()
    style_sheet_text = get_style_sheet_text(file_name)
    if style_sheet_text is None:
        return
    qp = QtWidgets.QApplication.instance()
    if qp:
        qp.setStyleSheet(style_sheet_text)
    return 
[docs]def get_style_sheet_text(style_file_name):
    """
    Return the contents of the style sheet file at
    $SCHRODINGER/mmshare-v*/data/{style_file_name}.
    :param style_file_name: the name of a .sty file in the mmshare build data
            directory
    :type style_file_name: str
    :return: the contents of the style file, if it can be retrieved
    :rtype: str or NoneType
    """
    mmshare_exec = os.getenv("MMSHARE_EXEC")
    if not mmshare_exec:
        return "Warning: MMSHARE_EXEC is not set, cannot locate style sheet"
    mmshare_idx = mmshare_exec.rfind('bin')
    mmshare_path = mmshare_exec[0:mmshare_idx]
    style_sheet_file = os.path.join(mmshare_path, 'data', style_file_name)
    status, text, error_msg = read_style_sheet_file(style_sheet_file)
    if status:
        return text
    else:
        print(error_msg) 
[docs]def apply_legacy_spacing_style(widget):
    """
    Attempt to set the legacy spacing style sheet on the supplied widget.
    :param widget: a widget
    :type widget: QtWidgets.QWidget
    """
    file_name = get_legacy_spacing_style_sheet_file()
    style_sheet_text = get_style_sheet_text(file_name)
    if style_sheet_text is not None:
        widget.setStyleSheet(style_sheet_text)