/
webbrowserwindowwindow.cpp
433 lines (307 loc) · 14.5 KB
/
webbrowserwindowwindow.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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.
OpenCOR 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 this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Web Browser window
//==============================================================================
#include "borderedwidget.h"
#include "coreguiutils.h"
#include "progressbarwidget.h"
#include "toolbarwidget.h"
#include "webbrowserwindowwidget.h"
#include "webbrowserwindowwindow.h"
//==============================================================================
#include "ui_webbrowserwindowwindow.h"
//==============================================================================
#include <QClipboard>
#include <QLineEdit>
#include <QMenu>
#include <QPrinterInfo>
#include <QSettings>
#include <QTimer>
#include <QWebHistory>
#include <QWebView>
//==============================================================================
namespace OpenCOR {
namespace WebBrowserWindow {
//==============================================================================
WebBrowserWindowWindow::WebBrowserWindowWindow(QWidget *pParent) :
Core::WindowWidget(pParent),
mGui(new Ui::WebBrowserWindowWindow)
{
// Set up the GUI
mGui->setupUi(this);
connect(mGui->actionClear, &QAction::triggered,
this, &WebBrowserWindowWindow::actionClearTriggered);
connect(mGui->actionBack, &QAction::triggered,
this, &WebBrowserWindowWindow::actionBackTriggered);
connect(mGui->actionForward, &QAction::triggered,
this, &WebBrowserWindowWindow::actionForwardTriggered);
connect(mGui->actionCopy, &QAction::triggered,
this, &WebBrowserWindowWindow::actionCopyTriggered);
connect(mGui->actionNormalSize, &QAction::triggered,
this, &WebBrowserWindowWindow::actionNormalSizeTriggered);
connect(mGui->actionZoomIn, &QAction::triggered,
this, &WebBrowserWindowWindow::actionZoomInTriggered);
connect(mGui->actionZoomOut, &QAction::triggered,
this, &WebBrowserWindowWindow::actionZoomOutTriggered);
connect(mGui->actionPrint, &QAction::triggered,
this, &WebBrowserWindowWindow::actionPrintTriggered);
connect(mGui->actionInspect, &QAction::triggered,
this, &WebBrowserWindowWindow::actionInspectTriggered);
connect(mGui->actionReload, &QAction::triggered,
this, &WebBrowserWindowWindow::actionReloadTriggered);
// Create a tool bar widget with a URL value and refresh button
// Note: the spacer is a little trick to improve the rendering of our tool
// bar widget...
auto topToolBarWidget = new ToolBarWidget::ToolBarWidget(this);
topToolBarWidget->addSpacerWidgetAction(QSizePolicy::Fixed, QSizePolicy::Expanding);
mUrlValueAction = topToolBarWidget->addLineEditWidgetAction();
connect(mUrlValueAction, &ToolBarWidget::ToolBarLineEditWidgetAction::created,
this, &WebBrowserWindowWindow::urlValueCreated);
connect(mUrlValueAction, &ToolBarWidget::ToolBarLineEditWidgetAction::returnPressed,
this, &WebBrowserWindowWindow::urlValueReturnPressed);
topToolBarWidget->addAction(mGui->actionReload);
mGui->layout->addWidget(topToolBarWidget);
// Create a tool bar widget with different buttons
auto bottomToolBarWidget = new ToolBarWidget::ToolBarWidget(this);
bottomToolBarWidget->addAction(mGui->actionClear);
bottomToolBarWidget->addSeparator();
bottomToolBarWidget->addAction(mGui->actionBack);
bottomToolBarWidget->addAction(mGui->actionForward);
bottomToolBarWidget->addSeparator();
bottomToolBarWidget->addAction(mGui->actionCopy);
bottomToolBarWidget->addSeparator();
bottomToolBarWidget->addAction(mGui->actionNormalSize);
bottomToolBarWidget->addSeparator();
bottomToolBarWidget->addAction(mGui->actionZoomIn);
bottomToolBarWidget->addAction(mGui->actionZoomOut);
bottomToolBarWidget->addSeparator();
bottomToolBarWidget->addAction(mGui->actionPrint);
mGui->layout->addWidget(Core::newLineWidget(this));
mGui->layout->addWidget(bottomToolBarWidget);
// Create and add a Web browser window widget
mWebBrowserWindowWidget = new WebBrowserWindowWidget(this);
mWebBrowserWindowWidget->setObjectName("WebBrowserWindowWidget");
mWebBrowserWindowWidget->showProgressBar();
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
mGui->layout->addWidget(new Core::BorderedWidget(mWebBrowserWindowWidget,
true, true, true, true));
#else
mGui->layout->addWidget(new Core::BorderedWidget(mWebBrowserWindowWidget,
true, false, false, false));
#endif
// Various connections to handle our Web browser window widget
connect(mWebBrowserWindowWidget->webView(), &QWebView::urlChanged,
this, &WebBrowserWindowWindow::urlChanged);
// Create and populate our context menu
mContextMenu = new QMenu(this);
mContextMenu->addAction(mGui->actionClear);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionBack);
mContextMenu->addAction(mGui->actionForward);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionCopy);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionNormalSize);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionZoomIn);
mContextMenu->addAction(mGui->actionZoomOut);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionPrint);
mContextMenu->addSeparator();
mContextMenu->addAction(mGui->actionInspect);
// We want our own context menu for our Web browser window widget (indeed,
// we don't want the default one, which has the reload menu item and not the
// other actions that we have in our tool bar widget)
mWebBrowserWindowWidget->webView()->setContextMenuPolicy(Qt::CustomContextMenu);
connect(mWebBrowserWindowWidget->webView(), &QWebView::customContextMenuRequested,
this, &WebBrowserWindowWindow::showCustomContextMenu);
// Some connections to update the enabled state of our various actions
connect(mWebBrowserWindowWidget, QOverload<bool>::of(&WebBrowserWindowWidget::homePage),
mGui->actionClear, &QAction::setDisabled);
connect(mWebBrowserWindowWidget, &WebBrowserWindowWidget::backEnabled,
mGui->actionBack, &QAction::setEnabled);
connect(mWebBrowserWindowWidget, &WebBrowserWindowWidget::forwardEnabled,
mGui->actionForward, &QAction::setEnabled);
connect(mWebBrowserWindowWidget, &WebBrowserWindowWidget::defaultZoomLevel,
mGui->actionNormalSize, &QAction::setDisabled);
connect(mWebBrowserWindowWidget, &WebBrowserWindowWidget::zoomingOutEnabled,
mGui->actionZoomOut, &QAction::setEnabled);
connect(mWebBrowserWindowWidget, &WebBrowserWindowWidget::copyTextEnabled,
mGui->actionCopy, &QAction::setEnabled);
// En/disable the printing action, depending on whether printers are
// available
mGui->actionPrint->setEnabled(!QPrinterInfo::availablePrinterNames().isEmpty());
}
//==============================================================================
WebBrowserWindowWindow::~WebBrowserWindowWindow()
{
// Delete the GUI
delete mGui;
}
//==============================================================================
void WebBrowserWindowWindow::retranslateUi()
{
// Retranslate our whole window
mGui->retranslateUi(this);
}
//==============================================================================
static const char *SettingsUrl = "Url";
//==============================================================================
void WebBrowserWindowWindow::loadSettings(QSettings &pSettings)
{
// Retrieve the settings of our Web browser window widget
pSettings.beginGroup(mWebBrowserWindowWidget->objectName());
mWebBrowserWindowWidget->loadSettings(pSettings);
pSettings.endGroup();
// Retrieve our current URL (and load it)
QString url = pSettings.value(SettingsUrl).toString();
mUrlValueAction->setText(url);
loadUrl(url);
}
//==============================================================================
void WebBrowserWindowWindow::saveSettings(QSettings &pSettings) const
{
// Keep track of the settings of our Web browser window widget
pSettings.beginGroup(mWebBrowserWindowWidget->objectName());
mWebBrowserWindowWidget->saveSettings(pSettings);
pSettings.endGroup();
// Keep track of our current URL
pSettings.setValue(SettingsUrl, mUrlValue);
}
//==============================================================================
void WebBrowserWindowWindow::urlChanged(const QUrl &pUrl)
{
// The URL has changed, so update our URL value
QString url = pUrl.toString();
mUrlValueAction->setText((url != mWebBrowserWindowWidget->homePage())?url:QString());
}
//==============================================================================
void WebBrowserWindowWindow::actionClearTriggered()
{
// Clear the contents of our Web browser window widget
// Note: we disable the progress bar since we don't want to see its
// progress...
mWebBrowserWindowWidget->progressBarWidget()->setEnabled(false);
mWebBrowserWindowWidget->clear();
}
//==============================================================================
void WebBrowserWindowWindow::actionBackTriggered()
{
// Go to the previous page
// Note: we enable/disable the progress bar based on whether the back URL is
// our homepage since we don't want to see its progress in the latter
// case...
mWebBrowserWindowWidget->progressBarWidget()->setEnabled(mWebBrowserWindowWidget->webView()->history()->backItem().url().toString() != mWebBrowserWindowWidget->homePage());
mWebBrowserWindowWidget->webView()->back();
}
//==============================================================================
void WebBrowserWindowWindow::actionForwardTriggered()
{
// Go to the next page
// Note: we enable/disable the progress bar based on whether the forward URL
// is our homepage since we don't want to see its progress in the
// latter case...
mWebBrowserWindowWidget->progressBarWidget()->setEnabled(mWebBrowserWindowWidget->webView()->history()->forwardItem().url().toString() != mWebBrowserWindowWidget->homePage());
mWebBrowserWindowWidget->webView()->forward();
}
//==============================================================================
void WebBrowserWindowWindow::actionCopyTriggered()
{
// Copy the current slection to the clipboard
QApplication::clipboard()->setText(mWebBrowserWindowWidget->webView()->selectedText());
}
//==============================================================================
void WebBrowserWindowWindow::actionNormalSizeTriggered()
{
// Reset the zoom level of the page contents
mWebBrowserWindowWidget->resetZoom();
}
//==============================================================================
void WebBrowserWindowWindow::actionZoomInTriggered()
{
// Zoom in the page contents
mWebBrowserWindowWidget->zoomIn();
}
//==============================================================================
void WebBrowserWindowWindow::actionZoomOutTriggered()
{
// Zoom out the page contents
mWebBrowserWindowWidget->zoomOut();
}
//==============================================================================
void WebBrowserWindowWindow::actionPrintTriggered()
{
// Print the page contents
mWebBrowserWindowWidget->print();
}
//==============================================================================
void WebBrowserWindowWindow::actionInspectTriggered()
{
// Inspect the current page using our Web inspector
mWebBrowserWindowWidget->showWebInspector();
}
//==============================================================================
void WebBrowserWindowWindow::actionReloadTriggered()
{
// Reload the URL
mWebBrowserWindowWidget->webView()->reload();
}
//==============================================================================
void WebBrowserWindowWindow::urlValueCreated(QLineEdit *pLineEdit)
{
// Configure our URL value, if still valid
if (!mUrlValueAction->validLineEdit(pLineEdit)) {
return;
}
#ifdef Q_OS_MAC
pLineEdit->setAttribute(Qt::WA_MacShowFocusRect, false);
#endif
pLineEdit->setText(mUrlValue);
// Make our URL value our focus proxy
setFocusProxy(pLineEdit);
}
//==============================================================================
void WebBrowserWindowWindow::loadUrl(const QString &pUrl)
{
// Load the given URL
// Note: we enable/disable the progress bar based on whether the URL is our
// homepage since we don't want to see its progress in the latter
// case...
mWebBrowserWindowWidget->progressBarWidget()->setEnabled(pUrl != mWebBrowserWindowWidget->homePage());
mWebBrowserWindowWidget->webView()->load(pUrl);
mUrlValue = pUrl;
}
//==============================================================================
void WebBrowserWindowWindow::urlValueReturnPressed(const QString &pText)
{
// Go to our home page (i.e. blank page), if the URL is empty, or load the
// URL
loadUrl(pText.isEmpty()?
mWebBrowserWindowWidget->homePage():
pText);
}
//==============================================================================
void WebBrowserWindowWindow::showCustomContextMenu() const
{
// Show our context menu which items match the contents of our tool bar
// widget
mContextMenu->exec(QCursor::pos());
}
//==============================================================================
} // namespace WebBrowserWindow
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================