-
Notifications
You must be signed in to change notification settings - Fork 28
/
Inspector.cpp
196 lines (174 loc) · 6.32 KB
/
Inspector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/************************************************************************
**
** Copyright (C) 2019-2024 Kevin B. Hendricks, Stratford Ontario Canada
**
** This file is part of PageEdit.
**
** PageEdit is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** PageEdit is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QByteArray>
#include <QtWebEngineWidgets>
#include <QtWebEngineCore>
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QWebEngineSettings>
#include <QWebEngineProfile>
#include <QKeySequence>
#include <QApplication>
#include <QDir>
#include <QDebug>
#include "Utility.h"
#include "SettingsStore.h"
#include "Inspector.h"
static const QString SETTINGS_GROUP = "inspect_dialog";
const float ZOOM_STEP = 0.1f;
const float ZOOM_MIN = 0.09f;
const float ZOOM_MAX = 5.0f;
const float ZOOM_NORMAL = 1.0f;
#define DBG if(0)
Inspector::Inspector(QWidget *parent) :
QDockWidget(parent),
m_inspectView(new QWebEngineView(this)),
m_view(nullptr),
m_LoadingFinished(false),
m_LoadOkay(false),
m_ZoomIn(new QShortcut(QKeySequence(Qt::META | Qt::Key_Plus), this)),
m_ZoomOut(new QShortcut(QKeySequence(Qt::META | Qt::Key_Minus), this)),
m_ZoomReset(new QShortcut(QKeySequence(Qt::META | Qt::Key_0), this))
{
QWidget *basewidget = new QWidget(this);
QLayout *layout = new QVBoxLayout(basewidget);
basewidget->setLayout(layout);
layout->addWidget(m_inspectView);
layout->setContentsMargins(0, 0, 0, 0);
basewidget->setObjectName("PrimaryFrame");
setWidget(basewidget);
// QtWebEngine WebInspector needs to run javascript in MainWorld
// so override the app default but just for the inspector
m_inspectView->page()->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
// The Inspector also needs access to local-storage to save its dark mode settings
// between launches
m_inspectView->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
QString inspectorStorePath = Utility::DefinePrefsDir() + "/local-devtools";
QDir storageDir(inspectorStorePath);
if (!storageDir.exists()) {
storageDir.mkpath(inspectorStorePath);
}
m_inspectView->page()->profile()->setPersistentStoragePath(inspectorStorePath);
LoadSettings();
setWindowTitle(tr("Inspect Page or Element"));
connect(m_inspectView->page(), SIGNAL(loadFinished(bool)), this, SLOT(UpdateFinishedState(bool)));
connect(m_inspectView->page(), SIGNAL(loadStarted()), this, SLOT(LoadingStarted()));
connect(m_ZoomIn, SIGNAL(activated()), this, SLOT(ZoomIn()));
connect(m_ZoomOut, SIGNAL(activated()), this, SLOT(ZoomOut()));
connect(m_ZoomReset, SIGNAL(activated()), this, SLOT(ZoomReset()));
}
Inspector::~Inspector()
{
if (m_inspectView) {
m_inspectView->close();
m_view = nullptr;
m_inspectView->page()->setInspectedPage(nullptr);
delete m_inspectView;
m_inspectView = nullptr;
}
}
// Start of Zoom Related Routines
void Inspector::SetZoomFactor(float factor)
{
if (factor > ZOOM_MAX) factor = ZOOM_MAX;
if (factor < ZOOM_MIN) factor = ZOOM_MIN;
SettingsStore settings;
settings.setZoomInspector(factor);
SetCurrentZoomFactor(factor);
Zoom();
}
void Inspector::SetCurrentZoomFactor(float factor) { m_CurrentZoomFactor = factor; }
float Inspector::GetZoomFactor() const { return m_CurrentZoomFactor; }
void Inspector::ZoomIn() { ZoomByStep(true); }
void Inspector::ZoomOut() { ZoomByStep(false); }
void Inspector::ZoomReset() { SetZoomFactor(ZOOM_NORMAL); }
void Inspector::Zoom()
{
m_inspectView->setZoomFactor(m_CurrentZoomFactor);
}
void Inspector::ZoomByStep(bool zoom_in)
{
// zoom out - neg. zoom step, round down; zoom in - pos. zoom step, round UP
float zoom_stepping = zoom_in ? ZOOM_STEP : - ZOOM_STEP;
float rounding_helper = zoom_in ? 0.05f : - 0.05f;
float current_zoom_factor = GetZoomFactor();
float rounded_zoom_factor = Utility::RoundToOneDecimal(current_zoom_factor + rounding_helper);
if (qAbs(current_zoom_factor - rounded_zoom_factor) < 0.01f) {
SetZoomFactor(Utility::RoundToOneDecimal(current_zoom_factor + zoom_stepping));
} else {
SetZoomFactor(rounded_zoom_factor);
}
}
// End of Zoom Related Routines
void Inspector::LoadingStarted()
{
m_LoadingFinished = false;
m_LoadOkay = false;
}
void Inspector::UpdateFinishedState(bool okay)
{
m_LoadingFinished = true;
m_LoadOkay = okay;
}
void Inspector::InspectPageofView(QWebEngineView* view)
{
m_view = view;
if (m_view) {
m_inspectView->page()->setInspectedPage(m_view->page());
}
}
void Inspector::StopInspection()
{
SaveSettings();
m_view = nullptr;
m_inspectView->page()->setInspectedPage(nullptr);
}
QSize Inspector::sizeHint()
{
return QSize(400,200);
}
void Inspector::closeEvent(QCloseEvent* event)
{
DBG qDebug() << "Inspector Close Event";
StopInspection();
QDockWidget::closeEvent(event);
}
void Inspector::LoadSettings()
{
SettingsStore settings;
// in the basic user_preferences group
SetCurrentZoomFactor(settings.zoomInspector());
settings.beginGroup(SETTINGS_GROUP);
QByteArray geometry = settings.value("geometry").toByteArray();
if (!geometry.isNull()) {
restoreGeometry(geometry);
}
settings.endGroup();
}
void Inspector::SaveSettings()
{
SettingsStore settings;
settings.beginGroup(SETTINGS_GROUP);
QApplication::setOverrideCursor(Qt::WaitCursor);
settings.setValue("geometry", saveGeometry());
QApplication::restoreOverrideCursor();
settings.endGroup();
}