File: __init__.py

package info (click to toggle)
qt-material 2.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,000 kB
  • sloc: python: 1,153; xml: 261; makefile: 22; sh: 8
file content (765 lines) | stat: -rw-r--r-- 24,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import os
import sys
import logging
import base64
from pathlib import Path
import platform
from xml.dom.minidom import parse

from qt_material.resources import ResourseGenerator, RESOURCES_PATH

GUI = True

if 'PySide2' in sys.modules:
    from PySide2.QtGui import (
        QFontDatabase,
        QColor,
        QGuiApplication,
        QPalette,
    )
    from PySide2.QtWidgets import QAction, QColorDialog, QActionGroup
    from PySide2.QtUiTools import QUiLoader
    from PySide2.QtCore import Qt, QDir

elif 'PySide6' in sys.modules:
    from PySide6.QtGui import (
        QFontDatabase,
        QAction,
        QColor,
        QGuiApplication,
        QPalette,
        QActionGroup,
    )
    from PySide6.QtWidgets import QColorDialog
    from PySide6.QtUiTools import QUiLoader
    from PySide6.QtCore import Qt, QDir

elif 'PyQt5' in sys.modules:
    from PyQt5.QtGui import QFontDatabase, QColor, QGuiApplication, QPalette
    from PyQt5.QtWidgets import QAction, QColorDialog, QActionGroup
    from PyQt5.QtCore import Qt, QDir
    from PyQt5 import uic

elif 'PyQt6' in sys.modules:
    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
else:
    GUI = False
    logging.warning("qt_material must be imported after PySide or PyQt!")

import jinja2

TEMPLATE_FILE = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'material.css.template'
)


# ----------------------------------------------------------------------
def export_theme(
    theme='',
    qss=None,
    rcc=None,
    invert_secondary=False,
    extra={},
    output='theme',
    prefix='icon:/',
):
    """"""
    if not os.path.isabs(output) and not output.startswith('.'):
        output = f'.{output}'

    stylesheet = build_stylesheet(
        theme, invert_secondary, extra, output, export=True
    )

    if output.startswith('.'):
        output = output[1:]

    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']:
                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(
                        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')


# ----------------------------------------------------------------------
def build_stylesheet(
    theme='',
    invert_secondary=False,
    extra={},
    parent='theme',
    template=TEMPLATE_FILE,
    export=False,
):
    """"""

    if not export:
        try:
            add_fonts()
        except Exception as e:
            logging.warning(e)

    theme = get_theme(theme, invert_secondary)
    if theme is None:
        return None

    set_icons_theme(theme, parent=parent)

    # 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')

    theme.update(extra)

    if GUI:
        default_palette = QGuiApplication.palette()
        color = QColor(
            *[
                int(theme['primaryColor'][i : i + 2], 16)
                for i in range(1, 6, 2)
            ]
            + [92]
        )

        try:
            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,
    }

    environ.update(theme)
    return stylesheet.render(environ)


# ----------------------------------------------------------------------
def get_theme(theme_name, invert_secondary=False):
    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
        theme = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'themes',
            'light_cyan_500.xml',
        )
    elif not os.path.exists(theme_name):
        theme = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), 'themes', theme_name
        )
    else:
        theme = theme_name

    if not os.path.exists(theme):
        logging.warning(f"{theme} not exist!")
        return None

    document = parse(theme)
    theme = {
        child.getAttribute('name'): child.firstChild.nodeValue
        for child in document.getElementsByTagName('color')
    }

    for k in theme:
        os.environ[str(k)] = theme[k]

    if invert_secondary:
        (
            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

    return theme


# ----------------------------------------------------------------------
def add_fonts():
    """"""
    fonts_path = os.path.join(os.path.dirname(__file__), 'fonts')

    for font_dir in ['roboto']:
        for font in filter(
            lambda s: s.endswith('.ttf'),
            os.listdir(os.path.join(fonts_path, font_dir)),
        ):
            try:
                QFontDatabase.addApplicationFont(
                    os.path.join(fonts_path, font_dir, font)
                )
            except:  # snake_case, true_property
                QFontDatabase.add_application_font(
                    os.path.join(fonts_path, font_dir, font)
                )


# ----------------------------------------------------------------------
def apply_stylesheet(
    app,
    theme='',
    style=None,
    save_as=None,
    invert_secondary=False,
    extra={},
    parent='theme',
    css_file=None,
):
    """"""
    if style:
        try:
            try:
                app.setStyle(style)
            except:  # snake_case, true_property
                app.style = style
        except:
            logging.error(f"The style '{style}' does not exist.")
            pass

    if 'QMenu' in extra:
        for k in extra['QMenu']:
            extra[f'qmenu_{k}'] = extra['QMenu'][k]
        extra['QMenu'] = True

    stylesheet = build_stylesheet(theme, invert_secondary, extra, parent)
    if stylesheet is None:
        return

    if save_as:
        with open(save_as, 'w') as file:
            file.writelines(stylesheet)

    if css_file and os.path.exists(css_file):
        with open(css_file) as file:
            stylesheet += file.read().format(**os.environ)

    try:
        app.setStyleSheet(stylesheet)
    except:
        app.style_sheet = stylesheet


# ----------------------------------------------------------------------
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})'


# ----------------------------------------------------------------------
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'

    if isinstance(value, str):
        value = float(value.replace('px', ''))

    density = (
        value + (density_interval * int(density_scale)) - (border * 2)
    ) * scale

    if density <= 0:
        density = min_
    return density


# ----------------------------------------------------------------------
def set_icons_theme(theme, parent='theme'):
    """"""
    source = os.path.join(os.path.dirname(__file__), 'resources', 'source')
    resources = ResourseGenerator(
        primary=theme['primaryColor'],
        secondary=theme['secondaryColor'],
        disabled=theme['secondaryLightColor'],
        source=source,
        parent=parent,
    )
    resources.generate()

    if GUI:
        try:
            QDir.addSearchPath('icon', resources.index)
            QDir.addSearchPath(
                'qt_material',
                os.path.join(os.path.dirname(__file__), 'resources'),
            )
        except:  # snake_case, true_property
            QDir.add_search_path('icon', resources.index)
            QDir.add_search_path(
                'qt_material',
                os.path.join(os.path.dirname(__file__), 'resources'),
            )


# ----------------------------------------------------------------------
def list_themes():
    """"""
    themes = os.listdir(
        os.path.join(os.path.dirname(os.path.abspath(__file__)), 'themes')
    )
    themes = filter(lambda a: a.endswith('xml'), themes)
    return sorted(list(themes))


# ----------------------------------------------------------------------
def deprecated(replace):
    """"""
    # ----------------------------------------------------------------------
    def wrap1(fn):
        # ----------------------------------------------------------------------
        def wrap2(*args, **kwargs):
            logging.warning(
                f'This function is deprecated, please use "{replace}" instead.'
            )
            fn(*args, **kwargs)

        return wrap2

    return wrap1


########################################################################
class QtStyleTools:
    """"""

    extra_values = {}

    # ----------------------------------------------------------------------
    @deprecated('set_extra')
    def set_extra_colors(self, extra):
        """"""
        self.extra_values = extra

    # ----------------------------------------------------------------------
    def set_extra(self, extra):
        """"""
        self.extra_values = extra

    # ----------------------------------------------------------------------
    def add_menu_theme(self, parent, menu):
        """"""
        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()):
            action = QAction(parent)
            # action.triggered.connect(self._wrapper(parent, theme, self.extra_values, self.update_buttons))
            action.triggered.connect(lambda: self.update_theme_event(parent))
            try:
                action.setText(theme)
                action.setCheckable(True)
                action.setChecked(not bool(i))
                action.setActionGroup(action_group)
                menu.addAction(action)
                action_group.addAction(action)
            except:  # snake_case, true_property
                action.text = theme
                action.checkable = True
                action.checked = not bool(i)
                action.action_group = action_group
                menu.add_action(action)
                action_group.add_action(action)

    # ----------------------------------------------------------------------
    def add_menu_density(self, parent, menu):
        """"""
        self.menu_density_ = menu
        action_group = QActionGroup(menu)

        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_):
    # """"""
    # def iner():
    # self._apply_theme(parent, theme, extra, callable_)
    # return iner

    # # ----------------------------------------------------------------------
    # def _apply_theme(self, parent, theme, extra={}, callable_=None):
    # """"""
    # self.apply_stylesheet(parent, theme=theme, invert_secondary=theme.startswith(
    # 'light'), extra=extra, callable_=callable_)

    # ----------------------------------------------------------------------
    def apply_stylesheet(
        self, parent, theme, invert_secondary=False, extra={}, callable_=None
    ):
        """"""
        if theme == 'default':
            try:
                parent.setStyleSheet('')
            except:
                parent.style_sheet = ''
            return

        apply_stylesheet(
            parent,
            theme=theme,
            invert_secondary=invert_secondary,
            extra=extra,
        )

        if callable_:
            callable_()

    # ----------------------------------------------------------------------
    def update_theme_event(self, parent):
        """"""
        try:
            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]
        except:
            density = [
                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]

        self.extra_values['density_scale'] = density

        self.apply_stylesheet(
            parent,
            theme=theme,
            invert_secondary=theme.startswith('light'),
            extra=self.extra_values,
            callable_=self.update_buttons,
        )

    # ----------------------------------------------------------------------
    def update_buttons(self):
        """"""
        if not hasattr(self, 'colors'):
            return

        theme = {
            color_: os.environ[f'QTMATERIAL_{color_.upper()}']
            for color_ in self.colors
        }

        if 'light' in os.environ['QTMATERIAL_THEME']:
            self.dock_theme.checkBox_ligh_theme.setChecked(True)
        elif 'dark' in os.environ['QTMATERIAL_THEME']:
            self.dock_theme.checkBox_ligh_theme.setChecked(False)

        try:
            if self.dock_theme.checkBox_ligh_theme.isChecked():
                (
                    theme['secondaryColor'],
                    theme['secondaryLightColor'],
                    theme['secondaryDarkColor'],
                ) = (
                    theme['secondaryColor'],
                    theme['secondaryDarkColor'],
                    theme['secondaryLightColor'],
                )
        except:  # snake_case, true_property
            if self.dock_theme.checkBox_ligh_theme.checked:
                (
                    theme['secondaryColor'],
                    theme['secondaryLightColor'],
                    theme['secondaryDarkColor'],
                ) = (
                    theme['secondaryColor'],
                    theme['secondaryDarkColor'],
                    theme['secondaryLightColor'],
                )

        for color_ in self.colors:
            button = getattr(self.dock_theme, f'pushButton_{color_}')

            color = theme[color_]

            try:
                if self.get_color(color).getHsv()[2] < 128:
                    text_color = '#ffffff'
                else:
                    text_color = '#000000'
            except:  # snake_case, true_property
                if self.get_color(color).get_hsv()[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)])

    # ----------------------------------------------------------------------
    def update_theme(self, parent):
        """"""
        with open('my_theme.xml', 'w') as file:
            file.write(
                """
            <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>
            """.format(
                    **self.custom_colors
                )
            )
        try:
            light = self.dock_theme.checkBox_ligh_theme.isChecked()
        except:  # snake_case, true_property
            light = self.dock_theme.checkBox_ligh_theme.checked

        self.apply_stylesheet(
            parent,
            'my_theme.xml',
            invert_secondary=light,
            extra=self.extra_values,
            callable_=self.update_buttons,
        )

    # ----------------------------------------------------------------------
    def set_color(self, parent, button_):
        """"""

        def iner():
            initial = self.get_color(self.custom_colors[button_])
            color_dialog = QColorDialog(parent=parent)
            try:
                color_dialog.setCurrentColor(initial)
            except:  # snake_case, true_property
                color_dialog.current_color = initial
            done = color_dialog.exec_()

            try:
                color_ = color_dialog.currentColor()
            except:  # snake_case, true_property
                color_ = color_dialog.current_color

            try:
                if done and color_.isValid():
                    rgb_255 = [color_.red(), color_.green(), color_.blue()]
                    color = '#' + ''.join(
                        [hex(v)[2:].ljust(2, '0') for v in rgb_255]
                    )
                    self.custom_colors[button_] = color
                    self.update_theme(parent)
            except:  # snake_case, true_property
                if done and color_.is_valid():
                    rgb_255 = [color_.red(), color_.green(), color_.blue()]
                    color = '#' + ''.join(
                        [hex(v)[2:].ljust(2, '0') for v in rgb_255]
                    )
                    self.custom_colors[button_] = color
                    self.update_theme(parent)

        return iner

    # ----------------------------------------------------------------------
    def show_dock_theme(self, parent):
        """"""
        self.colors = [
            'primaryColor',
            'primaryLightColor',
            'secondaryColor',
            'secondaryLightColor',
            'secondaryDarkColor',
            'primaryTextColor',
            'secondaryTextColor',
        ]

        self.custom_colors = {
            v: os.environ.get(f'QTMATERIAL_{v.upper()}', '')
            for v in self.colors
        }

        if 'PySide2' in sys.modules or 'PySide6' in sys.modules:
            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:
            self.dock_theme = uic.loadUi(
                os.path.join(os.path.dirname(__file__), 'dock_theme.ui')
            )

        try:
            parent.addDockWidget(
                Qt.DockWidgetArea.LeftDockWidgetArea, self.dock_theme
            )
            self.dock_theme.setFloating(True)
        except:  # snake_case, true_property
            parent.add_dock_widget(
                Qt.DockWidgetArea.LeftDockWidgetArea, self.dock_theme
            )
            self.dock_theme.floating = True

        self.update_buttons()
        self.dock_theme.checkBox_ligh_theme.clicked.connect(
            lambda: self.update_theme(self.main)
        )

        for color in self.colors:
            button = getattr(self.dock_theme, f'pushButton_{color}')
            button.clicked.connect(self.set_color(parent, color))


# ----------------------------------------------------------------------
def get_hook_dirs():
    package_folder = Path(__file__).parent
    return [str(package_folder.absolute())]