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

Proper fix for #608 saving page screen.

It now split the page by 20000 pixels and render it.
Images are then saved on disk in parts.
This commit is contained in:
nowrep 2013-01-22 16:17:50 +01:00
parent 3729649f6b
commit 90a6b79050
2 changed files with 113 additions and 39 deletions

View File

@ -23,9 +23,11 @@
#include <QFileDialog> #include <QFileDialog>
#include <QWebFrame> #include <QWebFrame>
#include <QLabel>
#include <QTimer> #include <QTimer>
#include <QMovie> #include <QMovie>
#include <QPushButton> #include <QPushButton>
#include <QCloseEvent>
#if QT_VERSION >= 0x050000 #if QT_VERSION >= 0x050000
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
@ -33,22 +35,13 @@
#include <QtConcurrentRun> #include <QtConcurrentRun>
#endif #endif
static QSize limitSize(const QSize &originalSize)
{
if (originalSize.height() > 20000) {
return QSize(originalSize.width(), 20000);
}
return originalSize;
}
PageScreen::PageScreen(WebView* view, QWidget* parent) PageScreen::PageScreen(WebView* view, QWidget* parent)
: QDialog(parent) : QDialog(parent)
, ui(new Ui::PageScreen) , ui(new Ui::PageScreen)
, m_view(view) , m_view(view)
, m_blockClose(false)
, m_fileSaving(0)
, m_imageScaling(0) , m_imageScaling(0)
, m_horizontalScrollbarSize(0)
, m_verticalScrollbarSize(0)
{ {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this); ui->setupUi(this);
@ -67,53 +60,130 @@ PageScreen::PageScreen(WebView* view, QWidget* parent)
void PageScreen::dialogAccepted() void PageScreen::dialogAccepted()
{ {
const QString &suggestedPath = QDir::homePath() + "/" + QString("%1.png").arg(qz_filterCharsFromFilename(m_pageTitle)); const QString &suggestedPath = QString("%1/%2.png").arg(QDir::homePath(),
QString path = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), qz_filterCharsFromFilename(m_pageTitle));
suggestedPath); m_filePath = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), suggestedPath);
if (!path.isEmpty()) { if (!m_filePath.isEmpty()) {
if (!path.endsWith(QLatin1String(".png"), Qt::CaseInsensitive)) { QApplication::setOverrideCursor(Qt::WaitCursor);
path.append(QLatin1String(".png")); m_blockClose = true;
m_fileSaving = new QFutureWatcher<void>(this);
m_fileSaving->setFuture(QtConcurrent::run(this, &PageScreen::saveScreen));
connect(m_fileSaving, SIGNAL(finished()), SLOT(screenSaved()));
}
} }
m_pageImage.save(path, "PNG"); void PageScreen::saveScreen()
QTimer::singleShot(0, this, SLOT(close())); {
QString pathWithoutSuffix = m_filePath;
if (pathWithoutSuffix.endsWith(QLatin1String(".png"), Qt::CaseInsensitive)) {
pathWithoutSuffix = pathWithoutSuffix.mid(0, pathWithoutSuffix.length() - 4);
} }
if (m_pageImages.count() == 1) {
m_pageImages.first().save(pathWithoutSuffix + ".png", "PNG");
}
else {
int part = 1;
foreach(const QImage & image, m_pageImages) {
const QString &fileName = pathWithoutSuffix + ".part" + QString::number(part);
image.save(fileName + ".png", "PNG");
part++;
}
}
m_blockClose = false;
}
void PageScreen::screenSaved()
{
QApplication::restoreOverrideCursor();
}
void PageScreen::closeEvent(QCloseEvent* event)
{
if (m_blockClose) {
event->ignore();
return;
}
QDialog::closeEvent(event);
} }
void PageScreen::createThumbnail() void PageScreen::createThumbnail()
{ {
QWebPage* page = m_view->page(); QWebPage* page = m_view->page();
QSize originalSize = page->viewportSize();
page->setViewportSize(limitSize(page->mainFrame()->contentsSize()));
m_pageImage = QImage(page->viewportSize(), QImage::Format_ARGB32_Premultiplied); const int heightLimit = 20000;
QPainter painter(&m_pageImage); const QPoint originalScrollPosition = page->mainFrame()->scrollPosition();
const QSize &originalSize = page->viewportSize();
const QSize &frameSize = page->mainFrame()->contentsSize();
const int verticalScrollbarSize = page->mainFrame()->scrollBarGeometry(Qt::Vertical).width();
const int horizontalScrollbarSize = page->mainFrame()->scrollBarGeometry(Qt::Horizontal).height();
int yPosition = 0;
bool canScroll = frameSize.height() > heightLimit;
/* We will split rendering page into smaller parts to avoid infinite loops
* or crashes.
*/
do {
int remainingHeight = frameSize.height() - yPosition;
if (remainingHeight <= 0) {
break;
}
QSize size(frameSize.width(),
remainingHeight > heightLimit ? heightLimit : remainingHeight);
page->setViewportSize(size);
page->mainFrame()->scroll(0, qMax(0, yPosition - horizontalScrollbarSize));
QImage image(page->viewportSize().width() - verticalScrollbarSize,
page->viewportSize().height() - horizontalScrollbarSize,
QImage::Format_ARGB32_Premultiplied);
QPainter painter(&image);
page->mainFrame()->render(&painter); page->mainFrame()->render(&painter);
painter.end(); painter.end();
m_verticalScrollbarSize = page->mainFrame()->scrollBarGeometry(Qt::Vertical).width(); m_pageImages.append(image);
m_horizontalScrollbarSize = page->mainFrame()->scrollBarGeometry(Qt::Horizontal).height();
canScroll = remainingHeight > heightLimit;
yPosition += size.height();
}
while (canScroll);
page->setViewportSize(originalSize); page->setViewportSize(originalSize);
page->mainFrame()->setScrollBarValue(Qt::Vertical, originalScrollPosition.y());
page->mainFrame()->setScrollBarValue(Qt::Horizontal, originalScrollPosition.x());
m_imageScaling = new QFutureWatcher<QImage>(this); m_imageScaling = new QFutureWatcher<QImage>(this);
connect(m_imageScaling, SIGNAL(finished()), SLOT(showImage()));
m_imageScaling->setFuture(QtConcurrent::run(this, &PageScreen::scaleImage)); m_imageScaling->setFuture(QtConcurrent::run(this, &PageScreen::scaleImage));
connect(m_imageScaling, SIGNAL(finished()), SLOT(showImage()));
} }
QImage PageScreen::scaleImage() QImage PageScreen::scaleImage()
{ {
if (m_verticalScrollbarSize > 0 || m_horizontalScrollbarSize > 0) { QList<QImage> scaledImages;
QRect newRect = m_pageImage.rect(); int sumHeight = 0;
newRect.setWidth(newRect.width() - m_verticalScrollbarSize);
newRect.setHeight(newRect.height() - m_horizontalScrollbarSize);
m_pageImage = m_pageImage.copy(newRect); foreach(const QImage & image, m_pageImages) {
QImage scaled = image.scaledToWidth(450, Qt::SmoothTransformation);
scaledImages.append(scaled);
sumHeight += scaled.height();
} }
return m_pageImage.scaledToWidth(450, Qt::SmoothTransformation); QImage finalImage(QSize(450, sumHeight), QImage::Format_ARGB32_Premultiplied);
QPainter painter(&finalImage);
int offset = 0;
foreach(const QImage & image, scaledImages) {
painter.drawImage(0, offset, image);
offset += image.height();
}
return finalImage;
} }
void PageScreen::showImage() void PageScreen::showImage()

View File

@ -45,18 +45,22 @@ private slots:
void showImage(); void showImage();
void dialogAccepted(); void dialogAccepted();
void saveScreen();
void screenSaved();
private: private:
void closeEvent(QCloseEvent* event);
void createPixmap(); void createPixmap();
Ui::PageScreen* ui; Ui::PageScreen* ui;
WebView* m_view; WebView* m_view;
QImage m_pageImage;
QString m_pageTitle; QString m_pageTitle;
bool m_blockClose;
QFutureWatcher<void>* m_fileSaving;
QFutureWatcher<QImage>* m_imageScaling; QFutureWatcher<QImage>* m_imageScaling;
int m_horizontalScrollbarSize; QString m_filePath;
int m_verticalScrollbarSize; QList<QImage> m_pageImages;
}; };
#endif // PAGESCREEN_H #endif // PAGESCREEN_H