Falkon Develop
Cross-platform Qt-based web browser
tabbedwebview.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "tabbedwebview.h"
19#include "browserwindow.h"
20#include "webpage.h"
21#include "tabwidget.h"
22#include "mainapplication.h"
23#include "tabbar.h"
24#include "webtab.h"
25#include "statusbar.h"
26#include "progressbar.h"
27#include "navigationbar.h"
28#include "iconprovider.h"
30#include "enhancedmenu.h"
31#include "locationbar.h"
32#include "webhittestresult.h"
33#include "webinspector.h"
34
35#include <QHostInfo>
36#include <QContextMenuEvent>
37
39 : WebView(webTab)
40 , m_window(nullptr)
41 , m_webTab(webTab)
42 , m_menu(new Menu(this))
43{
44 m_menu->setCloseOnMiddleClick(true);
45
46 connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
47 connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
48 connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
49}
50
52{
54
55 connect(page, &WebPage::linkHovered, this, &TabbedWebView::linkHovered);
56}
57
59{
60 return m_window;
61}
62
64{
65 m_window = window;
66}
67
68void TabbedWebView::inspectElement()
69{
70 if (m_webTab->haveInspector())
71 triggerPageAction(QWebEnginePage::InspectElement);
72 else
73 m_webTab->showWebInspector(true);
74}
75
77{
78 return m_webTab;
79}
80
81QString TabbedWebView::getIp() const
82{
83 return m_currentIp;
84}
85
86void TabbedWebView::slotLoadProgress(int prog)
87{
88 Q_UNUSED(prog)
89
90 if (m_webTab->isCurrentTab() && m_window) {
91 m_window->updateLoadingActions();
92 }
93}
94
96{
97 load(req);
98}
99
100void TabbedWebView::slotLoadStarted()
101{
102 m_currentIp.clear();
103}
104
105void TabbedWebView::slotLoadFinished()
106{
107 QHostInfo::lookupHost(url().host(), this, SLOT(setIp(QHostInfo)));
108
109 if (m_webTab->isCurrentTab() && m_window) {
110 m_window->updateLoadingActions();
111 }
112}
113
114void TabbedWebView::setIp(const QHostInfo &info)
115{
116 if (info.addresses().isEmpty()) {
117 return;
118 }
119
120 m_currentIp = QStringLiteral("%1 (%2)").arg(info.hostName(), info.addresses().at(0).toString());
121
122 if (m_webTab->isCurrentTab()) {
123 Q_EMIT ipChanged(m_currentIp);
124 }
125}
126
127void TabbedWebView::linkHovered(const QString &link)
128{
129 if (m_webTab->isCurrentTab() && m_window) {
130 if (link.isEmpty()) {
131 m_window->statusBar()->clearMessage();
132 }
133 else {
134 m_window->statusBar()->showMessage(link);
135 }
136 }
137}
138
140{
141 return m_webTab->tabIndex();
142}
143
145{
146 return m_webTab;
147}
148
150{
151 Q_EMIT wantsCloseTab(tabIndex());
152}
153
154void TabbedWebView::loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position)
155{
156 if (m_window) {
157 int index = m_window->tabWidget()->addView(QUrl(), position);
158 TabbedWebView *view = m_window->weView(index);
159 webTab()->addChildTab(view->webTab());
160 view->webTab()->locationBar()->showUrl(req.url());
161 view->load(req);
162 }
163}
164
166{
167 return m_window && m_window->isFullScreen();
168}
169
171{
172 if (!m_window)
173 return;
174
175 m_window->requestHtmlFullScreen(this, enable);
176}
177
179{
180 if (m_window) {
181 m_window->tabWidget()->setCurrentWidget(m_webTab);
182 }
183}
184
185void TabbedWebView::_contextMenuEvent(QContextMenuEvent *event)
186{
187 m_menu->clear();
188
189 WebHitTestResult hitTest = page()->hitTestContent(event->pos());
190 createContextMenu(m_menu, hitTest);
191
193 m_menu->addSeparator();
194 m_menu->addAction(tr("Inspect Element"), this, &TabbedWebView::inspectElement);
195 }
196
197 if (!m_menu->isEmpty()) {
198 // Prevent choosing first option with double rightclick
199 const QPoint pos = event->globalPos();
200 QPoint p(pos.x(), pos.y() + 1);
201
202 m_menu->popup(p);
203 return;
204 }
205
207}
208
209void TabbedWebView::_mouseMoveEvent(QMouseEvent *event)
210{
211 if (m_window && m_window->isFullScreen()) {
212 if (m_window->fullScreenNavigationVisible()) {
214 }
215 else if (event->position().toPoint().y() < 5) {
217 }
218 }
219
221}
bool fullScreenNavigationVisible() const
TabWidget * tabWidget() const
void updateLoadingActions()
void showNavigationWithFullScreen()
void requestHtmlFullScreen(TabbedWebView *view, bool enable)
StatusBar * statusBar() const
TabbedWebView * weView() const
void hideNavigationWithFullScreen()
QUrl url() const
Definition: loadrequest.cpp:44
void showUrl(const QUrl &url)
void setCloseOnMiddleClick(bool close)
void clearMessage()
Definition: statusbar.cpp:211
void showMessage(const QString &message, int timeout=0)
Definition: statusbar.cpp:179
void setCurrentWidget(QWidget *widget)
int addView(const LoadRequest &req, const Qz::NewTabPositionFlags &openFlags, bool selectLine=false, bool pinned=false)
Definition: tabwidget.cpp:314
void closeView() override
QString getIp() const
void ipChanged(const QString &)
void requestFullScreen(bool enable) override
void setPage(WebPage *page)
void userLoadAction(const LoadRequest &req)
BrowserWindow * browserWindow() const
int tabIndex() const
bool isFullScreen() override
void loadInNewTab(const LoadRequest &req, Qz::NewTabPositionFlags position) override
void setBrowserWindow(BrowserWindow *window)
QWidget * overlayWidget() override
TabbedWebView(WebTab *webTab)
void setAsCurrentTab()
void wantsCloseTab(int)
WebTab * webTab() const
static bool isEnabled()
WebHitTestResult hitTestContent(const QPoint &pos) const
Definition: webpage.cpp:189
Definition: webtab.h:40
void showWebInspector(bool inspectElement=false)
Definition: webtab.cpp:219
bool isCurrentTab() const
Definition: webtab.cpp:684
int tabIndex() const
Definition: webtab.cpp:710
LocationBar * locationBar() const
Definition: webtab.cpp:459
bool haveInspector() const
Definition: webtab.cpp:214
void addChildTab(WebTab *tab, int index=-1)
Definition: webtab.cpp:502
void createContextMenu(QMenu *menu, WebHitTestResult &hitTest)
Definition: webview.cpp:672
WebPage * page() const
Definition: webview.cpp:132
void setPage(WebPage *page)
Definition: webview.cpp:137
void load(const QUrl &url)
Definition: webview.cpp:177
virtual void _contextMenuEvent(QContextMenuEvent *event)
Definition: webview.cpp:1237
virtual void _mouseMoveEvent(QMouseEvent *event)
Definition: webview.cpp:1160