Source code for controllers.control_stylesheet

"""
This module contains the ControlStyleSheet class, which provides methods for changing style sheets
of various UI elements based on user interactions and theme settings.
"""

from PyQt6 import QtWidgets


[docs] class ControlStyleSheet: """ Class for controlling the style sheets of UI elements. Attributes: _controller: The controller object. _model: The model object. _ui_object: The UI object. Methods: __init__(self, controller): Initializes the ControlStyleSheet object. change_stylesheet_selected_menu(self): Change the stylesheet of a selected menu button. set_style_selected_menu(self): Returns the CSS stylesheet for the currently selected menu item. set_style_deselect_menu(self, get_style): Replaces the selected menu style with an unselected style. reset_style(self, widget): Resets the stylesheet of all push buttons in the frame_button_view. stylesheet_selected_menu_dark_theme(cls): Return the CSS stylesheet for a selected menu button in dark theme. stylesheet_selected_menu_light_theme(cls): Returns the light theme stylesheet for the selected menu. """
[docs] def __init__(self, controller): super().__init__() self._controller = controller self._model = controller.model self._ui_object = self._controller.ui_object
[docs] def change_stylesheet_selected_menu(self): """ Change the stylesheet of a selected menu button. If an image is available, this function updates the style of a selected menu button by applying a new stylesheet that highlights it. The old stylesheet is reset for all other buttons. Arg: self: An instance of the class that this method belongs to. Return: None """ if self._model.debug_mode: self._model.activity_logger.info("ControlStyleSheet: change_stylesheet_selected_menu(), " "Change stylesheet selected menu") if self._controller.model.image_original is not None: try: btn = self._controller.sender() self.reset_style(btn.objectName()) btn.setStyleSheet(self.set_style_selected_menu()) except AttributeError as e: print(f"Error while resetting: {e}")
# selected menu button
[docs] def set_style_selected_menu(self): """ Return the CSS stylesheet for the currently selected menu item. The returned stylesheet is based on the current theme set in the `model` attribute. Return: str: The CSS stylesheet for the selected menu item. """ if self._model.debug_mode: self._model.activity_logger.info("ControlStyleSheet: set_style_selected_menu(), " "Set active stylesheet selected menu") select = self.stylesheet_selected_menu_light_theme() if self._controller.model.theme_mode == "light": select = self.stylesheet_selected_menu_dark_theme() return select
# deselected menu button
[docs] def set_style_deselect_menu(self, get_style): """ Replace the selected menu style with an unselected style in the current theme. Arg: get_style (str): The current style of the menu to be deselected. Return: str: The updated style of the menu with the selected style replaced with the unselected style. """ if self._model.debug_mode: self._model.activity_logger.info("ControlStyleSheet: set_style_deselect_menu(), " "Set active stylesheet deselected menu") deselect = get_style.replace(self.stylesheet_selected_menu_light_theme(), "") if self._controller.model.theme_mode == "light": deselect = get_style.replace(self.stylesheet_selected_menu_dark_theme(), "") return deselect
# reset selection button
[docs] def reset_style(self, widget): """ Reset the stylesheet of all push buttons in the frame_button_view except for the one with the given object name. Arg: widget (str): The object name of the push button to exclude. Return: None """ if self._model.debug_mode: self._model.activity_logger.info("ControlStyleSheet: reset_style(), " "Reset active stylesheet menu") for w in self._ui_object.frame_button_view.findChildren(QtWidgets.QPushButton): if w.objectName() != widget: w.setStyleSheet(self.set_style_deselect_menu(w.styleSheet()))
[docs] @classmethod def stylesheet_selected_menu_dark_theme(cls): """ Return the CSS stylesheet for a selected menu button in dark theme. Return: str: The CSS stylesheet for a selected menu button in dark theme. """ stylesheet = """ border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(255, 121, 198, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: rgb(45, 49, 55); """ return stylesheet
[docs] @classmethod def stylesheet_selected_menu_light_theme(cls): """ Return the light theme stylesheet for the selected menu. Return: str: The stylesheet for the selected menu in the light theme. """ stylesheet = """ border-left: 22px solid qlineargradient(spread:pad, x1:0.034, y1:0, x2:0.216, y2:0, stop:0.499 rgba(47, 55, 58, 255), stop:0.5 rgba(85, 170, 255, 0)); background-color: rgb(248, 248, 248); """ return stylesheet