1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

WebInspector: Implement Inspect Element action

This commit is contained in:
David Rosca 2015-10-15 17:19:16 +02:00
parent 34eac0d9db
commit 74fc271e25
7 changed files with 55 additions and 17 deletions

View File

@ -74,9 +74,15 @@ void PopupWebView::requestFullScreen(bool enable)
void PopupWebView::inspectElement() void PopupWebView::inspectElement()
{ {
WebInspector *inspector = new WebInspector; if (m_inspector) {
inspector->setView(this); triggerPageAction(QWebEnginePage::InspectElement);
inspector->show(); return;
}
m_inspector = new WebInspector;
m_inspector->setView(this);
m_inspector->inspectElement();
m_inspector->show();
} }
void PopupWebView::_contextMenuEvent(QContextMenuEvent *event) void PopupWebView::_contextMenuEvent(QContextMenuEvent *event)

View File

@ -18,11 +18,14 @@
#ifndef POPUPWEBVIEW_H #ifndef POPUPWEBVIEW_H
#define POPUPWEBVIEW_H #define POPUPWEBVIEW_H
#include <QPointer>
#include "qzcommon.h" #include "qzcommon.h"
#include "webview.h" #include "webview.h"
class LoadRequest;
class Menu; class Menu;
class LoadRequest;
class WebInspector;
class QUPZILLA_EXPORT PopupWebView : public WebView class QUPZILLA_EXPORT PopupWebView : public WebView
{ {
@ -44,6 +47,7 @@ private:
void _contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE; void _contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
Menu* m_menu; Menu* m_menu;
QPointer<WebInspector> m_inspector;
}; };
#endif // POPUPWEBVIEW_H #endif // POPUPWEBVIEW_H

View File

@ -28,6 +28,7 @@ QList<QWebEngineView*> WebInspector::s_views;
WebInspector::WebInspector(QWidget *parent) WebInspector::WebInspector(QWidget *parent)
: QWebEngineView(parent) : QWebEngineView(parent)
, m_view(Q_NULLPTR)
{ {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
setObjectName(QSL("web-inspector")); setObjectName(QSL("web-inspector"));
@ -36,7 +37,7 @@ WebInspector::WebInspector(QWidget *parent)
registerView(this); registerView(this);
connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater); connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater);
connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::updateCloseButton); connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::loadFinished);
} }
WebInspector::~WebInspector() WebInspector::~WebInspector()
@ -46,8 +47,10 @@ WebInspector::~WebInspector()
void WebInspector::setView(QWebEngineView *view) void WebInspector::setView(QWebEngineView *view)
{ {
m_view = view;
QUrl inspectorUrl = QUrl(QSL("http://localhost:%1").arg(WEBINSPECTOR_PORT)); QUrl inspectorUrl = QUrl(QSL("http://localhost:%1").arg(WEBINSPECTOR_PORT));
int index = s_views.indexOf(view); int index = s_views.indexOf(m_view);
QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(inspectorUrl.resolved(QUrl("json/list")))); QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(inspectorUrl.resolved(QUrl("json/list"))));
connect(reply, &QNetworkReply::finished, this, [=]() { connect(reply, &QNetworkReply::finished, this, [=]() {
@ -63,6 +66,11 @@ void WebInspector::setView(QWebEngineView *view)
}); });
} }
void WebInspector::inspectElement()
{
m_inspectElement = true;
}
void WebInspector::pushView(QWebEngineView *view) void WebInspector::pushView(QWebEngineView *view)
{ {
s_views.removeOne(view); s_views.removeOne(view);
@ -79,8 +87,9 @@ void WebInspector::unregisterView(QWebEngineView *view)
s_views.removeOne(view); s_views.removeOne(view);
} }
void WebInspector::updateCloseButton() void WebInspector::loadFinished()
{ {
// Update close button
page()->runJavaScript(QL1S("var toolbar = document.getElementsByClassName('inspector-view-toolbar')[1];" page()->runJavaScript(QL1S("var toolbar = document.getElementsByClassName('inspector-view-toolbar')[1];"
"var button = document.createElement('button');" "var button = document.createElement('button');"
"button.style.width = '22px';" "button.style.width = '22px';"
@ -93,6 +102,12 @@ void WebInspector::updateCloseButton()
" window.close();" " window.close();"
"});" "});"
"toolbar.appendChild(button);")); "toolbar.appendChild(button);"));
// Inspect element
if (m_inspectElement) {
m_view->triggerPageAction(QWebEnginePage::InspectElement);
m_inspectElement = false;
}
} }
void WebInspector::keyPressEvent(QKeyEvent *event) void WebInspector::keyPressEvent(QKeyEvent *event)

View File

@ -35,19 +35,23 @@ public:
~WebInspector(); ~WebInspector();
void setView(QWebEngineView *view); void setView(QWebEngineView *view);
void inspectElement();
static void pushView(QWebEngineView *view); static void pushView(QWebEngineView *view);
static void registerView(QWebEngineView *view); static void registerView(QWebEngineView *view);
static void unregisterView(QWebEngineView *view); static void unregisterView(QWebEngineView *view);
private slots: private slots:
void updateCloseButton(); void loadFinished();
private: private:
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE; void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
static QList<QWebEngineView*> s_views; static QList<QWebEngineView*> s_views;
bool m_inspectElement;
QWebEngineView *m_view;
}; };
#endif // WEBINSPECTORDOCKWIDGET_H #endif // WEBINSPECTORDOCKWIDGET_H

View File

@ -69,7 +69,10 @@ void TabbedWebView::setBrowserWindow(BrowserWindow* window)
void TabbedWebView::inspectElement() void TabbedWebView::inspectElement()
{ {
m_webTab->showWebInspector(); if (m_webTab->haveInspector())
triggerPageAction(QWebEnginePage::InspectElement);
else
m_webTab->showWebInspector(true);
} }
WebTab* TabbedWebView::webTab() const WebTab* TabbedWebView::webTab() const

View File

@ -142,24 +142,29 @@ TabbedWebView* WebTab::webView() const
return m_webView; return m_webView;
} }
void WebTab::showWebInspector() bool WebTab::haveInspector() const
{ {
if (m_splitter->count() != 1) return m_splitter->count() > 1 && m_splitter->widget(1)->inherits("WebInspector");
}
void WebTab::showWebInspector(bool inspectElement)
{
if (haveInspector())
return; return;
WebInspector *inspector = new WebInspector(this); WebInspector *inspector = new WebInspector(this);
inspector->setView(m_webView); inspector->setView(m_webView);
if (inspectElement)
inspector->inspectElement();
m_splitter->addWidget(inspector); m_splitter->addWidget(inspector);
} }
void WebTab::toggleWebInspector() void WebTab::toggleWebInspector()
{ {
if (m_splitter->count() == 1) { if (!haveInspector())
showWebInspector(); showWebInspector();
return; else
}
if (m_splitter->count() > 1 && m_splitter->widget(1)->inherits("WebInspector"))
delete m_splitter->widget(1); delete m_splitter->widget(1);
} }

View File

@ -85,7 +85,8 @@ public:
bool isCurrentTab() const; bool isCurrentTab() const;
void showWebInspector(); bool haveInspector() const;
void showWebInspector(bool inspectElement = false);
void toggleWebInspector(); void toggleWebInspector();
bool isRestored() const; bool isRestored() const;