xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/simplebrowser/tabwidget.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "tabwidget.h"
52 #include "webpage.h"
53 #include "webview.h"
54 #include <QLabel>
55 #include <QMenu>
56 #include <QTabBar>
57 #include <QWebEngineProfile>
58 
TabWidget(QWebEngineProfile * profile,QWidget * parent)59 TabWidget::TabWidget(QWebEngineProfile *profile, QWidget *parent)
60     : QTabWidget(parent)
61     , m_profile(profile)
62 {
63     QTabBar *tabBar = this->tabBar();
64     tabBar->setTabsClosable(true);
65     tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
66     tabBar->setMovable(true);
67     tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
68     connect(tabBar, &QTabBar::customContextMenuRequested, this, &TabWidget::handleContextMenuRequested);
69     connect(tabBar, &QTabBar::tabCloseRequested, this, &TabWidget::closeTab);
70     connect(tabBar, &QTabBar::tabBarDoubleClicked, [this](int index) {
71         if (index == -1)
72             createTab();
73     });
74 
75     setDocumentMode(true);
76     setElideMode(Qt::ElideRight);
77 
78     connect(this, &QTabWidget::currentChanged, this, &TabWidget::handleCurrentChanged);
79 
80     if (profile->isOffTheRecord()) {
81         QLabel *icon = new QLabel(this);
82         QPixmap pixmap(QStringLiteral(":ninja.png"));
83         icon->setPixmap(pixmap.scaledToHeight(tabBar->height()));
84         setStyleSheet(QStringLiteral("QTabWidget::tab-bar { left: %1px; }").
85                       arg(icon->pixmap()->width()));
86     }
87 }
88 
handleCurrentChanged(int index)89 void TabWidget::handleCurrentChanged(int index)
90 {
91     if (index != -1) {
92         WebView *view = webView(index);
93         if (!view->url().isEmpty())
94             view->setFocus();
95         emit titleChanged(view->title());
96         emit loadProgress(view->loadProgress());
97         emit urlChanged(view->url());
98         emit favIconChanged(view->favIcon());
99         emit webActionEnabledChanged(QWebEnginePage::Back, view->isWebActionEnabled(QWebEnginePage::Back));
100         emit webActionEnabledChanged(QWebEnginePage::Forward, view->isWebActionEnabled(QWebEnginePage::Forward));
101         emit webActionEnabledChanged(QWebEnginePage::Stop, view->isWebActionEnabled(QWebEnginePage::Stop));
102         emit webActionEnabledChanged(QWebEnginePage::Reload,view->isWebActionEnabled(QWebEnginePage::Reload));
103     } else {
104         emit titleChanged(QString());
105         emit loadProgress(0);
106         emit urlChanged(QUrl());
107         emit favIconChanged(QIcon());
108         emit webActionEnabledChanged(QWebEnginePage::Back, false);
109         emit webActionEnabledChanged(QWebEnginePage::Forward, false);
110         emit webActionEnabledChanged(QWebEnginePage::Stop, false);
111         emit webActionEnabledChanged(QWebEnginePage::Reload, true);
112     }
113 }
114 
handleContextMenuRequested(const QPoint & pos)115 void TabWidget::handleContextMenuRequested(const QPoint &pos)
116 {
117     QMenu menu;
118     menu.addAction(tr("New &Tab"), this, &TabWidget::createTab, QKeySequence::AddTab);
119     int index = tabBar()->tabAt(pos);
120     if (index != -1) {
121         QAction *action = menu.addAction(tr("Clone Tab"));
122         connect(action, &QAction::triggered, this, [this,index]() {
123             cloneTab(index);
124         });
125         menu.addSeparator();
126         action = menu.addAction(tr("&Close Tab"));
127         action->setShortcut(QKeySequence::Close);
128         connect(action, &QAction::triggered, this, [this,index]() {
129             closeTab(index);
130         });
131         action = menu.addAction(tr("Close &Other Tabs"));
132         connect(action, &QAction::triggered, this, [this,index]() {
133             closeOtherTabs(index);
134         });
135         menu.addSeparator();
136         action = menu.addAction(tr("Reload Tab"));
137         action->setShortcut(QKeySequence::Refresh);
138         connect(action, &QAction::triggered, this, [this,index]() {
139             reloadTab(index);
140         });
141     } else {
142         menu.addSeparator();
143     }
144     menu.addAction(tr("Reload All Tabs"), this, &TabWidget::reloadAllTabs);
145     menu.exec(QCursor::pos());
146 }
147 
currentWebView() const148 WebView *TabWidget::currentWebView() const
149 {
150     return webView(currentIndex());
151 }
152 
webView(int index) const153 WebView *TabWidget::webView(int index) const
154 {
155     return qobject_cast<WebView*>(widget(index));
156 }
157 
setupView(WebView * webView)158 void TabWidget::setupView(WebView *webView)
159 {
160     QWebEnginePage *webPage = webView->page();
161 
162     connect(webView, &QWebEngineView::titleChanged, [this, webView](const QString &title) {
163         int index = indexOf(webView);
164         if (index != -1) {
165             setTabText(index, title);
166             setTabToolTip(index, title);
167         }
168         if (currentIndex() == index)
169             emit titleChanged(title);
170     });
171     connect(webView, &QWebEngineView::urlChanged, [this, webView](const QUrl &url) {
172         int index = indexOf(webView);
173         if (index != -1)
174             tabBar()->setTabData(index, url);
175         if (currentIndex() == index)
176             emit urlChanged(url);
177     });
178     connect(webView, &QWebEngineView::loadProgress, [this, webView](int progress) {
179         if (currentIndex() == indexOf(webView))
180             emit loadProgress(progress);
181     });
182     connect(webPage, &QWebEnginePage::linkHovered, [this, webView](const QString &url) {
183         if (currentIndex() == indexOf(webView))
184             emit linkHovered(url);
185     });
186     connect(webView, &WebView::favIconChanged, [this, webView](const QIcon &icon) {
187         int index = indexOf(webView);
188         if (index != -1)
189             setTabIcon(index, icon);
190         if (currentIndex() == index)
191             emit favIconChanged(icon);
192     });
193     connect(webView, &WebView::webActionEnabledChanged, [this, webView](QWebEnginePage::WebAction action, bool enabled) {
194         if (currentIndex() ==  indexOf(webView))
195             emit webActionEnabledChanged(action,enabled);
196     });
197     connect(webPage, &QWebEnginePage::windowCloseRequested, [this, webView]() {
198         int index = indexOf(webView);
199         if (index >= 0)
200             closeTab(index);
201     });
202     connect(webView, &WebView::devToolsRequested, this, &TabWidget::devToolsRequested);
203 }
204 
createTab()205 WebView *TabWidget::createTab()
206 {
207     WebView *webView = createBackgroundTab();
208     setCurrentWidget(webView);
209     return webView;
210 }
211 
createBackgroundTab()212 WebView *TabWidget::createBackgroundTab()
213 {
214     WebView *webView = new WebView;
215     WebPage *webPage = new WebPage(m_profile, webView);
216     webView->setPage(webPage);
217     setupView(webView);
218     int index = addTab(webView, tr("(Untitled)"));
219     setTabIcon(index, webView->favIcon());
220     // Workaround for QTBUG-61770
221     webView->resize(currentWidget()->size());
222     webView->show();
223     return webView;
224 }
225 
reloadAllTabs()226 void TabWidget::reloadAllTabs()
227 {
228     for (int i = 0; i < count(); ++i)
229         webView(i)->reload();
230 }
231 
closeOtherTabs(int index)232 void TabWidget::closeOtherTabs(int index)
233 {
234     for (int i = count() - 1; i > index; --i)
235         closeTab(i);
236     for (int i = index - 1; i >= 0; --i)
237         closeTab(i);
238 }
239 
closeTab(int index)240 void TabWidget::closeTab(int index)
241 {
242     if (WebView *view = webView(index)) {
243         bool hasFocus = view->hasFocus();
244         removeTab(index);
245         if (hasFocus && count() > 0)
246             currentWebView()->setFocus();
247         if (count() == 0)
248             createTab();
249         view->deleteLater();
250     }
251 }
252 
cloneTab(int index)253 void TabWidget::cloneTab(int index)
254 {
255     if (WebView *view = webView(index)) {
256         WebView *tab = createTab();
257         tab->setUrl(view->url());
258     }
259 }
260 
setUrl(const QUrl & url)261 void TabWidget::setUrl(const QUrl &url)
262 {
263     if (WebView *view = currentWebView()) {
264         view->setUrl(url);
265         view->setFocus();
266     }
267 }
268 
triggerWebPageAction(QWebEnginePage::WebAction action)269 void TabWidget::triggerWebPageAction(QWebEnginePage::WebAction action)
270 {
271     if (WebView *webView = currentWebView()) {
272         webView->triggerPageAction(action);
273         webView->setFocus();
274     }
275 }
276 
nextTab()277 void TabWidget::nextTab()
278 {
279     int next = currentIndex() + 1;
280     if (next == count())
281         next = 0;
282     setCurrentIndex(next);
283 }
284 
previousTab()285 void TabWidget::previousTab()
286 {
287     int next = currentIndex() - 1;
288     if (next < 0)
289         next = count() - 1;
290     setCurrentIndex(next);
291 }
292 
reloadTab(int index)293 void TabWidget::reloadTab(int index)
294 {
295     if (WebView *view = webView(index))
296         view->reload();
297 }
298