Ejemplo n.º 1
0
    def set_viewport(self, size, raise_if_empty=False):
        """
        Set viewport size.
        If size is "full" viewport size is detected automatically.
        If can also be "<width>x<height>".

        .. note::

           This will update all JS geometry variables, but window resize event
           is delivered asynchronously and so ``window.resize`` will not be
           invoked until control is yielded to the event loop.

        """
        if size == 'full':
            size = self.web_page.mainFrame().contentsSize()
            self.logger.log("Contents size: %s" % size, min_level=2)
            if size.isEmpty():
                if raise_if_empty:
                    raise RuntimeError("Cannot detect viewport size")
                else:
                    size = defaults.VIEWPORT_SIZE
                    self.logger.log("Viewport is empty, falling back to: %s" %
                                    size)

        if not isinstance(size, QSize):
            validate_size_str(size)
            size = parse_size(size)
        self.web_page.setViewportSize(size)
        self._force_relayout()
        w, h = int(size.width()), int(size.height())
        self.logger.log("viewport size is set to %sx%s" % (w, h), min_level=2)
        return w, h
Ejemplo n.º 2
0
    def set_viewport(self, size, raise_if_empty=False):
        """
        Set viewport size.
        If size is "full" viewport size is detected automatically.
        If can also be "<width>x<height>".

        FIXME: Currently the implementation just resizes the window, which
        causes Splash to crash on large sizes(?).
        Actully it is not changing the viewport.

        XXX: As an effect, this function changes window.outerWidth/outerHeight,
        while in Webkit implementation window.innerWidth/innerHeight
        is changed.
        """
        if size == 'full':
            size = self.web_page.contentsSize()
            self.logger.log("Contents size: %s" % size, min_level=2)
            if size.isEmpty():
                if raise_if_empty:
                    raise RuntimeError("Cannot detect viewport size")
                else:
                    size = defaults.VIEWPORT_SIZE
                    self.logger.log("Viewport is empty, falling back to: %s" %
                                    size)

        if not isinstance(size, (QSize, QSizeF)):
            validate_size_str(size)
            size = parse_size(size)
        w, h = int(size.width()), int(size.height())

        # XXX: it was crashing with large windows, but then the problem
        # seemed to go away. Need to keep an eye on it.
        # # FIXME: don't resize the window?
        # # FIXME: figure out exact limits
        # MAX_WIDTH = 1280
        # MAX_HEIGHT = 1920
        #
        # if w > MAX_WIDTH:
        #     raise RuntimeError("Width {} > {} is currently prohibited".format(
        #         w, MAX_WIDTH
        #     ))
        #
        # if h > MAX_HEIGHT:
        #     raise RuntimeError("Height {} > {} is currently prohibited".format(
        #         h, MAX_HEIGHT
        #     ))
        self.web_view.resize(w, h)

        # self._force_relayout()
        self.logger.log("viewport size is set to %sx%s" % (w, h), min_level=2)
        self.logger.log("real viewport size: %s" % self.web_view.size(), min_level=2)
        return w, h
Ejemplo n.º 3
0
    def _init_webpage(self, verbosity, network_manager, splash_proxy_factory,
                      render_options):
        """ Create and initialize QWebPage and QWebView """
        self.web_page = WebkitWebPage(verbosity)
        self.web_page.setNetworkAccessManager(network_manager)
        self.web_page.splash_proxy_factory = splash_proxy_factory
        self.web_page.render_options = render_options

        self._set_default_webpage_options(self.web_page)
        self._setup_webpage_events()

        self.web_view = SplashQWebView()
        self.web_view.setPage(self.web_page)
        self.web_view.setAttribute(Qt.WA_DeleteOnClose, True)
        self.web_view.onBeforeClose = self._on_before_close

        if self.visible:
            self.web_view.move(0, 0)
            self.web_view.show()

        self.set_viewport(defaults.VIEWPORT_SIZE)
        # XXX: hack to ensure that default window size is not 640x480.
        self.web_view.resize(parse_size(defaults.VIEWPORT_SIZE))