Falkon Develop
Cross-platform Qt-based web browser
qmltab.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@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 "qmltab.h"
19#include "loadrequest.h"
20#include "tabbedwebview.h"
21#include "webpage.h"
22#include "qml/qmlstaticdata.h"
23#include <QWebEngineHistory>
24#include <QQmlEngine>
25
26QmlTab::QmlTab(WebTab *webTab, QObject *parent)
27 : QObject(parent)
28 , m_webTab(webTab)
29{
30 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
31
32 if (!m_webTab) {
33 return;
34 }
35
36 createConnections();
37}
38
40{
41 if (!m_webTab) {
42 return;
43 }
44
45 m_webTab->detach();
46}
47
48void QmlTab::setZoomLevel(int zoomLevel)
49{
50 if (!m_webTab) {
51 return;
52 }
53
54 m_webTab->setZoomLevel(zoomLevel);
55}
56
58{
59 if (!m_webTab) {
60 return;
61 }
62
63 m_webTab->stop();
64}
65
67{
68 if (!m_webTab) {
69 return;
70 }
71
72 m_webTab->reload();
73}
74
76{
77 if (!m_webTab) {
78 return;
79 }
80
81 m_webTab->unload();
82}
83
84void QmlTab::load(const QString &url)
85{
86 if (!m_webTab) {
87 return;
88 }
89
90 LoadRequest req;
91 req.setUrl(QUrl::fromEncoded(url.toUtf8()));
92 m_webTab->load(req);
93}
94
96{
97 if (!m_webTab) {
98 return;
99 }
100
101 m_webTab->webView()->zoomIn();
102}
103
105{
106 if (!m_webTab) {
107 return;
108 }
109
110 m_webTab->webView()->zoomOut();
111}
112
114{
115 if (!m_webTab) {
116 return;
117 }
118
119 m_webTab->webView()->zoomReset();
120}
121
123{
124 if (!m_webTab) {
125 return;
126 }
127
128 m_webTab->webView()->editUndo();
129}
130
132{
133 if (!m_webTab) {
134 return;
135 }
136
137 m_webTab->webView()->editRedo();
138}
139
141{
142 if (!m_webTab) {
143 return;
144 }
145
146 m_webTab->webView()->editSelectAll();
147}
148
150{
151 if (!m_webTab) {
152 return;
153 }
154
155 m_webTab->webView()->reloadBypassCache();
156}
157
159{
160 if (!m_webTab) {
161 return;
162 }
163
164 m_webTab->webView()->back();
165}
166
168{
169 if (!m_webTab) {
170 return;
171 }
172
173 m_webTab->webView()->forward();
174}
175
177{
178 if (!m_webTab) {
179 return;
180 }
181
182 m_webTab->webView()->printPage();
183}
184
186{
187 if (!m_webTab) {
188 return;
189 }
190
191 m_webTab->webView()->showSource();
192}
193
195{
196 if (!m_webTab) {
197 return;
198 }
199
200 m_webTab->webView()->sendPageByMail();
201}
202
203QVariant QmlTab::execJavaScript(const QJSValue &value)
204{
205 if (!m_webPage && !m_webTab) {
206 return {};
207 }
208 WebPage *webPage = m_webPage;
209 if (!m_webPage) {
210 webPage = m_webTab->webView()->page();
211 }
212 return webPage->execJavaScript(value.toString());
213}
214
216{
217 if (!m_webPage && !m_webTab) {
218 return nullptr;
219 }
220 WebPage *webPage = m_webPage;
221 if (!m_webPage) {
222 webPage = m_webTab->webView()->page();
223 }
224 const WebHitTestResult result = webPage->hitTestContent(point);
225 return new QmlWebHitTestResult(result);
226}
227
228QString QmlTab::url() const
229{
230 if (!m_webTab) {
231 return {};
232 }
233
234 return QString::fromUtf8(m_webTab->url().toEncoded());
235}
236
237QString QmlTab::title() const
238{
239 if (!m_webTab) {
240 return {};
241 }
242
243 return m_webTab->title();
244}
245
246
247int QmlTab::zoomLevel() const
248{
249 if (!m_webTab) {
250 return -1;
251 }
252
253 return m_webTab->zoomLevel();
254}
255
256int QmlTab::index() const
257{
258 if (!m_webTab) {
259 return -1;
260 }
261
262 return m_webTab->tabIndex();
263}
264
265bool QmlTab::pinned() const
266{
267 if (!m_webTab) {
268 return false;
269 }
270
271 return m_webTab->isPinned();
272}
273
274bool QmlTab::muted() const
275{
276 if (!m_webTab) {
277 return false;
278 }
279
280 return m_webTab->isMuted();
281}
282
283bool QmlTab::restored() const
284{
285 if (!m_webTab) {
286 return false;
287 }
288
289 return m_webTab->isRestored();
290}
291
292bool QmlTab::current() const
293{
294 if (!m_webTab) {
295 return false;
296 }
297
298 return m_webTab->isCurrentTab();
299}
300
301bool QmlTab::playing() const
302{
303 if (!m_webTab) {
304 return false;
305 }
306
307 return m_webTab->isPlaying();
308}
309
311{
312 if (!m_webTab) {
313 return nullptr;
314 }
315
316 return QmlStaticData::instance().getWindow(m_webTab->browserWindow());
317}
318
319bool QmlTab::loading() const
320{
321 if (!m_webTab) {
322 return false;
323 }
324
325 return m_webTab->webView()->isLoading();
326}
327
328int QmlTab::loadingProgress() const
329{
330 if (!m_webTab) {
331 return -1;
332 }
333
334 return m_webTab->webView()->loadingProgress();
335}
336
338{
339 if (!m_webTab) {
340 return false;
341 }
342
343 return m_webTab->webView()->backgroundActivity();
344}
345
346bool QmlTab::canGoBack() const
347{
348 if (!m_webTab) {
349 return false;
350 }
351
352 return m_webTab->webView()->history()->canGoBack();
353}
354
355bool QmlTab::canGoForward() const
356{
357 if (!m_webTab) {
358 return false;
359 }
360
361 return m_webTab->webView()->history()->canGoForward();
362}
363
365{
366 if (m_webPage) {
367 removeConnections();
368 }
369 m_webPage = webPage;
370 auto *tabbedWebView = qobject_cast<TabbedWebView*>(m_webPage->view());
371 m_webTab = tabbedWebView->webTab();
372 if (m_webTab) {
373 createConnections();
374 }
375}
376
377void QmlTab::createConnections()
378{
379 Q_ASSERT(m_lambdaConnections.length() == 0);
380
381 auto titleChangedConnection = connect(m_webTab, &WebTab::titleChanged, this, [this](const QString &title){
382 Q_EMIT titleChanged(title);
383 });
384 m_lambdaConnections.append(titleChangedConnection);
385
386 auto pinnedChangedConnection = connect(m_webTab, &WebTab::pinnedChanged, this, [this](bool pinned){
387 Q_EMIT pinnedChanged(pinned);
388 });
389 m_lambdaConnections.append(pinnedChangedConnection);
390
391 auto loadingChangedConnection = connect(m_webTab, &WebTab::loadingChanged, this, [this](bool loading){
392 Q_EMIT loadingChanged(loading);
393 });
394 m_lambdaConnections.append(loadingChangedConnection);
395
396 auto mutedChangedConnection = connect(m_webTab, &WebTab::mutedChanged, this, [this](bool muted){
397 Q_EMIT mutedChanged(muted);
398 });
399 m_lambdaConnections.append(mutedChangedConnection);
400
401 auto restoredChangedConnection = connect(m_webTab, &WebTab::restoredChanged, this, [this](bool restored){
403 });
404 m_lambdaConnections.append(restoredChangedConnection);
405
406 auto playingChangedConnection = connect(m_webTab, &WebTab::playingChanged, this, [this](bool playing){
407 Q_EMIT playingChanged(playing);
408 });
409 m_lambdaConnections.append(playingChangedConnection);
410
413
414 if (m_webPage) {
416 }
417}
418
419void QmlTab::removeConnections()
420{
421 disconnect(this);
422}
void setUrl(const QUrl &url)
Definition: loadrequest.cpp:49
static QmlStaticData & instance()
QmlWindow * getWindow(BrowserWindow *window)
Q_INVOKABLE void detach()
Detaches the tab.
Definition: qmltab.cpp:39
int index
index of the tab
Definition: qmltab.h:55
bool pinned
checks if the tab is pinned
Definition: qmltab.h:60
Q_INVOKABLE void printPage()
Prints the page.
Definition: qmltab.cpp:176
bool restored
checks if the tab is restored
Definition: qmltab.h:70
Q_INVOKABLE void reload()
Reloads webview associated with the tab.
Definition: qmltab.cpp:66
void pinnedChanged(bool pinned)
The signal emitted when pinned state of the tab is changed.
void setWebPage(WebPage *webPage)
Definition: qmltab.cpp:364
bool loading
checks if the tab is loading
Definition: qmltab.h:90
void backgroundActivityChanged(int backgroundActivityChanged)
The signal emitted when background activity of the tab is changed.
int loadingProgress
get the loading progress of the tab
Definition: qmltab.h:95
bool backgroundActivity
checks if the tab has associated background activity
Definition: qmltab.h:100
QString title
title of the tab
Definition: qmltab.h:43
int zoomLevel
zoom level of the tab
Definition: qmltab.h:50
Q_INVOKABLE void showSource()
Shows the page source.
Definition: qmltab.cpp:185
Q_INVOKABLE void stop()
Stops webview associated with the tab.
Definition: qmltab.cpp:57
Q_INVOKABLE void reloadBypassCache()
Reloads the tab by bypassing the cache.
Definition: qmltab.cpp:149
bool current
checks if the tab is the current tab
Definition: qmltab.h:75
void titleChanged(const QString &title)
The signal emitted when the tab title is changed.
QmlTab(WebTab *webTab=nullptr, QObject *parent=nullptr)
Definition: qmltab.cpp:26
Q_INVOKABLE void selectAll()
Performs edit select-all on the tab.
Definition: qmltab.cpp:140
Q_INVOKABLE void setZoomLevel(int zoomLevel)
Set the zoom level of the tab.
Definition: qmltab.cpp:48
void restoredChanged(bool restored)
The signal emitted when restored state of the tab is changed.
bool canGoForward
checks if the tab is can go forward
Definition: qmltab.h:110
Q_INVOKABLE void back()
Loads the previous page.
Definition: qmltab.cpp:158
void zoomLevelChanged(int zoomLevel)
The signal emitted when zoom level of the tab is changed.
void navigationRequestAccepted(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
The signal emitted when navigation request is accepted.
Q_INVOKABLE void unload()
Unloads the tab.
Definition: qmltab.cpp:75
void playingChanged(bool playing)
The signal emitted when playing state of the tab is changed.
Q_INVOKABLE void forward()
Loads the next page.
Definition: qmltab.cpp:167
Q_INVOKABLE void undo()
Performs edit undo on the tab.
Definition: qmltab.cpp:122
QString url
url of the tab
Definition: qmltab.h:38
void loadingChanged(bool loading)
The signal emitted when loading state of the tab is changed.
Q_INVOKABLE QVariant execJavaScript(const QJSValue &value)
execute JavaScript function in a page
Definition: qmltab.cpp:203
bool muted
checks if the tab is muted
Definition: qmltab.h:65
Q_INVOKABLE void load(const QString &url)
Loads webview associated with the tab.
Definition: qmltab.cpp:84
Q_INVOKABLE void zoomOut()
Increases the zoom level of the tab.
Definition: qmltab.cpp:104
Q_INVOKABLE void zoomReset()
Resets the tab zoom level.
Definition: qmltab.cpp:113
bool playing
checks if the tab is playing
Definition: qmltab.h:80
Q_INVOKABLE void redo()
Performs edit redo on the tab.
Definition: qmltab.cpp:131
void mutedChanged(bool muted)
The signal emitted when muted state of the tab is changed.
Q_INVOKABLE void zoomIn()
Decreases the zoom level of the tab.
Definition: qmltab.cpp:95
Q_INVOKABLE QmlWebHitTestResult * hitTestContent(const QPoint &point)
Gets result of web hit test at a given point.
Definition: qmltab.cpp:215
Q_INVOKABLE void sendPageByMail()
Sends page by mail.
Definition: qmltab.cpp:194
bool canGoBack
checks if the tab is can go back
Definition: qmltab.h:105
QmlWindow * browserWindow
window of the tab
Definition: qmltab.h:85
The class exposing result of WebHitTest to QML.
The class exposing Browser window to QML.
Definition: qmlwindow.h:27
QVariant execJavaScript(const QString &scriptSource, quint32 worldId=UnsafeJsWorld, int timeout=500)
Definition: webpage.cpp:165
WebView * view() const
Definition: webpage.cpp:140
void navigationRequestAccepted(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
WebHitTestResult hitTestContent(const QPoint &pos) const
Definition: webpage.cpp:189
Definition: webtab.h:40
bool isMuted() const
Definition: webtab.cpp:433
void setZoomLevel(int level)
Definition: webtab.cpp:309
void mutedChanged(bool muted)
void titleChanged(const QString &title)
bool isCurrentTab() const
Definition: webtab.cpp:684
int tabIndex() const
Definition: webtab.cpp:710
bool isPlaying() const
Definition: webtab.cpp:438
bool isRestored() const
Definition: webtab.cpp:548
void restoredChanged(bool restored)
QUrl url() const
Definition: webtab.cpp:263
void pinnedChanged(bool pinned)
void reload()
Definition: webtab.cpp:386
QString title(bool allowEmpty=false) const
Definition: webtab.cpp:276
void load(const LoadRequest &request)
Definition: webtab.cpp:391
void unload()
Definition: webtab.cpp:401
bool isPinned() const
Definition: webtab.cpp:414
BrowserWindow * browserWindow() const
Definition: webtab.cpp:204
TabbedWebView * webView() const
Definition: webtab.cpp:209
int zoomLevel() const
Definition: webtab.cpp:304
void stop()
Definition: webtab.cpp:381
void loadingChanged(bool loading)
void playingChanged(bool playing)
void detach()
Definition: webtab.cpp:314
bool backgroundActivity() const
Definition: webview.cpp:224
int loadingProgress() const
Definition: webview.cpp:219
void backgroundActivityChanged(bool)
WebPage * page() const
Definition: webview.cpp:132
void printPage()
Definition: webview.cpp:385
void zoomReset()
Definition: webview.cpp:314
void editRedo()
Definition: webview.cpp:327
void sendPageByMail()
Definition: webview.cpp:485
void zoomIn()
Definition: webview.cpp:298
void editSelectAll()
Definition: webview.cpp:347
void reloadBypassCache()
Definition: webview.cpp:358
void zoomOut()
Definition: webview.cpp:306
bool isLoading() const
Definition: webview.cpp:214
void editUndo()
Definition: webview.cpp:322
void zoomLevelChanged(int)
void forward()
Definition: webview.cpp:374
void back()
Definition: webview.cpp:363
void showSource()
Definition: webview.cpp:548
int value(const QColor &c)
Definition: colors.cpp:238