qt-material/qt_material/__init__.py

766 lines
24 KiB
Python
Raw Normal View History

2020-12-05 21:38:34 -05:00
import os
import sys
import logging
2021-04-23 17:51:27 -05:00
import base64
2021-05-07 19:37:20 +02:00
from pathlib import Path
import platform
from xml.dom.minidom import parse
2020-12-05 21:38:34 -05:00
2021-04-23 17:51:27 -05:00
from qt_material.resources import ResourseGenerator, RESOURCES_PATH
2022-05-01 14:28:43 -05:00
2021-04-23 17:51:27 -05:00
GUI = True
2020-12-05 21:38:34 -05:00
if 'PySide2' in sys.modules:
2022-05-01 14:28:43 -05:00
from PySide2.QtGui import (
QFontDatabase,
QColor,
QGuiApplication,
QPalette,
)
2021-12-24 13:16:47 -05:00
from PySide2.QtWidgets import QAction, QColorDialog, QActionGroup
2020-12-13 14:40:16 -05:00
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import Qt, QDir
2020-12-11 11:41:13 -05:00
elif 'PySide6' in sys.modules:
2022-05-01 14:28:43 -05:00
from PySide6.QtGui import (
QFontDatabase,
QAction,
QColor,
QGuiApplication,
QPalette,
QActionGroup,
)
2020-12-13 14:40:16 -05:00
from PySide6.QtWidgets import QColorDialog
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import Qt, QDir
2020-12-11 11:41:13 -05:00
2020-12-05 21:38:34 -05:00
elif 'PyQt5' in sys.modules:
from PyQt5.QtGui import QFontDatabase, QColor, QGuiApplication, QPalette
2021-12-24 13:16:47 -05:00
from PyQt5.QtWidgets import QAction, QColorDialog, QActionGroup
from PyQt5.QtCore import Qt, QDir
from PyQt5 import uic
elif 'PyQt6' in sys.modules:
2022-05-01 14:28:43 -05:00
from PyQt6.QtGui import (
QFontDatabase,
QColor,
QGuiApplication,
QPalette,
QAction,
QActionGroup,
)
from PyQt6.QtWidgets import QColorDialog
from PyQt6.QtCore import Qt, QDir
from PyQt6 import uic
2020-12-11 11:41:13 -05:00
else:
2021-04-23 17:51:27 -05:00
GUI = False
2020-12-12 14:18:24 -05:00
logging.warning("qt_material must be imported after PySide or PyQt!")
2020-12-05 21:38:34 -05:00
import jinja2
2022-05-01 14:28:43 -05:00
TEMPLATE_FILE = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'material.css.template'
)
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
2022-05-01 14:28:43 -05:00
def export_theme(
theme='',
qss=None,
rcc=None,
invert_secondary=False,
extra={},
output='theme',
prefix='icon:/',
):
2021-04-23 17:51:27 -05:00
""""""
if not os.path.isabs(output) and not output.startswith('.'):
2022-05-01 14:28:43 -05:00
output = f'.{output}'
2021-04-23 17:51:27 -05:00
2022-02-23 19:06:58 -05:00
stylesheet = build_stylesheet(
2022-05-01 14:28:43 -05:00
theme, invert_secondary, extra, output, export=True
)
if output.startswith('.'):
output = output[1:]
2021-04-23 17:51:27 -05:00
with open(qss, 'w') as file:
file.writelines(stylesheet.replace('icon:/', prefix))
if rcc:
with open(rcc, 'w') as file:
file.write('<RCC>\n')
file.write(f' <qresource prefix="{prefix[:-2]}">\n')
for subfolder in ['disabled', 'primary']:
2022-05-01 14:28:43 -05:00
files = os.listdir(
os.path.join(os.path.abspath(output), subfolder)
)
files = filter(lambda s: s.endswith('svg'), files)
for filename in files:
file.write(
2022-05-01 14:28:43 -05:00
f' <file>{output}/{subfolder}/{filename}</file>\n'
)
file.write(' </qresource>\n')
file.write(f' <qresource prefix="file">\n')
if qss:
file.write(f' <file>{qss}</file>\n')
file.write(' </qresource>\n')
file.write('</RCC>\n')
2021-04-23 17:51:27 -05:00
# ----------------------------------------------------------------------
2022-05-01 14:28:43 -05:00
def build_stylesheet(
theme='',
invert_secondary=False,
extra={},
parent='theme',
template=TEMPLATE_FILE,
export=False,
):
2020-12-05 21:38:34 -05:00
""""""
2022-02-23 19:06:58 -05:00
if not export:
try:
add_fonts()
except Exception as e:
logging.warning(e)
2021-09-03 15:08:00 -05:00
theme = get_theme(theme, invert_secondary)
2020-12-05 21:38:34 -05:00
if theme is None:
return None
set_icons_theme(theme, parent=parent)
2020-12-05 21:38:34 -05:00
# Render custom template
if os.path.exists(template):
parent, template = os.path.split(template)
loader = jinja2.FileSystemLoader(parent)
env = jinja2.Environment(autoescape=False, loader=loader)
env.filters['opacity'] = opacity
env.filters['density'] = density
stylesheet = env.get_template(template)
else:
env = jinja2.Environment(autoescape=False, loader=jinja2.BaseLoader)
env.filters['opacity'] = opacity
env.filters['density'] = density
stylesheet.from_string(template)
theme.setdefault('icon', None)
theme.setdefault('font_family', 'Roboto')
theme.setdefault('danger', '#dc3545')
theme.setdefault('warning', '#ffc107')
theme.setdefault('success', '#17a2b8')
theme.setdefault('density_scale', '0')
theme.setdefault('button_shape', 'default')
2020-12-05 21:38:34 -05:00
theme.update(extra)
if GUI:
default_palette = QGuiApplication.palette()
2022-05-01 14:28:43 -05:00
color = QColor(
*[
int(theme['primaryColor'][i : i + 2], 16)
for i in range(1, 6, 2)
]
+ [92]
)
try:
2022-05-01 14:28:43 -05:00
if hasattr(
QPalette, 'PlaceholderText'
): # pyqt5, pyside2, pyside6
default_palette.setColor(QPalette.PlaceholderText, color)
else: # pyqt6
default_palette.setColor(QPalette.ColorRole.Text, color)
QGuiApplication.setPalette(default_palette)
except: # pyside6 & snake_case, true_property
default_palette.set_color(QPalette.ColorRole.Text, color)
QGuiApplication.set_palette(default_palette)
environ = {
'linux': platform.system() == 'Linux',
'windows': platform.system() == 'Windows',
'darwin': platform.system() == 'Darwin',
'pyqt5': 'PyQt5' in sys.modules,
'pyqt6': 'PyQt6' in sys.modules,
'pyside2': 'PySide2' in sys.modules,
'pyside6': 'PySide6' in sys.modules,
}
2021-12-24 13:16:47 -05:00
environ.update(theme)
return stylesheet.render(environ)
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
def get_theme(theme_name, invert_secondary=False):
2022-05-01 14:28:43 -05:00
if theme_name in [
# 'default.xml',
# 'default',
'default_dark.xml',
'default_dark',
]:
theme = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'themes',
'dark_teal.xml',
# 'light_cyan_500.xml',
)
elif theme_name in [
'default_light.xml',
'default_light',
'default.xml',
'default',
]:
invert_secondary = True
2022-05-01 14:28:43 -05:00
theme = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'themes',
'light_cyan_500.xml',
)
2020-12-11 22:48:04 -05:00
elif not os.path.exists(theme_name):
2022-05-01 14:28:43 -05:00
theme = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'themes', theme_name
)
2020-12-11 22:48:04 -05:00
else:
theme = theme_name
2020-12-05 21:38:34 -05:00
if not os.path.exists(theme):
logging.warning(f"{theme} not exist!")
return None
document = parse(theme)
2022-05-01 14:28:43 -05:00
theme = {
child.getAttribute('name'): child.firstChild.nodeValue
for child in document.getElementsByTagName('color')
}
2020-12-05 21:38:34 -05:00
for k in theme:
os.environ[str(k)] = theme[k]
if invert_secondary:
2022-05-01 14:28:43 -05:00
(
theme['secondaryColor'],
theme['secondaryLightColor'],
theme['secondaryDarkColor'],
) = (
theme['secondaryColor'],
theme['secondaryDarkColor'],
theme['secondaryLightColor'],
)
for color in [
'primaryColor',
'primaryLightColor',
'secondaryColor',
'secondaryLightColor',
'secondaryDarkColor',
'primaryTextColor',
'secondaryTextColor',
]:
os.environ[f'QTMATERIAL_{color.upper()}'] = theme[color]
os.environ['QTMATERIAL_THEME'] = theme_name
2020-12-05 21:38:34 -05:00
return theme
# ----------------------------------------------------------------------
def add_fonts():
""""""
fonts_path = os.path.join(os.path.dirname(__file__), 'fonts')
2020-12-27 13:17:28 -05:00
2021-04-14 09:02:23 -05:00
for font_dir in ['roboto']:
2022-05-01 14:28:43 -05:00
for font in filter(
lambda s: s.endswith('.ttf'),
os.listdir(os.path.join(fonts_path, font_dir)),
):
2021-12-23 15:29:40 -05:00
try:
QFontDatabase.addApplicationFont(
2022-05-01 14:28:43 -05:00
os.path.join(fonts_path, font_dir, font)
)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
QFontDatabase.add_application_font(
2022-05-01 14:28:43 -05:00
os.path.join(fonts_path, font_dir, font)
)
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
2022-05-01 14:28:43 -05:00
def apply_stylesheet(
app,
theme='',
style=None,
save_as=None,
invert_secondary=False,
extra={},
parent='theme',
2022-05-15 13:08:14 -05:00
css_file=None,
2022-05-01 14:28:43 -05:00
):
2020-12-05 21:38:34 -05:00
""""""
if style:
try:
2021-12-23 15:29:40 -05:00
try:
app.setStyle(style)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
app.style = style
2020-12-05 21:38:34 -05:00
except:
logging.error(f"The style '{style}' does not exist.")
2020-12-05 21:38:34 -05:00
pass
2021-12-23 15:29:40 -05:00
2022-05-15 10:55:10 -05:00
if 'QMenu' in extra:
for k in extra['QMenu']:
extra[f'qmenu_{k}'] = extra['QMenu'][k]
extra['QMenu'] = True
2022-05-01 14:28:43 -05:00
stylesheet = build_stylesheet(theme, invert_secondary, extra, parent)
2020-12-05 21:38:34 -05:00
if stylesheet is None:
return
if save_as:
with open(save_as, 'w') as file:
file.writelines(stylesheet)
2022-05-15 13:08:14 -05:00
if css_file and os.path.exists(css_file):
with open(css_file) as file:
stylesheet += file.read().format(**os.environ)
2021-12-25 11:13:11 -05:00
try:
app.setStyleSheet(stylesheet)
except:
app.style_sheet = stylesheet
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
def opacity(theme, value=0.5):
""""""
r, g, b = theme[1:][0:2], theme[1:][2:4], theme[1:][4:]
r, g, b = int(r, 16), int(g, 16), int(b, 16)
return f'rgba({r}, {g}, {b}, {value})'
2020-12-05 22:54:28 -05:00
# ----------------------------------------------------------------------
def density(
value, density_scale, border=0, scale=1, density_interval=4, min_=4
):
""""""
# https://material.io/develop/web/supporting/density
if isinstance(value, str) and value.startswith('@'):
return value[1:] * scale
if value == 'unset':
return 'unset'
2021-12-24 13:16:47 -05:00
if isinstance(value, str):
value = float(value.replace('px', ''))
2022-05-01 14:28:43 -05:00
density = (
value + (density_interval * int(density_scale)) - (border * 2)
) * scale
if density <= 0:
density = min_
return density
2020-12-11 11:41:13 -05:00
# ----------------------------------------------------------------------
def set_icons_theme(theme, parent='theme'):
2020-12-05 21:38:34 -05:00
""""""
2021-01-23 14:37:30 -05:00
source = os.path.join(os.path.dirname(__file__), 'resources', 'source')
2022-05-01 14:28:43 -05:00
resources = ResourseGenerator(
primary=theme['primaryColor'],
secondary=theme['secondaryColor'],
disabled=theme['secondaryLightColor'],
source=source,
parent=parent,
)
resources.generate()
2021-04-23 17:51:27 -05:00
if GUI:
2021-12-23 15:29:40 -05:00
try:
QDir.addSearchPath('icon', resources.index)
2022-05-01 14:28:43 -05:00
QDir.addSearchPath(
'qt_material',
os.path.join(os.path.dirname(__file__), 'resources'),
)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
QDir.add_search_path('icon', resources.index)
2022-05-01 14:28:43 -05:00
QDir.add_search_path(
'qt_material',
os.path.join(os.path.dirname(__file__), 'resources'),
)
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
def list_themes():
""""""
2022-05-01 14:28:43 -05:00
themes = os.listdir(
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'themes')
)
2020-12-05 21:38:34 -05:00
themes = filter(lambda a: a.endswith('xml'), themes)
return sorted(list(themes))
# ----------------------------------------------------------------------
def deprecated(replace):
""""""
# ----------------------------------------------------------------------
def wrap1(fn):
# ----------------------------------------------------------------------
def wrap2(*args, **kwargs):
logging.warning(
2022-05-01 14:28:43 -05:00
f'This function is deprecated, please use "{replace}" instead.'
)
fn(*args, **kwargs)
2022-05-01 14:28:43 -05:00
return wrap2
return wrap1
2020-12-05 21:38:34 -05:00
########################################################################
2020-12-13 14:40:16 -05:00
class QtStyleTools:
2020-12-05 21:38:34 -05:00
""""""
2022-05-01 14:28:43 -05:00
extra_values = {}
2020-12-05 21:38:34 -05:00
2020-12-11 22:48:04 -05:00
# ----------------------------------------------------------------------
@deprecated('set_extra')
2020-12-13 14:40:16 -05:00
def set_extra_colors(self, extra):
2020-12-05 21:38:34 -05:00
""""""
self.extra_values = extra
# ----------------------------------------------------------------------
def set_extra(self, extra):
""""""
self.extra_values = extra
2020-12-13 14:40:16 -05:00
# ----------------------------------------------------------------------
2020-12-13 16:36:56 -05:00
def add_menu_theme(self, parent, menu):
2020-12-13 14:40:16 -05:00
""""""
2021-12-24 13:16:47 -05:00
self.menu_theme_ = menu
action_group = QActionGroup(menu)
try:
action_group.setExclusive(True)
except:
action_group.exclusive = True
for i, theme in enumerate(['default'] + list_themes()):
2020-12-05 21:38:34 -05:00
action = QAction(parent)
2021-12-24 13:16:47 -05:00
# action.triggered.connect(self._wrapper(parent, theme, self.extra_values, self.update_buttons))
action.triggered.connect(lambda: self.update_theme_event(parent))
2021-12-23 15:29:40 -05:00
try:
action.setText(theme)
2021-12-24 13:16:47 -05:00
action.setCheckable(True)
action.setChecked(not bool(i))
action.setActionGroup(action_group)
2021-12-23 15:29:40 -05:00
menu.addAction(action)
2021-12-24 13:16:47 -05:00
action_group.addAction(action)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
action.text = theme
2021-12-24 13:16:47 -05:00
action.checkable = True
action.checked = not bool(i)
action.action_group = action_group
2021-12-23 15:29:40 -05:00
menu.add_action(action)
2021-12-24 13:16:47 -05:00
action_group.add_action(action)
2020-12-05 21:38:34 -05:00
# ----------------------------------------------------------------------
2021-12-24 13:16:47 -05:00
def add_menu_density(self, parent, menu):
2020-12-05 21:38:34 -05:00
""""""
2021-12-24 13:16:47 -05:00
self.menu_density_ = menu
action_group = QActionGroup(menu)
2020-12-05 21:38:34 -05:00
2021-12-24 13:16:47 -05:00
try:
action_group.setExclusive(True)
except:
action_group.exclusive = True
for density in map(str, range(-3, 4)):
action = QAction(parent)
# action.triggered.connect(self._wrapper(parent, density, self.extra_values, self.update_buttons))
action.triggered.connect(lambda: self.update_theme_event(parent))
try:
action.setText(density)
action.setCheckable(True)
action.setChecked(density == '0')
action.setActionGroup(action_group)
menu.addAction(action)
action_group.addAction(action)
except: # snake_case, true_property
action.text = density
action.checkable = True
action.checked = density == '0'
action.action_group = action_group
menu.add_action(action)
action_group.add_action(action)
# menu.add_action(action_group)
# # ----------------------------------------------------------------------
# def _wrapper(self, parent, theme, extra, callable_):
2022-05-01 14:28:43 -05:00
# """"""
# def iner():
# self._apply_theme(parent, theme, extra, callable_)
# return iner
2021-12-24 13:16:47 -05:00
# # ----------------------------------------------------------------------
# def _apply_theme(self, parent, theme, extra={}, callable_=None):
2022-05-01 14:28:43 -05:00
# """"""
# self.apply_stylesheet(parent, theme=theme, invert_secondary=theme.startswith(
# 'light'), extra=extra, callable_=callable_)
2020-12-11 22:48:04 -05:00
# ----------------------------------------------------------------------
2022-05-01 14:28:43 -05:00
def apply_stylesheet(
self, parent, theme, invert_secondary=False, extra={}, callable_=None
):
2020-12-05 21:38:34 -05:00
""""""
if theme == 'default':
2022-05-01 14:28:43 -05:00
try:
parent.setStyleSheet('')
except:
parent.style_sheet = ''
2020-12-05 21:38:34 -05:00
return
2022-05-01 14:28:43 -05:00
apply_stylesheet(
parent,
theme=theme,
invert_secondary=invert_secondary,
extra=extra,
)
if callable_:
callable_()
2020-12-13 14:40:16 -05:00
2021-12-24 13:16:47 -05:00
# ----------------------------------------------------------------------
def update_theme_event(self, parent):
""""""
try:
2022-05-01 14:28:43 -05:00
density = [
action.text()
for action in self.menu_density_.actions()
if action.isChecked()
][0]
theme = [
action.text()
for action in self.menu_theme_.actions()
if action.isChecked()
][0]
2021-12-24 13:16:47 -05:00
except:
density = [
2022-05-01 14:28:43 -05:00
action.text
for action in self.menu_density_.actions()
if action.checked
][0]
theme = [
action.text
for action in self.menu_theme_.actions()
if action.checked
][0]
2021-12-24 13:16:47 -05:00
self.extra_values['density_scale'] = density
2022-05-01 14:28:43 -05:00
self.apply_stylesheet(
parent,
theme=theme,
invert_secondary=theme.startswith('light'),
extra=self.extra_values,
callable_=self.update_buttons,
)
2021-12-24 13:16:47 -05:00
2020-12-13 14:40:16 -05:00
# ----------------------------------------------------------------------
def update_buttons(self):
""""""
if not hasattr(self, 'colors'):
return
2022-05-01 14:28:43 -05:00
theme = {
color_: os.environ[f'QTMATERIAL_{color_.upper()}']
for color_ in self.colors
}
2020-12-13 14:40:16 -05:00
if 'light' in os.environ['QTMATERIAL_THEME']:
2020-12-13 14:40:16 -05:00
self.dock_theme.checkBox_ligh_theme.setChecked(True)
elif 'dark' in os.environ['QTMATERIAL_THEME']:
2020-12-13 14:40:16 -05:00
self.dock_theme.checkBox_ligh_theme.setChecked(False)
2021-12-23 15:29:40 -05:00
try:
if self.dock_theme.checkBox_ligh_theme.isChecked():
2022-05-01 14:28:43 -05:00
(
theme['secondaryColor'],
theme['secondaryLightColor'],
theme['secondaryDarkColor'],
) = (
theme['secondaryColor'],
theme['secondaryDarkColor'],
theme['secondaryLightColor'],
)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
if self.dock_theme.checkBox_ligh_theme.checked:
2022-05-01 14:28:43 -05:00
(
theme['secondaryColor'],
theme['secondaryLightColor'],
theme['secondaryDarkColor'],
) = (
theme['secondaryColor'],
theme['secondaryDarkColor'],
theme['secondaryLightColor'],
)
2020-12-13 14:40:16 -05:00
for color_ in self.colors:
button = getattr(self.dock_theme, f'pushButton_{color_}')
color = theme[color_]
2021-12-23 15:29:40 -05:00
try:
if self.get_color(color).getHsv()[2] < 128:
text_color = '#ffffff'
else:
text_color = '#000000'
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
if self.get_color(color).get_hsv()[2] < 128:
text_color = '#ffffff'
else:
text_color = '#000000'
2020-12-13 14:40:16 -05:00
2022-05-01 14:28:43 -05:00
button.setStyleSheet(
f"""
2020-12-13 14:40:16 -05:00
*{{
background-color: {color};
color: {text_color};
border: none;
2022-05-01 14:28:43 -05:00
}}"""
)
2020-12-13 14:40:16 -05:00
self.custom_colors[color_] = color
# ----------------------------------------------------------------------
def get_color(self, color):
""""""
2022-05-01 14:28:43 -05:00
return QColor(*[int(color[s : s + 2], 16) for s in range(1, 6, 2)])
2020-12-13 14:40:16 -05:00
# ----------------------------------------------------------------------
def update_theme(self, parent):
""""""
with open('my_theme.xml', 'w') as file:
2022-05-01 14:28:43 -05:00
file.write(
"""
2020-12-13 14:40:16 -05:00
<resources>
<color name="primaryColor">{primaryColor}</color>
<color name="primaryLightColor">{primaryLightColor}</color>
<color name="secondaryColor">{secondaryColor}</color>
<color name="secondaryLightColor">{secondaryLightColor}</color>
<color name="secondaryDarkColor">{secondaryDarkColor}</color>
<color name="primaryTextColor">{primaryTextColor}</color>
<color name="secondaryTextColor">{secondaryTextColor}</color>
</resources>
2022-05-01 14:28:43 -05:00
""".format(
**self.custom_colors
)
)
2021-12-23 15:29:40 -05:00
try:
light = self.dock_theme.checkBox_ligh_theme.isChecked()
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
light = self.dock_theme.checkBox_ligh_theme.checked
2022-05-01 14:28:43 -05:00
self.apply_stylesheet(
parent,
'my_theme.xml',
invert_secondary=light,
extra=self.extra_values,
callable_=self.update_buttons,
)
2020-12-13 14:40:16 -05:00
# ----------------------------------------------------------------------
def set_color(self, parent, button_):
""""""
2022-05-01 14:28:43 -05:00
2020-12-13 14:40:16 -05:00
def iner():
initial = self.get_color(self.custom_colors[button_])
color_dialog = QColorDialog(parent=parent)
2021-12-23 15:29:40 -05:00
try:
color_dialog.setCurrentColor(initial)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
color_dialog.current_color = initial
2020-12-13 14:40:16 -05:00
done = color_dialog.exec_()
2021-12-23 15:29:40 -05:00
try:
color_ = color_dialog.currentColor()
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
color_ = color_dialog.current_color
try:
if done and color_.isValid():
rgb_255 = [color_.red(), color_.green(), color_.blue()]
2022-05-01 14:28:43 -05:00
color = '#' + ''.join(
[hex(v)[2:].ljust(2, '0') for v in rgb_255]
)
2021-12-23 15:29:40 -05:00
self.custom_colors[button_] = color
self.update_theme(parent)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
if done and color_.is_valid():
rgb_255 = [color_.red(), color_.green(), color_.blue()]
2022-05-01 14:28:43 -05:00
color = '#' + ''.join(
[hex(v)[2:].ljust(2, '0') for v in rgb_255]
)
2021-12-23 15:29:40 -05:00
self.custom_colors[button_] = color
self.update_theme(parent)
2020-12-13 14:40:16 -05:00
return iner
# ----------------------------------------------------------------------
def show_dock_theme(self, parent):
""""""
2022-05-01 14:28:43 -05:00
self.colors = [
'primaryColor',
'primaryLightColor',
'secondaryColor',
'secondaryLightColor',
'secondaryDarkColor',
'primaryTextColor',
'secondaryTextColor',
]
2020-12-13 14:40:16 -05:00
2021-02-08 08:37:54 -05:00
self.custom_colors = {
2022-05-15 13:08:14 -05:00
v: os.environ.get(f'QTMATERIAL_{v.upper()}', '')
for v in self.colors
2022-05-01 14:28:43 -05:00
}
if 'PySide2' in sys.modules or 'PySide6' in sys.modules:
2022-05-01 14:28:43 -05:00
self.dock_theme = QUiLoader().load(
os.path.join(os.path.dirname(__file__), 'dock_theme.ui')
)
elif 'PyQt5' in sys.modules or 'PyQt6' in sys.modules:
2022-05-01 14:28:43 -05:00
self.dock_theme = uic.loadUi(
os.path.join(os.path.dirname(__file__), 'dock_theme.ui')
)
2021-12-23 15:29:40 -05:00
try:
parent.addDockWidget(
2022-05-01 14:28:43 -05:00
Qt.DockWidgetArea.LeftDockWidgetArea, self.dock_theme
)
2021-12-23 15:29:40 -05:00
self.dock_theme.setFloating(True)
except: # snake_case, true_property
2021-12-23 15:29:40 -05:00
parent.add_dock_widget(
2022-05-01 14:28:43 -05:00
Qt.DockWidgetArea.LeftDockWidgetArea, self.dock_theme
)
2021-12-23 15:29:40 -05:00
self.dock_theme.floating = True
2020-12-13 14:40:16 -05:00
self.update_buttons()
2021-02-08 08:37:54 -05:00
self.dock_theme.checkBox_ligh_theme.clicked.connect(
2022-05-01 14:28:43 -05:00
lambda: self.update_theme(self.main)
)
2020-12-13 14:40:16 -05:00
for color in self.colors:
button = getattr(self.dock_theme, f'pushButton_{color}')
button.clicked.connect(self.set_color(parent, color))
2021-05-07 19:37:20 +02:00
# ----------------------------------------------------------------------
def get_hook_dirs():
package_folder = Path(__file__).parent
2021-09-03 15:08:00 -05:00
return [str(package_folder.absolute())]