1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a context manager class for an override cursor and a |
|
8 QProcess class controlling an override cursor. |
|
9 """ |
|
10 |
|
11 import contextlib |
|
12 |
|
13 from PyQt6.QtCore import pyqtSlot, Qt, QProcess, QEventLoop |
|
14 from PyQt6.QtGui import QCursor, QGuiApplication |
|
15 |
|
16 |
|
17 class E5OverrideCursor(contextlib.AbstractContextManager): |
|
18 """ |
|
19 Class implementing a context manager class for an override cursor. |
|
20 """ |
|
21 def __init__(self, cursorShape=Qt.CursorShape.WaitCursor): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param cursorShape shape of the override cursor |
|
26 @type Qt.CursorShape |
|
27 """ |
|
28 self.__cursorShape = cursorShape |
|
29 |
|
30 def __enter__(self): |
|
31 """ |
|
32 Special method called when entering the runtime ccontext. |
|
33 |
|
34 @return reference to the context manager object |
|
35 @rtype E5OverrideCursor |
|
36 """ |
|
37 QGuiApplication.setOverrideCursor(QCursor(self.__cursorShape)) |
|
38 QGuiApplication.processEvents( |
|
39 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|
40 |
|
41 return self |
|
42 |
|
43 def __exit__(self, exc_type, exc_value, traceback): |
|
44 """ |
|
45 Special method called when exiting the runtime ccontext. |
|
46 |
|
47 @param exc_type type of an exception raised in the runtime context |
|
48 @param exc_value value of an exception raised in the runtime context |
|
49 @param traceback traceback of an exception raised in the runtime |
|
50 context |
|
51 @return always returns None to not suppress any exception |
|
52 @rtype None |
|
53 """ |
|
54 QGuiApplication.restoreOverrideCursor() |
|
55 QGuiApplication.processEvents( |
|
56 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|
57 |
|
58 return None # __IGNORE_WARNING_M831__ |
|
59 |
|
60 |
|
61 class E5OverridenCursor(contextlib.AbstractContextManager): |
|
62 """ |
|
63 Class implementing a context manager class for an overriden cursor. |
|
64 |
|
65 The cursor is reset upon entering the runtime context and restored |
|
66 upon exiting it. |
|
67 """ |
|
68 def __init__(self): |
|
69 """ |
|
70 Constructor |
|
71 """ |
|
72 self.__cursorShape = None |
|
73 |
|
74 def __enter__(self): |
|
75 """ |
|
76 Special method called when entering the runtime ccontext. |
|
77 |
|
78 @return reference to the context manager object |
|
79 @rtype E5OverrideCursor |
|
80 """ |
|
81 cursor = QGuiApplication.overrideCursor() |
|
82 if cursor is not None: |
|
83 self.__cursorShape = cursor.shape() |
|
84 QGuiApplication.restoreOverrideCursor() |
|
85 QGuiApplication.processEvents( |
|
86 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|
87 |
|
88 return self |
|
89 |
|
90 def __exit__(self, exc_type, exc_value, traceback): |
|
91 """ |
|
92 Special method called when exiting the runtime ccontext. |
|
93 |
|
94 @param exc_type type of an exception raised in the runtime context |
|
95 @param exc_value value of an exception raised in the runtime context |
|
96 @param traceback traceback of an exception raised in the runtime |
|
97 context |
|
98 @return always returns None to not suppress any exception |
|
99 @rtype None |
|
100 """ |
|
101 if self.__cursorShape is not None: |
|
102 QGuiApplication.setOverrideCursor(QCursor(self.__cursorShape)) |
|
103 QGuiApplication.processEvents( |
|
104 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|
105 |
|
106 return None # __IGNORE_WARNING_M831__ |
|
107 |
|
108 |
|
109 class E5OverrideCursorProcess(QProcess): |
|
110 """ |
|
111 Class implementing a QProcess subclass controlling an override cursor. |
|
112 """ |
|
113 def __init__(self, parent=None, cursorShape=Qt.CursorShape.WaitCursor): |
|
114 """ |
|
115 Constructor |
|
116 |
|
117 @param parent reference to the parent object |
|
118 @type QObject |
|
119 @param cursorShape shape of the override cursor |
|
120 @type Qt.CursorShape |
|
121 """ |
|
122 super().__init__(parent) |
|
123 |
|
124 self.__cursorShape = cursorShape |
|
125 |
|
126 self.started.connect(self.__processStarted) |
|
127 self.finished.connect(self.__processFinished) |
|
128 |
|
129 @pyqtSlot() |
|
130 def __processStarted(self): |
|
131 """ |
|
132 Private slot setting the cursor after the process has started. |
|
133 """ |
|
134 QGuiApplication.setOverrideCursor(QCursor(self.__cursorShape)) |
|
135 QGuiApplication.processEvents( |
|
136 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|
137 |
|
138 @pyqtSlot() |
|
139 def __processFinished(self): |
|
140 """ |
|
141 Private slot resetting the cursor when the process finished. |
|
142 """ |
|
143 QGuiApplication.restoreOverrideCursor() |
|
144 QGuiApplication.processEvents( |
|
145 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) |
|