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"
|
2013-05-31 13:46:45 +02:00
|
|
|
#include "qupzilla.h"
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QFileDialog>
|
2013-06-02 15:49:40 +02:00
|
|
|
#include <QMessageBox>
|
2012-02-29 18:33:50 +01:00
|
|
|
#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>
|
2013-05-31 13:46:45 +02:00
|
|
|
#include <QPrinter>
|
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)
|
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);
|
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
m_formats[0] = QLatin1String("PNG");
|
|
|
|
m_formats[1] = QLatin1String("BMP");
|
|
|
|
m_formats[2] = QLatin1String("JPG");
|
|
|
|
m_formats[3] = QLatin1String("PPM");
|
|
|
|
m_formats[4] = QLatin1String("TIFF");
|
2013-05-31 13:46:45 +02:00
|
|
|
m_formats[5] = QLatin1String("PDF");
|
2013-05-31 12:42:54 +02:00
|
|
|
|
|
|
|
QHashIterator<int, QString> i(m_formats);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
ui->formats->addItem(tr("Save as %1").arg(i.value()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set png as a default format
|
|
|
|
m_pageTitle = m_view->title();
|
|
|
|
ui->location->setText(QString("%1/%2.png").arg(QDir::homePath(), QzTools::filterCharsFromFilename(m_pageTitle)));
|
|
|
|
|
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
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
connect(ui->changeLocation, SIGNAL(clicked()), this, SLOT(changeLocation()));
|
|
|
|
connect(ui->formats, SIGNAL(currentIndexChanged(int)), this, SLOT(formatChanged()));
|
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
|
|
|
}
|
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
void PageScreen::formatChanged()
|
|
|
|
{
|
|
|
|
QString text = ui->location->text();
|
|
|
|
int pos = text.lastIndexOf(QLatin1Char('.'));
|
|
|
|
|
|
|
|
if (pos > -1) {
|
|
|
|
text = text.left(pos + 1) + m_formats[ui->formats->currentIndex()].toLower();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
text.append(QLatin1Char('.') + m_formats[ui->formats->currentIndex()].toLower());
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->location->setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageScreen::changeLocation()
|
2011-09-21 14:20:49 +02:00
|
|
|
{
|
2013-05-31 12:42:54 +02:00
|
|
|
const QString &suggestedPath = QString("%1/%2.%3").arg(QDir::homePath(), QzTools::filterCharsFromFilename(m_pageTitle),
|
|
|
|
m_formats[ui->formats->currentIndex()].toLower());
|
2013-05-31 18:11:19 +02:00
|
|
|
const QString &path = QFileDialog::getOpenFileName(this, tr("Choose location..."), suggestedPath);
|
2013-01-22 16:17:50 +01:00
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
if (!path.isEmpty()) {
|
|
|
|
ui->location->setText(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageScreen::dialogAccepted()
|
|
|
|
{
|
|
|
|
if (!ui->location->text().isEmpty()) {
|
2013-06-02 15:49:40 +02:00
|
|
|
if (QFile::exists(ui->location->text())) {
|
|
|
|
const QString &text = tr("File '%1' already exists. Do you want to overwrite it?").arg(ui->location->text());
|
|
|
|
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("File already exists"), text,
|
|
|
|
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
|
|
|
|
|
|
|
|
if (button != QMessageBox::Yes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
QApplication::setOverrideCursor(Qt::WaitCursor);
|
2013-05-31 13:46:45 +02:00
|
|
|
|
|
|
|
const QString &format = m_formats[ui->formats->currentIndex()];
|
2013-06-01 14:47:39 +02:00
|
|
|
if (format == QLatin1String("PDF")) {
|
2013-05-31 13:46:45 +02:00
|
|
|
saveAsDocument(format);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
saveAsImage(format);
|
|
|
|
}
|
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
QApplication::restoreOverrideCursor();
|
2013-01-22 16:17:50 +01:00
|
|
|
|
2013-05-31 12:42:54 +02:00
|
|
|
close();
|
2013-01-22 16:17:50 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-21 14:20:49 +02:00
|
|
|
|
2013-05-31 13:46:45 +02:00
|
|
|
void PageScreen::saveAsImage(const QString &format)
|
2013-01-22 16:17:50 +01:00
|
|
|
{
|
2013-05-31 13:46:45 +02:00
|
|
|
const QString &suffix = QLatin1Char('.') + format.toLower();
|
2013-05-31 12:42:54 +02:00
|
|
|
|
|
|
|
QString pathWithoutSuffix = ui->location->text();
|
|
|
|
if (pathWithoutSuffix.endsWith(suffix, Qt::CaseInsensitive)) {
|
|
|
|
pathWithoutSuffix = pathWithoutSuffix.mid(0, pathWithoutSuffix.length() - suffix.length());
|
2013-01-22 16:17:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pageImages.count() == 1) {
|
2013-05-31 12:42:54 +02:00
|
|
|
m_pageImages.first().save(pathWithoutSuffix + suffix, format.toUtf8());
|
2013-01-22 16:17:50 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int part = 1;
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QImage &image, m_pageImages) {
|
2013-01-22 16:17:50 +01:00
|
|
|
const QString &fileName = pathWithoutSuffix + ".part" + QString::number(part);
|
2013-05-31 12:42:54 +02:00
|
|
|
image.save(fileName + suffix, format.toUtf8());
|
2013-01-22 16:17:50 +01:00
|
|
|
part++;
|
2012-03-17 16:01:12 +01:00
|
|
|
}
|
2013-01-22 16:17:50 +01:00
|
|
|
}
|
2011-09-21 14:20:49 +02:00
|
|
|
}
|
|
|
|
|
2013-05-31 13:46:45 +02:00
|
|
|
void PageScreen::saveAsDocument(const QString &format)
|
|
|
|
{
|
|
|
|
const QString &suffix = QLatin1Char('.') + format.toLower();
|
|
|
|
|
|
|
|
QString pathWithoutSuffix = ui->location->text();
|
|
|
|
if (pathWithoutSuffix.endsWith(suffix, Qt::CaseInsensitive)) {
|
|
|
|
pathWithoutSuffix = pathWithoutSuffix.mid(0, pathWithoutSuffix.length() - suffix.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
QPrinter printer;
|
|
|
|
printer.setCreator(QupZilla::tr("QupZilla %1 (%2)").arg(QupZilla::VERSION, QupZilla::WWWADDRESS));
|
|
|
|
printer.setOutputFileName(pathWithoutSuffix + suffix);
|
2013-06-01 14:47:39 +02:00
|
|
|
printer.setOutputFormat(QPrinter::PdfFormat);
|
2013-05-31 13:46:45 +02:00
|
|
|
printer.setPaperSize(m_pageImages.first().size(), QPrinter::DevicePixel);
|
|
|
|
printer.setPageMargins(0, 0, 0, 0, QPrinter::DevicePixel);
|
|
|
|
printer.setFullPage(true);
|
|
|
|
|
|
|
|
QPainter painter;
|
|
|
|
painter.begin(&printer);
|
|
|
|
|
|
|
|
for (int i = 0; i < m_pageImages.size(); ++i) {
|
|
|
|
const QImage &image = m_pageImages.at(i);
|
|
|
|
painter.drawImage(0, 0, image);
|
|
|
|
|
|
|
|
if (i != m_pageImages.size() - 1) {
|
|
|
|
printer.newPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-05-31 13:46:45 +02:00
|
|
|
// We will split rendering page into smaller parts to avoid infinite loops
|
|
|
|
// or crashes.
|
|
|
|
|
2013-01-22 16:17:50 +01:00
|
|
|
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;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QImage &image, m_pageImages) {
|
2013-01-22 16:17:50 +01:00
|
|
|
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;
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QImage &image, scaledImages) {
|
2013-01-22 16:17:50 +01:00
|
|
|
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;
|
|
|
|
}
|