1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

[PageScreen] Add possibility to save into PDF and PostScript

Closes #903
This commit is contained in:
nowrep 2013-05-31 13:46:45 +02:00
parent bb03cfb133
commit 772427ef09
3 changed files with 77 additions and 25 deletions

View File

@ -20,6 +20,7 @@
#include "tabbedwebview.h"
#include "webpage.h"
#include "qztools.h"
#include "qupzilla.h"
#include <QFileDialog>
#include <QWebFrame>
@ -28,6 +29,7 @@
#include <QMovie>
#include <QPushButton>
#include <QCloseEvent>
#include <QPrinter>
#if QT_VERSION >= 0x050000
#include <QtConcurrent/QtConcurrentRun>
@ -49,6 +51,8 @@ PageScreen::PageScreen(WebView* view, QWidget* parent)
m_formats[2] = QLatin1String("JPG");
m_formats[3] = QLatin1String("PPM");
m_formats[4] = QLatin1String("TIFF");
m_formats[5] = QLatin1String("PDF");
m_formats[6] = QLatin1String("PS");
QHashIterator<int, QString> i(m_formats);
while (i.hasNext()) {
@ -102,17 +106,24 @@ void PageScreen::dialogAccepted()
{
if (!ui->location->text().isEmpty()) {
QApplication::setOverrideCursor(Qt::WaitCursor);
saveScreen();
const QString &format = m_formats[ui->formats->currentIndex()];
if (format == QLatin1String("PDF") || format == QLatin1String("PS")) {
saveAsDocument(format);
}
else {
saveAsImage(format);
}
QApplication::restoreOverrideCursor();
close();
}
}
void PageScreen::saveScreen()
void PageScreen::saveAsImage(const QString &format)
{
const QString &format = m_formats[ui->formats->currentIndex()];
const QString &suffix = QLatin1Char('.') + m_formats[ui->formats->currentIndex()].toLower();
const QString &suffix = QLatin1Char('.') + format.toLower();
QString pathWithoutSuffix = ui->location->text();
if (pathWithoutSuffix.endsWith(suffix, Qt::CaseInsensitive)) {
@ -132,6 +143,38 @@ void PageScreen::saveScreen()
}
}
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);
printer.setOutputFormat(format == QLatin1String("PDF") ? QPrinter::PdfFormat : QPrinter::PostScriptFormat);
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();
}
void PageScreen::createThumbnail()
{
QWebPage* page = m_view->page();
@ -146,9 +189,9 @@ void PageScreen::createThumbnail()
int yPosition = 0;
bool canScroll = frameSize.height() > heightLimit;
/* We will split rendering page into smaller parts to avoid infinite loops
* or crashes.
*/
// We will split rendering page into smaller parts to avoid infinite loops
// or crashes.
do {
int remainingHeight = frameSize.height() - yPosition;
if (remainingHeight <= 0) {

View File

@ -49,7 +49,9 @@ private slots:
void dialogAccepted();
private:
void saveScreen();
void saveAsImage(const QString &format);
void saveAsDocument(const QString &format);
void createPixmap();
Ui::PageScreen* ui;

View File

@ -28,7 +28,7 @@
<x>0</x>
<y>0</y>
<width>531</width>
<height>271</height>
<height>264</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
@ -50,21 +50,8 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
@ -109,7 +96,20 @@
</item>
</layout>
</item>
<item>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -122,6 +122,13 @@
</property>
</spacer>
</item>
<item row="0" column="0" colspan="3">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>