5 |
5 |
6 """ |
6 """ |
7 Module implementing a main window class with styling support. |
7 Module implementing a main window class with styling support. |
8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import QCoreApplication |
|
11 from PyQt6.QtWidgets import QMainWindow, QStyleFactory, QApplication |
10 from PyQt6.QtWidgets import QMainWindow, QStyleFactory, QApplication |
12 |
11 |
13 from .EricApplication import ericApp |
12 from .EricApplication import ericApp |
14 from . import EricMessageBox |
|
15 |
13 |
16 |
14 |
17 class EricMainWindow(QMainWindow): |
15 class EricMainWindow(QMainWindow): |
18 """ |
16 """ |
19 Class implementing a main window with styling support. |
17 Class implementing a main window with styling support. |
20 """ |
18 """ |
21 def __init__(self, parent=None): |
19 def __init__(self, parent=None): |
22 """ |
20 """ |
23 Constructor |
21 Constructor |
24 |
22 |
25 @param parent reference to the parent widget (QWidget) |
23 @param parent reference to the parent widget |
|
24 @type QWidget |
26 """ |
25 """ |
27 super().__init__(parent) |
26 super().__init__(parent) |
28 |
27 |
29 self.defaultStyleName = QApplication.style().objectName() |
28 self.defaultStyleName = QApplication.style().objectName() |
30 |
29 |
31 def setStyle(self, styleName, styleSheetFile): |
30 def setStyle(self, styleName, styleSheetFile): |
32 """ |
31 """ |
33 Public method to set the style of the interface. |
32 Public method to set the style of the interface. |
34 |
33 |
35 @param styleName name of the style to set (string) |
34 @param styleName name of the style to set |
|
35 @type str |
36 @param styleSheetFile name of a style sheet file to read to overwrite |
36 @param styleSheetFile name of a style sheet file to read to overwrite |
37 defaults of the given style (string) |
37 defaults of the given style |
|
38 @type str |
38 """ |
39 """ |
39 # step 1: set the style |
40 # step 1: set the style |
40 style = None |
41 style = None |
41 if styleName != "System" and styleName in QStyleFactory.keys(): |
42 if styleName != "System" and styleName in QStyleFactory.keys(): |
42 # __IGNORE_WARNING_Y118__ |
43 # __IGNORE_WARNING_Y118__ |
45 style = QStyleFactory.create(self.defaultStyleName) |
46 style = QStyleFactory.create(self.defaultStyleName) |
46 if style is not None: |
47 if style is not None: |
47 QApplication.setStyle(style) |
48 QApplication.setStyle(style) |
48 |
49 |
49 # step 2: set a style sheet |
50 # step 2: set a style sheet |
50 if styleSheetFile: |
51 ericApp().setStyleSheetFile(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) |
|