diff --git a/src/app/puzzle/puzzle.pri b/src/app/puzzle/puzzle.pri index 5c90f52f5..c0915d854 100644 --- a/src/app/puzzle/puzzle.pri +++ b/src/app/puzzle/puzzle.pri @@ -12,6 +12,7 @@ SOURCES += \ $$PWD/vpcommands.cpp \ $$PWD/vpgraphicspiece.cpp \ $$PWD/vpgraphicssheet.cpp \ + $$PWD/vpgraphicstilegrid.cpp \ $$PWD/vplayout.cpp \ $$PWD/vpmaingraphicsview.cpp \ $$PWD/vpmainwindow.cpp \ @@ -20,6 +21,7 @@ SOURCES += \ $$PWD/vppiecelist.cpp \ $$PWD/vpsettings.cpp \ $$PWD/vpsheet.cpp \ + $$PWD/vptilefactory.cpp \ $$PWD/xml/vplayoutfilereader.cpp \ $$PWD/xml/vplayoutfilewriter.cpp \ $$PWD/xml/vplayoutliterals.cpp @@ -36,6 +38,7 @@ HEADERS += \ $$PWD/vpcommands.h \ $$PWD/vpgraphicspiece.h \ $$PWD/vpgraphicssheet.h \ + $$PWD/vpgraphicstilegrid.h \ $$PWD/vplayout.h \ $$PWD/vpmaingraphicsview.h \ $$PWD/vpmainwindow.h \ @@ -45,6 +48,7 @@ HEADERS += \ $$PWD/vpsettings.h \ $$PWD/vpsheet.h \ $$PWD/vpstable.h \ + $$PWD/vptilefactory.h \ $$PWD/xml/vplayoutfilereader.h \ $$PWD/xml/vplayoutfilewriter.h \ $$PWD/xml/vplayoutliterals.h diff --git a/src/app/puzzle/vpgraphicssheet.cpp b/src/app/puzzle/vpgraphicssheet.cpp index e07037c56..5c9638faf 100644 --- a/src/app/puzzle/vpgraphicssheet.cpp +++ b/src/app/puzzle/vpgraphicssheet.cpp @@ -70,66 +70,6 @@ void VPGraphicsSheet::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->drawRect(GetSheetRect()); } - // show the tiles grid. Maybe it shouldn't be in the graphics sheet, maybe better in maingraphicsview - VPLayout* layout = m_sheet->GetLayout(); - if(layout->GetShowTiles()) - { - pen.setColor(QColor(255,0,0,127)); - pen.setStyle(Qt::DashLine); - painter->setPen(pen); - - QSizeF tilesSize = layout->GetTilesSize(); - QSizeF sheetSize = m_sheet->GetSheetSize(); - - QMarginsF tilesMargins = layout->GetTilesMargins(); - - PageOrientation tilesOrientation = layout->GetTilesOrientation(); - PageOrientation sheetOrientation = m_sheet->GetOrientation(); - - qreal colWidth = 0; - qreal rowHeight = 0; - if(tilesOrientation == PageOrientation::Portrait) - { - colWidth = tilesSize.width() - (tilesMargins.left()+ tilesMargins.right() + UnitConvertor(1, Unit::Cm, Unit::Px)); - rowHeight = tilesSize.height() - (tilesMargins.top()+ tilesMargins.bottom() + UnitConvertor(1, Unit::Cm, Unit::Px)); - } - else - { - colWidth = tilesSize.height() - (tilesMargins.left()+ tilesMargins.right() + UnitConvertor(1, Unit::Cm, Unit::Px)); - rowHeight = tilesSize.width() - (tilesMargins.top()+ tilesMargins.bottom() + UnitConvertor(1, Unit::Cm, Unit::Px)); - } - // the "+ UnitConvertor(1, Unit::Cm, Unit::Px)" is because of the part for gluing and where we - // have infos of the single tile. Maybe it's not the right value, to be corrected. - - - qreal drawingWidth = 0; - qreal drawingHeight = 0; - - if(sheetOrientation == PageOrientation::Portrait) - { - drawingWidth = sheetSize.width(); - drawingHeight = sheetSize.height(); - } - else - { - drawingWidth = sheetSize.height(); - drawingHeight = sheetSize.width(); - } - - int nbCol = qCeil(drawingWidth/colWidth); - int nbRow = qCeil(drawingHeight/rowHeight); - - for(int i=0;i<=nbCol;i++) - { - painter->drawLine(QPointF(i*colWidth, 0), QPointF(i*colWidth,nbRow*rowHeight)); - } - - for(int j=0;j<=nbRow;j++) - { - painter->drawLine(QPointF(0, j*rowHeight), QPointF(nbCol*colWidth, j*rowHeight)); - } - } - m_boundingRect = GetSheetRect(); } diff --git a/src/app/puzzle/vpgraphicstilegrid.cpp b/src/app/puzzle/vpgraphicstilegrid.cpp new file mode 100644 index 000000000..45e905bd9 --- /dev/null +++ b/src/app/puzzle/vpgraphicstilegrid.cpp @@ -0,0 +1,78 @@ +#include "vpgraphicstilegrid.h" + +#include "vptilefactory.h" +#include "vplayout.h" + +//--------------------------------------------------------------------------------------------------------------------- +VPGraphicsTileGrid::VPGraphicsTileGrid(VPLayout *layout, VPTileFactory *tileFactory,QGraphicsItem *parent): + QGraphicsItem(parent), + m_tileFactory(tileFactory), + m_layout(layout) +{ + +} + +//--------------------------------------------------------------------------------------------------------------------- +VPGraphicsTileGrid::~VPGraphicsTileGrid() +{ + +} + +//--------------------------------------------------------------------------------------------------------------------- +QRectF VPGraphicsTileGrid::boundingRect() const +{ + if(m_layout->GetShowTiles()) + { + return QRectF(0, + 0, + m_tileFactory->getColNb()* m_tileFactory->getDrawingAreaWidth(), + m_tileFactory->getRowNb()* m_tileFactory->getDrawingAreaHeight() + ); + } + else + { + return QRectF(0,0,0,0); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VPGraphicsTileGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + Q_UNUSED(option); + + if(m_layout->GetShowTiles()) + { + QPen pen(QColor(255,0,0,127), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); + pen.setCosmetic(true); + pen.setStyle(Qt::DashLine); + QBrush noBrush(Qt::NoBrush); + painter->setPen(pen); + painter->setBrush(noBrush); + + for(int i=0;i<=m_tileFactory->getColNb();i++) + { + painter->drawLine(QPointF( + i*m_tileFactory->getDrawingAreaWidth(), + 0), + QPointF( + i*m_tileFactory->getDrawingAreaWidth(), + m_tileFactory->getRowNb()*m_tileFactory->getDrawingAreaHeight() + ) + ); + } + + for(int j=0;j<=m_tileFactory->getRowNb();j++) + { + painter->drawLine(QPointF( + 0, + j*m_tileFactory->getDrawingAreaHeight() + ), + QPointF( + m_tileFactory->getColNb()*m_tileFactory->getDrawingAreaWidth(), + j*m_tileFactory->getDrawingAreaHeight() + ) + ); + } + } +} diff --git a/src/app/puzzle/vpgraphicstilegrid.h b/src/app/puzzle/vpgraphicstilegrid.h new file mode 100644 index 000000000..9559f3e55 --- /dev/null +++ b/src/app/puzzle/vpgraphicstilegrid.h @@ -0,0 +1,57 @@ +/************************************************************************ + ** + ** @file vpgraphicstilegrid.h + ** @author Ronan Le Tiec + ** @date 19 11, 2020 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2020 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ + +#ifndef VPGRAPHICSTILEGRID_H +#define VPGRAPHICSTILEGRID_H + +#include +#include + +#include "../vmisc/def.h" + +class VPTileFactory; +class VPLayout; + +class VPGraphicsTileGrid : public QGraphicsItem +{ +public: + explicit VPGraphicsTileGrid(VPLayout* layout, VPTileFactory *tileFactory, QGraphicsItem *parent = nullptr); + ~VPGraphicsTileGrid(); + + QRectF boundingRect() const override; + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + + +private: + Q_DISABLE_COPY(VPGraphicsTileGrid) + + VPTileFactory *m_tileFactory{nullptr}; + VPLayout *m_layout{nullptr}; +}; + +#endif // VPGRAPHICSTILEGRID_H diff --git a/src/app/puzzle/vpmaingraphicsview.cpp b/src/app/puzzle/vpmaingraphicsview.cpp index 6e63b4643..d7241a72c 100644 --- a/src/app/puzzle/vpmaingraphicsview.cpp +++ b/src/app/puzzle/vpmaingraphicsview.cpp @@ -37,6 +37,7 @@ #include "vplayout.h" #include "vpsheet.h" #include "../vwidgets/vmaingraphicsscene.h" +#include "vptilefactory.h" #include @@ -44,7 +45,7 @@ Q_LOGGING_CATEGORY(pMainGraphicsView, "p.mainGraphicsView") //--------------------------------------------------------------------------------------------------------------------- -VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, QWidget *parent) : +VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent) : VMainGraphicsView(parent), m_layout(layout) { @@ -58,6 +59,9 @@ VPMainGraphicsView::VPMainGraphicsView(VPLayout *layout, QWidget *parent) : setAcceptDrops(true); + m_graphicsTileGrid = new VPGraphicsTileGrid(layout, tileFactory); + m_scene->addItem(m_graphicsTileGrid); + // add the connections connect(m_layout, &VPLayout::PieceMovedToPieceList, this, &VPMainGraphicsView::on_PieceMovedToPieceList); connect(m_scene, &VMainGraphicsScene::selectionChanged, this, @@ -71,6 +75,8 @@ void VPMainGraphicsView::RefreshLayout() m_graphicsSheet->update(); + m_graphicsTileGrid->update(); + m_scene->update(); } diff --git a/src/app/puzzle/vpmaingraphicsview.h b/src/app/puzzle/vpmaingraphicsview.h index 5641bf2d2..b1c84bc90 100644 --- a/src/app/puzzle/vpmaingraphicsview.h +++ b/src/app/puzzle/vpmaingraphicsview.h @@ -31,16 +31,19 @@ #include "vpgraphicssheet.h" #include "vpgraphicspiece.h" +#include "vptilefactory.h" +#include "vpgraphicstilegrid.h" #include "../vwidgets/vmaingraphicsview.h" class VMainGraphicsScene; +class VPTileFactory; class VPMainGraphicsView : public VMainGraphicsView { Q_OBJECT public: - VPMainGraphicsView(VPLayout *layout, QWidget *parent); + VPMainGraphicsView(VPLayout *layout, VPTileFactory *tileFactory, QWidget *parent); ~VPMainGraphicsView() = default; /** @@ -74,6 +77,8 @@ protected: void keyPressEvent(QKeyEvent *event) override; + void drawTilesLine(); + private slots: /** * @brief on_PieceMovedToPieceList The slot is called when the given piece was moved from the given piece list to the other @@ -95,6 +100,9 @@ private: VMainGraphicsScene *m_scene{nullptr}; VPGraphicsSheet *m_graphicsSheet{nullptr}; + + VPGraphicsTileGrid *m_graphicsTileGrid{nullptr}; + VPLayout *m_layout{nullptr}; QList m_graphicsPieces{}; diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 07cef248a..a1983ed7d 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "ui_vpmainwindow.h" #include "dialogs/vpdialogabout.h" @@ -45,7 +46,6 @@ #include "vpsheet.h" #include -#include QT_WARNING_PUSH QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes") @@ -91,6 +91,11 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) : InitMenuBar(); InitProperties(); InitCarrousel(); + + // init the tile factory + m_tileFactory = new VPTileFactory(m_layout, qApp->Settings()); + m_tileFactory->refreshTileInfos(); + InitMainGraphics(); InitZoomToolBar(); @@ -98,6 +103,8 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) : SetPropertiesData(); ReadSettings(); + + } //--------------------------------------------------------------------------------------------------------------------- @@ -525,7 +532,7 @@ void VPMainWindow::SetPropertyTabLayoutData() //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::InitMainGraphics() { - m_graphicsView = new VPMainGraphicsView(m_layout, this); + m_graphicsView = new VPMainGraphicsView(m_layout, m_tileFactory, this); ui->centralWidget->layout()->addWidget(m_graphicsView); m_graphicsView->RefreshLayout(); @@ -708,10 +715,9 @@ void VPMainWindow::generateTiledPdf(QString fileName) if(not fileName.isEmpty()) { m_graphicsView->PrepareForExport(); + m_tileFactory->refreshTileInfos(); PageOrientation tilesOrientation = m_layout->GetTilesOrientation(); - QSizeF tilesSize = m_layout->GetTilesSize(); - QMarginsF tilesMargins = m_layout->GetTilesMargins(); // ------------- Set up the printer QPrinter* printer = new QPrinter(); @@ -724,11 +730,6 @@ void VPMainWindow::generateTiledPdf(QString fileName) 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); @@ -740,7 +741,6 @@ void VPMainWindow::generateTiledPdf(QString fileName) printer->setResolution(static_cast(PrintDPI)); printer->setDocName("Test"); // FIXME - // ------------- Set up the painter QPainter painter; if (not painter.begin(printer)) @@ -751,9 +751,6 @@ void VPMainWindow::generateTiledPdf(QString fileName) painter.setFont( QFont( QStringLiteral("Arial"), 8, QFont::Normal ) ); painter.setRenderHint(QPainter::Antialiasing, true); painter.setBrush ( QBrush ( Qt::NoBrush ) ); - QPen penTileInfos = QPen(QColor(180,180,180), qApp->Settings()->WidthHairLine(), Qt::DashLine, Qt::RoundCap, Qt::RoundJoin); - QPen penTileDrawing = QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); - if(tilesOrientation == PageOrientation::Landscape) { @@ -763,99 +760,9 @@ void VPMainWindow::generateTiledPdf(QString fileName) 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) + for(int row=0;rowgetRowNb();row++) // for each row of the tiling grid { - drawingWidth = sheetSize.width(); - drawingHeight = sheetSize.height(); - } - else - { - drawingWidth = sheetSize.height(); - drawingHeight = sheetSize.width(); - } - - int nbCol = qCeil(drawingWidth/tilesDrawingAreaWidth); - int nbRow = qCeil(drawingHeight/tilesDrawingAreaHeight); - - - // ------------- prepare triangles for positioning - // top triangle - QRectF rectTop = QRectF(tilesMargins.left()+ tilesDrawingAreaWidth/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), - tilesMargins.top(), - UnitConvertor(1, Unit::Cm, Unit::Px), - UnitConvertor(0.5, Unit::Cm, Unit::Px) - ); - QPainterPath triangleTop; - triangleTop.moveTo(rectTop.topLeft()); - triangleTop.lineTo(rectTop.topRight()); - triangleTop.lineTo(rectTop.left() + (rectTop.width() / 2), rectTop.bottom()); - triangleTop.lineTo(rectTop.topLeft()); - - // left triangle - QRectF rectLeft = QRectF(tilesMargins.left(), - tilesMargins.top() + tilesDrawingAreaHeight/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), - UnitConvertor(0.5, Unit::Cm, Unit::Px), - UnitConvertor(1, Unit::Cm, Unit::Px) - ); - QPainterPath triangleLeft; - triangleLeft.moveTo(rectLeft.topLeft()); - triangleLeft.lineTo(rectLeft.right(), rectLeft.top() + (rectLeft.height() / 2)); - triangleLeft.lineTo(rectLeft.bottomLeft()); - triangleLeft.lineTo(rectLeft.topLeft()); - - // bottom triangle - QRectF rectBottom = QRectF(tilesMargins.left()+ tilesDrawingAreaWidth/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), - tilesMargins.top()+tilesDrawingAreaHeight - UnitConvertor(0.5, Unit::Cm, Unit::Px), - UnitConvertor(1, Unit::Cm, Unit::Px), - UnitConvertor(0.5, Unit::Cm, Unit::Px) - ); - QPainterPath triangleBottom; - triangleBottom.moveTo(rectBottom.bottomLeft()); - triangleBottom.lineTo(rectBottom.left() + (rectBottom.width() / 2), rectBottom.top()); - triangleBottom.lineTo(rectBottom.bottomRight()); - triangleBottom.lineTo(rectBottom.bottomLeft()); - - // right triangle - QRectF rectRight = QRectF(tilesMargins.left() + tilesDrawingAreaWidth - UnitConvertor(0.5, Unit::Cm, Unit::Px), - tilesMargins.top() + tilesDrawingAreaHeight/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), - UnitConvertor(0.5, Unit::Cm, Unit::Px), - UnitConvertor(1, Unit::Cm, Unit::Px) - ); - QPainterPath triangleRight; - triangleRight.moveTo(rectRight.topRight()); - triangleRight.lineTo(rectRight.bottomRight()); - triangleRight.lineTo(rectRight.left(), rectRight.top() + (rectRight.height() / 2)); - triangleRight.lineTo(rectRight.topRight()); - - QBrush triangleBush = QBrush(QColor(200,200,200)); - - - // ------------- Perform the tiling - QSvgRenderer* svgRenderer = new QSvgRenderer(); - - for(int row=0;rowgetColNb();col++) // for each column of tiling grid { if(not (row == 0 && col == 0)) { @@ -866,132 +773,7 @@ void VPMainWindow::generateTiledPdf(QString fileName) } } - // add the tiles decorations (cutting and gluing lines, scissors, infos etc.) - penTileInfos.setStyle(Qt::DashLine); - painter.setPen(penTileInfos); - - if(row > 0) - { - // add top triangle - painter.fillPath(triangleTop, triangleBush); - - // scissors along the top line - svgRenderer->load(QStringLiteral("://puzzleicon/svg/icon_scissors_horizontal.svg")); - svgRenderer->render(&painter, QRectF(tilesMargins.left()+tilesDrawingAreaWidth, - tilesMargins.top(), - UnitConvertor(1, Unit::Cm, Unit::Px), - UnitConvertor(0.56, Unit::Cm, Unit::Px) - )); - - // top line - painter.drawLine(QPointF(tilesMargins.left(), - tilesMargins.top()), - QPointF(tilesMargins.left() + tilesDrawingAreaWidth + UnitConvertor(1, Unit::Cm, Unit::Px), - tilesMargins.top()) - ); - } - if(col > 0) - { - // add left triangle - painter.fillPath(triangleLeft, triangleBush); - - // scissors along the left line - svgRenderer->load(QStringLiteral("://puzzleicon/svg/icon_scissors_vertical.svg")); - svgRenderer->render(&painter, QRectF(tilesMargins.left(), - tilesMargins.top()+tilesDrawingAreaHeight, - UnitConvertor(0.56, Unit::Cm, Unit::Px), - UnitConvertor(1, Unit::Cm, Unit::Px) - )); - - // left line - painter.drawLine(QPointF(tilesMargins.left(), - tilesMargins.top()), - QPointF(tilesMargins.left(), - tilesMargins.top() + tilesDrawingAreaHeight + UnitConvertor(1, Unit::Cm, Unit::Px)) - ); - } - - penTileInfos.setStyle(Qt::DotLine); - painter.setPen(penTileInfos); - - if(row < nbRow-1) - { - // add bottom triangle - painter.fillPath(triangleBottom, triangleBush); - - // bottom line - painter.drawLine(QPointF(tilesMargins.left(), - tilesMargins.top() + tilesDrawingAreaHeight), - QPointF(tilesMargins.left() + tilesDrawingAreaWidth + UnitConvertor(1, Unit::Cm, Unit::Px), - tilesMargins.top() + tilesDrawingAreaHeight) - ); - } - - if(col < nbCol-1) - { - // add right triangle - painter.fillPath(triangleRight, triangleBush); - - // right line - painter.drawLine(QPointF(tilesMargins.left() + tilesDrawingAreaWidth, - tilesMargins.top()), - QPointF(tilesMargins.left() + tilesDrawingAreaWidth, - tilesMargins.top()+ tilesDrawingAreaHeight + UnitConvertor(1, Unit::Cm, Unit::Px)) - ); - } - - // paint the page - QRectF source = QRectF(col*tilesDrawingAreaWidth, - row*tilesDrawingAreaHeight, - tilesDrawingAreaWidth + UnitConvertor(1, Unit::Cm, Unit::Px), - tilesDrawingAreaHeight + UnitConvertor(1, Unit::Cm, Unit::Px) - ); - QRectF target = QRectF(tilesMargins.left(), - tilesMargins.top(), - source.width(), - source.height() - ); - - painter.setPen(penTileDrawing); - m_graphicsView->GetScene()->render(&painter, target, source, Qt::IgnoreAspectRatio); - - QTextDocument td; - - td.documentLayout()->setPaintDevice(printer); - td.setPageSize(QSizeF(tilesDrawingAreaWidth - UnitConvertor(2, Unit::Cm, Unit::Px), tilesDrawingAreaHeight)); - - - const QString grid = tr("Grid ( %1 , %2 )").arg(row+1).arg(col+1); - const QString page = tr("Page %1 of %2").arg(row*nbCol+col+1).arg(nbCol*nbRow); - - td.setHtml(QString("" - "" - "" - "" - "
%1
") - .arg(grid)); - painter.setPen(penTileInfos); - painter.save(); - painter.translate(QPointF(tilesMargins.left()+ UnitConvertor(1, Unit::Cm, Unit::Px), - tilesDrawingAreaHeight + tilesMargins.top() - )); - td.drawContents(&painter); - painter.restore(); - - td.setPageSize(QSizeF(tilesDrawingAreaHeight - UnitConvertor(2, Unit::Cm, Unit::Px), tilesDrawingAreaWidth)); - td.setHtml(QString("" - "" - "" - "" - "
%1 - %2
") - .arg(page).arg(m_layout->GetFocusedSheet()->GetName())); - painter.save(); - painter.rotate(-90); - painter.translate(QPointF(-(tilesDrawingAreaHeight+tilesMargins.top()) + UnitConvertor(1, Unit::Cm, Unit::Px), - tilesDrawingAreaWidth + tilesMargins.left() - )); - td.drawContents(&painter); - painter.restore(); + m_tileFactory->drawTile(&painter, m_graphicsView, row, col); } } @@ -1237,7 +1019,12 @@ void VPMainWindow::on_comboBoxSheetTemplate_currentIndexChanged(int index) //--------------------------------------------------------------------------------------------------------------------- void VPMainWindow::on_SheetSizeChanged() { - m_layout->GetFocusedSheet()->SetSheetSizeConverted(ui->doubleSpinBoxSheetWidth->value(), ui->doubleSpinBoxSheetLength->value()); + m_layout->GetFocusedSheet()->SetSheetSizeConverted( + ui->doubleSpinBoxSheetWidth->value(), + ui->doubleSpinBoxSheetLength->value() + ); + + m_tileFactory->refreshTileInfos(); // TODO Undo / Redo @@ -1256,6 +1043,7 @@ void VPMainWindow::on_SheetOrientationChanged() { m_layout->GetFocusedSheet()->SetOrientation(PageOrientation::Landscape); } + m_tileFactory->refreshTileInfos(); // TODO Undo / Redo @@ -1296,6 +1084,7 @@ void VPMainWindow::on_SheetMarginChanged() void VPMainWindow::on_TilesSizeChanged() { m_layout->SetTilesSizeConverted(ui->doubleSpinBoxTilesWidth->value(), ui->doubleSpinBoxTilesLength->value()); + m_tileFactory->refreshTileInfos(); // TODO Undo / Redo @@ -1314,6 +1103,7 @@ void VPMainWindow::on_TilesOrientationChanged() { m_layout->SetTilesOrientation(PageOrientation::Landscape); } + m_tileFactory->refreshTileInfos(); // TODO Undo / Redo @@ -1329,6 +1119,7 @@ void VPMainWindow::on_TilesMarginChanged() ui->doubleSpinBoxTilesMarginRight->value(), ui->doubleSpinBoxTilesMarginBottom->value() ); + m_tileFactory->refreshTileInfos(); // TODO Undo / Redo diff --git a/src/app/puzzle/vpmainwindow.h b/src/app/puzzle/vpmainwindow.h index e11a28956..462e8a8bf 100644 --- a/src/app/puzzle/vpmainwindow.h +++ b/src/app/puzzle/vpmainwindow.h @@ -39,6 +39,7 @@ #include "vplayout.h" #include "vppiece.h" #include "../vlayout/vlayoutpiece.h" +#include "vptilefactory.h" #include "vpcommandline.h" #include "../vlayout/vlayoutdef.h" @@ -105,6 +106,8 @@ private: VPLayout *m_layout{nullptr}; QListm_selectedPieces{QList()}; + VPTileFactory *m_tileFactory{nullptr}; + /** * @brief spin box with the scale factor of the graphic view */ diff --git a/src/app/puzzle/vptilefactory.cpp b/src/app/puzzle/vptilefactory.cpp new file mode 100644 index 000000000..a7606b1b4 --- /dev/null +++ b/src/app/puzzle/vptilefactory.cpp @@ -0,0 +1,330 @@ +#include "vptilefactory.h" + +#include + +#include "../vwidgets/vmaingraphicsscene.h" +#include "vpsheet.h" +#include "vpmaingraphicsview.h" + + +//--------------------------------------------------------------------------------------------------------------------- +VPTileFactory::VPTileFactory(VPLayout *layout, VCommonSettings *commonSettings): + m_layout(layout), + m_commonSettings(commonSettings) +{ + m_infoStripeWidth = UnitConvertor(1, Unit::Cm, Unit::Px); +} + +//--------------------------------------------------------------------------------------------------------------------- +VPTileFactory::~VPTileFactory() +{ + +} + + +//--------------------------------------------------------------------------------------------------------------------- +void VPTileFactory::refreshTileInfos() +{ + PageOrientation tilesOrientation = m_layout->GetTilesOrientation(); + QSizeF tilesSize = m_layout->GetTilesSize(); + QMarginsF tilesMargins = m_layout->GetTilesMargins(); + + // sets the drawing height + m_drawingAreaHeight = (tilesOrientation == PageOrientation::Portrait)? + tilesSize.height() : tilesSize.width(); + m_drawingAreaHeight -= + tilesMargins.top() + tilesMargins.bottom() + m_infoStripeWidth; + + // sets the drawing width + m_drawingAreaWidth = (tilesOrientation == PageOrientation::Portrait)? + tilesSize.width() : tilesSize.height(); + m_drawingAreaWidth -= + tilesMargins.left() + tilesMargins.right() + m_infoStripeWidth; + + + QSizeF sheetSize = m_layout->GetFocusedSheet()->GetSheetSize(); + qreal totalDrawingWidth = 0; + qreal totaldrawingHeight = 0; + + if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Portrait) + { + totalDrawingWidth = sheetSize.width(); + totaldrawingHeight = sheetSize.height(); + } + else + { + totalDrawingWidth = sheetSize.height(); + totaldrawingHeight = sheetSize.width(); + } + + m_nbCol = qCeil(totalDrawingWidth/m_drawingAreaWidth); + m_nbRow = qCeil(totaldrawingHeight/m_drawingAreaHeight); +} + + +//--------------------------------------------------------------------------------------------------------------------- +void VPTileFactory::drawTile(QPainter *painter, VPMainGraphicsView *graphicsView, int row, int col) +{ + QMarginsF tilesMargins = m_layout->GetTilesMargins(); + QPen penTileInfos = QPen(QColor(180,180,180), m_commonSettings->WidthHairLine(), Qt::DashLine, Qt::RoundCap, Qt::RoundJoin); + QPen penTileDrawing = QPen(Qt::black, m_commonSettings->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); + + QSvgRenderer* svgRenderer = new QSvgRenderer(); + + + // ------------- prepare triangles for positioning + + // top triangle + QRectF rectTop = QRectF(tilesMargins.left()+ m_drawingAreaWidth/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), + tilesMargins.top(), + UnitConvertor(1, Unit::Cm, Unit::Px), + UnitConvertor(0.5, Unit::Cm, Unit::Px) + ); + QPainterPath triangleTop; + triangleTop.moveTo(rectTop.topLeft()); + triangleTop.lineTo(rectTop.topRight()); + triangleTop.lineTo(rectTop.left() + (rectTop.width() / 2), rectTop.bottom()); + triangleTop.lineTo(rectTop.topLeft()); + + // left triangle + QRectF rectLeft = QRectF(tilesMargins.left(), + tilesMargins.top() + m_drawingAreaHeight/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), + UnitConvertor(0.5, Unit::Cm, Unit::Px), + UnitConvertor(1, Unit::Cm, Unit::Px) + ); + QPainterPath triangleLeft; + triangleLeft.moveTo(rectLeft.topLeft()); + triangleLeft.lineTo(rectLeft.right(), rectLeft.top() + (rectLeft.height() / 2)); + triangleLeft.lineTo(rectLeft.bottomLeft()); + triangleLeft.lineTo(rectLeft.topLeft()); + + // bottom triangle + QRectF rectBottom = QRectF(tilesMargins.left()+ m_drawingAreaWidth/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), + tilesMargins.top()+m_drawingAreaHeight - UnitConvertor(0.5, Unit::Cm, Unit::Px), + UnitConvertor(1, Unit::Cm, Unit::Px), + UnitConvertor(0.5, Unit::Cm, Unit::Px) + ); + QPainterPath triangleBottom; + triangleBottom.moveTo(rectBottom.bottomLeft()); + triangleBottom.lineTo(rectBottom.left() + (rectBottom.width() / 2), rectBottom.top()); + triangleBottom.lineTo(rectBottom.bottomRight()); + triangleBottom.lineTo(rectBottom.bottomLeft()); + + // right triangle + QRectF rectRight = QRectF(tilesMargins.left() + m_drawingAreaWidth - UnitConvertor(0.5, Unit::Cm, Unit::Px), + tilesMargins.top() + m_drawingAreaHeight/2 - UnitConvertor(0.5, Unit::Cm, Unit::Px), + UnitConvertor(0.5, Unit::Cm, Unit::Px), + UnitConvertor(1, Unit::Cm, Unit::Px) + ); + QPainterPath triangleRight; + triangleRight.moveTo(rectRight.topRight()); + triangleRight.lineTo(rectRight.bottomRight()); + triangleRight.lineTo(rectRight.left(), rectRight.top() + (rectRight.height() / 2)); + triangleRight.lineTo(rectRight.topRight()); + + QBrush triangleBush = QBrush(QColor(200,200,200)); + + // add the tiles decorations (cutting and gluing lines, scissors, infos etc.) + painter->setPen(penTileInfos); + + if(row > 0) + { + // add top triangle + painter->fillPath(triangleTop, triangleBush); + + // scissors along the top line + svgRenderer->load(QStringLiteral("://puzzleicon/svg/icon_scissors_horizontal.svg")); + svgRenderer->render(painter, QRectF(tilesMargins.left() + m_drawingAreaWidth, + tilesMargins.top(), + UnitConvertor(1, Unit::Cm, Unit::Px), + UnitConvertor(0.56, Unit::Cm, Unit::Px) + )); + + // dashed top line (for cutting) + penTileInfos.setStyle(Qt::DashLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top()), + QPointF(tilesMargins.left() + m_drawingAreaWidth + m_infoStripeWidth, + tilesMargins.top()) + ); + } + else + { + // solid top line stopping at the edge + penTileInfos.setStyle(Qt::SolidLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top()), + QPointF(tilesMargins.left() + m_drawingAreaWidth + ((col < m_nbCol-1)? m_infoStripeWidth : 0), + tilesMargins.top()) + ); + } + + if(col > 0) + { + // add left triangle + painter->fillPath(triangleLeft, triangleBush); + + // scissors along the left line + svgRenderer->load(QStringLiteral("://puzzleicon/svg/icon_scissors_vertical.svg")); + svgRenderer->render(painter, QRectF(tilesMargins.left(), + tilesMargins.top()+m_drawingAreaHeight, + UnitConvertor(0.56, Unit::Cm, Unit::Px), + UnitConvertor(1, Unit::Cm, Unit::Px) + )); + + // dashed left line (for cutting) + penTileInfos.setStyle(Qt::DashLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top()), + QPointF(tilesMargins.left(), + tilesMargins.top() + m_drawingAreaHeight + m_infoStripeWidth) + ); + } + else + { + // solid left line at the edge + penTileInfos.setStyle(Qt::SolidLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top()), + QPointF(tilesMargins.left(), + tilesMargins.top() + m_drawingAreaHeight + ((row < m_nbRow-1)? m_infoStripeWidth : 0)) + ); + } + + if(row < m_nbRow-1) + { + // add bottom triangle + painter->fillPath(triangleBottom, triangleBush); + + // dotted bottom line (for glueing) + penTileInfos.setStyle(Qt::DotLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top() + m_drawingAreaHeight), + QPointF(tilesMargins.left() + m_drawingAreaWidth + m_infoStripeWidth, + tilesMargins.top() + m_drawingAreaHeight) + ); + } else + { + // solid bottom line at the edge + penTileInfos.setStyle(Qt::SolidLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left(), + tilesMargins.top() + m_drawingAreaHeight), + QPointF(tilesMargins.left() + m_drawingAreaWidth + ((col < m_nbCol-1)? m_infoStripeWidth : 0), + tilesMargins.top() + m_drawingAreaHeight) + ); + } + + if(col < m_nbCol-1) + { + // add right triangle + painter->fillPath(triangleRight, triangleBush); + + // dotted right line (for glueing) + penTileInfos.setStyle(Qt::DotLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left() + m_drawingAreaWidth, + tilesMargins.top()), + QPointF(tilesMargins.left() + m_drawingAreaWidth, + tilesMargins.top()+ m_drawingAreaHeight + m_infoStripeWidth) + ); + } + else + { + // solid right line at the edge + penTileInfos.setStyle(Qt::SolidLine); + painter->setPen(penTileInfos); + painter->drawLine(QPointF(tilesMargins.left() + m_drawingAreaWidth, + tilesMargins.top()), + QPointF(tilesMargins.left() + m_drawingAreaWidth, + tilesMargins.top()+ m_drawingAreaHeight + ((row < m_nbRow-1) ? m_infoStripeWidth : 0)) + ); + } + + // paint the content of the page + QRectF source = QRectF(col*m_drawingAreaWidth, + row*m_drawingAreaHeight, + m_drawingAreaWidth + m_infoStripeWidth, + m_drawingAreaHeight + m_infoStripeWidth + ); + QRectF target = QRectF(tilesMargins.left(), + tilesMargins.top(), + source.width(), + source.height() + ); + + painter->setPen(penTileDrawing); + graphicsView->GetScene()->render(painter, target, source, Qt::IgnoreAspectRatio); + + // prepare the painting for the text information + QTextDocument td; + +// td.documentLayout()->setPaintDevice(printer); ?? + + td.setPageSize(QSizeF(m_drawingAreaWidth - UnitConvertor(2, Unit::Cm, Unit::Px), m_drawingAreaHeight)); + + // paint the grid information + const QString grid = tr("Grid ( %1 , %2 )").arg(row+1).arg(col+1); + + td.setHtml(QString("" + "" + "" + "" + "
%1
") + .arg(grid)); + painter->setPen(penTileInfos); + painter->save(); + painter->translate(QPointF(tilesMargins.left()+ UnitConvertor(1, Unit::Cm, Unit::Px), + m_drawingAreaHeight + tilesMargins.top() + )); + td.drawContents(painter); + painter->restore(); + + // paint the page information + const QString page = tr("Page %1 of %2").arg(row*m_nbCol+col+1).arg(m_nbCol*m_nbRow); + + td.setPageSize(QSizeF(m_drawingAreaHeight - UnitConvertor(2, Unit::Cm, Unit::Px), m_drawingAreaWidth)); + td.setHtml(QString("" + "" + "" + "" + "
%1 - %2
") + .arg(page).arg(m_layout->GetFocusedSheet()->GetName())); + painter->save(); + painter->rotate(-90); + painter->translate(QPointF(-(m_drawingAreaHeight+tilesMargins.top()) + UnitConvertor(1, Unit::Cm, Unit::Px), + m_drawingAreaWidth + tilesMargins.left() + )); + td.drawContents(painter); + painter->restore(); + +} + +//--------------------------------------------------------------------------------------------------------------------- +int VPTileFactory::getRowNb() +{ + return m_nbRow; +} + +//--------------------------------------------------------------------------------------------------------------------- +int VPTileFactory::getColNb() +{ + return m_nbCol; +} + +//--------------------------------------------------------------------------------------------------------------------- +qreal VPTileFactory::getDrawingAreaHeight() +{ + return m_drawingAreaHeight; +} + +//--------------------------------------------------------------------------------------------------------------------- +qreal VPTileFactory::getDrawingAreaWidth() +{ + return m_drawingAreaWidth; +} diff --git a/src/app/puzzle/vptilefactory.h b/src/app/puzzle/vptilefactory.h new file mode 100644 index 000000000..7b78a36f7 --- /dev/null +++ b/src/app/puzzle/vptilefactory.h @@ -0,0 +1,121 @@ +/************************************************************************ + ** + ** @file vptilefactory.h + ** @author Ronan Le Tiec + ** @date 19 11, 2020 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2020 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ + +#ifndef VPTILEFACTORY_H +#define VPTILEFACTORY_H + +#include +#include + +#include "vplayout.h" +#include "../vmisc/def.h" +#include "vcommonsettings.h" + +class VPMainGraphicsView; + +class VPTileFactory : QObject +{ + Q_OBJECT + +public: + VPTileFactory(VPLayout *layout, VCommonSettings *settings); + + ~VPTileFactory(); + + /** + * @brief drawTile draws the tile of given coordinate (row, col) from the + * current sheet of the layout with the given painter + * @param painter + * @param row + * @param col + */ + void drawTile(QPainter *painter, VPMainGraphicsView *graphicsView, int row, int col); + + /** + * @brief refreshTileInfos Resfreshes the tile infos (m_nbCol, m_nbRow, m_drawingAreaHeight, m_drawingAreaWidth) + */ + void refreshTileInfos(); + + /** + * @brief getRowNb Returns the number of row pages + * @return + */ + int getRowNb(); + + /** + * @brief getColNb Returns the number of col pages + * @return + */ + int getColNb(); + + /** + * @brief getDrawingAreaHeight Returns the usable height of the tile in Px + * @return + */ + qreal getDrawingAreaHeight(); + + /** + * @brief getDrawingAreaWidth Returns the usable width of the tile in Px + * @return + */ + qreal getDrawingAreaWidth(); + +private: + Q_DISABLE_COPY(VPTileFactory) + + VPLayout *m_layout{nullptr}; + VCommonSettings *m_commonSettings{nullptr}; + + /** + * @brief m_nbCol the number of column-pages for the current sheet of the layout + */ + int m_nbCol{0}; + + /** + * @brief m_nbRow the number of row-pages for the current sheet of the layout + */ + int m_nbRow{0}; + + /** + * @brief m_drawingAreaHeight the height of the drawing area + */ + qreal m_drawingAreaHeight{0}; + + /** + * @brief m_drawingAreaWidth the width of the drawing area + */ + qreal m_drawingAreaWidth{0}; + + /** + * @brief m_infoStripeWidth the width of the info / glueing stripe in Px + */ + qreal m_infoStripeWidth{0}; + +}; + +#endif // VPTILEFACTORY_H