Sun, 17 Feb 2013 19:07:15 +0100
Updated to Pygments 1.6.
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 | |
2302
f29e9405c851
Updated copyright for 2013.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
2101
diff
changeset
|
3 | # Copyright (c) 2012 - 2013 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 | |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | from PyQt4.QtGui import QMainWindow, QStyleFactory, QApplication |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
11 | |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | 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
|
13 | 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
|
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 | |
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 | 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
|
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 | 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
|
19 | """ |
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 | 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
|
21 | """ |
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 | 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
|
23 | |
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 | @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
|
25 | """ |
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 | super().__init__(parent) |
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 | |
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 | 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
|
29 | |
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 | 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
|
31 | """ |
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 | 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
|
33 | |
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 | @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
|
35 | @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
|
36 | 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
|
37 | """ |
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 | # 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
|
39 | 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
|
40 | 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
|
41 | 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
|
42 | 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
|
43 | 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
|
44 | 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
|
45 | 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
|
46 | |
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 | # 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
|
48 | 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
|
49 | 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
|
50 | 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
|
51 | 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
|
52 | 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
|
53 | except (IOError, OSError) as msg: |
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 | E5MessageBox.warning(self, |
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 | self.trUtf8("Loading 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
|
56 | self.trUtf8("""<p>The Qt Style Sheet file <b>{0}</b> could""" |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | """ not be read.<br>Reason: {1}</p>""") |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
58 | .format(styleSheetFile, str(msg))) |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | 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
|
60 | 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
|
61 | 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
|
62 | |
5bac7dee9e1a
Introduced the E5MainWindow class allowing to set a style for all the main windows.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | e5App().setStyleSheet(styleSheet) |