xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/simplebrowser/tabwidget.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 "tabwidget.h"
52*4882a593Smuzhiyun #include "webpage.h"
53*4882a593Smuzhiyun #include "webview.h"
54*4882a593Smuzhiyun #include <QLabel>
55*4882a593Smuzhiyun #include <QMenu>
56*4882a593Smuzhiyun #include <QTabBar>
57*4882a593Smuzhiyun #include <QWebEngineProfile>
58*4882a593Smuzhiyun 
TabWidget(QWebEngineProfile * profile,QWidget * parent)59*4882a593Smuzhiyun TabWidget::TabWidget(QWebEngineProfile *profile, QWidget *parent)
60*4882a593Smuzhiyun     : QTabWidget(parent)
61*4882a593Smuzhiyun     , m_profile(profile)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun     QTabBar *tabBar = this->tabBar();
64*4882a593Smuzhiyun     tabBar->setTabsClosable(true);
65*4882a593Smuzhiyun     tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
66*4882a593Smuzhiyun     tabBar->setMovable(true);
67*4882a593Smuzhiyun     tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
68*4882a593Smuzhiyun     connect(tabBar, &QTabBar::customContextMenuRequested, this, &TabWidget::handleContextMenuRequested);
69*4882a593Smuzhiyun     connect(tabBar, &QTabBar::tabCloseRequested, this, &TabWidget::closeTab);
70*4882a593Smuzhiyun     connect(tabBar, &QTabBar::tabBarDoubleClicked, [this](int index) {
71*4882a593Smuzhiyun         if (index == -1)
72*4882a593Smuzhiyun             createTab();
73*4882a593Smuzhiyun     });
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun     setDocumentMode(true);
76*4882a593Smuzhiyun     setElideMode(Qt::ElideRight);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun     connect(this, &QTabWidget::currentChanged, this, &TabWidget::handleCurrentChanged);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun     if (profile->isOffTheRecord()) {
81*4882a593Smuzhiyun         QLabel *icon = new QLabel(this);
82*4882a593Smuzhiyun         QPixmap pixmap(QStringLiteral(":ninja.png"));
83*4882a593Smuzhiyun         icon->setPixmap(pixmap.scaledToHeight(tabBar->height()));
84*4882a593Smuzhiyun         setStyleSheet(QStringLiteral("QTabWidget::tab-bar { left: %1px; }").
85*4882a593Smuzhiyun                       arg(icon->pixmap()->width()));
86*4882a593Smuzhiyun     }
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun 
handleCurrentChanged(int index)89*4882a593Smuzhiyun void TabWidget::handleCurrentChanged(int index)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun     if (index != -1) {
92*4882a593Smuzhiyun         WebView *view = webView(index);
93*4882a593Smuzhiyun         if (!view->url().isEmpty())
94*4882a593Smuzhiyun             view->setFocus();
95*4882a593Smuzhiyun         emit titleChanged(view->title());
96*4882a593Smuzhiyun         emit loadProgress(view->loadProgress());
97*4882a593Smuzhiyun         emit urlChanged(view->url());
98*4882a593Smuzhiyun         emit favIconChanged(view->favIcon());
99*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Back, view->isWebActionEnabled(QWebEnginePage::Back));
100*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Forward, view->isWebActionEnabled(QWebEnginePage::Forward));
101*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Stop, view->isWebActionEnabled(QWebEnginePage::Stop));
102*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Reload,view->isWebActionEnabled(QWebEnginePage::Reload));
103*4882a593Smuzhiyun     } else {
104*4882a593Smuzhiyun         emit titleChanged(QString());
105*4882a593Smuzhiyun         emit loadProgress(0);
106*4882a593Smuzhiyun         emit urlChanged(QUrl());
107*4882a593Smuzhiyun         emit favIconChanged(QIcon());
108*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Back, false);
109*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Forward, false);
110*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Stop, false);
111*4882a593Smuzhiyun         emit webActionEnabledChanged(QWebEnginePage::Reload, true);
112*4882a593Smuzhiyun     }
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
handleContextMenuRequested(const QPoint & pos)115*4882a593Smuzhiyun void TabWidget::handleContextMenuRequested(const QPoint &pos)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun     QMenu menu;
118*4882a593Smuzhiyun     menu.addAction(tr("New &Tab"), this, &TabWidget::createTab, QKeySequence::AddTab);
119*4882a593Smuzhiyun     int index = tabBar()->tabAt(pos);
120*4882a593Smuzhiyun     if (index != -1) {
121*4882a593Smuzhiyun         QAction *action = menu.addAction(tr("Clone Tab"));
122*4882a593Smuzhiyun         connect(action, &QAction::triggered, this, [this,index]() {
123*4882a593Smuzhiyun             cloneTab(index);
124*4882a593Smuzhiyun         });
125*4882a593Smuzhiyun         menu.addSeparator();
126*4882a593Smuzhiyun         action = menu.addAction(tr("&Close Tab"));
127*4882a593Smuzhiyun         action->setShortcut(QKeySequence::Close);
128*4882a593Smuzhiyun         connect(action, &QAction::triggered, this, [this,index]() {
129*4882a593Smuzhiyun             closeTab(index);
130*4882a593Smuzhiyun         });
131*4882a593Smuzhiyun         action = menu.addAction(tr("Close &Other Tabs"));
132*4882a593Smuzhiyun         connect(action, &QAction::triggered, this, [this,index]() {
133*4882a593Smuzhiyun             closeOtherTabs(index);
134*4882a593Smuzhiyun         });
135*4882a593Smuzhiyun         menu.addSeparator();
136*4882a593Smuzhiyun         action = menu.addAction(tr("Reload Tab"));
137*4882a593Smuzhiyun         action->setShortcut(QKeySequence::Refresh);
138*4882a593Smuzhiyun         connect(action, &QAction::triggered, this, [this,index]() {
139*4882a593Smuzhiyun             reloadTab(index);
140*4882a593Smuzhiyun         });
141*4882a593Smuzhiyun     } else {
142*4882a593Smuzhiyun         menu.addSeparator();
143*4882a593Smuzhiyun     }
144*4882a593Smuzhiyun     menu.addAction(tr("Reload All Tabs"), this, &TabWidget::reloadAllTabs);
145*4882a593Smuzhiyun     menu.exec(QCursor::pos());
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
currentWebView() const148*4882a593Smuzhiyun WebView *TabWidget::currentWebView() const
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun     return webView(currentIndex());
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
webView(int index) const153*4882a593Smuzhiyun WebView *TabWidget::webView(int index) const
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun     return qobject_cast<WebView*>(widget(index));
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
setupView(WebView * webView)158*4882a593Smuzhiyun void TabWidget::setupView(WebView *webView)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun     QWebEnginePage *webPage = webView->page();
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     connect(webView, &QWebEngineView::titleChanged, [this, webView](const QString &title) {
163*4882a593Smuzhiyun         int index = indexOf(webView);
164*4882a593Smuzhiyun         if (index != -1) {
165*4882a593Smuzhiyun             setTabText(index, title);
166*4882a593Smuzhiyun             setTabToolTip(index, title);
167*4882a593Smuzhiyun         }
168*4882a593Smuzhiyun         if (currentIndex() == index)
169*4882a593Smuzhiyun             emit titleChanged(title);
170*4882a593Smuzhiyun     });
171*4882a593Smuzhiyun     connect(webView, &QWebEngineView::urlChanged, [this, webView](const QUrl &url) {
172*4882a593Smuzhiyun         int index = indexOf(webView);
173*4882a593Smuzhiyun         if (index != -1)
174*4882a593Smuzhiyun             tabBar()->setTabData(index, url);
175*4882a593Smuzhiyun         if (currentIndex() == index)
176*4882a593Smuzhiyun             emit urlChanged(url);
177*4882a593Smuzhiyun     });
178*4882a593Smuzhiyun     connect(webView, &QWebEngineView::loadProgress, [this, webView](int progress) {
179*4882a593Smuzhiyun         if (currentIndex() == indexOf(webView))
180*4882a593Smuzhiyun             emit loadProgress(progress);
181*4882a593Smuzhiyun     });
182*4882a593Smuzhiyun     connect(webPage, &QWebEnginePage::linkHovered, [this, webView](const QString &url) {
183*4882a593Smuzhiyun         if (currentIndex() == indexOf(webView))
184*4882a593Smuzhiyun             emit linkHovered(url);
185*4882a593Smuzhiyun     });
186*4882a593Smuzhiyun     connect(webView, &WebView::favIconChanged, [this, webView](const QIcon &icon) {
187*4882a593Smuzhiyun         int index = indexOf(webView);
188*4882a593Smuzhiyun         if (index != -1)
189*4882a593Smuzhiyun             setTabIcon(index, icon);
190*4882a593Smuzhiyun         if (currentIndex() == index)
191*4882a593Smuzhiyun             emit favIconChanged(icon);
192*4882a593Smuzhiyun     });
193*4882a593Smuzhiyun     connect(webView, &WebView::webActionEnabledChanged, [this, webView](QWebEnginePage::WebAction action, bool enabled) {
194*4882a593Smuzhiyun         if (currentIndex() ==  indexOf(webView))
195*4882a593Smuzhiyun             emit webActionEnabledChanged(action,enabled);
196*4882a593Smuzhiyun     });
197*4882a593Smuzhiyun     connect(webPage, &QWebEnginePage::windowCloseRequested, [this, webView]() {
198*4882a593Smuzhiyun         int index = indexOf(webView);
199*4882a593Smuzhiyun         if (index >= 0)
200*4882a593Smuzhiyun             closeTab(index);
201*4882a593Smuzhiyun     });
202*4882a593Smuzhiyun     connect(webView, &WebView::devToolsRequested, this, &TabWidget::devToolsRequested);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
createTab()205*4882a593Smuzhiyun WebView *TabWidget::createTab()
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun     WebView *webView = createBackgroundTab();
208*4882a593Smuzhiyun     setCurrentWidget(webView);
209*4882a593Smuzhiyun     return webView;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
createBackgroundTab()212*4882a593Smuzhiyun WebView *TabWidget::createBackgroundTab()
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun     WebView *webView = new WebView;
215*4882a593Smuzhiyun     WebPage *webPage = new WebPage(m_profile, webView);
216*4882a593Smuzhiyun     webView->setPage(webPage);
217*4882a593Smuzhiyun     setupView(webView);
218*4882a593Smuzhiyun     int index = addTab(webView, tr("(Untitled)"));
219*4882a593Smuzhiyun     setTabIcon(index, webView->favIcon());
220*4882a593Smuzhiyun     // Workaround for QTBUG-61770
221*4882a593Smuzhiyun     webView->resize(currentWidget()->size());
222*4882a593Smuzhiyun     webView->show();
223*4882a593Smuzhiyun     return webView;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
reloadAllTabs()226*4882a593Smuzhiyun void TabWidget::reloadAllTabs()
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun     for (int i = 0; i < count(); ++i)
229*4882a593Smuzhiyun         webView(i)->reload();
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
closeOtherTabs(int index)232*4882a593Smuzhiyun void TabWidget::closeOtherTabs(int index)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun     for (int i = count() - 1; i > index; --i)
235*4882a593Smuzhiyun         closeTab(i);
236*4882a593Smuzhiyun     for (int i = index - 1; i >= 0; --i)
237*4882a593Smuzhiyun         closeTab(i);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
closeTab(int index)240*4882a593Smuzhiyun void TabWidget::closeTab(int index)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun     if (WebView *view = webView(index)) {
243*4882a593Smuzhiyun         bool hasFocus = view->hasFocus();
244*4882a593Smuzhiyun         removeTab(index);
245*4882a593Smuzhiyun         if (hasFocus && count() > 0)
246*4882a593Smuzhiyun             currentWebView()->setFocus();
247*4882a593Smuzhiyun         if (count() == 0)
248*4882a593Smuzhiyun             createTab();
249*4882a593Smuzhiyun         view->deleteLater();
250*4882a593Smuzhiyun     }
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
cloneTab(int index)253*4882a593Smuzhiyun void TabWidget::cloneTab(int index)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun     if (WebView *view = webView(index)) {
256*4882a593Smuzhiyun         WebView *tab = createTab();
257*4882a593Smuzhiyun         tab->setUrl(view->url());
258*4882a593Smuzhiyun     }
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
setUrl(const QUrl & url)261*4882a593Smuzhiyun void TabWidget::setUrl(const QUrl &url)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun     if (WebView *view = currentWebView()) {
264*4882a593Smuzhiyun         view->setUrl(url);
265*4882a593Smuzhiyun         view->setFocus();
266*4882a593Smuzhiyun     }
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
triggerWebPageAction(QWebEnginePage::WebAction action)269*4882a593Smuzhiyun void TabWidget::triggerWebPageAction(QWebEnginePage::WebAction action)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun     if (WebView *webView = currentWebView()) {
272*4882a593Smuzhiyun         webView->triggerPageAction(action);
273*4882a593Smuzhiyun         webView->setFocus();
274*4882a593Smuzhiyun     }
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
nextTab()277*4882a593Smuzhiyun void TabWidget::nextTab()
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun     int next = currentIndex() + 1;
280*4882a593Smuzhiyun     if (next == count())
281*4882a593Smuzhiyun         next = 0;
282*4882a593Smuzhiyun     setCurrentIndex(next);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
previousTab()285*4882a593Smuzhiyun void TabWidget::previousTab()
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun     int next = currentIndex() - 1;
288*4882a593Smuzhiyun     if (next < 0)
289*4882a593Smuzhiyun         next = count() - 1;
290*4882a593Smuzhiyun     setCurrentIndex(next);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
reloadTab(int index)293*4882a593Smuzhiyun void TabWidget::reloadTab(int index)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun     if (WebView *view = webView(index))
296*4882a593Smuzhiyun         view->reload();
297*4882a593Smuzhiyun }
298