Introducing VPrintLayout class.

Refactoring printing a layout.
This commit is contained in:
Roman Telezhynskyi 2020-12-23 15:29:55 +02:00
parent 8405ff3c6d
commit abb4397901
25 changed files with 1414 additions and 850 deletions

View File

@ -909,9 +909,6 @@ QSizeF DialogLayoutSettings::GetTemplateSize(const PaperSizeTemplate &tmpl, cons
}
}
//---------------------------------------------------------------------------------------------------------------------
QMarginsF DialogLayoutSettings::MinPrinterFields() const
{

View File

@ -32,7 +32,7 @@
#include <QCloseEvent>
#include <QMargins>
#include "vabstractlayoutdialog.h"
#include "../vlayout/dialogs/vabstractlayoutdialog.h"
#include "../vlayout/vbank.h"
namespace Ui

View File

@ -36,6 +36,5 @@
#include "dialogaboutapp.h"
#include "dialogpreferences.h"
#include "dialogfinalmeasurements.h"
#include "dialoglayoutscale.h"
#endif // DIALOGS_H

View File

@ -2,7 +2,6 @@
# This need for corect working file translations.pro
HEADERS += \
$$PWD/dialoglayoutscale.h \
$$PWD/dialogs.h \
$$PWD/dialogincrements.h \
$$PWD/dialoghistory.h \
@ -21,13 +20,11 @@ HEADERS += \
$$PWD/dialogdatetimeformats.h \
$$PWD/dialogknownmaterials.h \
$$PWD/dialogpatternmaterials.h \
$$PWD/dialogfinalmeasurements.h \
$$PWD/vabstractlayoutdialog.h
$$PWD/dialogfinalmeasurements.h
SOURCES += \
$$PWD/dialogincrements.cpp \
$$PWD/dialoghistory.cpp \
$$PWD/dialoglayoutscale.cpp \
$$PWD/dialogpatternproperties.cpp \
$$PWD/dialognewpattern.cpp \
$$PWD/dialogaboutapp.cpp \
@ -43,13 +40,11 @@ SOURCES += \
$$PWD/dialogdatetimeformats.cpp \
$$PWD/dialogknownmaterials.cpp \
$$PWD/dialogpatternmaterials.cpp \
$$PWD/dialogfinalmeasurements.cpp \
$$PWD/vabstractlayoutdialog.cpp
$$PWD/dialogfinalmeasurements.cpp
FORMS += \
$$PWD/dialogincrements.ui \
$$PWD/dialoghistory.ui \
$$PWD/dialoglayoutscale.ui \
$$PWD/dialogpatternproperties.ui \
$$PWD/dialognewpattern.ui \
$$PWD/dialogaboutapp.ui \

View File

@ -30,7 +30,7 @@
#define DIALOGSAVELAYOUT_H
#include "../vgeometry/vgeometrydef.h"
#include "vabstractlayoutdialog.h"
#include "../vlayout/dialogs/vabstractlayoutdialog.h"
#include "../vlayout/vlayoutdef.h"
namespace Ui

View File

@ -1641,10 +1641,7 @@ void MainWindow::customEvent(QEvent *event)
//---------------------------------------------------------------------------------------------------------------------
void MainWindow::CleanLayout()
{
qDeleteAll (scenes);
scenes.clear();
shadows.clear();
papers.clear();
m_layoutSettings->CleanLayout();
gcontours.clear();
ui->listWidget->clear();
SetLayoutModeActions();
@ -1654,14 +1651,14 @@ void MainWindow::CleanLayout()
void MainWindow::PrepareSceneList(PreviewQuatilty quality)
{
ui->listWidget->clear();
for (int i=1; i<=scenes.size(); ++i)
for (int i=1; i<=m_layoutSettings->LayoutScenes().size(); ++i)
{
QListWidgetItem *item = new QListWidgetItem(ScenePreview(i-1, ui->listWidget->iconSize(), quality),
QString::number(i));
ui->listWidget->addItem(item);
}
if (not scenes.isEmpty())
if (not m_layoutSettings->LayoutScenes().isEmpty())
{
ui->listWidget->setCurrentRow(0);
SetLayoutModeActions();
@ -3062,7 +3059,7 @@ void MainWindow::ActionLayout(bool checked)
ShowPaper(ui->listWidget->currentRow());
if (scenes.isEmpty() || isLayoutStale)
if (m_layoutSettings->LayoutScenes().isEmpty() || m_layoutSettings->IsLayoutStale())
{
ui->toolButtonLayoutSettings->click();
}
@ -3817,7 +3814,7 @@ void MainWindow::PatternChangesWereSaved(bool saved)
const bool state = doc->IsModified() || !saved;
setWindowModified(state);
not patternReadOnly ? ui->actionSave->setEnabled(state): ui->actionSave->setEnabled(false);
isLayoutStale = true;
m_layoutSettings->SetLayoutStale(true);
isNeedAutosave = not saved;
}
}
@ -4177,7 +4174,7 @@ QT_WARNING_POP
//---------------------------------------------------------------------------------------------------------------------
void MainWindow::SetLayoutModeActions()
{
const bool enabled = not scenes.isEmpty();
const bool enabled = not m_layoutSettings->LayoutScenes().isEmpty();
ui->toolButtonLayoutExportAs->setEnabled(enabled);
ui->actionExportAs->setEnabled(enabled);
@ -5011,7 +5008,6 @@ bool MainWindow::PatternPieceName(QString &name)
MainWindow::~MainWindow()
{
CancelTool();
qDeleteAll (scenes);
delete doc;
delete ui;
@ -5337,13 +5333,13 @@ void MainWindow::ToolBoxSizePolicy()
//---------------------------------------------------------------------------------------------------------------------
void MainWindow::ShowPaper(int index)
{
if (index < 0 || index >= scenes.size())
if (index < 0 || index >= m_layoutSettings->LayoutScenes().size())
{
ui->view->setScene(tempSceneLayout);
}
else
{
ui->view->setScene(scenes.at(index));
ui->view->setScene(m_layoutSettings->LayoutScenes().at(index));
}
ui->view->fitInView(ui->view->scene()->sceneRect(), Qt::KeepAspectRatio);
@ -5400,9 +5396,9 @@ void MainWindow::ExportLayoutAs()
{
auto Uncheck = qScopeGuard([this] {ui->toolButtonLayoutExportAs->setChecked(false);});
if (isLayoutStale)
if (m_layoutSettings->IsLayoutStale())
{
if (ContinueIfLayoutStale() == QMessageBox::No)
if (VPrintLayout::ContinueIfLayoutStale(this) == QMessageBox::No)
{
return;
}
@ -5410,8 +5406,8 @@ void MainWindow::ExportLayoutAs()
try
{
m_dialogSaveLayout = QSharedPointer<DialogSaveLayout>(new DialogSaveLayout(scenes.size(), Draw::Layout,
FileName(), this));
m_dialogSaveLayout = QSharedPointer<DialogSaveLayout>(
new DialogSaveLayout(m_layoutSettings->LayoutScenes().size(), Draw::Layout, FileName(), this));
if (m_dialogSaveLayout->exec() == QDialog::Rejected)
{
@ -5829,9 +5825,9 @@ bool MainWindow::DoExport(const VCommandLinePtr &expParams)
{
try
{
m_dialogSaveLayout = QSharedPointer<DialogSaveLayout>(new DialogSaveLayout(scenes.size(), Draw::Layout,
expParams->OptBaseName(),
this));
m_dialogSaveLayout = QSharedPointer<DialogSaveLayout>(
new DialogSaveLayout(m_layoutSettings->LayoutScenes().size(),
Draw::Layout, expParams->OptBaseName(), this));
m_dialogSaveLayout->SetDestinationPath(expParams->OptDestinationPath());
m_dialogSaveLayout->SelectFormat(static_cast<LayoutExportFormats>(expParams->OptExportType()));
m_dialogSaveLayout->SetBinaryDXFFormat(expParams->IsBinaryDXF());

View File

@ -132,32 +132,17 @@ MainWindowsNoGUI::MainWindowsNoGUI(QWidget *parent)
tempSceneLayout(nullptr),
pattern(new VContainer(qApp->TrVars(), qApp->patternUnitsP(), valentinaNamespace)),
doc(nullptr),
papers(),
shadows(),
scenes(),
details(),
gcontours(),
detailsOnLayout(),
undoAction(nullptr),
redoAction(nullptr),
actionDockWidgetToolOptions(nullptr),
actionDockWidgetGroups(nullptr),
isNoScaling(false),
isLayoutStale(true),
isNeedAutosave(false),
ignorePrinterFields(false),
margins(),
paperSize(),
m_dialogSaveLayout(),
#if defined(Q_OS_WIN32) && QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
m_taskbarButton(new QWinTaskbarButton(this)),
m_taskbarProgress(nullptr),
#endif
isTiled(false),
isAutoCropLength(false),
isAutoCropWidth(false),
isUnitePages(false),
layoutPrinterName()
m_dialogSaveLayout()
{
InitTempLayoutScene();
@ -166,6 +151,8 @@ MainWindowsNoGUI::MainWindowsNoGUI(QWidget *parent)
m_taskbarProgress = m_taskbarButton->progress();
m_taskbarProgress->setMinimum(0);
#endif
m_layoutSettings->SetParentWidget(this);
}
//---------------------------------------------------------------------------------------------------------------------
@ -193,7 +180,7 @@ void MainWindowsNoGUI::ToolLayoutSettings(bool checked)
tButton->setChecked(false);
return;
}
layoutPrinterName = layout.SelectedPrinter();
m_layoutSettings->SetLayoutPrinterName(layout.SelectedPrinter());
GenerateLayout(lGenerator);
tButton->setChecked(false);
}
@ -324,12 +311,14 @@ bool MainWindowsNoGUI::GenerateLayout(VLayoutGenerator& lGenerator)
}
CleanLayout();
papers = lGenerator.GetPapersItems();// Blank sheets
details = lGenerator.GetAllDetailsItems();// All details items
m_layoutSettings->SetLayoutPapers(lGenerator.GetPapersItems());// Blank sheets
m_layoutSettings->SetLayoutDetails(lGenerator.GetAllDetailsItems());// All details items
detailsOnLayout = lGenerator.GetAllDetails();// All details items
shadows = CreateShadows(papers);
isLayoutPortrait = lGenerator.IsPortrait();
scenes = CreateScenes(papers, shadows, details);
m_layoutSettings->SetLayoutShadows(CreateShadows(m_layoutSettings->LayoutPapers()));
m_layoutSettings->SetLayoutPortrait(lGenerator.IsPortrait());
m_layoutSettings->SetLayoutScenes(CreateScenes(m_layoutSettings->LayoutPapers(),
m_layoutSettings->LayoutShadows(),
m_layoutSettings->LayoutDetails()));
#if !defined(V_NO_ASSERT)
//Uncomment to debug, shows global contour
// gcontours = lGenerator.GetGlobalContours(); // uncomment for debugging
@ -339,13 +328,14 @@ bool MainWindowsNoGUI::GenerateLayout(VLayoutGenerator& lGenerator)
{
PrepareSceneList(PreviewQuatilty::Fast);
}
ignorePrinterFields = not lGenerator.IsUsePrinterFields();
margins = lGenerator.GetPrinterFields();
paperSize = QSizeF(lGenerator.GetPaperWidth(), lGenerator.GetPaperHeight());
isAutoCropLength = lGenerator.GetAutoCropLength();
isAutoCropWidth = lGenerator.GetAutoCropWidth();
isUnitePages = lGenerator.IsUnitePages();
isLayoutStale = false;
m_layoutSettings->SetIgnorePrinterMargins(not lGenerator.IsUsePrinterFields());
m_layoutSettings->SetLayoutMargins(lGenerator.GetPrinterFields());
m_layoutSettings->SetLayoutPaperSize(QSizeF(lGenerator.GetPaperWidth(),
lGenerator.GetPaperHeight()));
m_layoutSettings->SetAutoCropLength(lGenerator.GetAutoCropLength());
m_layoutSettings->SetAutoCropWidth(lGenerator.GetAutoCropWidth());
m_layoutSettings->SetUnitePages(lGenerator.IsUnitePages());
m_layoutSettings->SetLayoutStale(false);
papersCount = lGenerator.PapersCount();
hasResult = true;
qDebug() << "Layout efficiency: " << efficiency;
@ -534,7 +524,8 @@ void MainWindowsNoGUI::ExportData(const QVector<VLayoutPiece> &listDetails)
const QString name = m_dialogSaveLayout->Path() + '/' + m_dialogSaveLayout->FileName() +
QString::number(i+1) + DialogSaveLayout::ExportFormatSuffix(m_dialogSaveLayout->Format());
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(
m_layoutSettings->LayoutPapers().at(i));
SCASSERT(paper != nullptr)
ExportApparelLayout(detailsOnLayout.at(i), name, paper->rect().size().toSize());
@ -549,7 +540,9 @@ void MainWindowsNoGUI::ExportData(const QVector<VLayoutPiece> &listDetails)
{
if (m_dialogSaveLayout->Mode() == Draw::Layout)
{
ExportFlatLayout(scenes, papers, shadows, details, ignorePrinterFields, margins);
ExportFlatLayout(m_layoutSettings->LayoutScenes(), m_layoutSettings->LayoutPapers(),
m_layoutSettings->LayoutShadows(), m_layoutSettings->LayoutDetails(),
m_layoutSettings->IgnorePrinterMargins(), m_layoutSettings->LayoutMargins());
}
else
{
@ -577,8 +570,7 @@ void MainWindowsNoGUI::ExportFlatLayout(const QList<QGraphicsScene *> &scenes,
if (format == LayoutExportFormats::PDFTiled && m_dialogSaveLayout->Mode() == Draw::Layout)
{
const QString name = path + '/' + m_dialogSaveLayout->FileName() + QString::number(1)
+ DialogSaveLayout::ExportFormatSuffix(m_dialogSaveLayout->Format());
const QString name = path + '/' + m_dialogSaveLayout->FileName() + QChar('1');
PdfTiledFile(name);
}
else
@ -845,236 +837,70 @@ void MainWindowsNoGUI::ExportDetailsAsApparelLayout(QVector<VLayoutPiece> listDe
ExportApparelLayout(listDetails, name, rect.size());
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPages(QPrinter *printer)
{
// Here we try understand difference between printer's dpi and our.
// Get printer rect acording to our dpi.
const QRectF printerPageRect(0, 0, ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm),
ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm));
const QRect pageRect = printer->pageLayout().paintRectPixels(printer->resolution());
const double xscale = pageRect.width() / printerPageRect.width();
const double yscale = pageRect.height() / printerPageRect.height();
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 ) );
int count = 0;
QSharedPointer<QVector<PosterData>> poster;
QSharedPointer<VPoster> posterazor;
if (isTiled)
{
PageOrientation orientation;
if (not m_dialogSaveLayout.isNull())
{
orientation = m_dialogSaveLayout->GetTiledPageOrientation();
}
else
{
orientation = qApp->ValentinaSettings()->GetTiledPDFOrientation();
}
// when isTiled, the landscape tiles have to be rotated, because the pages
// stay portrait in the pdf
if(orientation == PageOrientation::Landscape)
{
painter.rotate(-90);
painter.translate(-ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm), 0);
}
poster = QSharedPointer<QVector<PosterData>>(new QVector<PosterData>());
posterazor = QSharedPointer<VPoster>(new VPoster(printer));
for (int i=0; i < scenes.size(); ++i)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
if (paper)
{
QRectF paperRect = paper->rect();
QSizeF image(paperRect.width() * m_xscale, paperRect.height() * m_yscale);
*poster += posterazor->Calc(image.toSize(), i, orientation);
}
}
count = poster->size();
}
else
{
count = scenes.size();
}
// Handle the fromPage(), toPage(), supportsMultipleCopies(), and numCopies() values from QPrinter.
int firstPage = printer->fromPage() - 1;
if (firstPage >= count)
{
return;
}
if (firstPage == -1)
{
firstPage = 0;
}
int lastPage = printer->toPage() - 1;
if (lastPage == -1 || lastPage >= count)
{
lastPage = count - 1;
}
const int numPages = lastPage - firstPage + 1;
int copyCount = 1;
if (not printer->supportsMultipleCopies())
{
copyCount = printer->copyCount();
}
VWatermarkData data;
const QString watermarkPath = AbsoluteMPath(qApp->GetPatternPath(), doc->GetWatermarkPath());
if (not watermarkPath.isEmpty())
{
try
{
VWatermarkConverter converter(watermarkPath);
VWatermark watermark;
watermark.setXMLContent(converter.Convert());
data = watermark.GetWatermark();
if (not data.path.isEmpty())
{
// Clean previous cache
QPixmapCache::remove(AbsoluteMPath(watermarkPath, data.path));
}
}
catch (VException &e)
{
const QString errorMsg = tr("File error.\n\n%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation());
qApp->IsPedantic() ? throw VException(errorMsg) :
qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg;
}
}
for (int i = 0; i < copyCount; ++i)
{
for (int j = 0; j < numPages; ++j)
{
if (i != 0 || j != 0)
{
if (not printer->newPage())
{
qWarning("failed in flushing page to disk, disk full?");
return;
}
}
int index;
if (printer->pageOrder() == QPrinter::FirstPageFirst)
{
index = firstPage + j;
}
else
{
index = lastPage - j;
}
int paperIndex = -1;
isTiled ? paperIndex = static_cast<int>(poster->at(index).index) : paperIndex = index;
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(paperIndex));
if (paper)
{
QVector<QGraphicsItem *> posterData;
if (isTiled)
{
// Draw tile
posterData = posterazor->Tile(paper, poster->at(index), scenes.size(), data, watermarkPath);
}
PreparePaper(paperIndex);
// Render
QRectF source;
isTiled ? source = poster->at(index).rect : source = paper->rect();
qreal x,y;
if(printer->fullPage())
{
QPageLayout layout = printer->pageLayout();
layout.setUnits(QPageLayout::Millimeter);
QMarginsF printerMargins = layout.margins();
x = qFloor(ToPixel(printerMargins.left(),Unit::Mm));
y = qFloor(ToPixel(printerMargins.top(),Unit::Mm));
}
else
{
x = 0; y = 0;
}
QRectF target(x * xscale, y * yscale, source.width() * xscale, source.height() * yscale);
scenes.at(paperIndex)->render(&painter, target, source, Qt::IgnoreAspectRatio);
if (isTiled)
{
// Remove borders
qDeleteAll(posterData);
}
// Restore
RestorePaper(paperIndex);
}
}
}
painter.end();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreviewOrigin()
{
if (not isPagesUniform())
{
qCritical()<<tr("For previewing multipage document all sheet should have the same size.");
return;
}
isTiled = false;
PrintPreview();
m_layoutSettings->SetFileName(FileName());
m_layoutSettings->PrintPreviewOrigin();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreviewTiled()
{
isTiled = true;
PrintPreview();
m_layoutSettings->SetFileName(FileName());
if (not m_dialogSaveLayout.isNull())
{
m_layoutSettings->SetTiledMargins(m_dialogSaveLayout->GetTiledMargins());
m_layoutSettings->SetTiledPDFOrientation(m_dialogSaveLayout->GetTiledPageOrientation());
VAbstractLayoutDialog::PaperSizeTemplate tiledFormat = m_dialogSaveLayout->GetTiledPageFormat();
m_layoutSettings->SetTiledPDFPaperSize(VAbstractLayoutDialog::GetTemplateSize(tiledFormat, Unit::Mm));
}
else
{
VSettings *settings = qApp->ValentinaSettings();
m_layoutSettings->SetTiledMargins(QMarginsF(settings->GetTiledPDFMargins(Unit::Mm)));
m_layoutSettings->SetTiledPDFOrientation(settings->GetTiledPDFOrientation());
m_layoutSettings->SetTiledPDFPaperSize(QSizeF(settings->GetTiledPDFPaperWidth(Unit::Mm),
settings->GetTiledPDFPaperHeight(Unit::Mm)));
}
m_layoutSettings->SetWatermarkPath(AbsoluteMPath(qApp->GetPatternPath(), doc->GetWatermarkPath()));
m_layoutSettings->PrintPreviewTiled();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintOrigin()
{
if (not isPagesUniform())
{
qCritical()<<tr("For printing multipages document all sheet should have the same size.");
return;
}
isTiled = false;
LayoutPrint();
m_layoutSettings->SetFileName(FileName());
m_layoutSettings->PrintOrigin();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintTiled()
{
isTiled = true;
LayoutPrint();
m_layoutSettings->SetFileName(FileName());
if (not m_dialogSaveLayout.isNull())
{
m_layoutSettings->SetTiledMargins(m_dialogSaveLayout->GetTiledMargins());
m_layoutSettings->SetTiledPDFOrientation(m_dialogSaveLayout->GetTiledPageOrientation());
VAbstractLayoutDialog::PaperSizeTemplate tiledFormat = m_dialogSaveLayout->GetTiledPageFormat();
m_layoutSettings->SetTiledPDFPaperSize(VAbstractLayoutDialog::GetTemplateSize(tiledFormat, Unit::Mm));
}
else
{
VSettings *settings = qApp->ValentinaSettings();
m_layoutSettings->SetTiledMargins(QMarginsF(settings->GetTiledPDFMargins(Unit::Mm)));
m_layoutSettings->SetTiledPDFOrientation(settings->GetTiledPDFOrientation());
m_layoutSettings->SetTiledPDFPaperSize(QSizeF(settings->GetTiledPDFPaperWidth(Unit::Mm),
settings->GetTiledPDFPaperHeight(Unit::Mm)));
}
m_layoutSettings->SetWatermarkPath(AbsoluteMPath(qApp->GetPatternPath(), doc->GetWatermarkPath()));
m_layoutSettings->PrintTiled();
}
//---------------------------------------------------------------------------------------------------------------------
@ -1141,7 +967,7 @@ QStringList MainWindowsNoGUI::RecentFileList() const
QIcon MainWindowsNoGUI::ScenePreview(int i, QSize iconSize, PreviewQuatilty quality) const
{
QImage image;
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(i));
QGraphicsRectItem *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutSettings->LayoutPapers().at(i));
if (paper)
{
if (quality == PreviewQuatilty::Fast)
@ -1165,7 +991,7 @@ QIcon MainWindowsNoGUI::ScenePreview(int i, QSize iconSize, PreviewQuatilty qual
painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
scenes.at(i)->render(&painter, r, r, Qt::IgnoreAspectRatio);
m_layoutSettings->LayoutScenes().at(i)->render(&painter, r, r, Qt::IgnoreAspectRatio);
painter.end();
}
else
@ -1239,412 +1065,30 @@ QList<QGraphicsScene *> MainWindowsNoGUI::CreateScenes(const QList<QGraphicsItem
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PdfTiledFile(const QString &name)
{
isTiled = true;
if (isLayoutStale)
if (not m_dialogSaveLayout.isNull())
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
QPrinter printer;
SetPrinterSettings(&printer, PrintType::PrintPDF);
m_layoutSettings->SetTiledMargins(m_dialogSaveLayout->GetTiledMargins());
m_layoutSettings->SetTiledPDFOrientation(m_dialogSaveLayout->GetTiledPageOrientation());
// Call IsPagesFit after setting a printer settings and check if pages is not bigger than printer's paper size
if (not isTiled && not IsPagesFit(printer.pageLayout().paintRectPixels(printer.resolution()).size()))
{
qWarning()<<tr("Pages will be cropped because they do not fit printer paper size.");
}
VAbstractLayoutDialog::PaperSizeTemplate tiledFormat = m_dialogSaveLayout->GetTiledPageFormat();
m_layoutSettings->SetTiledPDFPaperSize(VAbstractLayoutDialog::GetTemplateSize(tiledFormat, Unit::Mm));
m_xscale = m_dialogSaveLayout->GetXScale();
m_yscale = m_dialogSaveLayout->GetYScale();
printer.setOutputFileName(name);
printer.setResolution(static_cast<int>(PrintDPI));
PrintPages(&printer);
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PreparePaper(int index) const
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(index));
if (paper)
{
QBrush brush(Qt::white);
scenes.at(index)->setBackgroundBrush(brush);
shadows.at(index)->setVisible(false);
paper->setPen(QPen(Qt::white, 0.1, Qt::NoPen));// border
}
QTransform matrix;
matrix.scale(m_xscale, m_yscale);
QList<QGraphicsItem *> paperDetails = details.at(index);
for (auto detail : paperDetails)
{
QTransform m = detail->transform();
m *= matrix;
detail->setTransform(m);
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::RestorePaper(int index) const
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(index));
if (paper)
{
// Restore
paper->setPen(QPen(Qt::black, 1));
QBrush brush(Qt::gray);
scenes.at(index)->setBackgroundBrush(brush);
shadows.at(index)->setVisible(true);
}
QTransform matrix;
matrix.scale(1./m_xscale, 1./m_yscale);
QList<QGraphicsItem *> paperDetails = details.at(index);
for (auto detail : paperDetails)
{
QTransform m = detail->transform();
m *= matrix;
detail->setTransform(m);
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::PrintPreview()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
QPrinterInfo info = QPrinterInfo::printerInfo(layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
qUtf8Printable(tr("Cannot proceed because there are no available printers in your system.")));
return;
}
{
DialogLayoutScale layoutScale(isTiled, this);
layoutScale.SetXScale(1);
layoutScale.SetYScale(1);
layoutScale.exec();
m_xscale = layoutScale.GetXScale();
m_yscale = layoutScale.GetYScale();
}
SetPrinterSettings(printer.data(), PrintType::PrintPreview);
printer->setResolution(static_cast<int>(PrintDPI));
// display print preview dialog
QPrintPreviewDialog preview(printer.data());
connect(&preview, &QPrintPreviewDialog::paintRequested, this, &MainWindowsNoGUI::PrintPages);
preview.exec();
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::LayoutPrint()
{
if (isLayoutStale)
{
if (ContinueIfLayoutStale() == QMessageBox::No)
{
return;
}
}
// display print dialog and if accepted print
QPrinterInfo info = QPrinterInfo::printerInfo(layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info, QPrinter::HighResolution);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
qUtf8Printable(tr("Cannot proceed because there are no available printers in your system.")));
return;
}
{
DialogLayoutScale layoutScale(isTiled, this);
layoutScale.SetXScale(1);
layoutScale.SetYScale(1);
layoutScale.exec();
m_xscale = layoutScale.GetXScale();
m_yscale = layoutScale.GetYScale();
}
SetPrinterSettings(printer.data(), PrintType::PrintNative);
QPrintDialog dialog(printer.data(), this );
// If only user couldn't change page margins we could use method setMinMax();
dialog.setOption(QPrintDialog::PrintCurrentPage, false);
if ( dialog.exec() == QDialog::Accepted )
{
printer->setResolution(static_cast<int>(PrintDPI));
PrintPages(printer.data());
}
}
//---------------------------------------------------------------------------------------------------------------------
void MainWindowsNoGUI::SetPrinterSettings(QPrinter *printer, const PrintType &printType)
{
SCASSERT(printer != nullptr)
printer->setCreator(QGuiApplication::applicationDisplayName()+QChar(QChar::Space)+
QCoreApplication::applicationVersion());
printer->setPageOrientation(isLayoutPortrait ? QPageLayout::Portrait : QPageLayout::Landscape);
if (not isTiled)
{
qreal width = FromPixel(paperSize.width(), Unit::Mm);
qreal height = FromPixel(paperSize.height(), Unit::Mm);
if (isAutoCropLength || isUnitePages)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
if (paper)
{
if (isLayoutPortrait)
{
height = FromPixel(paper->rect().height() + margins.top() + margins.bottom(), Unit::Mm);
}
else
{
width = FromPixel(paper->rect().width() + margins.left() + margins.right(), Unit::Mm);
}
}
}
if (isAutoCropWidth)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
if (paper)
{
if (isLayoutPortrait)
{
width = FromPixel(paper->rect().width() + margins.left() + margins.right(), Unit::Mm);
}
else
{
height = FromPixel(paper->rect().height() + margins.top() + margins.bottom(), Unit::Mm);
}
}
}
QSizeF size = QSizeF(width, height);
if (not isLayoutPortrait)
{
size.transpose(); // QPrinter reverse this for landscape orientation
}
const QPageSize::PageSizeId pSZ = FindPageSizeId(size);
if (pSZ == QPageSize::Custom)
{
if (not printer->setPageSize(QPageSize(size, QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set custom printer page size");
}
}
else
{
if (not printer->setPageSize(QPageSize(pSZ)))
{
qWarning() << tr("Cannot set printer page size");
}
}
m_layoutSettings->SetXScale(m_dialogSaveLayout->GetXScale());
m_layoutSettings->SetYScale(m_dialogSaveLayout->GetYScale());
}
else
{
if (not m_dialogSaveLayout.isNull())
{
VAbstractLayoutDialog::PaperSizeTemplate tiledFormat = m_dialogSaveLayout->GetTiledPageFormat();
if (not printer->setPageSize(QPageSize(m_dialogSaveLayout->GetTemplateSize(tiledFormat, Unit::Mm),
QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set printer page size");
}
}
VSettings *settings = qApp->ValentinaSettings();
m_layoutSettings->SetTiledMargins(QMarginsF(settings->GetTiledPDFMargins(Unit::Mm)));
m_layoutSettings->SetTiledPDFOrientation(settings->GetTiledPDFOrientation());
m_layoutSettings->SetTiledPDFPaperSize(QSizeF(settings->GetTiledPDFPaperWidth(Unit::Mm),
settings->GetTiledPDFPaperHeight(Unit::Mm)));
m_layoutSettings->SetXScale(1);
m_layoutSettings->SetYScale(1);
}
printer->setFullPage(ignorePrinterFields);
qreal left, top, right, bottom;
if (not isTiled)
{
QMarginsF pageMargin = QMarginsF(UnitConvertor(margins, Unit::Px, Unit::Mm));
left = pageMargin.left();
top = pageMargin.top();
right = pageMargin.right();
bottom = pageMargin.bottom();
}
else
{
QMarginsF pageMargin;
PageOrientation orientation;
if (not m_dialogSaveLayout.isNull())
{
pageMargin = m_dialogSaveLayout->GetTiledMargins();
orientation = m_dialogSaveLayout->GetTiledPageOrientation();
}
else
{
VSettings *settings = qApp->ValentinaSettings();
pageMargin = QMarginsF(settings->GetTiledPDFMargins(Unit::Mm));
orientation = settings->GetTiledPDFOrientation();
}
if(orientation == PageOrientation::Landscape)
{
// because when painting we have a -90rotation in landscape modus,
// see function PrintPages.
left = pageMargin.bottom();
top = pageMargin.left();
right = pageMargin.top();
bottom = pageMargin.right();
}
else
{
left = pageMargin.left();
top = pageMargin.top();
right = pageMargin.right();
bottom = pageMargin.bottom();
}
}
const bool success = printer->setPageMargins(QMarginsF(left, top, right, bottom), QPageLayout::Millimeter);
if (not success)
{
qWarning() << tr("Cannot set printer margins");
}
switch(printType)
{
case PrintType::PrintPDF:
{
const QString outputFileName = QDir::homePath() + QDir::separator() + FileName();
#ifdef Q_OS_WIN
printer->setOutputFileName(outputFileName);
#else
printer->setOutputFileName(outputFileName + QStringLiteral(".pdf"));
#endif
#ifdef Q_OS_MAC
printer->setOutputFormat(QPrinter::NativeFormat);
#else
printer->setOutputFormat(QPrinter::PdfFormat);
#endif
break;
}
case PrintType::PrintNative:
printer->setOutputFileName(QString());//Disable printing to file if was enabled.
printer->setOutputFormat(QPrinter::NativeFormat);
break;
case PrintType::PrintPreview: /*do nothing*/
default:
break;
}
printer->setDocName(FileName());
}
//---------------------------------------------------------------------------------------------------------------------
QPageSize::PageSizeId MainWindowsNoGUI::FindPageSizeId(const QSizeF &size) const
{
if (size == QSizeF(841, 1189) || size == QSizeF(1189, 841))
{
return QPageSize::A0;
}
if (size == QSizeF(594, 841) || size == QSizeF(841, 594))
{
return QPageSize::A1;
}
if (size == QSizeF(420, 594) || size == QSizeF(594, 420))
{
return QPageSize::A2;
}
if (size == QSizeF(297, 420) || size == QSizeF(420, 297))
{
return QPageSize::A3;
}
if (size == QSizeF(210, 297) || size == QSizeF(297, 210))
{
return QPageSize::A4;
}
if (size == QSizeF(215.9, 355.6) || size == QSizeF(355.6, 215.9))
{
return QPageSize::Legal;
}
if (size == QSizeF(215.9, 279.4) || size == QSizeF(279.4, 215.9))
{
return QPageSize::Letter;
}
return QPageSize::Custom;
}
//---------------------------------------------------------------------------------------------------------------------
bool MainWindowsNoGUI::isPagesUniform() const
{
if (papers.size() < 2)
{
return true;
}
else
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(paper != nullptr)
for (auto paperItem : papers)
{
auto *p = qgraphicsitem_cast<QGraphicsRectItem *>(paperItem);
SCASSERT(p != nullptr)
if (paper->rect() != p->rect())
{
return false;
}
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------
bool MainWindowsNoGUI::IsPagesFit(const QSizeF &printPaper) const
{
// On previous stage already was checked if pages have uniform size
// Enough will be to check only one page
QGraphicsRectItem *p = qgraphicsitem_cast<QGraphicsRectItem *>(papers.at(0));
SCASSERT(p != nullptr)
const QSizeF pSize = p->rect().size();
if (pSize.height() <= printPaper.height() && pSize.width() <= printPaper.width())
{
return true;
}
return false;
m_layoutSettings->SetWatermarkPath(AbsoluteMPath(qApp->GetPatternPath(), doc->GetWatermarkPath()));
m_layoutSettings->PdfTiledFile(name);
}
//---------------------------------------------------------------------------------------------------------------------
@ -1941,19 +1385,3 @@ void MainWindowsNoGUI::CheckRequiredMeasurements(const VMeasurements *m) const
throw e;
}
}
//---------------------------------------------------------------------------------------------------------------------
int MainWindowsNoGUI::ContinueIfLayoutStale()
{
QMessageBox msgBox(this);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(tr("The layout is stale."));
msgBox.setText(tr("The layout was not updated since last pattern modification. Do you want to continue?"));
msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = static_cast<QGridLayout*>(msgBox.layout());
SCASSERT(layout != nullptr)
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
return msgBox.exec();
}

View File

@ -40,6 +40,7 @@
#include "../vlayout/vlayoutgenerator.h"
#include "../vwidgets/vabstractmainwindow.h"
#include "../vlayout/vtextmanager.h"
#include "../vlayout/vprintlayout.h"
class QGraphicsScene;
struct PosterData;
@ -98,13 +99,9 @@ protected:
/** @brief doc dom document container */
VPattern *doc;
QList<QGraphicsItem *> papers;
QList<QGraphicsItem *> shadows;
QList<QGraphicsScene *> scenes;
QList<QList<QGraphicsItem *> > details;
QList<QGraphicsItem *> gcontours;
QList<QGraphicsItem *> gcontours{};
QVector<QVector<VLayoutPiece> > detailsOnLayout;
QVector<QVector<VLayoutPiece> > detailsOnLayout{};
QAction *undoAction;
QAction *redoAction;
@ -112,12 +109,8 @@ protected:
QAction *actionDockWidgetGroups;
bool isNoScaling;
bool isLayoutStale;
bool isNeedAutosave;
bool ignorePrinterFields;
bool isLayoutPortrait{true};
QMarginsF margins;
QSizeF paperSize;
VPrintLayout *m_layoutSettings{new VPrintLayout(this)};
QSharedPointer<DialogSaveLayout> m_dialogSaveLayout;
@ -140,7 +133,6 @@ protected:
virtual QStringList RecentFileList() const override;
QIcon ScenePreview(int i, QSize iconSize, PreviewQuatilty quality) const;
bool GenerateLayout(VLayoutGenerator& lGenerator);
int ContinueIfLayoutStale();
QString FileName() const;
bool ExportFMeasurementsToCSVData(const QString &fileName,
@ -149,21 +141,9 @@ protected:
QSharedPointer<VMeasurements> OpenMeasurementFile(const QString &path) const;
void CheckRequiredMeasurements(const VMeasurements *m) const;
private slots:
void PrintPages (QPrinter *printer);
private:
Q_DISABLE_COPY(MainWindowsNoGUI)
bool isTiled;
bool isAutoCropLength;
bool isAutoCropWidth;
bool isUnitePages;
QString layoutPrinterName;
qreal m_xscale{1};
qreal m_yscale{1};
static QList<QGraphicsItem *> CreateShadows(const QList<QGraphicsItem *> &papers);
static QList<QGraphicsScene *> CreateScenes(const QList<QGraphicsItem *> &papers,
const QList<QGraphicsItem *> &shadows,
@ -171,20 +151,6 @@ private:
void PdfTiledFile(const QString &name);
void PreparePaper(int index) const;
void RestorePaper(int index) const;
void PrintPreview();
void LayoutPrint();
enum class PrintType : qint8 {PrintPDF, PrintPreview, PrintNative};
void SetPrinterSettings(QPrinter *printer, const PrintType &printType);
QPageSize::PageSizeId FindPageSizeId(const QSizeF &size) const;
bool isPagesUniform() const;
bool IsPagesFit(const QSizeF &printPaper) const;
void ExportScene(const QList<QGraphicsScene *> &scenes,
const QList<QGraphicsItem *> &papers,
const QList<QGraphicsItem *> &shadows,

View File

@ -28,7 +28,7 @@
#include "dialoglayoutscale.h"
#include "ui_dialoglayoutscale.h"
#include "../core/vapplication.h"
#include "../vmisc/vabstractvalapplication.h"
#include "../vmisc/vsettings.h"
#include <QPushButton>
@ -40,7 +40,7 @@ DialogLayoutScale::DialogLayoutScale(bool printTiled, QWidget *parent)
{
ui->setupUi(this);
qApp->ValentinaSettings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
qApp->Settings()->GetOsSeparator() ? setLocale(QLocale()) : setLocale(QLocale::c());
QPushButton *bOk = ui->buttonBox->button(QDialogButtonBox::Ok);
SCASSERT(bOk != nullptr)
@ -180,7 +180,7 @@ void DialogLayoutScale::VerticalScaleChanged(double d)
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutScale::ReadSettings()
{
VSettings *settings = qApp->ValentinaSettings();
VCommonSettings *settings = qApp->Settings();
const Unit unit = qApp->patternUnits();
// read Margins top, right, bottom, left
@ -200,7 +200,7 @@ void DialogLayoutScale::ReadSettings()
//---------------------------------------------------------------------------------------------------------------------
void DialogLayoutScale::WriteSettings() const
{
VSettings *settings = qApp->ValentinaSettings();
VCommonSettings *settings = qApp->Settings();
const Unit unit = qApp->patternUnits();
// write Margins top, right, bottom, left

View File

@ -0,0 +1,13 @@
# ADD TO EACH PATH $$PWD VARIABLE!!!!!!
# This need for corect working file translations.pro
HEADERS += \
$$PWD/dialoglayoutscale.h \
$$PWD/vabstractlayoutdialog.h
SOURCES += \
$$PWD/dialoglayoutscale.cpp \
$$PWD/vabstractlayoutdialog.cpp
FORMS += \
$$PWD/dialoglayoutscale.ui

View File

@ -27,7 +27,7 @@
*************************************************************************/
#include "vabstractlayoutdialog.h"
#include "../core/vapplication.h"
#include "../vmisc/vabstractapplication.h"
#include "../vmisc/vmath.h"
//must be the same order as PaperSizeTemplate constants
@ -61,8 +61,8 @@ VAbstractLayoutDialog::VAbstractLayoutDialog(QWidget *parent)
void VAbstractLayoutDialog::InitTemplates(QComboBox *comboBoxTemplates)
{
SCASSERT(comboBoxTemplates != nullptr)
const QIcon icoPaper("://icon/16x16/template.png");
const QIcon icoRoll("://icon/16x16/roll.png");
const QIcon icoPaper(QLatin1String("://icon/16x16/template.png"));
const QIcon icoRoll(QLatin1String("://icon/16x16/roll.png"));
const QString pdi = QStringLiteral("(%1ppi)").arg(PrintDPI);
auto cntr = static_cast<VIndexType>(PaperSizeTemplate::A0);
@ -85,7 +85,7 @@ void VAbstractLayoutDialog::InitTemplates(QComboBox *comboBoxTemplates)
}
//---------------------------------------------------------------------------------------------------------------------
QSizeF VAbstractLayoutDialog::GetTemplateSize(PaperSizeTemplate tmpl, Unit unit)
auto VAbstractLayoutDialog::GetTemplateSize(PaperSizeTemplate tmpl, Unit unit) -> QSizeF
{
qreal width = 0;
qreal height = 0;
@ -159,11 +159,11 @@ QSizeF VAbstractLayoutDialog::GetTemplateSize(PaperSizeTemplate tmpl, Unit unit)
default:
break;
}
return QSizeF();
return {};
}
//---------------------------------------------------------------------------------------------------------------------
QSizeF VAbstractLayoutDialog::RoundTemplateSize(qreal width, qreal height, Unit unit)
auto VAbstractLayoutDialog::RoundTemplateSize(qreal width, qreal height, Unit unit) -> QSizeF
{
qreal w = 0;
qreal h = 0;
@ -175,14 +175,14 @@ QSizeF VAbstractLayoutDialog::RoundTemplateSize(qreal width, qreal height, Unit
case Unit::Px:
w = vRound(width, 2);
h = vRound(height, 2);
return QSizeF(w, h);
return {w, h};
case Unit::Inch:
w = vRound(width, 5);
h = vRound(height, 5);
return QSizeF(w, h);
return {w, h};
default:
break;
}
return QSizeF(width, height);
return {width, height};
}

View File

@ -59,16 +59,16 @@ public:
explicit VAbstractLayoutDialog(QWidget *parent = nullptr);
static QSizeF GetTemplateSize(PaperSizeTemplate tmpl, Unit unit);
static auto GetTemplateSize(PaperSizeTemplate tmpl, Unit unit) -> QSizeF;
protected:
typedef QStringList FormatsVector;
const static FormatsVector pageFormatNames;
typedef int VIndexType;
static QSizeF RoundTemplateSize(qreal width, qreal height, Unit unit);
static auto RoundTemplateSize(qreal width, qreal height, Unit unit) -> QSizeF;
void InitTemplates(QComboBox *comboBoxTemplates);
static void InitTemplates(QComboBox *comboBoxTemplates);
private:
Q_DISABLE_COPY(VAbstractLayoutDialog)
};

View File

@ -38,6 +38,7 @@
#include "../vmisc/typedef.h"
#include "vlayoutpiece.h"
#include "vlayoutdef.h"
// An annoying char define, from the Windows team in <rpcndr.h>
// #define small char
@ -49,8 +50,6 @@
Q_DECLARE_LOGGING_CATEGORY(lBank)
enum class Cases : qint8 { CaseThreeGroup = 0, CaseTwoGroup, CaseDesc, UnknownCase};
class VBank
{
Q_DECLARE_TR_FUNCTIONS(VBank)

View File

@ -1,6 +1,9 @@
# ADD TO EACH PATH $$PWD VARIABLE!!!!!!
# This need for corect working file translations.pro
# Suport subdirectories. Just better project code tree.
include(dialogs/dialogs.pri)
HEADERS += \
$$PWD/stable.h \
$$PWD/testpath.h \
@ -14,6 +17,7 @@ HEADERS += \
$$PWD/vcontour_p.h \
$$PWD/vbestsquare.h \
$$PWD/vposition.h \
$$PWD/vprintlayout.h \
$$PWD/vsapoint.h \
$$PWD/vtextmanager.h \
$$PWD/vposter.h \
@ -36,6 +40,7 @@ SOURCES += \
$$PWD/vcontour.cpp \
$$PWD/vbestsquare.cpp \
$$PWD/vposition.cpp \
$$PWD/vprintlayout.cpp \
$$PWD/vtextmanager.cpp \
$$PWD/vposter.cpp \
$$PWD/vgraphicsfillitem.cpp \

View File

@ -115,6 +115,8 @@ struct VCachedPositions
QPainterPath layoutAllowancePath{};
};
enum class Cases : qint8 { CaseThreeGroup = 0, CaseTwoGroup, CaseDesc, UnknownCase};
/* Warning! Debugging doesn't work stable in debug mode. If you need big allocation use release mode. Or disable
* Address Sanitizer. See page https://bitbucket.org/dismine/valentina/wiki/developers/Address_Sanitizer
*/

View File

@ -0,0 +1,754 @@
/************************************************************************
**
** @file vprintlayout.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 22 12, 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
** <https://gitlab.com/smart-pattern/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "vprintlayout.h"
#include <QPrinterInfo>
#include <QMessageBox>
#include <QSpacerItem>
#include <QGridLayout>
#include <QPrintDialog>
#include <QGuiApplication>
#include <QDir>
#include <QDebug>
#include <QGraphicsRectItem>
#include <QPainter>
#include <QPixmapCache>
#include <QGraphicsScene>
#include <QPrintPreviewDialog>
#include <QPageSize>
#include "dialogs/dialoglayoutscale.h"
#include "../vmisc/vabstractvalapplication.h"
#include "dialogs/vabstractlayoutdialog.h"
#include "vposter.h"
#include "../vformat/vwatermark.h"
#include "../ifc/xml/vwatermarkconverter.h"
#include "../ifc/exception/vexception.h"
#include "../vmisc/vmath.h"
namespace
{
//---------------------------------------------------------------------------------------------------------------------
auto FindPageSizeId(QSizeF size) -> QPageSize::PageSizeId
{
auto TestSize = [size](float width, float height)
{
return size == QSizeF(width, height) || size == QSizeF(height, width);
};
constexpr int A0Width = 841;
constexpr int A0Height = 1189;
if (TestSize(A0Width, A0Height))
{
return QPageSize::A0;
}
constexpr int A1Width = 594;
constexpr int A1Height = 841;
if (TestSize(A1Width, A1Height))
{
return QPageSize::A1;
}
constexpr int A2Width = 420;
constexpr int A2Height = 594;
if (TestSize(A2Width, A2Height))
{
return QPageSize::A2;
}
constexpr int A3Width = 297;
constexpr int A3Height = 420;
if (TestSize(A3Width, A3Height))
{
return QPageSize::A3;
}
constexpr int A4Width = 210;
constexpr int A4Height = 297;
if (TestSize(A4Width, A4Height))
{
return QPageSize::A4;
}
constexpr float LegalWidth = 215.9F;
constexpr float LegalHeight = 355.6F;
if (TestSize(LegalWidth, LegalHeight))
{
return QPageSize::Legal;
}
constexpr float LetterWidth = 215.9F;
constexpr float LetterHeight = 279.4F;
if (TestSize(LetterWidth, LetterHeight))
{
return QPageSize::Letter;
}
return QPageSize::Custom;
}
//---------------------------------------------------------------------------------------------------------------------
Q_REQUIRED_RESULT auto PreparePrinter(
const QPrinterInfo &info, QPrinter::PrinterMode mode = QPrinter::ScreenResolution) -> QSharedPointer<QPrinter>;
auto PreparePrinter(const QPrinterInfo &info, QPrinter::PrinterMode mode) -> QSharedPointer<QPrinter>
{
QPrinterInfo tmpInfo = info;
if(tmpInfo.isNull() || tmpInfo.printerName().isEmpty())
{
const QStringList list = QPrinterInfo::availablePrinterNames();
if(list.isEmpty())
{
return QSharedPointer<QPrinter>();
}
tmpInfo = QPrinterInfo::printerInfo(list.first());
}
auto printer = QSharedPointer<QPrinter>(new QPrinter(tmpInfo, mode));
printer->setResolution(static_cast<int>(PrintDPI));
return printer;
}
} // namespace
//---------------------------------------------------------------------------------------------------------------------
VPrintLayout::VPrintLayout(QObject *parent)
: QObject(parent)
{}
//---------------------------------------------------------------------------------------------------------------------
VPrintLayout::~VPrintLayout()
{
qDeleteAll(m_layoutScenes);
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintTiled()
{
m_isTiled = true;
PrintLayout();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintOrigin()
{
if (not IsPagesUniform())
{
qCritical()<<tr("For printing multipages document all sheet should have the same size.");
return;
}
m_isTiled = false;
PrintLayout();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintPreviewTiled()
{
m_isTiled = true;
PrintPreview();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintPreviewOrigin()
{
if (not IsPagesUniform())
{
qCritical()<<tr("For previewing multipage document all sheet should have the same size.");
return;
}
m_isTiled = false;
PrintPreview();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PdfTiledFile(const QString &name)
{
m_isTiled = true;
if (m_isLayoutStale)
{
if (ContinueIfLayoutStale(m_parentWidget) == QMessageBox::No)
{
return;
}
}
QPrinter printer;
SetPrinterSettings(&printer, PrintType::PrintPDF, name);
// Call IsPagesFit after setting a printer settings and check if pages is not bigger than printer's paper size
if (not m_isTiled && not IsPagesFit(printer.pageLayout().paintRectPixels(printer.resolution()).size()))
{
qWarning()<<tr("Pages will be cropped because they do not fit printer paper size.");
}
printer.setResolution(static_cast<int>(PrintDPI));
PrintPages(&printer);
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::CleanLayout()
{
qDeleteAll(m_layoutScenes);
m_layoutScenes.clear();
m_layoutPapers.clear();
m_layoutShadows.clear();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintLayout()
{
if (m_isLayoutStale)
{
if (ContinueIfLayoutStale(m_parentWidget) == QMessageBox::No)
{
return;
}
}
// display print dialog and if accepted print
QPrinterInfo info = QPrinterInfo::printerInfo(m_layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info, QPrinter::HighResolution);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
qUtf8Printable(tr("Cannot proceed because there are no available printers in your system.")));
return;
}
{
DialogLayoutScale layoutScale(m_isTiled, m_parentWidget);
layoutScale.SetXScale(1);
layoutScale.SetYScale(1);
layoutScale.exec();
m_xscale = layoutScale.GetXScale();
m_yscale = layoutScale.GetYScale();
}
SetPrinterSettings(printer.data(), PrintType::PrintNative);
QPrintDialog dialog(printer.data(), m_parentWidget);
// If only user couldn't change page margins we could use method setMinMax();
dialog.setOption(QPrintDialog::PrintCurrentPage, false);
if (dialog.exec() == QDialog::Accepted)
{
printer->setResolution(static_cast<int>(PrintDPI));
PrintPages(printer.data());
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintPreview()
{
if (m_isLayoutStale)
{
if (ContinueIfLayoutStale(m_parentWidget) == QMessageBox::No)
{
return;
}
}
QPrinterInfo info = QPrinterInfo::printerInfo(m_layoutPrinterName);
if(info.isNull() || info.printerName().isEmpty())
{
info = QPrinterInfo::defaultPrinter();
}
QSharedPointer<QPrinter> printer = PreparePrinter(info);
if (printer.isNull())
{
qCritical("%s\n\n%s", qUtf8Printable(tr("Print error")),
qUtf8Printable(tr("Cannot proceed because there are no available printers in your system.")));
return;
}
{
DialogLayoutScale layoutScale(m_isTiled, m_parentWidget);
layoutScale.SetXScale(1);
layoutScale.SetYScale(1);
layoutScale.exec();
m_xscale = layoutScale.GetXScale();
m_yscale = layoutScale.GetYScale();
}
SetPrinterSettings(printer.data(), PrintType::PrintPreview);
printer->setResolution(static_cast<int>(PrintDPI));
// display print preview dialog
QPrintPreviewDialog preview(printer.data());
connect(&preview, &QPrintPreviewDialog::paintRequested, this, &VPrintLayout::PrintPages);
preview.exec();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PrintPages(QPrinter *printer)
{
// Here we try understand difference between printer's dpi and our.
// Get printer rect acording to our dpi.
const QRectF printerPageRect(0, 0, ToPixel(printer->pageRect(QPrinter::Millimeter).width(), Unit::Mm),
ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm));
const QRect pageRect = printer->pageLayout().paintRectPixels(printer->resolution());
const double xscale = pageRect.width() / printerPageRect.width();
const double yscale = pageRect.height() / printerPageRect.height();
QPainter painter;
if (not painter.begin(printer))
{ // failed to open file
qCritical() << tr("Failed to open file, is it writable?");
return;
}
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
int count = 0;
QSharedPointer<QVector<PosterData>> poster;
QSharedPointer<VPoster> posterazor;
if (m_isTiled)
{
// when isTiled, the landscape tiles have to be rotated, because the pages
// stay portrait in the pdf
if(m_tiledPDFOrientation == PageOrientation::Landscape)
{
const int angle = -90;
painter.rotate(angle);
painter.translate(-ToPixel(printer->pageRect(QPrinter::Millimeter).height(), Unit::Mm), 0);
}
poster = QSharedPointer<QVector<PosterData>>(new QVector<PosterData>());
posterazor = QSharedPointer<VPoster>(new VPoster(printer));
for (int i=0; i < m_layoutScenes.size(); ++i)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(i));
if (paper != nullptr)
{
QRectF paperRect = paper->rect();
QSizeF image(paperRect.width() * m_xscale, paperRect.height() * m_yscale);
*poster += posterazor->Calc(image.toSize(), i, m_tiledPDFOrientation);
}
}
count = poster->size();
}
else
{
count = m_layoutScenes.size();
}
// Handle the fromPage(), toPage(), supportsMultipleCopies(), and numCopies() values from QPrinter.
int firstPage = printer->fromPage() - 1;
if (firstPage >= count)
{
return;
}
if (firstPage == -1)
{
firstPage = 0;
}
int lastPage = printer->toPage() - 1;
if (lastPage == -1 || lastPage >= count)
{
lastPage = count - 1;
}
const int numPages = lastPage - firstPage + 1;
int copyCount = 1;
if (not printer->supportsMultipleCopies())
{
copyCount = printer->copyCount();
}
VWatermarkData data;
if (not m_watermarkPath.isEmpty())
{
try
{
VWatermarkConverter converter(m_watermarkPath);
VWatermark watermark;
watermark.setXMLContent(converter.Convert());
data = watermark.GetWatermark();
if (not data.path.isEmpty())
{
// Clean previous cache
QPixmapCache::remove(AbsoluteMPath(m_watermarkPath, data.path));
}
}
catch (VException &e)
{
const QString errorMsg = tr("File error.\n\n%1\n\n%2").arg(e.ErrorMessage(), e.DetailedInformation());
qApp->IsPedantic() ? throw VException(errorMsg) :
qWarning() << VAbstractValApplication::warningMessageSignature + errorMsg;
}
}
for (int i = 0; i < copyCount; ++i)
{
for (int j = 0; j < numPages; ++j)
{
if (i != 0 || j != 0)
{
if (not printer->newPage())
{
qCritical() << tr("Failed in flushing page to disk, disk full?");
return;
}
}
int index;
if (printer->pageOrder() == QPrinter::FirstPageFirst)
{
index = firstPage + j;
}
else
{
index = lastPage - j;
}
int paperIndex = -1;
m_isTiled ? paperIndex = static_cast<int>(poster->at(index).index) : paperIndex = index;
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(paperIndex));
if (paper != nullptr)
{
QVector<QGraphicsItem *> posterData;
if (m_isTiled)
{
// Draw tile
posterData = posterazor->Tile(paper, poster->at(index), m_layoutScenes.size(), data,
m_watermarkPath);
}
PreparePaper(paperIndex);
// Render
QRectF source;
m_isTiled ? source = poster->at(index).rect : source = paper->rect();
qreal x;
qreal y;
if(printer->fullPage())
{
QPageLayout layout = printer->pageLayout();
layout.setUnits(QPageLayout::Millimeter);
QMarginsF printerMargins = layout.margins();
x = qFloor(ToPixel(printerMargins.left(),Unit::Mm));
y = qFloor(ToPixel(printerMargins.top(),Unit::Mm));
}
else
{
x = 0; y = 0;
}
QRectF target(x * xscale, y * yscale, source.width() * xscale, source.height() * yscale);
m_layoutScenes.at(paperIndex)->render(&painter, target, source, Qt::IgnoreAspectRatio);
if (m_isTiled)
{
// Remove borders
qDeleteAll(posterData);
}
// Restore
RestorePaper(paperIndex);
}
}
}
painter.end();
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::SetPrinterSettings(QPrinter *printer, PrintType printType, const QString &filePath)
{
SCASSERT(printer != nullptr)
printer->setCreator(QGuiApplication::applicationDisplayName() + QChar(QChar::Space) +
QCoreApplication::applicationVersion());
printer->setPageOrientation(m_isLayoutPortrait ? QPageLayout::Portrait : QPageLayout::Landscape);
SetPrinterPageSize(printer);
printer->setFullPage(m_ignorePrinterMargins);
SetPrinterPrinterMargins(printer);
SetPrinterOutputFileName(printer, printType, filePath);
printer->setDocName(filePath.isEmpty() ? DocName() : QFileInfo(filePath).baseName());
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::SetPrinterPrinterMargins(QPrinter *printer)
{
SCASSERT(printer != nullptr)
QMarginsF printerMargins;
if (not m_isTiled)
{
printerMargins = QMarginsF(UnitConvertor(m_layoutMargins, Unit::Px, Unit::Mm));
}
else
{
if(m_tiledPDFOrientation == PageOrientation::Landscape)
{
// because when painting we have a -90rotation in landscape mode,
// see function PrintPages.
printerMargins = QMarginsF(m_tiledMargins.bottom(), m_tiledMargins.left(), m_tiledMargins.top(),
m_tiledMargins.right());
}
else
{
printerMargins = m_tiledMargins;
}
}
const bool success = printer->setPageMargins(printerMargins, QPageLayout::Millimeter);
if (not success)
{
qWarning() << tr("Cannot set printer margins");
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::SetPrinterOutputFileName(QPrinter *printer, PrintType printType, const QString &filePath)
{
SCASSERT(printer != nullptr)
switch(printType)
{
case PrintType::PrintPDF:
{
const QString outputFileName = filePath.isEmpty() ? QDir::homePath() + QDir::separator() + DocName()
: filePath;
#ifdef Q_OS_WIN
printer->setOutputFileName(outputFileName);
#else
printer->setOutputFileName(outputFileName + QStringLiteral(".pdf"));
#endif
#ifdef Q_OS_MAC
printer->setOutputFormat(QPrinter::NativeFormat);
#else
printer->setOutputFormat(QPrinter::PdfFormat);
#endif
break;
}
case PrintType::PrintNative:
printer->setOutputFileName(QString());//Disable printing to file if was enabled.
printer->setOutputFormat(QPrinter::NativeFormat);
break;
case PrintType::PrintPreview: /*do nothing*/
default:
break;
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::SetPrinterPageSize(QPrinter *printer)
{
SCASSERT(printer != nullptr)
if (not m_isTiled)
{
qreal width = FromPixel(m_layoutPaperSize.width(), Unit::Mm);
qreal height = FromPixel(m_layoutPaperSize.height(), Unit::Mm);
if (m_isAutoCropLength || m_isUnitePages)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(0));
if (paper != nullptr)
{
if (m_isLayoutPortrait)
{
height = FromPixel(paper->rect().height() + m_layoutMargins.top() + m_layoutMargins.bottom(),
Unit::Mm);
}
else
{
width = FromPixel(paper->rect().width() + m_layoutMargins.left() + m_layoutMargins.right(),
Unit::Mm);
}
}
}
if (m_isAutoCropWidth)
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(0));
if (paper != nullptr)
{
if (m_isLayoutPortrait)
{
width = FromPixel(paper->rect().width() + m_layoutMargins.left() + m_layoutMargins.right(),
Unit::Mm);
}
else
{
height = FromPixel(paper->rect().height() + m_layoutMargins.top() + m_layoutMargins.bottom(),
Unit::Mm);
}
}
}
QSizeF size = QSizeF(width, height);
if (not m_isLayoutPortrait)
{
size.transpose(); // QPrinter reverse this for landscape orientation
}
const QPageSize::PageSizeId pSZ = FindPageSizeId(size);
if (pSZ == QPageSize::Custom)
{
if (not printer->setPageSize(QPageSize(size, QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set custom printer page size");
}
}
else
{
if (not printer->setPageSize(QPageSize(pSZ)))
{
qWarning() << tr("Cannot set printer page size");
}
}
}
else
{
if (not printer->setPageSize(QPageSize(m_tiledPDFPaperSize, QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set printer tiled page size");
}
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPrintLayout::DocName() const -> QString
{
return not m_fileName.isEmpty() ? m_fileName : tr("unamed");
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::PreparePaper(int index) const
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(index));
if (paper != nullptr)
{
QBrush brush(Qt::white);
m_layoutScenes.at(index)->setBackgroundBrush(brush);
m_layoutShadows.at(index)->setVisible(false);
const float thinPen = 0.1F;
paper->setPen(QPen(Qt::white, thinPen, Qt::NoPen));// border
}
QTransform matrix;
matrix.scale(m_xscale, m_yscale);
QList<QGraphicsItem *> paperDetails = m_layoutDetails.at(index);
for (auto *detail : paperDetails)
{
QTransform m = detail->transform();
m *= matrix;
detail->setTransform(m);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPrintLayout::RestorePaper(int index) const
{
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(index));
if (paper != nullptr)
{
// Restore
paper->setPen(QPen(Qt::black, 1));
QBrush brush(Qt::gray);
m_layoutScenes.at(index)->setBackgroundBrush(brush);
m_layoutShadows.at(index)->setVisible(true);
}
QTransform matrix;
matrix.scale(1./m_xscale, 1./m_yscale);
QList<QGraphicsItem *> paperDetails = m_layoutDetails.at(index);
for (auto *detail : paperDetails)
{
QTransform m = detail->transform();
m *= matrix;
detail->setTransform(m);
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPrintLayout::IsPagesUniform() const -> bool
{
if (m_layoutPapers.size() < 2)
{
return true;
}
auto *paper = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(0));
SCASSERT(paper != nullptr)
return std::all_of(m_layoutPapers.begin(), m_layoutPapers.end(), [paper](QGraphicsItem *paperItem)
{
auto *p = qgraphicsitem_cast<QGraphicsRectItem *>(paperItem);
SCASSERT(p != nullptr)
return paper->rect() == p->rect();
});
}
//---------------------------------------------------------------------------------------------------------------------
auto VPrintLayout::IsPagesFit(QSizeF printPaper) const -> bool
{
// On previous stage already was checked if pages have uniform size
// Enough will be to check only one page
auto *p = qgraphicsitem_cast<QGraphicsRectItem *>(m_layoutPapers.at(0));
SCASSERT(p != nullptr)
const QSizeF pSize = p->rect().size();
return pSize.height() <= printPaper.height() && pSize.width() <= printPaper.width();
}
//---------------------------------------------------------------------------------------------------------------------
auto VPrintLayout::ContinueIfLayoutStale(QWidget *parent) -> int
{
QMessageBox msgBox(parent);
msgBox.setIcon(QMessageBox::Question);
msgBox.setWindowTitle(tr("The layout is stale."));
msgBox.setText(tr("The layout was not updated since last pattern modification. Do you want to continue?"));
msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
const int width = 500;
auto* horizontalSpacer = new QSpacerItem(width, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
auto* layout = qobject_cast<QGridLayout*>(msgBox.layout());
SCASSERT(layout != nullptr)
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
return msgBox.exec();
}

View File

@ -0,0 +1,424 @@
/************************************************************************
**
** @file vprintlayout.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 22 12, 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
** <https://gitlab.com/smart-pattern/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef VPRINTLAYOUT_H
#define VPRINTLAYOUT_H
#include <QMargins>
#include <QObject>
#include <QSizeF>
#include "../vmisc/def.h"
enum class PrintType : qint8 {PrintPDF, PrintPreview, PrintNative};
class QPrinter;
class QGraphicsScene;
class VPrintLayout : public QObject
{
Q_OBJECT
public:
VPrintLayout(QObject *parent = nullptr);
~VPrintLayout();
auto FileName() const -> QString;
void SetFileName(const QString &fileName);
auto LayoutPaperSize() const -> QSizeF;
void SetLayoutPaperSize(const QSizeF &paperSize);
auto LayoutMargins() const -> QMarginsF;
void SetLayoutMargins(const QMarginsF &layoutMargins);
auto ParentWidget() const -> QWidget *;
void SetParentWidget(QWidget *parentWidget);
auto IsLayoutPortrait() const -> bool;
void SetLayoutPortrait(bool isLayoutPortrait);
auto IgnorePrinterMargins() const -> bool;
void SetIgnorePrinterMargins(bool ignorePrinterFields);
auto IsAutoCropLength() const -> bool;
void SetAutoCropLength(bool isAutoCropLength);
auto IsAutoCropWidth() const -> bool;
void SetAutoCropWidth(bool isAutoCropWidth);
auto IsUnitePages() const -> bool;
void SetUnitePages(bool isUnitePages);
auto LayoutPrinterName() const -> QString;
void SetLayoutPrinterName(const QString &layoutPrinterName);
auto IsLayoutStale() const -> bool;
void SetLayoutStale(bool isLayoutStale);
auto TiledMargins() const -> QMarginsF;
void SetTiledMargins(const QMarginsF &tiledMargins);
auto TiledPDFOrientation() const -> PageOrientation;
void SetTiledPDFOrientation(const PageOrientation &tiledPDFOrientation);
auto TiledPDFPaperSize() const -> QSizeF;
void SetTiledPDFPaperSize(const QSizeF &tiledPDFPaperSize);
auto XScale() const -> qreal;
void SetXScale(const qreal &xscale);
auto YScale() const -> qreal;
void SetYScale(const qreal &yscale);
auto WatermarkPath() const -> QString;
void SetWatermarkPath(const QString &watermarkPath);
auto LayoutPapers() const -> QList<QGraphicsItem *>;
void SetLayoutPapers(const QList<QGraphicsItem *> &layoutPapers);
auto LayoutScenes() const -> QList<QGraphicsScene *>;
void SetLayoutScenes(const QList<QGraphicsScene *> &layoutScenes);
auto LayoutShadows() const -> QList<QGraphicsItem *>;
void SetLayoutShadows(const QList<QGraphicsItem *> &layoutShadows);
auto LayoutDetails() const -> QList<QList<QGraphicsItem *> >;
void SetLayoutDetails(const QList<QList<QGraphicsItem *> > &layoutDetails);
void PrintTiled();
void PrintOrigin();
void PrintPreviewTiled();
void PrintPreviewOrigin();
void PdfTiledFile(const QString &name);
void CleanLayout();
static auto ContinueIfLayoutStale(QWidget *parent) -> int;
private slots:
void PrintPages (QPrinter *printer);
private:
Q_DISABLE_COPY(VPrintLayout)
QString m_fileName{};
QSizeF m_layoutPaperSize{};
QMarginsF m_layoutMargins{};
QWidget *m_parentWidget{nullptr};
bool m_isLayoutPortrait{true};
bool m_ignorePrinterMargins{false};
bool m_isAutoCropLength{false};
bool m_isAutoCropWidth{false};
bool m_isUnitePages{false};
QString m_layoutPrinterName{};
bool m_isLayoutStale{true};
QMarginsF m_tiledMargins{};
PageOrientation m_tiledPDFOrientation{PageOrientation::Portrait};
QSizeF m_tiledPDFPaperSize{};
QString m_watermarkPath{};
QList<QGraphicsItem *> m_layoutPapers{};
QList<QGraphicsScene *> m_layoutScenes{};
QList<QGraphicsItem *> m_layoutShadows{};
QList<QList<QGraphicsItem *> > m_layoutDetails{};
bool m_isTiled{false};
qreal m_xscale{1};
qreal m_yscale{1};
void PrintLayout();
void PrintPreview();
void SetPrinterSettings(QPrinter *printer, PrintType printType, const QString &filePath = QString());
void SetPrinterPrinterMargins(QPrinter *printer);
void SetPrinterOutputFileName(QPrinter *printer, PrintType printType, const QString &filePath = QString());
void SetPrinterPageSize(QPrinter *printer);
QString DocName() const;
void PreparePaper(int index) const;
void RestorePaper(int index) const;
bool IsPagesUniform() const;
bool IsPagesFit(QSizeF printPaper) const;
};
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::FileName() const -> QString
{
return m_fileName;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetFileName(const QString &fileName)
{
m_fileName = fileName;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutPaperSize() const -> QSizeF
{
return m_layoutPaperSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutPaperSize(const QSizeF &paperSize)
{
m_layoutPaperSize = paperSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutMargins() const -> QMarginsF
{
return m_layoutMargins;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutMargins(const QMarginsF &layoutMargins)
{
m_layoutMargins = layoutMargins;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::ParentWidget() const -> QWidget *
{
return m_parentWidget;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetParentWidget(QWidget *parentWidget)
{
m_parentWidget = parentWidget;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::IsLayoutPortrait() const -> bool
{
return m_isLayoutPortrait;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutPortrait(bool isLayoutPortrait)
{
m_isLayoutPortrait = isLayoutPortrait;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::IgnorePrinterMargins() const -> bool
{
return m_ignorePrinterMargins;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetIgnorePrinterMargins(bool ignorePrinterFields)
{
m_ignorePrinterMargins = ignorePrinterFields;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::IsAutoCropLength() const -> bool
{
return m_isAutoCropLength;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetAutoCropLength(bool isAutoCropLength)
{
m_isAutoCropLength = isAutoCropLength;
}
//---------------------------------------------------------------------------------------------------------------------
inline bool VPrintLayout::IsAutoCropWidth() const
{
return m_isAutoCropWidth;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetAutoCropWidth(bool isAutoCropWidth)
{
m_isAutoCropWidth = isAutoCropWidth;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::IsUnitePages() const -> bool
{
return m_isUnitePages;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetUnitePages(bool isUnitePages)
{
m_isUnitePages = isUnitePages;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutPrinterName() const -> QString
{
return m_layoutPrinterName;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutPrinterName(const QString &layoutPrinterName)
{
m_layoutPrinterName = layoutPrinterName;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::IsLayoutStale() const -> bool
{
return m_isLayoutStale;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutStale(bool isLayoutStale)
{
m_isLayoutStale = isLayoutStale;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::TiledMargins() const -> QMarginsF
{
return m_tiledMargins;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetTiledMargins(const QMarginsF &tiledMargins)
{
m_tiledMargins = tiledMargins;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::TiledPDFOrientation() const -> PageOrientation
{
return m_tiledPDFOrientation;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetTiledPDFOrientation(const PageOrientation &tiledPDFOrientation)
{
m_tiledPDFOrientation = tiledPDFOrientation;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::TiledPDFPaperSize() const -> QSizeF
{
return m_tiledPDFPaperSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetTiledPDFPaperSize(const QSizeF &tiledPDFPaperSize)
{
m_tiledPDFPaperSize = tiledPDFPaperSize;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutPapers() const -> QList<QGraphicsItem *>
{
return m_layoutPapers;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutPapers(const QList<QGraphicsItem *> &layoutPapers)
{
m_layoutPapers = layoutPapers;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutScenes() const -> QList<QGraphicsScene *>
{
return m_layoutScenes;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutScenes(const QList<QGraphicsScene *> &layoutScenes)
{
m_layoutScenes = layoutScenes;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutShadows() const -> QList<QGraphicsItem *>
{
return m_layoutShadows;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutShadows(const QList<QGraphicsItem *> &layoutShadows)
{
m_layoutShadows = layoutShadows;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::LayoutDetails() const -> QList<QList<QGraphicsItem *> >
{
return m_layoutDetails;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetLayoutDetails(const QList<QList<QGraphicsItem *> > &layoutDetails)
{
m_layoutDetails = layoutDetails;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::YScale() const -> qreal
{
return m_yscale;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetYScale(const qreal &yscale)
{
m_yscale = yscale;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::XScale() const -> qreal
{
return m_xscale;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetXScale(const qreal &xscale)
{
m_xscale = xscale;
}
//---------------------------------------------------------------------------------------------------------------------
inline auto VPrintLayout::WatermarkPath() const -> QString
{
return m_watermarkPath;
}
//---------------------------------------------------------------------------------------------------------------------
inline void VPrintLayout::SetWatermarkPath(const QString &watermarkPath)
{
m_watermarkPath = watermarkPath;
}
#endif // VPRINTLAYOUT_H

View File

@ -277,28 +277,6 @@ QString AbsoluteMPath(const QString &patternPath, const QString &relativeMPath)
return QFileInfo(QFileInfo(patternPath).absoluteDir(), relativeMPath).absoluteFilePath();
}
//---------------------------------------------------------------------------------------------------------------------
QSharedPointer<QPrinter> PreparePrinter(const QPrinterInfo &info, QPrinter::PrinterMode mode)
{
QPrinterInfo tmpInfo = info;
if(tmpInfo.isNull() || tmpInfo.printerName().isEmpty())
{
const QStringList list = QPrinterInfo::availablePrinterNames();
if(list.isEmpty())
{
return QSharedPointer<QPrinter>();
}
else
{
tmpInfo = QPrinterInfo::printerInfo(list.first());
}
}
auto printer = QSharedPointer<QPrinter>(new QPrinter(tmpInfo, mode));
printer->setResolution(static_cast<int>(PrintDPI));
return printer;
}
//---------------------------------------------------------------------------------------------------------------------
QMarginsF GetMinPrinterFields(const QSharedPointer<QPrinter> &printer)
{

View File

@ -602,9 +602,6 @@ Q_REQUIRED_RESULT QString AbsoluteMPath(const QString &patternPath, const QStrin
QStringList SplitFilePaths(const QString &path);
Q_REQUIRED_RESULT QSharedPointer<QPrinter> PreparePrinter(const QPrinterInfo &info,
QPrinter::PrinterMode mode = QPrinter::ScreenResolution);
QMarginsF GetMinPrinterFields(const QSharedPointer<QPrinter> &printer);
QMarginsF GetPrinterFields(const QSharedPointer<QPrinter> &printer);

View File

@ -47,6 +47,8 @@
#include "../vmisc/compatibility.h"
#include "../vpatterndb/pmsystems.h"
Q_DECLARE_METATYPE(QMarginsF)
namespace
{
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingPathsIndividualMeasurements, (QLatin1String("paths/individual_measurements")))
@ -105,6 +107,9 @@ Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelUserDateFormats, (QLatin1St
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelTimeFormat, (QLatin1String("label/timeFormat")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingLabelUserTimeFormats, (QLatin1String("label/userTimeFormats")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFMargins, (QLatin1String("tiledPDF/margins")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFOrientation, (QLatin1String("tiledPDF/orientation")))
// Reading settings file is very expensive, cache curve approximation to speed up getting value
qreal curveApproximationCached = -1;
Q_GLOBAL_STATIC(QString, localeCached)
@ -1216,3 +1221,43 @@ qreal VCommonSettings::WidthHairLine() const
{
return WidthMainLine()/3.0;
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetTiledPDFMargins returns the tiled pdf margins in the given unit. When the setting is
* called for the first time, the 4 default margins are 10mm.
* @param unit the unit in which are the value. Necessary because we save the values
* internaly as mm so there is conversion beeing made.
* @return tiled pdf margins
*/
auto VCommonSettings::GetTiledPDFMargins(const Unit &unit) const -> QMarginsF
{
// default value is 10mm. We save the margins in mm in the setting.
return UnitConvertor(ValueOrDef<QMarginsF>(*settingTiledPDFMargins, QMarginsF(10, 10, 10, 10)), Unit::Mm, unit);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief SetTiledPDFMargins sets the setting tiled pdf margins to the given value.
* @param value the margins to save
* @param unit the unit in which are the value. Necessary because we save the values
* internaly as mm so there is conversion beeing made.
*/
void VCommonSettings::SetTiledPDFMargins(const QMarginsF &value, const Unit &unit)
{
setValue(*settingTiledPDFMargins, QVariant::fromValue(UnitConvertor(value, unit, Unit::Mm)));
}
//---------------------------------------------------------------------------------------------------------------------
auto VCommonSettings::GetTiledPDFOrientation() const -> PageOrientation
{
bool defaultValue = static_cast<bool>(PageOrientation::Portrait);
bool result = value(*settingTiledPDFOrientation, defaultValue).toBool();
return static_cast<PageOrientation>(result);
}
//---------------------------------------------------------------------------------------------------------------------
void VCommonSettings::SetTiledPDFOrientation(PageOrientation value)
{
setValue(*settingTiledPDFOrientation, static_cast<bool> (value));
}

View File

@ -38,6 +38,11 @@
#include <QStringList>
#include <QtGlobal>
#include "../vmisc/def.h"
#include "../vlayout/vlayoutdef.h"
class QMarginsF;
class VCommonSettings : public QSettings
{
Q_OBJECT
@ -224,6 +229,18 @@ public:
void SetLineWidth(qreal width);
qreal WidthMainLine() const;
qreal WidthHairLine() const;
// settings for the tiled PDFs
auto GetTiledPDFMargins(const Unit &unit) const -> QMarginsF;
void SetTiledPDFMargins(const QMarginsF &value, const Unit &unit);
auto GetTiledPDFOrientation() const -> PageOrientation;
void SetTiledPDFOrientation(PageOrientation value);
protected:
template <class T>
T ValueOrDef(const QString &setting, const T &defValue) const;
private:
Q_DISABLE_COPY(VCommonSettings)
};
@ -246,4 +263,28 @@ inline qreal VCommonSettings::MaximalLineWidth()
return 5.0; // mm
}
//---------------------------------------------------------------------------------------------------------------------
template <class T>
inline T VCommonSettings::ValueOrDef(const QString &setting, const T &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(defValue));
return val.canConvert<T>() ? val.value<T>() : defValue;
}
//---------------------------------------------------------------------------------------------------------------------
template <>
inline Cases VCommonSettings::ValueOrDef<Cases>(const QString &setting, const Cases &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(static_cast<int>(defValue)));
const int g = val.canConvert<int>() ? val.value<int>() : static_cast<int>(defValue);
if (g < static_cast<int>(Cases::CaseThreeGroup) || g >= static_cast<int>(Cases::UnknownCase))
{
return defValue;
}
else
{
return static_cast<Cases>(g);
}
}
#endif // VCOMMONSETTINGS_H

View File

@ -104,10 +104,8 @@ Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTextAsPaths, (QLatin1String("lay
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingNestingTime, (QLatin1String("layout/time")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingEfficiencyCoefficient, (QLatin1String("layout/efficiencyCoefficient")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFMargins, (QLatin1String("tiledPDF/margins")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFPaperHeight, (QLatin1String("tiledPDF/paperHeight")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFPaperWidth, (QLatin1String("tiledPDF/paperWidth")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingTiledPDFOrientation, (QLatin1String("tiledPDF/orientation")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingScrollingDuration, (QLatin1String("scrolling/duration")))
Q_GLOBAL_STATIC_WITH_ARGS(const QString, settingScrollingUpdateInterval, (QLatin1String("scrolling/updateInterval")))
@ -148,30 +146,6 @@ VSettings::VSettings(const QString &fileName, QSettings::Format format, QObject
qRegisterMetaTypeStreamOperators<QMarginsF>("QMarginsF");
}
//---------------------------------------------------------------------------------------------------------------------
template <class T>
inline T VSettings::ValueOrDef(const QString &setting, const T &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(defValue));
return val.canConvert<T>() ? val.value<T>() : defValue;
}
//---------------------------------------------------------------------------------------------------------------------
template <>
inline Cases VSettings::ValueOrDef<Cases>(const QString &setting, const Cases &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(static_cast<int>(defValue)));
const int g = val.canConvert<int>() ? val.value<int>() : static_cast<int>(defValue);
if (g < static_cast<int>(Cases::CaseThreeGroup) || g >= static_cast<int>(Cases::UnknownCase))
{
return defValue;
}
else
{
return static_cast<Cases>(g);
}
}
//---------------------------------------------------------------------------------------------------------------------
QString VSettings::GetLabelLanguage() const
{
@ -563,32 +537,6 @@ void VSettings::SetRememberPatternMaterials(bool value)
}
// settings for the tiled PDFs
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetTiledPDFMargins returns the tiled pdf margins in the given unit. When the setting is
* called for the first time, the 4 default margins are 10mm.
* @param unit the unit in which are the value. Necessary because we save the values
* internaly as mm so there is conversion beeing made.
* @return tiled pdf margins
*/
QMarginsF VSettings::GetTiledPDFMargins(const Unit &unit) const
{
// default value is 10mm. We save the margins in mm in the setting.
return UnitConvertor(ValueOrDef<QMarginsF>(*settingTiledPDFMargins, QMarginsF(10, 10, 10, 10)), Unit::Mm, unit);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief SetTiledPDFMargins sets the setting tiled pdf margins to the given value.
* @param value the margins to save
* @param unit the unit in which are the value. Necessary because we save the values
* internaly as mm so there is conversion beeing made.
*/
void VSettings::SetTiledPDFMargins(const QMarginsF &value, const Unit &unit)
{
setValue(*settingTiledPDFMargins, QVariant::fromValue(UnitConvertor(value, unit, Unit::Mm)));
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief GetTiledPDFPaperHeight returns the paper height of tiled pdf in the desired unit.
@ -635,20 +583,6 @@ void VSettings::SetTiledPDFPaperWidth(qreal value, const Unit &unit)
setValue(*settingTiledPDFPaperWidth, UnitConvertor(value,unit, Unit::Mm));
}
//---------------------------------------------------------------------------------------------------------------------
PageOrientation VSettings::GetTiledPDFOrientation() const
{
bool defaultValue = static_cast<bool>(PageOrientation::Portrait);
bool result = value(*settingTiledPDFOrientation, defaultValue).toBool();
return static_cast<PageOrientation>(result);
}
//---------------------------------------------------------------------------------------------------------------------
void VSettings::SetTiledPDFOrientation(PageOrientation value)
{
setValue(*settingTiledPDFOrientation, static_cast<bool> (value));
}
//---------------------------------------------------------------------------------------------------------------------
int VSettings::GetScrollingDuration() const
{

View File

@ -141,18 +141,12 @@ public:
void SetRememberPatternMaterials(bool value);
// settings for the tiled PDFs
QMarginsF GetTiledPDFMargins(const Unit &unit) const;
void SetTiledPDFMargins(const QMarginsF &value, const Unit &unit);
qreal GetTiledPDFPaperHeight(const Unit &unit) const;
void SetTiledPDFPaperHeight(qreal value, const Unit &unit);
qreal GetTiledPDFPaperWidth(const Unit &unit) const;
void SetTiledPDFPaperWidth(qreal value, const Unit &unit);
PageOrientation GetTiledPDFOrientation() const;
void SetTiledPDFOrientation(PageOrientation value);
static const int defaultScrollingDuration;
static const int scrollingDurationMin;
static const int scrollingDurationMax;
@ -213,10 +207,7 @@ private:
Q_DISABLE_COPY(VSettings)
template <typename T>
T GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const;
template <class T>
T ValueOrDef(const QString &setting, const T &defValue) const;
T GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const;
};
#endif // VSETTINGS_H