def process_palette(palette, compile_for='qtpy'): """Process palette class to create a new palette file/folders. It generates all files below, in this order: - Palette files (svg/.png) under docs/images/[palette_id] - Image files (.png) under [palette_id]/rc folder. - QRC file in [palette_id]/[palette_id]style.qrc (C++). - SCSS variables in [palette_id]/_variables.scss file. - QSS file in [palette_id]/[palette_id]style.qss. - Compiled QRC file in [palette_id]/[palette_id]style_rc.py TODO: - Must generalize to create custom palettes and folder paths. - Must create/copy all files under [palette_id], such as main.scss, __init__.py, palette.py. - Add option or avoid adding the palette under docs for custom palettes. Args: palette (Palette): Palette. compile_for (list, optional): Prefix used in resources. Defaults to 'qtpy'. Possible values are 'qtpy', 'pyqtgraph', 'pyqt', 'pyqt5', 'pyside', 'pyside2', 'qt', 'qt5', 'all'. """ if palette is None: _logger.error("Please pass a palette class in order to create its " "associated images") sys.exit(1) if palette.ID is None: _logger.error("A QDarkStyle palette requires an ID!") sys.exit(1) id_ = palette.ID print(f"-- PROCESSING THEME: {id_}") # TODO: delete/remove all files and folders to ensure that old files # are not used print(f"-- GENERATING PALETTE IMAGE FOR: {id_}") create_palette_image(palette=palette) print(f"-- GENERATING IMAGE FILES (.svg > .png) FOR: {id_}") create_images(palette=palette) print(f"-- GENERATING QRC FILE FOR: {id_}") generate_qrc_file(palette=palette) print(f"-- GENERATING QSS FILE (.scss > .qss) FOR: {id_}") create_qss(palette=palette) print(f"-- CONVERTING RESOURCE FILE (. qrc > _rc.py/.rcc) FOR: {id_}") compile_qrc_file(compile_for=compile_for, palette=palette)
def load_stylesheet_pyqt5(): """ Load the stylesheet for use in a pyqt5 application. :param pyside: True to load the pyside rc file, False to load the PyQt rc file :return the stylesheet string """ warnings.warn( "load_stylesheet_pyqt5() will be deprecated in version 3," "set QtPy environment variable to specify the Qt binding and " "use load_stylesheet()", PendingDeprecationWarning ) # Compiles SCSS/SASS files to QSS from qdarkstyle.utils.scss import create_qss create_qss() # Smart import of the rc file import qdarkstyle.pyqt5_style_rc # Load the stylesheet content from resources from PyQt5.QtCore import QCoreApplication, QFile, QTextStream from PyQt5.QtGui import QColor, QPalette # Apply palette fix. See issue #139 _apply_palette_fix(QCoreApplication, QPalette, QColor) f = QFile(":qdarkstyle/style.qss") if not f.exists(): _logger().error("Unable to load stylesheet, file not found in " "resources") return "" else: f.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(f) stylesheet = ts.readAll() # Apply OS specific patches # stylesheet = _apply_stylesheet_patches(stylesheet) return stylesheet
def load_stylesheet_pyqt5(): """ Load the stylesheet for use in a pyqt5 application. :param pyside: True to load the pyside rc file, False to load the PyQt rc file :return the stylesheet string """ warnings.warn( "load_stylesheet_pyqt5() will be deprecated in version 3," "set QtPy environment variable to specify the Qt binding and " "use load_stylesheet()", PendingDeprecationWarning) # Compiles SCSS/SASS files to QSS from qdarkstyle.utils.scss import create_qss create_qss() # Smart import of the rc file import qdarkstyle.pyqt5_style_rc # Load the stylesheet content from resources from PyQt5.QtCore import QCoreApplication, QFile, QTextStream from PyQt5.QtGui import QColor, QPalette # Apply palette fix. See issue #139 _apply_palette_fix(QCoreApplication, QPalette, QColor) f = QFile(":qdarkstyle/style.qss") if not f.exists(): _logger().error("Unable to load stylesheet, file not found in " "resources") return "" else: f.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(f) stylesheet = ts.readAll() # Apply OS specific patches # stylesheet = _apply_stylesheet_patches(stylesheet) return stylesheet
def run_process(args): """Process qrc files.""" # Generate qrc file based on the content of the resources folder # Create palette and resources png images print('Generating palette image ...') create_palette_image() print('Generating images ...') create_images() print('Generating qrc ...') generate_qrc_file() print('Converting .qrc to _rc.py and/or .rcc ...') os.chdir(args.qrc_dir) for qrc_file in glob.glob('*.qrc'): # get name without extension filename = os.path.splitext(qrc_file)[0] print(filename, '...') ext = '_rc.py' ext_c = '.rcc' # Create variables SCSS files and compile SCSS files to QSS print('Compiling SCSS/SASS files to QSS ...') create_qss() # creating names py_file_pyqt5 = 'pyqt5_' + filename + ext py_file_pyqt = 'pyqt_' + filename + ext py_file_pyside = 'pyside_' + filename + ext py_file_pyside2 = 'pyside2_' + filename + ext py_file_qtpy = '' + filename + ext py_file_pyqtgraph = 'pyqtgraph_' + filename + ext # calling external commands if args.create in ['pyqt', 'pyqtgraph', 'all']: print("Compiling for PyQt4 ...") try: call(['pyrcc4', '-py3', qrc_file, '-o', py_file_pyqt]) except FileNotFoundError: print("You must install pyrcc4") if args.create in ['pyqt5', 'qtpy', 'all']: print("Compiling for PyQt5 ...") try: call(['pyrcc5', qrc_file, '-o', py_file_pyqt5]) except FileNotFoundError: print("You must install pyrcc5") if args.create in ['pyside', 'all']: print("Compiling for PySide ...") try: call(['pyside-rcc', '-py3', qrc_file, '-o', py_file_pyside]) except FileNotFoundError: print("You must install pyside-rcc") if args.create in ['pyside2', 'all']: print("Compiling for PySide 2...") try: call(['pyside2-rcc', qrc_file, '-o', py_file_pyside2]) except FileNotFoundError: print("You must install pyside2-rcc") if args.create in ['qtpy', 'all']: print("Compiling for QtPy ...") # special case - qtpy - syntax is PyQt5 with open(py_file_pyqt5, 'r') as file: filedata = file.read() # replace the target string filedata = filedata.replace('from PyQt5', 'from qtpy') with open(py_file_qtpy, 'w+') as file: # write the file out again file.write(filedata) if args.create not in ['pyqt5']: os.remove(py_file_pyqt5) if args.create in ['pyqtgraph', 'all']: print("Compiling for PyQtGraph ...") # special case - pyqtgraph - syntax is PyQt4 with open(py_file_pyqt, 'r') as file: filedata = file.read() # replace the target string filedata = filedata.replace('from PyQt4', 'from pyqtgraph.Qt') with open(py_file_pyqtgraph, 'w+') as file: # write the file out again file.write(filedata)
def test_create_qss_light(): # Should not raise a CompileError create_qss(LightPalette)
def test_create_qss_dark(): # Should not raise a CompileError create_qss(DarkPalette)
def load_stylesheet(pyside=True): """ Load the stylesheet. Takes care of importing the rc module. :param pyside: True to load the pyside rc file, False to load the PyQt rc file :return the stylesheet string """ warnings.warn( "load_stylesheet() will not receive pyside parameter in version 3. " "Set QtPy environment variable to specify the Qt binding insteady.", PendingDeprecationWarning ) # Compiles SCSS/SASS files to QSS from qdarkstyle.utils.scss import create_qss create_qss() # Smart import of the rc file pyside_ver = None if pyside: # Detect the PySide version available try: import PySide except ImportError: # Compatible with py27 import PySide2 pyside_ver = 2 else: pyside_ver = 1 if pyside_ver == 1: import qdarkstyle.pyside_style_rc else: import qdarkstyle.pyside2_style_rc else: import qdarkstyle.pyqt_style_rc # Load the stylesheet content from resources if not pyside: from PyQt4.QtCore import QCoreApplication, QFile, QTextStream from PyQt4.QtGui import QColor, QPalette else: if pyside_ver == 1: from PySide.QtCore import QCoreApplication, QFile, QTextStream from PySide.QtGui import QColor, QPalette else: from PySide2.QtCore import QCoreApplication, QFile, QTextStream from PySide2.QtGui import QColor, QPalette # Apply palette fix. See issue #139 _apply_palette_fix(QCoreApplication, QPalette, QColor) f = QFile(":qdarkstyle/style.qss") if not f.exists(): _logger().error("Unable to load stylesheet, file not found in " "resources") return "" else: f.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(f) stylesheet = ts.readAll() # Apply OS specific patches stylesheet = _apply_stylesheet_patches(stylesheet) return stylesheet
def run_process(args): """Process qrc files.""" # Generate qrc file based on the content of the resources folder print('Generating style.qrc files ...') generate_qrc_file() # Create palette and resources png images print('Generating palette images ...') create_palette_image() create_images() print('Changing directory to: ', args.qrc_dir) os.chdir(args.qrc_dir) print('Converting .qrc to _rc.py and/or .rcc ...') for qrc_file in glob.glob('*.qrc'): # get name without extension filename = os.path.splitext(qrc_file)[0] print(filename, '...') ext = '_rc.py' ext_c = '.rcc' # Create variables SCSS files and compile SCSS files to QSS print('Compiling SCSS/SASS files to QSS ...') create_qss() # creating names py_file_pyqt5 = 'pyqt5_' + filename + ext py_file_pyqt = 'pyqt_' + filename + ext py_file_pyside = 'pyside_' + filename + ext py_file_pyside2 = 'pyside2_' + filename + ext py_file_qtpy = 'qtpy_' + filename + ext py_file_pyqtgraph = 'pyqtgraph_' + filename + ext # calling external commands if args.create in ['pyqt', 'pyqtgraph', 'all']: print("Compiling for PyQt4 ...") try: call(['pyrcc4', '-py3', qrc_file, '-o', py_file_pyqt]) except FileNotFoundError: print("You must install pyrcc4") if args.create in ['pyqt5', 'qtpy', 'all']: print("Compiling for PyQt5 ...") try: call(['pyrcc5', qrc_file, '-o', py_file_pyqt5]) except FileNotFoundError: print("You must install pyrcc5") if args.create in ['pyside', 'all']: print("Compiling for PySide ...") try: call(['pyside-rcc', '-py3', qrc_file, '-o', py_file_pyside]) except FileNotFoundError: print("You must install pyside-rcc") if args.create in ['pyside2', 'all']: print("Compiling for PySide 2...") try: call(['pyside2-rcc', '-py3', qrc_file, '-o', py_file_pyside2]) except FileNotFoundError: print("You must install pyside2-rcc") if args.create in ['qtpy', 'all']: print("Compiling for QtPy ...") # special case - qtpy - syntax is PyQt5 with open(py_file_pyqt5, 'r') as file: filedata = file.read() # replace the target string filedata = filedata.replace('from PyQt5', 'from qtpy') with open(py_file_qtpy, 'w+') as file: # write the file out again file.write(filedata) if args.create in ['pyqtgraph', 'all']: print("Compiling for PyQtGraph ...") # special case - pyqtgraph - syntax is PyQt4 with open(py_file_pyqt, 'r') as file: filedata = file.read() # replace the target string filedata = filedata.replace('from PyQt4', 'from pyqtgraph.Qt') with open(py_file_pyqtgraph, 'w+') as file: # write the file out again file.write(filedata)
def test_create_qss(): # Should not raise a CompileError create_qss()
def load_stylesheet(pyside=True): """ Load the stylesheet. Takes care of importing the rc module. :param pyside: True to load the pyside rc file, False to load the PyQt rc file :return the stylesheet string """ warnings.warn( "load_stylesheet() will not receive pyside parameter in version 3. " "Set QtPy environment variable to specify the Qt binding insteady.", PendingDeprecationWarning) # Compiles SCSS/SASS files to QSS from qdarkstyle.utils.scss import create_qss create_qss() # Smart import of the rc file pyside_ver = None if pyside: # Detect the PySide version available try: import PySide except ImportError: # Compatible with py27 import PySide2 pyside_ver = 2 else: pyside_ver = 1 if pyside_ver == 1: import qdarkstyle.pyside_style_rc else: import qdarkstyle.pyside2_style_rc else: import qdarkstyle.pyqt_style_rc # Load the stylesheet content from resources if not pyside: from PyQt4.QtCore import QCoreApplication, QFile, QTextStream from PyQt4.QtGui import QColor, QPalette else: if pyside_ver == 1: from PySide.QtCore import QCoreApplication, QFile, QTextStream from PySide.QtGui import QColor, QPalette else: from PySide2.QtCore import QCoreApplication, QFile, QTextStream from PySide2.QtGui import QColor, QPalette # Apply palette fix. See issue #139 _apply_palette_fix(QCoreApplication, QPalette, QColor) f = QFile(":qdarkstyle/style.qss") if not f.exists(): _logger().error("Unable to load stylesheet, file not found in " "resources") return "" else: f.open(QFile.ReadOnly | QFile.Text) ts = QTextStream(f) stylesheet = ts.readAll() # Apply OS specific patches stylesheet = _apply_stylesheet_patches(stylesheet) return stylesheet