Exemplo n.º 1
0
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            debug = debug or _ORANGE_DEBUG
            if debug:
                port = os.environ.setdefault('QTWEBENGINE_REMOTE_DEBUGGING', '12088')
                warnings.warn(
                    'To debug QWebEngineView, set environment variable '
                    'QTWEBENGINE_REMOTE_DEBUGGING={port} and then visit '
                    'http://127.0.0.1:{port}/ in a Chromium-based browser. '
                    'See https://doc.qt.io/qt-5/qtwebengine-debugging.html '
                    'This has also been done for you.'.format(port=port))
            super().__init__(parent,
                             sizeHint=QSize(500, 400),
                             sizePolicy=QSizePolicy(QSizePolicy.Expanding,
                                                    QSizePolicy.Expanding),
                             **kwargs)
            self.bridge = bridge
            self.debug = debug
            with open(_WEBVIEW_HELPERS, encoding="utf-8") as f:
                self._onloadJS(f.read(),
                               name='webview_helpers',
                               injection_point=QWebEngineScript.DocumentCreation)

            qtwebchannel_js = QFile("://qtwebchannel/qwebchannel.js")
            if qtwebchannel_js.open(QFile.ReadOnly):
                source = bytes(qtwebchannel_js.readAll()).decode("utf-8")
                with open(_WEBENGINE_INIT_WEBCHANNEL, encoding="utf-8") as f:
                    init_webchannel_src = f.read()
                self._onloadJS(source + init_webchannel_src %
                               dict(exposeObject_prefix=self._EXPOSED_OBJ_PREFIX),
                               name='webchannel_init',
                               injection_point=QWebEngineScript.DocumentCreation)
            else:
                warnings.warn(
                    "://qtwebchannel/qwebchannel.js is not readable.",
                    RuntimeWarning)

            self._onloadJS(';window.__load_finished = true;',
                           name='load_finished',
                           injection_point=QWebEngineScript.DocumentReady)

            channel = QWebChannel(self)
            if bridge is not None:
                if isinstance(bridge, QWidget):
                    warnings.warn(
                        "Don't expose QWidgets in WebView. Construct minimal "
                        "QObjects instead.", OrangeDeprecationWarning,
                        stacklevel=2)
                channel.registerObject("pybridge", bridge)

            channel.registerObject('__bridge', _QWidgetJavaScriptWrapper(self))

            self.page().setWebChannel(channel)
Exemplo n.º 2
0
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            if debug:
                import os

                port = os.environ.setdefault("QTWEBENGINE_REMOTE_DEBUGGING", "12088")
                warnings.warn(
                    "To debug QWebEngineView, set environment variable "
                    "QTWEBENGINE_REMOTE_DEBUGGING={port} and then visit "
                    "http://localhost:{port}/ in a Chromium-based browser. "
                    "See https://doc.qt.io/qt-5/qtwebengine-debugging.html "
                    "This has also been done for you.".format(port=port)
                )
            super().__init__(
                parent,
                sizeHint=QSize(500, 400),
                sizePolicy=QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding),
                **kwargs
            )
            self.bridge = bridge
            self.debug = debug

            self._onloadJS(
                open(_WEBVIEW_HELPERS).read(), name="webview_helpers", injection_point=QWebEngineScript.DocumentCreation
            )

            qtwebchannel_js = QFile("://qtwebchannel/qwebchannel.js")
            if qtwebchannel_js.open(QFile.ReadOnly):
                source = bytes(qtwebchannel_js.readAll()).decode("utf-8")
                self._onloadJS(
                    source
                    + open(_WEBENGINE_INIT_WEBCHANNEL).read() % dict(exposeObject_prefix=self._EXPOSED_OBJ_PREFIX),
                    name="webchannel_init",
                    injection_point=QWebEngineScript.DocumentCreation,
                )
            else:
                warnings.warn("://qtwebchannel/qwebchannel.js is not readable.", RuntimeWarning)

            self._onloadJS(
                ";window.__load_finished = true;", name="load_finished", injection_point=QWebEngineScript.DocumentReady
            )

            channel = QWebChannel(self)
            if bridge is not None:
                channel.registerObject("pybridge", bridge)
            channel.registerObject("__self", self)  # Subclasses rely in this
            self.page().setWebChannel(channel)
Exemplo n.º 3
0
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            debug = debug or _ORANGE_DEBUG
            if debug:
                port = os.environ.setdefault('QTWEBENGINE_REMOTE_DEBUGGING', '12088')
                warnings.warn(
                    'To debug QWebEngineView, set environment variable '
                    'QTWEBENGINE_REMOTE_DEBUGGING={port} and then visit '
                    'http://127.0.0.1:{port}/ in a Chromium-based browser. '
                    'See https://doc.qt.io/qt-5/qtwebengine-debugging.html '
                    'This has also been done for you.'.format(port=port))
            super().__init__(parent,
                             sizeHint=QSize(500, 400),
                             sizePolicy=QSizePolicy(QSizePolicy.Expanding,
                                                    QSizePolicy.Expanding),
                             visible=False,
                             **kwargs)
            self.bridge = bridge
            self.debug = debug
            with open(_WEBVIEW_HELPERS, encoding="utf-8") as f:
                self._onloadJS(f.read(),
                               name='webview_helpers',
                               injection_point=QWebEngineScript.DocumentCreation)

            qtwebchannel_js = QFile("://qtwebchannel/qwebchannel.js")
            if qtwebchannel_js.open(QFile.ReadOnly):
                source = bytes(qtwebchannel_js.readAll()).decode("utf-8")
                with open(_WEBENGINE_INIT_WEBCHANNEL, encoding="utf-8") as f:
                    init_webchannel_src = f.read()
                self._onloadJS(source + init_webchannel_src %
                               dict(exposeObject_prefix=self._EXPOSED_OBJ_PREFIX),
                               name='webchannel_init',
                               injection_point=QWebEngineScript.DocumentCreation)
            else:
                warnings.warn(
                    "://qtwebchannel/qwebchannel.js is not readable.",
                    RuntimeWarning)

            self._onloadJS(';window.__load_finished = true;',
                           name='load_finished',
                           injection_point=QWebEngineScript.DocumentReady)

            channel = QWebChannel(self)
            if bridge is not None:
                if isinstance(bridge, QWidget):
                    warnings.warn(
                        "Don't expose QWidgets in WebView. Construct minimal "
                        "QObjects instead.", OrangeDeprecationWarning,
                        stacklevel=2)
                channel.registerObject("pybridge", bridge)

            channel.registerObject('__bridge', _QWidgetJavaScriptWrapper(self))

            self.page().setWebChannel(channel)

            # Delay showing the widget. Observation indicate this results in
            # fewer window resizes.
            QTimer.singleShot(1, self.show)
Exemplo n.º 4
0
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            if debug:
                import os
                port = os.environ.setdefault('QTWEBENGINE_REMOTE_DEBUGGING', '12088')
                warnings.warn(
                    'To debug QWebEngineView, set environment variable '
                    'QTWEBENGINE_REMOTE_DEBUGGING={port} and then visit '
                    'http://localhost:{port}/ in a Chromium-based browser. '
                    'See https://doc.qt.io/qt-5/qtwebengine-debugging.html '
                    'This has also been done for you.'.format(port=port))
            super().__init__(parent,
                             sizeHint=QSize(500, 400),
                             sizePolicy=QSizePolicy(QSizePolicy.Expanding,
                                                    QSizePolicy.Expanding),
                             **kwargs)
            self.bridge = bridge
            self.debug = debug
            with open(_WEBVIEW_HELPERS, encoding="utf-8") as f:
                self._onloadJS(f.read(),
                               name='webview_helpers',
                               injection_point=QWebEngineScript.DocumentCreation)

            qtwebchannel_js = QFile("://qtwebchannel/qwebchannel.js")
            if qtwebchannel_js.open(QFile.ReadOnly):
                source = bytes(qtwebchannel_js.readAll()).decode("utf-8")
                with open(_WEBENGINE_INIT_WEBCHANNEL, encoding="utf-8") as f:
                    init_webchannel_src = f.read()
                self._onloadJS(source + init_webchannel_src %
                                   dict(exposeObject_prefix=self._EXPOSED_OBJ_PREFIX),
                               name='webchannel_init',
                               injection_point=QWebEngineScript.DocumentCreation)
            else:
                warnings.warn(
                    "://qtwebchannel/qwebchannel.js is not readable.",
                    RuntimeWarning)

            self._onloadJS(';window.__load_finished = true;',
                           name='load_finished',
                           injection_point=QWebEngineScript.DocumentReady)

            channel = QWebChannel(self)
            if bridge is not None:
                channel.registerObject("pybridge", bridge)
            channel.registerObject('__self', self)  # Subclasses rely in this
            self.page().setWebChannel(channel)