Falkon Develop
Cross-platform Qt-based web browser
webinspector.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 "webinspector.h"
19#include "mainapplication.h"
20#include "networkmanager.h"
21#include "settings.h"
22#include "webview.h"
23#include "webpage.h"
24
25#include <QJsonArray>
26#include <QJsonObject>
27#include <QJsonDocument>
28#include <QNetworkReply>
29#include <QWebEngineSettings>
30#include <QtWebEngineWidgetsVersion>
31
32QList<QWebEngineView*> WebInspector::s_views;
33
35 : QWebEngineView(parent)
36 , m_view(nullptr)
37{
38 setAttribute(Qt::WA_DeleteOnClose);
39 setObjectName(QSL("web-inspector"));
40 setMinimumHeight(80);
41
42 m_height = Settings().value(QSL("Web-Inspector/height"), 80).toInt();
43 m_windowSize = Settings().value(QSL("Web-Inspector/windowSize"), QSize(640, 480)).toSize();
44
45 registerView(this);
46
47 connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater);
48 connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::loadFinished);
49}
50
52{
53 if (m_view && hasFocus()) {
54 m_view->setFocus();
55 }
56
57 unregisterView(this);
58
59 if (isWindow()) {
60 Settings().setValue(QSL("Web-Inspector/windowSize"), size());
61 } else {
62 Settings().setValue(QSL("Web-Inspector/height"), height());
63 }
64}
65
67{
68 m_view = view;
69 Q_ASSERT(isEnabled());
70
71 page()->setInspectedPage(m_view->page());
72 connect(m_view, &WebView::pageChanged, this, &WebInspector::deleteLater);
73}
74
76{
77 m_inspectElement = true;
78}
79
81{
82 if (!mApp->webSettings()->testAttribute(QWebEngineSettings::JavascriptEnabled)) {
83 return false;
84 }
85 return true;
86}
87
88void WebInspector::pushView(QWebEngineView *view)
89{
90 s_views.removeOne(view);
91 s_views.prepend(view);
92}
93
94void WebInspector::registerView(QWebEngineView *view)
95{
96 s_views.prepend(view);
97}
98
99void WebInspector::unregisterView(QWebEngineView *view)
100{
101 s_views.removeOne(view);
102}
103
104void WebInspector::loadFinished()
105{
106 // Inspect element
107 if (m_inspectElement) {
108 m_view->triggerPageAction(QWebEnginePage::InspectElement);
109 m_inspectElement = false;
110 }
111}
112
114{
115 if (isWindow()) {
116 return m_windowSize;
117 }
118 QSize s = QWebEngineView::sizeHint();
119 s.setHeight(m_height);
120 return s;
121}
122
123void WebInspector::keyPressEvent(QKeyEvent *event)
124{
125 Q_UNUSED(event)
126 // Stop propagation
127}
128
129void WebInspector::keyReleaseEvent(QKeyEvent *event)
130{
131 Q_UNUSED(event)
132 // Stop propagation
133}
QVariant value(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:74
void setValue(const QString &key, const QVariant &defaultValue=QVariant())
Definition: settings.cpp:69
QSize sizeHint() const override
~WebInspector() override
static bool isEnabled()
void inspectElement()
static void pushView(QWebEngineView *view)
void setView(WebView *view)
static void unregisterView(QWebEngineView *view)
WebInspector(QWidget *parent=nullptr)
static void registerView(QWebEngineView *view)
WebPage * page() const
Definition: webview.cpp:132
void pageChanged(WebPage *page)
#define mApp
#define QSL(x)
Definition: qzcommon.h:40