xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/simplebrowser/webview.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun **
3*4882a593Smuzhiyun ** Copyright (C) 2016 The Qt Company Ltd.
4*4882a593Smuzhiyun ** Contact: https://www.qt.io/licensing/
5*4882a593Smuzhiyun **
6*4882a593Smuzhiyun ** This file is part of the examples of the Qt Toolkit.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun ** $QT_BEGIN_LICENSE:BSD$
9*4882a593Smuzhiyun ** Commercial License Usage
10*4882a593Smuzhiyun ** Licensees holding valid commercial Qt licenses may use this file in
11*4882a593Smuzhiyun ** accordance with the commercial license agreement provided with the
12*4882a593Smuzhiyun ** Software or, alternatively, in accordance with the terms contained in
13*4882a593Smuzhiyun ** a written agreement between you and The Qt Company. For licensing terms
14*4882a593Smuzhiyun ** and conditions see https://www.qt.io/terms-conditions. For further
15*4882a593Smuzhiyun ** information use the contact form at https://www.qt.io/contact-us.
16*4882a593Smuzhiyun **
17*4882a593Smuzhiyun ** BSD License Usage
18*4882a593Smuzhiyun ** Alternatively, you may use this file under the terms of the BSD license
19*4882a593Smuzhiyun ** as follows:
20*4882a593Smuzhiyun **
21*4882a593Smuzhiyun ** "Redistribution and use in source and binary forms, with or without
22*4882a593Smuzhiyun ** modification, are permitted provided that the following conditions are
23*4882a593Smuzhiyun ** met:
24*4882a593Smuzhiyun **   * Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun **   * Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun **     the documentation and/or other materials provided with the
29*4882a593Smuzhiyun **     distribution.
30*4882a593Smuzhiyun **   * Neither the name of The Qt Company Ltd nor the names of its
31*4882a593Smuzhiyun **     contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun **     from this software without specific prior written permission.
33*4882a593Smuzhiyun **
34*4882a593Smuzhiyun **
35*4882a593Smuzhiyun ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*4882a593Smuzhiyun ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*4882a593Smuzhiyun ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*4882a593Smuzhiyun ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*4882a593Smuzhiyun ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*4882a593Smuzhiyun ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*4882a593Smuzhiyun ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*4882a593Smuzhiyun ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*4882a593Smuzhiyun ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46*4882a593Smuzhiyun **
47*4882a593Smuzhiyun ** $QT_END_LICENSE$
48*4882a593Smuzhiyun **
49*4882a593Smuzhiyun ****************************************************************************/
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "browser.h"
52*4882a593Smuzhiyun #include "browserwindow.h"
53*4882a593Smuzhiyun #include "tabwidget.h"
54*4882a593Smuzhiyun #include "webpage.h"
55*4882a593Smuzhiyun #include "webpopupwindow.h"
56*4882a593Smuzhiyun #include "webview.h"
57*4882a593Smuzhiyun #include <QContextMenuEvent>
58*4882a593Smuzhiyun #include <QDebug>
59*4882a593Smuzhiyun #include <QMenu>
60*4882a593Smuzhiyun #include <QMessageBox>
61*4882a593Smuzhiyun #include <QTimer>
62*4882a593Smuzhiyun 
WebView(QWidget * parent)63*4882a593Smuzhiyun WebView::WebView(QWidget *parent)
64*4882a593Smuzhiyun     : QWebEngineView(parent)
65*4882a593Smuzhiyun     , m_loadProgress(100)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun     connect(this, &QWebEngineView::loadStarted, [this]() {
68*4882a593Smuzhiyun         m_loadProgress = 0;
69*4882a593Smuzhiyun         emit favIconChanged(favIcon());
70*4882a593Smuzhiyun     });
71*4882a593Smuzhiyun     connect(this, &QWebEngineView::loadProgress, [this](int progress) {
72*4882a593Smuzhiyun         m_loadProgress = progress;
73*4882a593Smuzhiyun     });
74*4882a593Smuzhiyun     connect(this, &QWebEngineView::loadFinished, [this](bool success) {
75*4882a593Smuzhiyun         m_loadProgress = success ? 100 : -1;
76*4882a593Smuzhiyun         emit favIconChanged(favIcon());
77*4882a593Smuzhiyun     });
78*4882a593Smuzhiyun     connect(this, &QWebEngineView::iconChanged, [this](const QIcon &) {
79*4882a593Smuzhiyun         emit favIconChanged(favIcon());
80*4882a593Smuzhiyun     });
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun     connect(this, &QWebEngineView::renderProcessTerminated,
83*4882a593Smuzhiyun             [this](QWebEnginePage::RenderProcessTerminationStatus termStatus, int statusCode) {
84*4882a593Smuzhiyun         QString status;
85*4882a593Smuzhiyun         switch (termStatus) {
86*4882a593Smuzhiyun         case QWebEnginePage::NormalTerminationStatus:
87*4882a593Smuzhiyun             status = tr("Render process normal exit");
88*4882a593Smuzhiyun             break;
89*4882a593Smuzhiyun         case QWebEnginePage::AbnormalTerminationStatus:
90*4882a593Smuzhiyun             status = tr("Render process abnormal exit");
91*4882a593Smuzhiyun             break;
92*4882a593Smuzhiyun         case QWebEnginePage::CrashedTerminationStatus:
93*4882a593Smuzhiyun             status = tr("Render process crashed");
94*4882a593Smuzhiyun             break;
95*4882a593Smuzhiyun         case QWebEnginePage::KilledTerminationStatus:
96*4882a593Smuzhiyun             status = tr("Render process killed");
97*4882a593Smuzhiyun             break;
98*4882a593Smuzhiyun         }
99*4882a593Smuzhiyun         QMessageBox::StandardButton btn = QMessageBox::question(window(), status,
100*4882a593Smuzhiyun                                                    tr("Render process exited with code: %1\n"
101*4882a593Smuzhiyun                                                       "Do you want to reload the page ?").arg(statusCode));
102*4882a593Smuzhiyun         if (btn == QMessageBox::Yes)
103*4882a593Smuzhiyun             QTimer::singleShot(0, [this] { reload(); });
104*4882a593Smuzhiyun     });
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
setPage(WebPage * page)107*4882a593Smuzhiyun void WebView::setPage(WebPage *page)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun     createWebActionTrigger(page,QWebEnginePage::Forward);
110*4882a593Smuzhiyun     createWebActionTrigger(page,QWebEnginePage::Back);
111*4882a593Smuzhiyun     createWebActionTrigger(page,QWebEnginePage::Reload);
112*4882a593Smuzhiyun     createWebActionTrigger(page,QWebEnginePage::Stop);
113*4882a593Smuzhiyun     QWebEngineView::setPage(page);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
loadProgress() const116*4882a593Smuzhiyun int WebView::loadProgress() const
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun     return m_loadProgress;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
createWebActionTrigger(QWebEnginePage * page,QWebEnginePage::WebAction webAction)121*4882a593Smuzhiyun void WebView::createWebActionTrigger(QWebEnginePage *page, QWebEnginePage::WebAction webAction)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun     QAction *action = page->action(webAction);
124*4882a593Smuzhiyun     connect(action, &QAction::changed, [this, action, webAction]{
125*4882a593Smuzhiyun         emit webActionEnabledChanged(webAction, action->isEnabled());
126*4882a593Smuzhiyun     });
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
isWebActionEnabled(QWebEnginePage::WebAction webAction) const129*4882a593Smuzhiyun bool WebView::isWebActionEnabled(QWebEnginePage::WebAction webAction) const
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun     return page()->action(webAction)->isEnabled();
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
favIcon() const134*4882a593Smuzhiyun QIcon WebView::favIcon() const
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun     QIcon favIcon = icon();
137*4882a593Smuzhiyun     if (!favIcon.isNull())
138*4882a593Smuzhiyun         return favIcon;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun     if (m_loadProgress < 0) {
141*4882a593Smuzhiyun         static QIcon errorIcon(QStringLiteral(":dialog-error.png"));
142*4882a593Smuzhiyun         return errorIcon;
143*4882a593Smuzhiyun     } else if (m_loadProgress < 100) {
144*4882a593Smuzhiyun         static QIcon loadingIcon(QStringLiteral(":view-refresh.png"));
145*4882a593Smuzhiyun         return loadingIcon;
146*4882a593Smuzhiyun     } else {
147*4882a593Smuzhiyun         static QIcon defaultIcon(QStringLiteral(":text-html.png"));
148*4882a593Smuzhiyun         return defaultIcon;
149*4882a593Smuzhiyun     }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
createWindow(QWebEnginePage::WebWindowType type)152*4882a593Smuzhiyun QWebEngineView *WebView::createWindow(QWebEnginePage::WebWindowType type)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun     BrowserWindow *mainWindow = qobject_cast<BrowserWindow*>(window());
155*4882a593Smuzhiyun     if (!mainWindow)
156*4882a593Smuzhiyun         return nullptr;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun     switch (type) {
159*4882a593Smuzhiyun     case QWebEnginePage::WebBrowserTab: {
160*4882a593Smuzhiyun         return mainWindow->tabWidget()->createTab();
161*4882a593Smuzhiyun     }
162*4882a593Smuzhiyun     case QWebEnginePage::WebBrowserBackgroundTab: {
163*4882a593Smuzhiyun         return mainWindow->tabWidget()->createBackgroundTab();
164*4882a593Smuzhiyun     }
165*4882a593Smuzhiyun     case QWebEnginePage::WebBrowserWindow: {
166*4882a593Smuzhiyun         return mainWindow->browser()->createWindow()->currentTab();
167*4882a593Smuzhiyun     }
168*4882a593Smuzhiyun     case QWebEnginePage::WebDialog: {
169*4882a593Smuzhiyun         WebPopupWindow *popup = new WebPopupWindow(page()->profile());
170*4882a593Smuzhiyun         connect(popup->view(), &WebView::devToolsRequested, this, &WebView::devToolsRequested);
171*4882a593Smuzhiyun         return popup->view();
172*4882a593Smuzhiyun     }
173*4882a593Smuzhiyun     }
174*4882a593Smuzhiyun     return nullptr;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
contextMenuEvent(QContextMenuEvent * event)177*4882a593Smuzhiyun void WebView::contextMenuEvent(QContextMenuEvent *event)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun     QMenu *menu = page()->createStandardContextMenu();
180*4882a593Smuzhiyun     const QList<QAction *> actions = menu->actions();
181*4882a593Smuzhiyun     auto inspectElement = std::find(actions.cbegin(), actions.cend(), page()->action(QWebEnginePage::InspectElement));
182*4882a593Smuzhiyun     if (inspectElement == actions.cend()) {
183*4882a593Smuzhiyun         auto viewSource = std::find(actions.cbegin(), actions.cend(), page()->action(QWebEnginePage::ViewSource));
184*4882a593Smuzhiyun         if (viewSource == actions.cend())
185*4882a593Smuzhiyun             menu->addSeparator();
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun         QAction *action = new QAction(menu);
188*4882a593Smuzhiyun         action->setText("Open inspector in new window");
189*4882a593Smuzhiyun         connect(action, &QAction::triggered, [this]() { emit devToolsRequested(page()); });
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun         QAction *before(inspectElement == actions.cend() ? nullptr : *inspectElement);
192*4882a593Smuzhiyun         menu->insertAction(before, action);
193*4882a593Smuzhiyun     } else {
194*4882a593Smuzhiyun         (*inspectElement)->setText(tr("Inspect element"));
195*4882a593Smuzhiyun     }
196*4882a593Smuzhiyun     menu->popup(event->globalPos());
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199