E5Gui/E5MainWindow.py

Sun, 31 Dec 2017 16:52:09 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 31 Dec 2017 16:52:09 +0100
changeset 6048
82ad8ec9548c
parent 5674
a0ad2dcb27f9
child 6645
ad476851d7e0
permissions
-rw-r--r--

Updated copyright for 2018.

2101
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6048
82ad8ec9548c Updated copyright for 2018.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5674
diff changeset
3 # Copyright (c) 2012 - 2018 Detlev Offenbach <detlev@die-offenbachs.de>
2101
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a main window class with styling support.
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3060
diff changeset
10 from __future__ import unicode_literals
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2302
diff changeset
11
5674
a0ad2dcb27f9 Corrected some translations related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
12 from PyQt5.QtCore import QCoreApplication
3656
441956d8fce5 Started porting eric5 to PyQt5.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3539
diff changeset
13 from PyQt5.QtWidgets import QMainWindow, QStyleFactory, QApplication
2101
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from .E5Application import e5App
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from . import E5MessageBox
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 class E5MainWindow(QMainWindow):
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 Class implementing a main window with styling support.
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self, parent=None):
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @param parent reference to the parent widget (QWidget)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2302
diff changeset
29 super(E5MainWindow, self).__init__(parent)
2101
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self.defaultStyleName = QApplication.style().objectName()
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 def setStyle(self, styleName, styleSheetFile):
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 Public method to set the style of the interface.
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @param styleName name of the style to set (string)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 @param styleSheetFile name of a style sheet file to read to overwrite
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 defaults of the given style (string)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 # step 1: set the style
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 style = None
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 if styleName != "System" and styleName in QStyleFactory.keys():
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 style = QStyleFactory.create(styleName)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 if style is None:
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 style = QStyleFactory.create(self.defaultStyleName)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 if style is not None:
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 QApplication.setStyle(style)
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 # step 2: set a style sheet
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 if styleSheetFile:
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 try:
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 f = open(styleSheetFile, "r", encoding="utf-8")
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 styleSheet = f.read()
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 f.close()
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 except (IOError, OSError) as msg:
3020
542e97d4ecb3 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2990
diff changeset
57 E5MessageBox.warning(
542e97d4ecb3 Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2990
diff changeset
58 self,
5674
a0ad2dcb27f9 Corrected some translations related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
59 QCoreApplication.translate(
a0ad2dcb27f9 Corrected some translations related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
60 "E5MainWindow", "Loading Style Sheet"),
a0ad2dcb27f9 Corrected some translations related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
61 QCoreApplication.translate(
a0ad2dcb27f9 Corrected some translations related issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
62 "E5MainWindow",
2990
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
63 """<p>The Qt Style Sheet file <b>{0}</b> could"""
583beaf0b4b8 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
64 """ not be read.<br>Reason: {1}</p>""")
3035
36e9f388958b Fixed a bunch of indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
65 .format(styleSheetFile, str(msg)))
2101
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 return
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 else:
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 styleSheet = ""
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
5bac7dee9e1a Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 e5App().setStyleSheet(styleSheet)

eric ide

mercurial