When export to SVG or PNG file apply margins.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-01-03 19:33:15 +02:00
parent a5e990400f
commit a7332adc75
2 changed files with 20 additions and 13 deletions

View File

@ -913,22 +913,26 @@ QList<QGraphicsScene *> MainWindowsNoGUI::CreateScenes(const QList<QGraphicsItem
* @brief SvgFile save layout to svg file. * @brief SvgFile save layout to svg file.
* @param name name layout file. * @param name name layout file.
*/ */
void MainWindowsNoGUI::SvgFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene) const void MainWindowsNoGUI::SvgFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene,
const QMarginsF &margins) const
{ {
const QRectF r = paper->rect();
QSvgGenerator generator; QSvgGenerator generator;
generator.setFileName(name); generator.setFileName(name);
generator.setSize(paper->rect().size().toSize()); generator.setSize(QSize(qFloor(r.width() + margins.left() + margins.right()),
generator.setViewBox(paper->rect()); qFloor(r.height() + margins.top() + margins.bottom())));
generator.setViewBox(QRectF(0, 0, r.width() + margins.left() + margins.right(),
r.height() + margins.top() + margins.bottom()));
generator.setTitle(tr("Pattern")); generator.setTitle(tr("Pattern"));
generator.setDescription(doc->GetDescription().toHtmlEscaped()); generator.setDescription(doc->GetDescription().toHtmlEscaped());
generator.setResolution(static_cast<int>(PrintDPI)); generator.setResolution(static_cast<int>(PrintDPI));
QPainter painter; QPainter painter;
painter.begin(&generator); painter.begin(&generator);
painter.setFont( QFont( "Arial", 8, QFont::Normal ) ); painter.translate(margins.left(), margins.top());
painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->Settings()->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.setPen(QPen(Qt::black, qApp->Settings()->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) ); painter.setBrush ( QBrush ( Qt::NoBrush ) );
scene->render(&painter, paper->rect(), paper->rect(), Qt::IgnoreAspectRatio); scene->render(&painter, r, r, Qt::IgnoreAspectRatio);
painter.end(); painter.end();
} }
@ -937,14 +941,17 @@ void MainWindowsNoGUI::SvgFile(const QString &name, QGraphicsRectItem *paper, QG
* @brief PngFile save layout to png file. * @brief PngFile save layout to png file.
* @param name name layout file. * @param name name layout file.
*/ */
void MainWindowsNoGUI::PngFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene) const void MainWindowsNoGUI::PngFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene,
const QMarginsF &margins) const
{ {
const QRectF r = paper->rect(); const QRectF r = paper->rect();
// Create the image with the exact size of the shrunk scene // Create the image with the exact size of the shrunk scene
QImage image(r.size().toSize(), QImage::Format_ARGB32); QImage image(QSize(qFloor(r.width() + margins.left() + margins.right()),
image.fill(Qt::transparent); // Start all pixels transparent qFloor(r.height() + margins.top() + margins.bottom())),
QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image); QPainter painter(&image);
painter.setFont( QFont( "Arial", 8, QFont::Normal ) ); painter.translate(margins.left(), margins.top());
painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.setPen(QPen(Qt::black, qApp->Settings()->WidthMainLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) ); painter.setBrush ( QBrush ( Qt::NoBrush ) );
@ -1572,14 +1579,14 @@ void MainWindowsNoGUI::ExportScene(const QList<QGraphicsScene *> &scenes,
{ {
case LayoutExportFormats::SVG: case LayoutExportFormats::SVG:
paper->setVisible(false); paper->setVisible(false);
SvgFile(name, paper, scene); SvgFile(name, paper, scene, margins);
paper->setVisible(true); paper->setVisible(true);
break; break;
case LayoutExportFormats::PDF: case LayoutExportFormats::PDF:
PdfFile(name, paper, scene, ignorePrinterFields, margins); PdfFile(name, paper, scene, ignorePrinterFields, margins);
break; break;
case LayoutExportFormats::PNG: case LayoutExportFormats::PNG:
PngFile(name, paper, scene); PngFile(name, paper, scene, margins);
break; break;
case LayoutExportFormats::OBJ: case LayoutExportFormats::OBJ:
paper->setVisible(false); paper->setVisible(false);

View File

@ -139,8 +139,8 @@ private:
const QList<QGraphicsItem *> &shadows, const QList<QGraphicsItem *> &shadows,
const QList<QList<QGraphicsItem *> > &details); const QList<QList<QGraphicsItem *> > &details);
void SvgFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene)const; void SvgFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene, const QMarginsF &margins)const;
void PngFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene)const; void PngFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene, const QMarginsF &margins)const;
void PdfFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene, bool ignorePrinterFields, void PdfFile(const QString &name, QGraphicsRectItem *paper, QGraphicsScene *scene, bool ignorePrinterFields,
const QMarginsF &margins)const; const QMarginsF &margins)const;
void PdfTiledFile(const QString &name); void PdfTiledFile(const QString &name);