mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36: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:
parent
3729649f6b
commit
90a6b79050
@ -23,9 +23,11 @@
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QWebFrame>
|
||||
#include <QLabel>
|
||||
#include <QTimer>
|
||||
#include <QMovie>
|
||||
#include <QPushButton>
|
||||
#include <QCloseEvent>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtConcurrent/QtConcurrentRun>
|
||||
@ -33,22 +35,13 @@
|
||||
#include <QtConcurrentRun>
|
||||
#endif
|
||||
|
||||
static QSize limitSize(const QSize &originalSize)
|
||||
{
|
||||
if (originalSize.height() > 20000) {
|
||||
return QSize(originalSize.width(), 20000);
|
||||
}
|
||||
|
||||
return originalSize;
|
||||
}
|
||||
|
||||
PageScreen::PageScreen(WebView* view, QWidget* parent)
|
||||
: QDialog(parent)
|
||||
, ui(new Ui::PageScreen)
|
||||
, m_view(view)
|
||||
, m_blockClose(false)
|
||||
, m_fileSaving(0)
|
||||
, m_imageScaling(0)
|
||||
, m_horizontalScrollbarSize(0)
|
||||
, m_verticalScrollbarSize(0)
|
||||
{
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
ui->setupUi(this);
|
||||
@ -67,53 +60,130 @@ PageScreen::PageScreen(WebView* view, QWidget* parent)
|
||||
|
||||
void PageScreen::dialogAccepted()
|
||||
{
|
||||
const QString &suggestedPath = QDir::homePath() + "/" + QString("%1.png").arg(qz_filterCharsFromFilename(m_pageTitle));
|
||||
QString path = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."),
|
||||
suggestedPath);
|
||||
const QString &suggestedPath = QString("%1/%2.png").arg(QDir::homePath(),
|
||||
qz_filterCharsFromFilename(m_pageTitle));
|
||||
m_filePath = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), suggestedPath);
|
||||
|
||||
if (!path.isEmpty()) {
|
||||
if (!path.endsWith(QLatin1String(".png"), Qt::CaseInsensitive)) {
|
||||
path.append(QLatin1String(".png"));
|
||||
}
|
||||
if (!m_filePath.isEmpty()) {
|
||||
QApplication::setOverrideCursor(Qt::WaitCursor);
|
||||
m_blockClose = true;
|
||||
|
||||
m_pageImage.save(path, "PNG");
|
||||
QTimer::singleShot(0, this, SLOT(close()));
|
||||
m_fileSaving = new QFutureWatcher<void>(this);
|
||||
m_fileSaving->setFuture(QtConcurrent::run(this, &PageScreen::saveScreen));
|
||||
connect(m_fileSaving, SIGNAL(finished()), SLOT(screenSaved()));
|
||||
}
|
||||
}
|
||||
|
||||
void PageScreen::saveScreen()
|
||||
{
|
||||
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()
|
||||
{
|
||||
QWebPage* page = m_view->page();
|
||||
QSize originalSize = page->viewportSize();
|
||||
page->setViewportSize(limitSize(page->mainFrame()->contentsSize()));
|
||||
|
||||
m_pageImage = QImage(page->viewportSize(), QImage::Format_ARGB32_Premultiplied);
|
||||
QPainter painter(&m_pageImage);
|
||||
page->mainFrame()->render(&painter);
|
||||
painter.end();
|
||||
const int heightLimit = 20000;
|
||||
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();
|
||||
|
||||
m_verticalScrollbarSize = page->mainFrame()->scrollBarGeometry(Qt::Vertical).width();
|
||||
m_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);
|
||||
painter.end();
|
||||
|
||||
m_pageImages.append(image);
|
||||
|
||||
canScroll = remainingHeight > heightLimit;
|
||||
yPosition += size.height();
|
||||
}
|
||||
while (canScroll);
|
||||
|
||||
page->setViewportSize(originalSize);
|
||||
page->mainFrame()->setScrollBarValue(Qt::Vertical, originalScrollPosition.y());
|
||||
page->mainFrame()->setScrollBarValue(Qt::Horizontal, originalScrollPosition.x());
|
||||
|
||||
m_imageScaling = new QFutureWatcher<QImage>(this);
|
||||
connect(m_imageScaling, SIGNAL(finished()), SLOT(showImage()));
|
||||
|
||||
m_imageScaling->setFuture(QtConcurrent::run(this, &PageScreen::scaleImage));
|
||||
connect(m_imageScaling, SIGNAL(finished()), SLOT(showImage()));
|
||||
}
|
||||
|
||||
QImage PageScreen::scaleImage()
|
||||
{
|
||||
if (m_verticalScrollbarSize > 0 || m_horizontalScrollbarSize > 0) {
|
||||
QRect newRect = m_pageImage.rect();
|
||||
newRect.setWidth(newRect.width() - m_verticalScrollbarSize);
|
||||
newRect.setHeight(newRect.height() - m_horizontalScrollbarSize);
|
||||
QList<QImage> scaledImages;
|
||||
int sumHeight = 0;
|
||||
|
||||
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()
|
||||
|
@ -45,18 +45,22 @@ private slots:
|
||||
void showImage();
|
||||
|
||||
void dialogAccepted();
|
||||
void saveScreen();
|
||||
void screenSaved();
|
||||
|
||||
private:
|
||||
void closeEvent(QCloseEvent* event);
|
||||
void createPixmap();
|
||||
|
||||
Ui::PageScreen* ui;
|
||||
WebView* m_view;
|
||||
QImage m_pageImage;
|
||||
QString m_pageTitle;
|
||||
bool m_blockClose;
|
||||
|
||||
QFutureWatcher<void>* m_fileSaving;
|
||||
QFutureWatcher<QImage>* m_imageScaling;
|
||||
int m_horizontalScrollbarSize;
|
||||
int m_verticalScrollbarSize;
|
||||
QString m_filePath;
|
||||
QList<QImage> m_pageImages;
|
||||
};
|
||||
|
||||
#endif // PAGESCREEN_H
|
||||
|
Loading…
Reference in New Issue
Block a user