5#include <QLoggingCategory>
6#include "CaptureFullPage.h"
8static Q_LOGGING_CATEGORY(log,
"WebBrowser.Capture.Page")
13void CCaptureFullPage::Start(QWebEngineView *view, QUrl url, QString szFile)
15 if(!view || url.isEmpty() || szFile.isEmpty())
22void CCaptureFullPage::stepInit()
24 qDebug(log) <<
"contents size:" << m_view->page()->contentsSize();
26 m_totalHeight = m_view->page()->contentsSize().height();
27 m_viewportHeight = m_view->size().height();
28 m_deltaY = m_viewportHeight;
31 m_view->page()->runJavaScript(
32 "document.documentElement.scrollTop;",
33 [
this](
const QVariant &result) {
34 m_originY = result.toInt();
40void CCaptureFullPage::stepScroll()
42 if (m_currentY >= m_totalHeight) {
48 m_view->page()->runJavaScript(
49 QString(
"window.scrollTo(0, %1);").arg(m_currentY),
50 [
this](
const QVariant &) {
52 QTimer::singleShot(500,
this, &CCaptureFullPage::stepGrab);
57void CCaptureFullPage::stepGrab()
59 QImage img = m_view->grab().toImage();
61 int expectedHeight = (m_currentY + m_deltaY > m_totalHeight) ? (m_totalHeight - m_currentY) : m_deltaY;
62 if (img.height() > expectedHeight) {
63 img = img.copy(0, 0, img.width(), expectedHeight);
67 m_currentY += m_deltaY;
71void CCaptureFullPage::composeImage()
73 if (!m_images.isEmpty()) {
74 int fullWidth = m_images[0].width();
76 foreach (
const QImage &img, m_images) fullHeight += img.height();
78 QImage result(fullWidth, fullHeight, m_images[0].format());
79 QPainter painter(&result);
81 foreach (
const QImage &img, m_images) {
82 painter.drawImage(0, y, img);
86 result.save(m_szFile);
87 qDebug() <<
"Full page screenshot saved to" << m_szFile;
89 m_view->page()->runJavaScript(
90 QString(
"window.scrollTo(0, %1);").arg(m_originY),
91 [
this](
const QVariant &) {});