Added menu switcher.

This commit is contained in:
Yeison 2020-09-19 16:38:38 -05:00
parent 8b906ddd49
commit 1b1de94ab0

View File

@ -8,11 +8,16 @@ import jinja2
template = 'material.css.template' template = 'material.css.template'
_resource = os.path.join('resources', 'resource_rc.py') _resource = os.path.join('resources', 'resource_rc.py')
from PySide2.QtWidgets import QAction
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def build_stylesheet(theme='', light_secondary=False, resources=[], extra={}): def build_stylesheet(theme='', light_secondary=False, resources=[], extra={}):
"""""" """"""
theme = get_theme(theme, light_secondary) theme = get_theme(theme, light_secondary)
if theme is None:
return None
set_icons_theme(theme) set_icons_theme(theme)
loader = jinja2.FileSystemLoader(os.path.join( loader = jinja2.FileSystemLoader(os.path.join(
@ -33,25 +38,20 @@ def build_stylesheet(theme='', light_secondary=False, resources=[], extra={}):
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def get_theme(theme_name, light_secondary=False): def get_theme(theme_name, light_secondary=False):
if theme_name in ['default.xml', 'default_dark.xml']: if theme_name in ['default.xml', 'default_dark.xml', 'default', 'default_dark']:
default_theme = os.path.join(os.path.dirname( theme = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'themes', 'dark_teal.xml') os.path.abspath(__file__)), 'themes', 'dark_teal.xml')
elif theme_name in ['default_light.xml']: elif theme_name in ['default_light.xml', 'default_light']:
light_secondary = True light_secondary = True
default_theme = os.path.join(os.path.dirname( theme = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'themes', 'light_blue.xml') os.path.abspath(__file__)), 'themes', 'light_blue.xml')
else:
if not os.path.exists(theme_name):
theme = os.path.join(os.path.dirname( theme = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'themes', theme_name) os.path.abspath(__file__)), 'themes', theme_name)
if not os.path.exists(theme):
if theme: if not os.path.exists(theme):
logging.warning( logging.warning(f"{theme} not exist!")
f"{theme} not exist, using {default_theme} by default.") return None
theme = default_theme
tree = ElementTree.parse(theme) tree = ElementTree.parse(theme)
theme = {child.attrib['name']: child.text for child in tree.getroot()} theme = {child.attrib['name']: child.text for child in tree.getroot()}
@ -97,6 +97,8 @@ def apply_stylesheet(app, theme='', style='Fusion', save_as=None, light_secondar
except: except:
pass pass
stylesheet = build_stylesheet(theme, light_secondary, resources, extra) stylesheet = build_stylesheet(theme, light_secondary, resources, extra)
if stylesheet is None:
return
if save_as: if save_as:
with open(save_as, 'w') as file: with open(save_as, 'w') as file:
@ -115,7 +117,7 @@ def opacity(theme, value=0.5):
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def set_icons_theme(theme, resource=None): def set_icons_theme(theme, resource=None, output=None):
"""""" """"""
try: try:
theme = get_theme(theme) theme = get_theme(theme)
@ -142,6 +144,11 @@ def set_icons_theme(theme, resource=None):
for c in colors: for c in colors:
content = content.replace(c, theme[replace]) content = content.replace(c, theme[replace])
if output:
with open(output, 'w') as file:
file.write(content)
return
try: try:
qCleanupResources() # this method is created after the first call to resource_rc qCleanupResources() # this method is created after the first call to resource_rc
except: except:
@ -158,3 +165,34 @@ def list_themes():
os.path.dirname(os.path.abspath(__file__)), 'themes')) os.path.dirname(os.path.abspath(__file__)), 'themes'))
themes = filter(lambda a: a.endswith('xml'), themes) themes = filter(lambda a: a.endswith('xml'), themes)
return list(themes) return list(themes)
########################################################################
class PySideStyleSwitcher:
""""""
# ----------------------------------------------------------------------
def set_style_switcher(self, parent, menu):
""""""
for theme in ['default'] + list_themes():
action = QAction(self)
action.setText(theme)
action.triggered.connect(self._wrapper(parent, theme))
menu.addAction(action)
# ----------------------------------------------------------------------
def _wrapper(self, parent, theme):
""""""
def iner():
self._apply_theme(parent, theme)
return iner
# ----------------------------------------------------------------------
def _apply_theme(self, parent, theme):
""""""
if theme == 'default':
parent.setStyleSheet('')
return
apply_stylesheet(parent, theme=theme, light_secondary=theme.startswith(
'light'), extra=self.extra)