/
browserutils.cpp
194 lines (166 loc) · 5.66 KB
/
browserutils.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
/*
* Copyright 2013 Canonical Ltd.
*
* This file is part of webbrowser-app.
*
* webbrowser-app 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; version 3.
*
* webbrowser-app 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 <http://www.gnu.org/licenses/>.
*/
#include "browserutils.h"
// Qt
#include <QtCore/QtGlobal>
#include <QtCore/QMetaObject>
#include <QtCore/QTimer>
#include <QtQuick/private/qsgrenderer_p.h>
#include <QtWebEngine/private/qquickwebengineview_p.h>
#include <QDateTime>
#include <QImage>
#include <QStandardPaths>
#include <QDir>
const char* kIconMaskFile = "/usr/palm/applications/org.webosports.app.browser/qml/images/launcher-bookmark-alpha.png";
const char* kIconOverlayFile = "/usr/palm/applications/org.webosports.app.browser/qml/images/launcher-bookmark-overlay.png";
class BindableFbo : public QSGBindable
{
public:
BindableFbo(QOpenGLFramebufferObject* fbo) : m_fbo(fbo) {}
virtual void bind() const { m_fbo->bind(); }
private:
QOpenGLFramebufferObject *m_fbo;
};
BrowserUtils::BrowserUtils(QQuickItem* parent)
: QQuickItem(parent)
, m_webview(0)
{
}
QQuickWebEngineView* BrowserUtils::webview() const
{
return m_webview;
}
void BrowserUtils::setWebview(QQuickWebEngineView* webview)
{
if (webview != m_webview) {
m_webview = webview;
setFlag(QQuickItem::ItemHasContents, false);
Q_EMIT webviewChanged();
}
}
void BrowserUtils::saveViewToFile(const QString outFile, const QSize imageSize)
{
m_outFile = outFile;
m_imageSize = imageSize;
// Delay the actual rendering to give all elements on the page
// a chance to be fully rendered.
QTimer::singleShot(1000, this, SLOT(doSaveWebView()));
}
void BrowserUtils::generateIconFromFile(const QString inFile, const QString outFile, const QSize imageSize)
{
QImage inImage(inFile);
if (inImage.isNull()) {
qWarning() << "generateIconFromFile - failed to open source file";
Q_EMIT iconGenerated(false, outFile);
return;
}
const int nMargin = 4;// Must agree with pixel data in image files
const int nIconSize = 64;// Width & height of output image
const int nIconWidth = nIconSize-2*nMargin;// Width of icon image within file
const int nIconHeight = nIconSize-2*nMargin;
QImage outImage(nIconSize, nIconSize, QImage::Format_ARGB32_Premultiplied);
outImage.fill(0);
QPainter painter(&outImage);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
QRectF source(0.0, 0.0, imageSize.width(), imageSize.height());
QRectF target(nMargin, nMargin, nIconWidth, nIconHeight);
QRectF size(0.0, 0.0, nIconSize, nIconSize);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.drawImage(target, inImage, source);
painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
QImage maskImage(kIconMaskFile);
painter.drawImage(target, maskImage, target);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
QImage overlayImage(kIconOverlayFile);
painter.drawImage(size, overlayImage, size);
QFileInfo imageInfo(outFile);
QDir imageDir(imageInfo.path());
if (!imageDir.exists()) {
imageDir.mkpath(".");
}
bool saved = outImage.save(outFile);
Q_EMIT iconGenerated(saved, outFile);
}
void BrowserUtils::doSaveWebView()
{
if (m_webview) {
setFlag(QQuickItem::ItemHasContents);
update();
}
}
QSGNode* BrowserUtils::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* updatePaintNodeData)
{
Q_UNUSED(updatePaintNodeData);
if (!(m_webview && (flags() & QQuickItem::ItemHasContents))) {
return oldNode;
}
setFlag(QQuickItem::ItemHasContents, false);
#if 0
QQuickWebPage* page = m_webview->page();
qreal xmin = qMin(page->width(), m_webview->width());
qreal ymin = qMin(m_webview->height(), page->height());
#else
// Here the screenshot of the page might be too large if the page is tiny
qreal xmin = m_webview->width();
qreal ymin = m_webview->height();
#endif
ymin = qMin(static_cast<int>(ymin), m_imageSize.height());
xmin = qMin(static_cast<int>(xmin), m_imageSize.width());
QSize size(xmin, ymin);
QSGNode* node = QQuickItemPrivate::get(m_webview)->itemNode();
QSGNode* parent = node->QSGNode::parent();
QSGNode* previousSibling = node->previousSibling();
if (parent) {
parent->removeChildNode(node);
}
QSGRootNode root;
root.appendChildNode(node);
QSGRenderer* renderer;
#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
renderer = QQuickItemPrivate::get(this)->sceneGraphContext()->createRenderer();
#else
renderer = QQuickItemPrivate::get(this)->sceneGraphRenderContext()->createRenderer();
#endif
renderer->setRootNode(static_cast<QSGRootNode*>(&root));
QOpenGLFramebufferObject fbo(size);
renderer->setDeviceRect(size);
renderer->setViewportRect(size);
renderer->setProjectionMatrixToRect(QRectF(QPointF(), size));
renderer->setClearColor(Qt::transparent);
renderer->renderScene(BindableFbo(&fbo));
fbo.release();
QImage image = fbo.toImage().scaled(m_imageSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QFileInfo imageInfo(m_outFile);
QDir imageDir(imageInfo.path());
if (!imageDir.exists()) {
imageDir.mkpath(".");
}
bool saved = image.save(m_outFile);
root.removeChildNode(node);
renderer->setRootNode(0);
delete renderer;
if (parent) {
if (previousSibling) {
parent->insertChildNodeAfter(node, previousSibling);
} else {
parent->prependChildNode(node);
}
}
Q_EMIT webViewSaved(saved, m_outFile);
return oldNode;
}