|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a main window class with styling support. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import QCoreApplication |
|
11 from PyQt6.QtWidgets import QMainWindow, QStyleFactory, QApplication |
|
12 |
|
13 from .EricApplication import ericApp |
|
14 from . import EricMessageBox |
|
15 |
|
16 |
|
17 class EricMainWindow(QMainWindow): |
|
18 """ |
|
19 Class implementing a main window with styling support. |
|
20 """ |
|
21 def __init__(self, parent=None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param parent reference to the parent widget (QWidget) |
|
26 """ |
|
27 super().__init__(parent) |
|
28 |
|
29 self.defaultStyleName = QApplication.style().objectName() |
|
30 |
|
31 def setStyle(self, styleName, styleSheetFile): |
|
32 """ |
|
33 Public method to set the style of the interface. |
|
34 |
|
35 @param styleName name of the style to set (string) |
|
36 @param styleSheetFile name of a style sheet file to read to overwrite |
|
37 defaults of the given style (string) |
|
38 """ |
|
39 # step 1: set the style |
|
40 style = None |
|
41 if styleName != "System" and styleName in QStyleFactory.keys(): |
|
42 # __IGNORE_WARNING_Y118__ |
|
43 style = QStyleFactory.create(styleName) |
|
44 if style is None: |
|
45 style = QStyleFactory.create(self.defaultStyleName) |
|
46 if style is not None: |
|
47 QApplication.setStyle(style) |
|
48 |
|
49 # step 2: set a style sheet |
|
50 if styleSheetFile: |
|
51 try: |
|
52 with open(styleSheetFile, "r", encoding="utf-8") as f: |
|
53 styleSheet = f.read() |
|
54 except OSError as msg: |
|
55 EricMessageBox.warning( |
|
56 self, |
|
57 QCoreApplication.translate( |
|
58 "EricMainWindow", "Loading Style Sheet"), |
|
59 QCoreApplication.translate( |
|
60 "EricMainWindow", |
|
61 """<p>The Qt Style Sheet file <b>{0}</b> could""" |
|
62 """ not be read.<br>Reason: {1}</p>""") |
|
63 .format(styleSheetFile, str(msg))) |
|
64 return |
|
65 else: |
|
66 styleSheet = "" |
|
67 |
|
68 ericApp().setStyleSheet(styleSheet) |