From cbf0651486fb4ef97f6e21b32e4aa382908f8e49 Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sun, 15 Nov 2020 13:06:57 +0100 Subject: [PATCH] All tiles exported --- src/app/puzzle/vpmainwindow.cpp | 225 ++++++++++++++++++-------------- src/app/puzzle/vpmainwindow.h | 6 + 2 files changed, 135 insertions(+), 96 deletions(-) diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index fa49e302f..14f621bc3 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -702,6 +702,132 @@ bool VPMainWindow::MaybeSave() return true; } +//--------------------------------------------------------------------------------------------------------------------- +void VPMainWindow::generateTiledPdf(QString fileName) +{ + if(not fileName.isEmpty()) + { + m_graphicsView->PrepareForExport(); + + PageOrientation tilesOrientation = m_layout->GetTilesOrientation(); + QSizeF tilesSize = m_layout->GetTilesSize(); + QMarginsF tilesMargins = m_layout->GetTilesMargins(); + + + // ------------- Set up the printer + QPrinter* printer = new QPrinter(); + + printer->setCreator(QGuiApplication::applicationDisplayName()+QChar(QChar::Space)+ + QCoreApplication::applicationVersion()); + printer->setOrientation(QPrinter::Portrait); // in the pdf file the pages should always be in portrait + + // here we might need to so some rounding for the size. + printer->setPageSize(QPageSize(m_layout->GetTilesSize(Unit::Mm), + QPageSize::Millimeter)); + printer->setFullPage(true); + const bool success = printer->setPageMargins(m_layout->GetTilesMargins(Unit::Mm), QPageLayout::Millimeter); + if (not success) + { + qWarning() << tr("Cannot set printer margins"); + } + + #ifdef Q_OS_MAC + printer->setOutputFormat(QPrinter::NativeFormat); + #else + printer->setOutputFormat(QPrinter::PdfFormat); + #endif + + printer->setOutputFileName(fileName); + printer->setResolution(static_cast(PrintDPI)); + + printer->setDocName("Test"); + + + // ------------- Set up the painter + QPainter painter; + if (not painter.begin(printer)) + { // failed to open file + qCritical() << tr("Failed to open file, is it writable?"); + return; + } + painter.setFont( QFont( QStringLiteral("Arial"), 8, QFont::Normal ) ); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + painter.setBrush ( QBrush ( Qt::NoBrush ) ); + + if(tilesOrientation == PageOrientation::Landscape) + { + // The landscape tiles have to be rotated, because the pages + // stay portrait in the pdf + painter.rotate(90); + painter.translate(0, -ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm)); + } + + + // ------------- Prepare infos for the tiling + qreal tilesDrawingAreaHeight = (tilesOrientation == PageOrientation::Portrait)? + tilesSize.height() : tilesSize.width(); + tilesDrawingAreaHeight -= + tilesMargins.top() + tilesMargins.bottom() + UnitConvertor(1, Unit::Cm, Unit::Px); + + // the -1cm is for test purpuses, it correspondings to the overlaping for gluing the parts, + // later we'll have a proper abstract value + + qreal tilesDrawingAreaWidth = (tilesOrientation == PageOrientation::Portrait)? + tilesSize.width() : tilesSize.height(); + tilesDrawingAreaWidth -= + tilesMargins.left() + tilesMargins.right() + UnitConvertor(1, Unit::Cm, Unit::Px); + + + QSizeF sheetSize = m_layout->GetFocusedSheet()->GetSheetSize(); + qreal drawingWidth = 0; + qreal drawingHeight = 0; + + if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Portrait) + { + drawingWidth = sheetSize.width(); + drawingHeight = sheetSize.height(); + } + else + { + drawingWidth = sheetSize.height(); + drawingHeight = sheetSize.width(); + } + + int nbCol = qCeil(drawingWidth/tilesDrawingAreaWidth); + int nbRow = qCeil(drawingHeight/tilesDrawingAreaHeight); + + // ------------- Perform the tiling + for(int i=0;inewPage()) + { + qWarning("failed in flushing page to disk, disk full?"); + return; + } + } + + QRectF source = QRectF(j*tilesDrawingAreaWidth, i*tilesDrawingAreaHeight, tilesDrawingAreaWidth, tilesDrawingAreaHeight); + QRectF target = QRectF(m_layout->GetTilesMargins().left(), m_layout->GetTilesMargins().top(), + source.width(), source.height()); + + // TODO: add the lines etc + + m_graphicsView->GetScene()->render(&painter, target, source, Qt::IgnoreAspectRatio); + } + } + + painter.end(); + + m_graphicsView->CleanAfterExport(); + } +} + + //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::on_actionNew_triggered() { @@ -811,7 +937,8 @@ void VPMainWindow::on_actionSave_triggered() //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::on_actionSaveAs_triggered() { - // TODO / FIXME : See valentina how the save is done over there. we need to add the extension .vlt, check for empty file names etc. + // TODO / FIXME : See valentina how the save is done over there. we need to add the + // extension .vlt, check for empty file names etc. //Get list last open files QStringList recentFiles = qApp->PuzzleSettings()->GetRecentFileList(); @@ -1060,101 +1187,7 @@ void VPMainWindow::on_pushButtonTilesExport_clicked() #endif ); - if(not fileName.isEmpty()) - { - // tests for now, later we want this in a separated function - - m_graphicsView->PrepareForExport(); - - PageOrientation tilesOrientation = m_layout->GetTilesOrientation(); - QSizeF tilesSize = m_layout->GetTilesSize(); - QMarginsF tilesMargins = m_layout->GetTilesMargins(); - - - // ------------- Set up the printer - QPrinter* printer = new QPrinter(); - - printer->setCreator(QGuiApplication::applicationDisplayName()+QChar(QChar::Space)+ - QCoreApplication::applicationVersion()); - printer->setOrientation(QPrinter::Portrait); // in the pdf file the pages should always be in portrait - - // here we might need to so some rounding for the size. - printer->setPageSize(QPageSize(m_layout->GetTilesSize(Unit::Mm), - QPageSize::Millimeter)); - printer->setFullPage(true); - const bool success = printer->setPageMargins(m_layout->GetTilesMargins(Unit::Mm), QPageLayout::Millimeter); - if (not success) - { - qWarning() << tr("Cannot set printer margins"); - } - - #ifdef Q_OS_MAC - printer->setOutputFormat(QPrinter::NativeFormat); - #else - printer->setOutputFormat(QPrinter::PdfFormat); - #endif - - printer->setOutputFileName(fileName); - printer->setResolution(static_cast(PrintDPI)); - - printer->setDocName("Test"); - - - // ------------- Set up the painter - QPainter painter; - if (not painter.begin(printer)) - { // failed to open file - qCritical() << tr("Failed to open file, is it writable?"); - return; - } - painter.setFont( QFont( QStringLiteral("Arial"), 8, QFont::Normal ) ); - painter.setRenderHint(QPainter::Antialiasing, true); - painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); - painter.setBrush ( QBrush ( Qt::NoBrush ) ); - - if(tilesOrientation == PageOrientation::Landscape) - { - // The landscape tiles have to be rotated, because the pages - // stay portrait in the pdf - painter.rotate(90); - painter.translate(0, -ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm)); - } - - - // ------------- Perform the Tiling - qreal tilesDrawingAreaHeight = (tilesOrientation == PageOrientation::Portrait)? - tilesSize.height() : tilesSize.width(); - tilesDrawingAreaHeight -= - tilesMargins.top() + tilesMargins.bottom() + UnitConvertor(1, Unit::Cm, Unit::Px); - - // the -1cm is for test purpuses, it correspondings to the overlaping for gluing the parts, - // later we'll have a proper abstract value - - qreal tilesDrawingAreaWidth = (tilesOrientation == PageOrientation::Portrait)? - tilesSize.width() : tilesSize.height(); - tilesDrawingAreaWidth -= - tilesMargins.left() + tilesMargins.right() + UnitConvertor(1, Unit::Cm, Unit::Px); - - - QRectF source = QRectF(0,0,tilesDrawingAreaWidth, tilesDrawingAreaHeight); - - QRectF target = QRectF(m_layout->GetTilesMargins().left(), m_layout->GetTilesMargins().top(), - source.width(), source.height()); - m_graphicsView->GetScene()->render(&painter, target, source, Qt::IgnoreAspectRatio); - - if (not printer->newPage()) - { - qWarning("failed in flushing page to disk, disk full?"); - return; - } - - source = QRectF(tilesDrawingAreaWidth,0,tilesDrawingAreaWidth, tilesDrawingAreaHeight); - m_graphicsView->GetScene()->render(&painter, target, source, Qt::IgnoreAspectRatio); - - painter.end(); - - m_graphicsView->CleanAfterExport(); - } + generateTiledPdf(fileName); } diff --git a/src/app/puzzle/vpmainwindow.h b/src/app/puzzle/vpmainwindow.h index dc1145e26..e11a28956 100644 --- a/src/app/puzzle/vpmainwindow.h +++ b/src/app/puzzle/vpmainwindow.h @@ -217,6 +217,12 @@ private: bool MaybeSave(); + /** + * @brief generateTiledPdf Generates the tiled Pdf in the given filename + * @param filename + */ + void generateTiledPdf(QString fileName); + private slots: /**