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

View File

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

View File

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