eric6/E5Gui/E5OverrideCursor.py

changeset 7771
787a6b3f8c9f
child 7774
9eed155411f0
equal deleted inserted replaced
7770:49f3377aebf1 7771:787a6b3f8c9f
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2020 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 PyQt5.QtCore import pyqtSlot, Qt, QProcess, QEventLoop
14 from PyQt5.QtGui import QCursor, QGuiApplication
15
16
17 # TODO: add similar class for QMutexLocker
18 class E5OverrideCursor(contextlib.AbstractContextManager):
19 """
20 Class implementing a context manager class for an override cursor.
21 """
22 def __init__(self, cursorShape=Qt.WaitCursor):
23 """
24 Constructor
25
26 @param cursorShape shape of the override cursor
27 @type Qt.CursorShape
28 """
29 self.__cursorShape = cursorShape
30
31 def __enter__(self):
32 """
33 Special method called when entering the runtime ccontext.
34
35 @return reference to the context manager object
36 @rtype E5OverrideCursor
37 """
38 QGuiApplication.setOverrideCursor(QCursor(self.__cursorShape))
39 QGuiApplication.processEvents(QEventLoop.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(QEventLoop.ExcludeUserInputEvents)
56
57 return None
58
59
60 class E5OverridenCursor(contextlib.AbstractContextManager):
61 """
62 Class implementing a context manager class for an overriden cursor.
63
64 The cursor is reset upon entering the runtime context and restored
65 upon exiting it.
66 """
67 def __init__(self):
68 """
69 Constructor
70 """
71 self.__cursor = None
72
73 def __enter__(self):
74 """
75 Special method called when entering the runtime ccontext.
76
77 @return reference to the context manager object
78 @rtype E5OverrideCursor
79 """
80 self.__cursor = QGuiApplication.overrideCursor()
81 if self.__cursor is not None:
82 QGuiApplication.restoreOverrideCursor()
83 QGuiApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
84
85 return self
86
87 def __exit__(self, exc_type, exc_value, traceback):
88 """
89 Special method called when exiting the runtime ccontext.
90
91 @param exc_type type of an exception raised in the runtime context
92 @param exc_value value of an exception raised in the runtime context
93 @param traceback traceback of an exception raised in the runtime
94 context
95 @return always returns None to not suppress any exception
96 @rtype None
97 """
98 if self.__cursor is not None:
99 QGuiApplication.setOverrideCursor(self.__cursor)
100 QGuiApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
101
102 return None
103
104
105 class E5OverrideCursorProcess(QProcess):
106 """
107 Class implementing a QProcess subclass controlling an override cursor.
108 """
109 def __init__(self, parent=None, cursorShape=Qt.WaitCursor):
110 """
111 Constructor
112
113 @param parent reference to the parent object
114 @type QObject
115 @param cursorShape shape of the override cursor
116 @type Qt.CursorShape
117 """
118 super(E5OverrideCursorProcess, self).__init__(parent)
119
120 self.__cursorShape = cursorShape
121
122 self.started.connect(self.__processStarted)
123 self.finished.connect(self.__processFinished)
124
125 @pyqtSlot()
126 def __processStarted(self):
127 """
128 Private slot setting the cursor after the process has started.
129 """
130 QGuiApplication.setOverrideCursor(QCursor(self.__cursorShape))
131 QGuiApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
132
133 @pyqtSlot()
134 def __processFinished(self):
135 """
136 Private slot resetting the cursor when the process finished.
137 """
138 QGuiApplication.restoreOverrideCursor()
139 QGuiApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

eric ide

mercurial