def light_or_dark(self, is_dark): """ set the UI to a light or dark scheme. Qt doesn't seem to support controlling the MdiArea's background from a style sheet. Set the widget directly and save the original color to reset defaults. """ if not hasattr(self, 'mdi_background'): self.mdi_background = self.mdi.background() if is_dark: self.qtapp.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) rgb = DarkPalette.color_palette() self.mdi.setBackground(QColor(rgb['COLOR_BACKGROUND_NORMAL'])) else: self.qtapp.setStyleSheet('') self.mdi.setBackground(self.mdi_background) return is_dark
def create_custom_qss( name, path, color_background_light, color_background_normal, color_background_dark, color_foreground_light, color_foreground_normal, color_foreground_dark, color_selection_light, color_selection_normal, color_selection_dark, border_radius, ): """ Create a custom palette based on the parameters defined. The `name` must be a valid Python identifier and will be stored as a lowercased folder (even if the identifier had uppercase letters). This fuction returns the custom stylesheet pointing to resources stored at .../path/name/. """ stylesheet = '' # Check if name is valid if is_identifier(name): name = name if name[0].isupper() else name.capitalize() else: raise Exception('The custom palette name must be a valid Python ' 'identifier!') # Copy resources folder rc_loc = os.path.basename(RC_PATH) qss_loc = os.path.basename(QSS_PATH) theme_root_path = os.path.join(path, name.lower()) theme_rc_path = os.path.join(theme_root_path, rc_loc) if os.path.isdir(theme_root_path): shutil.rmtree(theme_root_path) shutil.copytree(RC_PATH, theme_rc_path) # Copy QSS folder and contents theme_qss_path = os.path.join(theme_root_path, qss_loc) if os.path.isdir(theme_qss_path): os.removedirs(theme_qss_path) shutil.copytree(QSS_PATH, theme_qss_path) # Create custom palette custom_palette = type(name, (DarkPalette, ), {}) custom_palette.COLOR_BACKGROUND_LIGHT = color_background_light custom_palette.COLOR_BACKGROUND_NORMAL = color_background_normal custom_palette.COLOR_BACKGROUND_DARK = color_background_dark custom_palette.COLOR_FOREGROUND_LIGHT = color_foreground_light custom_palette.COLOR_FOREGROUND_NORMAL = color_foreground_normal custom_palette.COLOR_FOREGROUND_DARK = color_foreground_dark custom_palette.COLOR_SELECTION_LIGHT = color_selection_light custom_palette.COLOR_SELECTION_NORMAL = color_selection_normal custom_palette.COLOR_SELECTION_DARK = color_selection_dark custom_palette.SIZE_BORDER_RADIUS = border_radius custom_palette.PATH_RESOURCES = "'{}'".format(theme_root_path) # Process images and save them to the custom platte rc folder create_images(rc_path=theme_rc_path, palette=custom_palette) create_palette_image(path=theme_root_path, palette=custom_palette) # Compile SCSS variables_scss_filepath = os.path.join(theme_qss_path, VARIABLES_SCSS_FILE) theme_main_scss_filepath = os.path.join(theme_qss_path, MAIN_SCSS_FILE) theme_qss_filepath = os.path.join(theme_root_path, QSS_FILE) stylesheet = create_qss( qss_filepath=theme_qss_filepath, main_scss_filepath=theme_main_scss_filepath, variables_scss_filepath=variables_scss_filepath, palette=custom_palette, ) # Update colors in text with open(theme_main_scss_filepath, 'r') as fh: data = fh.read() for key, color in DarkPalette.color_palette().items(): custom_color = custom_palette.color_palette()[key].upper() data = data.replace(color, custom_color) stylesheet = stylesheet.replace(color, custom_color) with open(theme_main_scss_filepath, 'w') as fh: fh.write(data) with open(theme_qss_filepath, 'w') as fh: fh.write(stylesheet) return stylesheet