diff --git a/test/generator.py b/test/generator.py index 988149d..12ace41 100644 --- a/test/generator.py +++ b/test/generator.py @@ -6,7 +6,7 @@ themes = list_themes() themes = [t.replace('.xml', '') for t in themes] for theme in themes: - os.system(f'python main.py --pyside {theme}') + os.system(f'python main.py --pyside6 {theme}') os.chdir('screenshots') @@ -17,6 +17,7 @@ commands = ( 'rm ../../docs/source/images/dark.gif', 'cp light.gif ../../docs/source/images/light.gif', 'cp dark.gif ../../docs/source/images/dark.gif', + 'cp theme.png ../../docs/source/images/theme.png', ) for command in commands: diff --git a/test/main.py b/test/main.py index af9c1ed..1c3c937 100644 --- a/test/main.py +++ b/test/main.py @@ -1,28 +1,27 @@ import os import sys import logging - +from multiprocessing import freeze_support +import psutil +import signal if '--pyside2' in sys.argv: - from PySide2.QtWidgets import QApplication - from PySide2 import QtWidgets - from PySide2.QtCore import QTimer + from PySide2.QtWidgets import QApplication, QMainWindow, QColorDialog + from PySide2.QtCore import QTimer, Qt, QCoreApplication + from PySide2.QtGui import QColor from PySide2.QtUiTools import QUiLoader - from PySide2.QtCore import Qt, QCoreApplication elif '--pyside6' in sys.argv: - from PySide6.QtWidgets import QApplication - from PySide6 import QtWidgets - from PySide6.QtCore import QTimer + from PySide6.QtWidgets import QApplication, QMainWindow, QColorDialog + from PySide6.QtCore import QTimer, Qt, QCoreApplication + from PySide6.QtGui import QColor from PySide6.QtUiTools import QUiLoader - from PySide6.QtCore import Qt, QCoreApplication elif '--pyqt5' in sys.argv: - from PyQt5.QtWidgets import QApplication - from PyQt5 import QtWidgets, uic - from PyQt5.QtCore import QTimer - from PyQt5.QtCore import Qt, QCoreApplication - from PyQt5 import QtWebEngineWidgets + from PyQt5.QtWidgets import QApplication, QMainWindow, QColorDialog + from PyQt5.QtCore import QTimer, Qt, QCoreApplication + from PyQt5.QtGui import QColor + from PyQt5 import uic, QtWebEngineWidgets from qt_material import apply_stylesheet, QtStyleSwitcher @@ -37,25 +36,41 @@ elif 'PySide6' in sys.modules: elif 'Qt' in sys.modules: from qt_material.resources import logos_pyqt5_rc +freeze_support() QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts) app = QApplication([]) app.processEvents() +app.setQuitOnLastWindowClosed(False) +# app.lastWindowClosed.connect(kill_childs) +# app.lastWindowClosed.connect(lambda: app.quit()) + app.setStyle('Fusion') # For better looking +extra = {'danger': '#dc3545', + 'warning': '#ffc107', + 'success': '#17a2b8', + } + ######################################################################## -class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher): +class RuntimeStylesheets(QMainWindow, QtStyleSwitcher): # ---------------------------------------------------------------------- def __init__(self): """Constructor""" super().__init__() # Extra stylesheets - self.extra = {'danger': '#dc3545', - 'warning': '#ffc107', - 'success': '#17a2b8', - } + + self.colors = ['primaryColor', + 'primaryLightColor', + 'secondaryColor', + 'secondaryLightColor', + 'secondaryDarkColor', + 'primaryTextColor', + 'secondaryTextColor'] + + self.custom_colors = {v: os.environ[f'PYSIDEMATERIAL_{v.upper()}'] for v in self.colors} if '--pyside2' in sys.argv: self.main = QUiLoader().load('main_window.ui', self) @@ -74,7 +89,33 @@ class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher): sys.exit() self.custom_styles() - self.set_style_switcher(self.main, self.main.menuStyles, self.extra) + self.update_buttons() + self.set_style_switcher(self.main, self.main.menuStyles, extra, self.update_buttons) + + self.main.checkBox_ligh_theme.clicked.connect(self.update_theme) + + for color in self.colors: + button = getattr(self.main, f'pushButton_{color}') + button.clicked.connect(self.set_color(color)) + + self.main.dockWidget_theme.setFloating(True) + + # ---------------------------------------------------------------------- + def set_color(self, button_): + """""" + def iner(): + initial = self.get_color(self.custom_colors[button_]) + color_dialog = QColorDialog(parent=self) + color_dialog.setCurrentColor(initial) + done = color_dialog.exec_() + color_ = color_dialog.currentColor() + + if done and color_.isValid(): + color = '#' + ''.join([hex(v)[2:].ljust(2, '0') for v in color_.toTuple()[:3]]) + self.custom_colors[button_] = color + self.update_theme() + + return iner # ---------------------------------------------------------------------- def custom_styles(self): @@ -84,6 +125,64 @@ class RuntimeStylesheets(QtWidgets.QMainWindow, QtStyleSwitcher): tool_button.setMaximumWidth(150) tool_button.setMinimumWidth(150) + # ---------------------------------------------------------------------- + def update_theme(self, event=None): + """""" + with open('my_theme.xml', 'w') as file: + file.write(""" + + {primaryColor} + {primaryLightColor} + {secondaryColor} + {secondaryLightColor} + {secondaryDarkColor} + {primaryTextColor} + {secondaryTextColor} + + """.format(**self.custom_colors)) + + light = self.main.checkBox_ligh_theme.isChecked() + self.apply_stylesheet(self.main, 'my_theme.xml', invert_secondary=light, extra=extra, callable_=self.update_buttons) + + # ---------------------------------------------------------------------- + def update_buttons(self): + """""" + theme = {color_: os.environ[f'PYSIDEMATERIAL_{color_.upper()}'] for color_ in self.colors} + + if 'light' in os.environ['PYSIDEMATERIAL_THEME']: + self.main.checkBox_ligh_theme.setChecked(True) + elif 'dark' in os.environ['PYSIDEMATERIAL_THEME']: + self.main.checkBox_ligh_theme.setChecked(False) + + if self.main.checkBox_ligh_theme.isChecked(): + theme['secondaryColor'], theme['secondaryLightColor'], theme['secondaryDarkColor'] = theme[ + 'secondaryColor'], theme['secondaryDarkColor'], theme['secondaryLightColor'] + # theme['primaryTextColor'] = theme['secondaryTextColor'] + + for color_ in self.colors: + button = getattr(self.main, f'pushButton_{color_}') + + color = theme[color_] + + if self.get_color(color).getHsv()[2] < 128: + text_color = '#ffffff' + else: + text_color = '#000000' + + button.setStyleSheet(f""" + *{{ + background-color: {color}; + color: {text_color}; + border: none; + }}""") + + self.custom_colors[color_] = color + + # ---------------------------------------------------------------------- + def get_color(self, color): + """""" + return QColor(*[int(color[s:s + 2], 16) for s in range(1, 6, 2)]) + T0 = 1000 @@ -101,11 +200,13 @@ if __name__ == "__main__": QTimer.singleShot(T0 * 2, app.closeAllWindows) except: theme = 'default' - theme = 'default_light' + # theme = 'default_light' # Set theme on in itialization apply_stylesheet(app, theme + '.xml', - light_secondary=('light' in theme and 'dark' not in theme)) + invert_secondary=('light' in theme and 'dark' not in theme), + extra=extra) + # QIcon.setThemeName("breeze-dark") frame = RuntimeStylesheets() frame.main.show() diff --git a/test/main_window.ui b/test/main_window.ui index eee98cf..281ea31 100644 --- a/test/main_window.ui +++ b/test/main_window.ui @@ -6,8 +6,8 @@ 0 0 - 1544 - 725 + 1403 + 907 @@ -446,446 +446,6 @@ - - - - QTabWidget::East - - - 0 - - - true - - - - Page - - - - - - 2 - - - - - 0 - 0 - 680 - 269 - - - - Page 1 - - - - - - - https://www.python.org/ - - - - - - - - - - 0 - 0 - 680 - 269 - - - - Page - - - - - - 0 - 0 - 659 - 292 - - - - Page - - - - - - - - - - - false - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - - - - false - - - Lorem ipsum dolor sit amet - - - - - - - Lorem ipsum dolor sit amet - - - - - - - false - - - true - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - - - - true - - - New Item - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - New Item - - - - - - - - - New Item1 - - - - - New Item2 - - - - - New Item3 - - - - - New Item4 - - - - - New Item5 - - - - - New Item6 - - - - - New Item7 - - - - - - - - false - - - false - - - - New Item1 - - - - - New Item2 - - - - - New Item3 - - - - - New Item4 - - - - - New Item5 - - - - - New Item6 - - - - - New Item7 - - - - - - - - false - - - - - - - false - - - - New Item1 - - - - - New Item2 - - - - - New Item3 - - - - - New Item4 - - - - - New Item5 - - - - - New Item6 - - - - - New Item7 - - - - - - - - - - - false - - - - - - - - - Material theme - - - - - - - false - - - Material theme - - - - - - - - - Danger - - - danger - - - - - - - Warning - - - warning - - - - - - - Success - - - success - - - - - - - - - Qt::Horizontal - - - - 427 - 20 - - - - - - - - - 75 - true - - - - Material theme - - - - - - - - - - - - Long Page Name - - - - - - - - - @@ -1410,6 +970,447 @@ + + + + QTabWidget::East + + + 0 + + + true + + + + Page + + + + + + 2 + + + + + 0 + 0 + 539 + 451 + + + + Page 1 + + + + + + + https://www.python.org/ + + + + + + + + + + 0 + 0 + 539 + 451 + + + + Page + + + + + + + 0 + 0 + 539 + 451 + + + + Page + + + + + + + + + + + false + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + + + + false + + + Lorem ipsum dolor sit amet + + + + + + + Lorem ipsum dolor sit amet + + + + + + + false + + + true + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + + + + true + + + New Item + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + New Item + + + + + + + + + New Item1 + + + + + New Item2 + + + + + New Item3 + + + + + New Item4 + + + + + New Item5 + + + + + New Item6 + + + + + New Item7 + + + + + + + + false + + + false + + + + New Item1 + + + + + New Item2 + + + + + New Item3 + + + + + New Item4 + + + + + New Item5 + + + + + New Item6 + + + + + New Item7 + + + + + + + + false + + + + + + + false + + + + New Item1 + + + + + New Item2 + + + + + New Item3 + + + + + New Item4 + + + + + New Item5 + + + + + New Item6 + + + + + New Item7 + + + + + + + + + + + false + + + + + + + + + Material theme + + + + + + + false + + + Material theme + + + + + + + + + Danger + + + danger + + + + + + + Warning + + + warning + + + + + + + Success + + + success + + + + + + + + + Qt::Horizontal + + + + 427 + 20 + + + + + + + + + 75 + true + + + + Material theme + + + + + + + + + + + + Long Page Name + + + + + + + + + @@ -1430,7 +1431,7 @@ 0 0 - 1544 + 1403 30 @@ -1544,34 +1545,74 @@ - + - Right Dock + Change theme 2 - - - - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">textEdit</p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></body></html> - - - - - - plainTextEdit -Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - - + + + + + primaryColor + + + + + + + primaryLightColor + + + + + + + secondaryColor + + + + + + + secondaryLightColor + + + + + + + secondaryDarkColor + + + + + + + primaryTextColor + + + + + + + secondaryTextColor + + + + + + + Invert secondary colors + + + + @@ -1609,6 +1650,38 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i + + + Right Doc + + + 2 + + + + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Noto Sans'; font-size:10pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">textEdit</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></body></html> + + + + + + + plainTextEdit +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + + + + + true