File: _pyside2.py

package info (click to toggle)
python-vispy 0.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,344 kB
  • sloc: python: 57,412; javascript: 6,810; makefile: 63; sh: 5
file content (59 lines) | stat: -rw-r--r-- 2,256 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.

""" PySide2 proxy backend for the qt backend.
"""

import sys
from .. import backends
from ...util import logger
from ... import config

USE_EGL = config['gl_backend'].lower().startswith('es')

try:
    # Make sure no conflicting libraries have been imported.
    for lib in ['PySide', 'PyQt4', 'PyQt5']:
        lib += '.QtCore'
        if lib in sys.modules:
            raise RuntimeError("Refusing to import PySide2 because %s is "
                               "already imported." % lib)
    # Try importing (QtOpenGL first to fail without import QtCore)
    if not USE_EGL:
        from PySide2 import QtOpenGL  # noqa
    from PySide2 import QtGui, QtCore  # noqa
except Exception as exp:
    # Fail: this backend cannot be used
    available, testable, why_not, which = False, False, str(exp), None
else:
    # Success
    available, testable, why_not = True, True, None
    # What is this for? PyQt5 sets this to true, PySide sets this to false
    has_uic = False
    import PySide2

    # PySide2 doesn't have qWait for some reason see:
    # https://github.com/pyqtgraph/pyqtgraph/pull/376/commits/8bdc19be75a7552cc0043bf8b5f5e0ee796edda0
    from PySide2 import QtTest
    if not hasattr(QtTest.QTest, 'qWait'):
        @staticmethod
        def qWait(msec):
            import time
            start = time.time()
            PySide2.QtWidgets.QApplication.processEvents()
            while time.time() < start + msec * 0.001:
                PySide2.QtWidgets.QApplication.processEvents()
        QtTest.QTest.qWait = qWait

    which = ('PySide2', PySide2.__version__, QtCore.__version__)
    # Remove _qt module to force an import even if it was already imported
    sys.modules.pop(__name__.replace('_pyside2', '_qt'), None)
    # Import _qt. Keep a ref to the module object!
    if backends.qt_lib is None:
        backends.qt_lib = 'pyside2'  # Signal to _qt what it should import
        from . import _qt  # noqa
        from ._qt import *  # noqa
    else:
        logger.info('%s already imported, cannot switch to %s'
                    % (backends.qt_lib, 'pyside2'))