From f90f8ca01b5ded2977228ee9a0b75399a8548adf Mon Sep 17 00:00:00 2001 From: Ronan Le Tiec Date: Sun, 15 Nov 2020 12:30:29 +0100 Subject: [PATCH] First tiles export --- src/app/puzzle/vplayout.cpp | 18 +++++-- src/app/puzzle/vplayout.h | 21 ++++++-- src/app/puzzle/vpmainwindow.cpp | 90 +++++++++++++++++++++++++++++++-- src/app/puzzle/vpmainwindow.ui | 12 ++++- 4 files changed, 129 insertions(+), 12 deletions(-) diff --git a/src/app/puzzle/vplayout.cpp b/src/app/puzzle/vplayout.cpp index 6744850d1..ef81a0694 100644 --- a/src/app/puzzle/vplayout.cpp +++ b/src/app/puzzle/vplayout.cpp @@ -268,16 +268,22 @@ QSizeF VPLayout::GetTilesSize() const } //--------------------------------------------------------------------------------------------------------------------- -QSizeF VPLayout::GetTilesSizeConverted() const +QSizeF VPLayout::GetTilesSize(Unit unit) const { QSizeF convertedSize = QSizeF( - UnitConvertor(m_tilesSize.width(), Unit::Px, GetUnit()), - UnitConvertor(m_tilesSize.height(), Unit::Px, GetUnit()) + UnitConvertor(m_tilesSize.width(), Unit::Px, unit), + UnitConvertor(m_tilesSize.height(), Unit::Px, unit) ); return convertedSize; } +//--------------------------------------------------------------------------------------------------------------------- +QSizeF VPLayout::GetTilesSizeConverted() const +{ + return GetTilesSize(GetUnit()); +} + //--------------------------------------------------------------------------------------------------------------------- PageOrientation VPLayout::GetTilesOrientation() { @@ -330,6 +336,12 @@ QMarginsF VPLayout::GetTilesMargins() const return m_tilesMargins; } +//--------------------------------------------------------------------------------------------------------------------- +QMarginsF VPLayout::GetTilesMargins(Unit unit) const +{ + return UnitConvertor(m_tilesMargins, Unit::Px, unit); +} + //--------------------------------------------------------------------------------------------------------------------- QMarginsF VPLayout::GetTilesMarginsConverted() const { diff --git a/src/app/puzzle/vplayout.h b/src/app/puzzle/vplayout.h index 144578b46..e8ca4d933 100644 --- a/src/app/puzzle/vplayout.h +++ b/src/app/puzzle/vplayout.h @@ -154,21 +154,27 @@ public: /** * @brief SetTilesSize sets the size of the tiles, the values have to be in Unit::Px - * @param size sheet size + * @param size tiles size */ void SetTilesSize(const QSizeF &size); /** * @brief SetTilesSizeConverted sets the size of the tiles, the values have to be in the layout's unit - * @param size sheet size + * @param size tiles size */ void SetTilesSizeConverted(const QSizeF &size); /** * @brief GetTilesSize Returns the size of the tiles in Unit::Px - * @return sheet size in Unit::Px + * @return tiles size in Unit::Px */ QSizeF GetTilesSize() const; + /** + * @brief GetTilesSize Returns the size of the tiles in given Unit + * @return tiles size + */ + QSizeF GetTilesSize(Unit unit) const; + /** * @brief GetTilesSizeConverted Returns the size of the tiles in the layout's unit * @return the size in the layout's unit @@ -219,10 +225,17 @@ public: /** * @brief GetTilesMargins Returns margins of the tiles in Unit::Px - * @return the size in Unit::Px + * @return the margins in Unit::Px */ QMarginsF GetTilesMargins() const; + /** + * @brief GetTilesMargins Returns margins of the tiles in the given unit + * @param unit the unit in which we want the margins + * @return the margins in the given unit + */ + QMarginsF GetTilesMargins(Unit unit) const; + /** * @brief GetTilesMarginsConverted Returns the margins of the tiles in the layout's unit * @return the margins in the tiles's unit diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 9b4a182d2..fa49e302f 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -496,7 +496,7 @@ void VPMainWindow::SetPropertyTabTilesData() SetDoubleSpinBoxValue(ui->doubleSpinBoxTilesMarginBottom, margins.bottom()); // set "show tiles" checkbox - ui->checkBoxTilesShowTiles->setChecked(m_layout->GetShowTiles()); + SetCheckBoxValue(ui->checkBoxTilesShowTiles, m_layout->GetShowTiles()); } //--------------------------------------------------------------------------------------------------------------------- @@ -913,8 +913,9 @@ void VPMainWindow::on_comboBoxLayoutUnit_currentIndexChanged(int index) m_layout->SetUnit(Unit::Inch); } - SetPropertyTabSheetData(); SetPropertyTabCurrentPieceData(); + SetPropertyTabSheetData(); + SetPropertyTabTilesData(); } //--------------------------------------------------------------------------------------------------------------------- @@ -1061,13 +1062,96 @@ void VPMainWindow::on_pushButtonTilesExport_clicked() 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(); - // TODO : Tiles export + // ------------- 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(); } diff --git a/src/app/puzzle/vpmainwindow.ui b/src/app/puzzle/vpmainwindow.ui index 6e7a241c5..72f44bece 100644 --- a/src/app/puzzle/vpmainwindow.ui +++ b/src/app/puzzle/vpmainwindow.ui @@ -1014,7 +1014,11 @@ - + + + 100000.000000000000000 + + @@ -1024,7 +1028,11 @@ - + + + 100000.000000000000000 + +