1#############################################################################
2##
3## Copyright (C) 2018 The Qt Company Ltd.
4## Contact: http://www.qt.io/licensing/
5##
6## This file is part of the Qt for Python examples of the Qt Toolkit.
7##
8## $QT_BEGIN_LICENSE:BSD$
9## You may use this file under the terms of the BSD license as follows:
10##
11## "Redistribution and use in source and binary forms, with or without
12## modification, are permitted provided that the following conditions are
13## met:
14##   * Redistributions of source code must retain the above copyright
15##     notice, this list of conditions and the following disclaimer.
16##   * Redistributions in binary form must reproduce the above copyright
17##     notice, this list of conditions and the following disclaimer in
18##     the documentation and/or other materials provided with the
19##     distribution.
20##   * Neither the name of The Qt Company Ltd nor the names of its
21##     contributors may be used to endorse or promote products derived
22##     from this software without specific prior written permission.
23##
24##
25## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36##
37## $QT_END_LICENSE$
38##
39#############################################################################
40
41from PySide2.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
42
43from PySide2 import QtCore
44
45_web_actions = [QWebEnginePage.Back, QWebEnginePage.Forward,
46                QWebEnginePage.Reload,
47                QWebEnginePage.Undo, QWebEnginePage.Redo,
48                QWebEnginePage.Cut, QWebEnginePage.Copy,
49                QWebEnginePage.Paste, QWebEnginePage.SelectAll]
50
51
52class WebEngineView(QWebEngineView):
53
54    enabled_changed = QtCore.Signal(QWebEnginePage.WebAction, bool)
55
56    @staticmethod
57    def web_actions():
58        return _web_actions
59
60    @staticmethod
61    def minimum_zoom_factor():
62        return 0.25
63
64    @staticmethod
65    def maximum_zoom_factor():
66        return 5
67
68    def __init__(self, tab_factory_func, window_factory_func):
69        super(WebEngineView, self).__init__()
70        self._tab_factory_func = tab_factory_func
71        self._window_factory_func = window_factory_func
72        page = self.page()
73        self._actions = {}
74        for web_action in WebEngineView.web_actions():
75            action = page.action(web_action)
76            action.changed.connect(self._enabled_changed)
77            self._actions[action] = web_action
78
79    def is_web_action_enabled(self, web_action):
80        return self.page().action(web_action).isEnabled()
81
82    def createWindow(self, window_type):
83        if (window_type == QWebEnginePage.WebBrowserTab or
84            window_type == QWebEnginePage.WebBrowserBackgroundTab):
85            return self._tab_factory_func()
86        return self._window_factory_func()
87
88    def _enabled_changed(self):
89        action = self.sender()
90        web_action = self._actions[action]
91        self.enabled_changed.emit(web_action, action.isEnabled())
92