2011-09-23 22:06:21 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-02-26 12:56:11 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2011-09-23 22:06:21 +02:00
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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/>.
|
|
|
|
* ============================================================ */
|
2011-09-21 14:20:49 +02:00
|
|
|
#include "pagescreen.h"
|
|
|
|
#include "ui_pagescreen.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2012-04-03 19:28:12 +02:00
|
|
|
#include "webpage.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QWebFrame>
|
2013-01-22 16:17:50 +01:00
|
|
|
#include <QLabel>
|
2012-03-05 14:33:24 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QMovie>
|
|
|
|
#include <QPushButton>
|
2013-01-22 16:17:50 +01:00
|
|
|
#include <QCloseEvent>
|
2012-03-05 14:33:24 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
|
|
#else
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
#endif
|
|
|
|
|
2011-10-26 19:23:50 +02:00
|
|
|
PageScreen::PageScreen(WebView* view, QWidget* parent)
|
|
|
|
: QDialog(parent)
|
2011-09-21 14:20:49 +02:00
|
|
|
, ui(new Ui::PageScreen)
|
|
|
|
, m_view(view)
|
2013-01-22 16:17:50 +01:00
|
|
|
, m_blockClose(false)
|
|
|
|
, m_fileSaving(0)
|
2012-03-07 12:19:54 +01:00
|
|
|
, m_imageScaling(0)
|
2011-09-21 14:20:49 +02:00
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2012-03-05 14:33:24 +01:00
|
|
|
QMovie* mov = new QMovie(":html/loading.gif");
|
|
|
|
ui->label->setMovie(mov);
|
|
|
|
mov->start();
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2012-03-07 12:19:54 +01:00
|
|
|
m_pageTitle = m_view->title();
|
|
|
|
|
2012-03-05 14:33:24 +01:00
|
|
|
connect(ui->buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()), this, SLOT(dialogAccepted()));
|
|
|
|
connect(ui->buttonBox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(close()));
|
|
|
|
|
|
|
|
QTimer::singleShot(200, this, SLOT(createThumbnail()));
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
|
|
|
|
2012-03-05 14:33:24 +01:00
|
|
|
void PageScreen::dialogAccepted()
|
2011-09-21 14:20:49 +02:00
|
|
|
{
|
2013-01-22 16:17:50 +01:00
|
|
|
const QString &suggestedPath = QString("%1/%2.png").arg(QDir::homePath(),
|
2013-01-22 19:04:22 +01:00
|
|
|
QzTools::filterCharsFromFilename(m_pageTitle));
|
2013-01-22 16:17:50 +01:00
|
|
|
m_filePath = QFileDialog::getSaveFileName(this, tr("Save Page Screen..."), suggestedPath);
|
|
|
|
|
|
|
|
if (!m_filePath.isEmpty()) {
|
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
|
|
|
m_blockClose = true;
|
|
|
|
|
|
|
|
m_fileSaving = new QFutureWatcher<void>(this);
|
|
|
|
m_fileSaving->setFuture(QtConcurrent::run(this, &PageScreen::saveScreen));
|
|
|
|
connect(m_fileSaving, SIGNAL(finished()), SLOT(screenSaved()));
|
|
|
|
}
|
|
|
|
}
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
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++;
|
2012-03-17 16:01:12 +01:00
|
|
|
}
|
2013-01-22 16:17:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_blockClose = false;
|
|
|
|
}
|
2012-03-07 20:01:57 +01:00
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
void PageScreen::screenSaved()
|
|
|
|
{
|
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageScreen::closeEvent(QCloseEvent* event)
|
|
|
|
{
|
|
|
|
if (m_blockClose) {
|
|
|
|
event->ignore();
|
|
|
|
return;
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
2013-01-22 16:17:50 +01:00
|
|
|
|
|
|
|
QDialog::closeEvent(event);
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
|
|
|
|
2012-03-05 14:33:24 +01:00
|
|
|
void PageScreen::createThumbnail()
|
2011-09-21 14:20:49 +02:00
|
|
|
{
|
|
|
|
QWebPage* page = m_view->page();
|
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
canScroll = remainingHeight > heightLimit;
|
|
|
|
yPosition += size.height();
|
|
|
|
}
|
|
|
|
while (canScroll);
|
2012-03-07 12:19:54 +01:00
|
|
|
|
2011-09-21 14:20:49 +02:00
|
|
|
page->setViewportSize(originalSize);
|
2013-01-22 16:17:50 +01:00
|
|
|
page->mainFrame()->setScrollBarValue(Qt::Vertical, originalScrollPosition.y());
|
|
|
|
page->mainFrame()->setScrollBarValue(Qt::Horizontal, originalScrollPosition.x());
|
2012-03-05 14:33:24 +01:00
|
|
|
|
|
|
|
m_imageScaling = new QFutureWatcher<QImage>(this);
|
2012-03-07 12:19:54 +01:00
|
|
|
m_imageScaling->setFuture(QtConcurrent::run(this, &PageScreen::scaleImage));
|
2013-01-22 16:17:50 +01:00
|
|
|
connect(m_imageScaling, SIGNAL(finished()), SLOT(showImage()));
|
2012-03-07 12:19:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QImage PageScreen::scaleImage()
|
|
|
|
{
|
2013-02-26 12:56:11 +01:00
|
|
|
QVector<QImage> scaledImages;
|
2013-01-22 16:17:50 +01:00
|
|
|
int sumHeight = 0;
|
|
|
|
|
|
|
|
foreach(const QImage & image, m_pageImages) {
|
|
|
|
QImage scaled = image.scaledToWidth(450, Qt::SmoothTransformation);
|
|
|
|
|
|
|
|
scaledImages.append(scaled);
|
|
|
|
sumHeight += scaled.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage finalImage(QSize(450, sumHeight), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
QPainter painter(&finalImage);
|
2012-03-07 12:19:54 +01:00
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
int offset = 0;
|
|
|
|
foreach(const QImage & image, scaledImages) {
|
|
|
|
painter.drawImage(0, offset, image);
|
|
|
|
offset += image.height();
|
2012-03-07 12:19:54 +01:00
|
|
|
}
|
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
return finalImage;
|
2012-03-05 14:33:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageScreen::showImage()
|
|
|
|
{
|
|
|
|
delete ui->label->movie();
|
|
|
|
|
|
|
|
ui->label->setPixmap(QPixmap::fromImage(m_imageScaling->result()));
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PageScreen::~PageScreen()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|