Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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
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)