File: BusyCursor.py

package info (click to toggle)
python-pyqtgraph 0.13.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,520 kB
  • sloc: python: 52,773; makefile: 115; ansic: 40; sh: 2
file content (36 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from contextlib import contextmanager

from ..Qt import QtCore, QtGui, QtWidgets

__all__ = ["BusyCursor"]


@contextmanager
def BusyCursor():
    """
    Display a busy mouse cursor during long operations.
    Usage::

        with BusyCursor():
            doLongOperation()

    May be nested. If called from a non-gui thread, then the cursor will not be affected.
    """
    app = QtCore.QCoreApplication.instance()
    in_gui_thread = (app is not None) and (QtCore.QThread.currentThread() == app.thread())
    try:
        if in_gui_thread:
            guard = QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CursorShape.WaitCursor))
            # on PySide6 6.3.0, setOverrideCursor() returns a QOverrideCursorGuard object
            # that, on its destruction, calls restoreOverrideCursor() if the user had not
            # already done so.
            # if the user wants to call it manually, they must do it via the returned object,
            # and not via the QtWidgets.QApplication static method; otherwise the restore
            # would get called twice.
        yield
    finally:
        if in_gui_thread:
            if hasattr(guard, 'restoreOverrideCursor'):
                guard.restoreOverrideCursor()
            else:
                QtWidgets.QApplication.restoreOverrideCursor()