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