VPExporter inherits from VLayoutExporter

This commit is contained in:
Ronan 2021-05-22 11:00:42 +02:00
parent fb2a7f8714
commit 1c6aebec4f
5 changed files with 46 additions and 182 deletions

View File

@ -260,6 +260,24 @@ DEPENDPATH += $$PWD/../../libs/vgeometry
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vgeometry/$${DESTDIR}/vgeometry.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vgeometry/$${DESTDIR}/libvgeometry.a
# VObj static library
unix|win32: LIBS += -L$$OUT_PWD/../../libs/vobj/$${DESTDIR}/ -lvobj
INCLUDEPATH += $$PWD/../../libs/vobj
DEPENDPATH += $$PWD/../../libs/vobj
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vobj/$${DESTDIR}/vobj.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vobj/$${DESTDIR}/libvobj.a
# VDxf static library
unix|win32: LIBS += -L$$OUT_PWD/../../libs/vdxf/$${DESTDIR}/ -lvdxf
INCLUDEPATH += $$PWD/../../libs/vdxf
DEPENDPATH += $$PWD/../../libs/vdxf
win32:!win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/vdxf.lib
else:unix|win32-g++: PRE_TARGETDEPS += $$OUT_PWD/../../libs/vdxf/$${DESTDIR}/libvdxf.a
# QMuParser library
win32:CONFIG(release, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2
else:win32:CONFIG(debug, debug|release): LIBS += -L$${OUT_PWD}/../../libs/qmuparser/$${DESTDIR} -lqmuparser2

View File

@ -1,8 +1,6 @@
#include "vpexporter.h"
#include <QtSvg>
#include <QImage>
#include <QImageWriter>
#include <QFileDialog>
#include "../vwidgets/vmaingraphicsscene.h"
#include "vpsheet.h"
@ -10,9 +8,7 @@
//---------------------------------------------------------------------------------------------------------------------
VPExporter::VPExporter(VPLayout* layout, VCommonSettings *commonSettings):
m_layout(layout),
m_commonSettings(commonSettings)
VPExporter::VPExporter()
{
}
@ -24,14 +20,14 @@ VPExporter::~VPExporter()
}
//---------------------------------------------------------------------------------------------------------------------
void VPExporter::Export(LayoutExportFormats format, VPMainGraphicsView *mainGraphicsView)
void VPExporter::Export(VPLayout* layout, LayoutExportFormats format, VPMainGraphicsView *mainGraphicsView)
{
QString dir = QDir::homePath();
QString filters(ExportFormatDescription(format));
// is it ok to have a null ptr hier as a parent?
QString fileName = QFileDialog::getSaveFileName(nullptr, tr("Save as"),
dir + QLatin1String("/") + m_layout->GetFocusedSheet()->GetName() + ExportFormatSuffix(format),
dir + QLatin1String("/") + layout->GetFocusedSheet()->GetName() + ExportFormatSuffix(format),
filters, nullptr
#ifdef Q_OS_LINUX
, QFileDialog::DontUseNativeDialog
@ -42,161 +38,41 @@ void VPExporter::Export(LayoutExportFormats format, VPMainGraphicsView *mainGrap
{
mainGraphicsView->PrepareForExport();
SetFileName(fileName);
QSizeF size = QSizeF(layout->GetFocusedSheet()->GetSheetSize());
if(layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Landscape)
{
size.transpose();
}
const QRectF rect = QRectF(0, 0, size.width(), size.height());
SetImageRect(rect);
switch(format)
{
case LayoutExportFormats::SVG:
ExportToSVG(fileName, mainGraphicsView);
ExportToSVG(mainGraphicsView->scene());
break;
case LayoutExportFormats::PDF:
ExportToPDF(fileName, mainGraphicsView);
break;
case LayoutExportFormats::PNG:
ExportToPNG(fileName, mainGraphicsView);
ExportToPDF(mainGraphicsView->scene());
break;
case LayoutExportFormats::TIF:
ExportToTIF(fileName, mainGraphicsView);
ExportToTIF(mainGraphicsView->scene());
break;
default:
// do nothing
break;
}
mainGraphicsView->CleanAfterExport();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPExporter::ExportToSVG(QString fileName, VPMainGraphicsView *mainGraphicsView)
{
QSizeF size = QSizeF(m_layout->GetFocusedSheet()->GetSheetSize());
if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Landscape)
{
size.transpose();
}
const QRectF rect = QRectF(0, 0, size.width(), size.height());
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setResolution(static_cast<int>(PrintDPI));
generator.setSize(QSize(qRound(size.width()),qRound(size.height())));
generator.setViewBox(rect);
generator.setTitle(m_layout->GetFocusedSheet()->GetName());
generator.setDescription(m_layout->GetDescription().toHtmlEscaped());
QPainter painter;
painter.begin(&generator);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, m_commonSettings->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
mainGraphicsView->GetScene()->render(&painter, rect, rect, Qt::IgnoreAspectRatio);
painter.end();
}
//---------------------------------------------------------------------------------------------------------------------
void VPExporter::ExportToPDF(QString fileName, VPMainGraphicsView *mainGraphicsView)
{
QSizeF size = QSizeF(m_layout->GetFocusedSheet()->GetSheetSize());
if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Landscape)
{
size.transpose();
}
const QRectF rect = QRectF(0, 0, size.width(), size.height());
QPrinter printer;
printer.setCreator(QGuiApplication::applicationDisplayName() + QChar(QChar::Space) +
QCoreApplication::applicationVersion());
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
printer.setDocName(QFileInfo(fileName).fileName());
printer.setResolution(static_cast<int>(PrintDPI));
printer.setPageOrientation(QPageLayout::Portrait);
printer.setFullPage(true);
qreal width = FromPixel(size.width(), Unit::Mm);
qreal height = FromPixel(size.height(), Unit::Mm);
if (not printer.setPageSize(QPageSize(QSizeF(width, height), QPageSize::Millimeter)))
{
qWarning() << tr("Cannot set printer page size");
}
QPainter painter;
if (not painter.begin(&printer))
{ // failed to open file
qCritical() << qUtf8Printable(tr("Can't open file '%1'").arg(fileName));
return;
}
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, m_commonSettings->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush ( QBrush ( Qt::NoBrush ) );
mainGraphicsView->GetScene()->render(&painter, rect, rect, Qt::IgnoreAspectRatio);
painter.end();
}
//---------------------------------------------------------------------------------------------------------------------
void VPExporter::ExportToPNG(QString filename, VPMainGraphicsView *mainGraphicsView)
{
QSizeF size = QSizeF(m_layout->GetFocusedSheet()->GetSheetSize());
if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Landscape)
{
size.transpose();
}
const QRectF rect = QRectF(0, 0, size.width(), size.height());
QImage image(QSize(qFloor(size.width()), qFloor((size.height()))), QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, m_commonSettings->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush(QBrush(Qt::NoBrush));
mainGraphicsView->GetScene()->render(&painter, rect, rect, Qt::IgnoreAspectRatio);
image.save(filename);
}
//---------------------------------------------------------------------------------------------------------------------
void VPExporter::ExportToTIF(QString filename, VPMainGraphicsView *mainGraphicsView)
{
QSizeF size = QSizeF(m_layout->GetFocusedSheet()->GetSheetSize());
if(m_layout->GetFocusedSheet()->GetOrientation() == PageOrientation::Landscape)
{
size.transpose();
}
const QRectF rect = QRectF(0, 0, size.width(), size.height());
QImage image(QSize(qFloor(size.width()), qFloor((size.height()))), QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, m_commonSettings->WidthHairLine(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.setBrush(QBrush(Qt::NoBrush));
mainGraphicsView->GetScene()->render(&painter, rect, rect, Qt::IgnoreAspectRatio);
// FIXME, not working in ubuntu 21.04 of rlt "format not supported"
QImageWriter writer;
writer.setFormat("tiff");
writer.setCompression(1); // LZW-compression
writer.setFileName(filename);
if (not writer.write(image))
{ // failed to save file
qCritical() << qUtf8Printable(tr("Can't save file '%1'. Error: %2.").arg(filename, writer.errorString()));
return;
}
}
// FIXME Bad copy paste from DialogSaveLayout, because I didn't know how to call this function from here
// to be removed as soon as I know how to call the central function from valentina
//---------------------------------------------------------------------------------------------------------------------
@ -361,7 +237,7 @@ QVector<std::pair<QString, LayoutExportFormats> > VPExporter::InitFormats()
InitFormat(LayoutExportFormats::SVG);
InitFormat(LayoutExportFormats::PDF);
InitFormat(LayoutExportFormats::PNG);
// InitFormat(LayoutExportFormats::PNG);
// InitFormat(LayoutExportFormats::OBJ);

View File

@ -7,20 +7,19 @@
#include "../vmisc/def.h"
#include "vcommonsettings.h"
#include "../vlayout/vlayoutdef.h"
#include "../vlayout/vlayoutexporter.h"
//#include "../dialogs/dialogsavelayout.h"
class VPMainGraphicsView;
class VPExporter : QObject
class VPExporter : VLayoutExporter
{
Q_OBJECT
public:
VPExporter(VPLayout* layout, VCommonSettings *commonSettings);
VPExporter();
~VPExporter();
void Export(LayoutExportFormats format, VPMainGraphicsView *mainGraphicsView);
void Export(VPLayout* layout, LayoutExportFormats format, VPMainGraphicsView *mainGraphicsView);
// FIXME Bad copy paste from DialogSaveLayout, because I didn't know how to call this function from here
@ -28,34 +27,7 @@ public:
QVector<std::pair<QString, LayoutExportFormats> > InitFormats();
private:
Q_DISABLE_COPY(VPExporter)
VPLayout *m_layout{nullptr};
VCommonSettings *m_commonSettings{nullptr};
/**
* @brief ExportSVG exports the current maingraphicViews to an svg file
* @param fileName the file name of the export
*/
void ExportToSVG(QString fileName, VPMainGraphicsView *mainGraphicsView);
/**
* @brief ExportSVG exports the current maingraphicViews to a pdf file
* @param fileName the file name of the export
*/
void ExportToPDF(QString filename, VPMainGraphicsView *mainGraphicsView);
/**
* @brief ExportSVG exports the current maingraphicViews to a png file
* @param fileName the file name of the export
*/
void ExportToPNG(QString filename, VPMainGraphicsView *mainGraphicsView);
/**
* @brief ExportSVG exports the current maingraphicViews to a TIF file
* @param fileName the file name of the export
*/
void ExportToTIF(QString filename, VPMainGraphicsView *mainGraphicsView);
// FIXME Bad copy paste from DialogSaveLayout, because I didn't know how to call this function from here

View File

@ -98,9 +98,6 @@ VPMainWindow::VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent) :
m_tileFactory = new VPTileFactory(m_layout, VPApplication::VApp()->Settings());
m_tileFactory->refreshTileInfos();
// init the export tool
m_exporter = new VPExporter(m_layout, VPApplication::VApp()->Settings());
// init status bar
statusBar()->addPermanentWidget(m_statusLabel, 1);
@ -494,7 +491,8 @@ void VPMainWindow::InitPropertyTabCurrentSheet()
// ---------------------- export format --------------------------
for (auto &v : m_exporter->InitFormats())
VPExporter exporter;
for (auto &v : exporter.InitFormats())
{
ui->comboBoxSheetExportFormat->addItem(v.first, QVariant(static_cast<int>(v.second)));
}
@ -1684,7 +1682,8 @@ void VPMainWindow::on_pushButtonSheetExport_clicked()
{
LayoutExportFormats format = static_cast<LayoutExportFormats>(ui->comboBoxSheetExportFormat->currentData().toInt());
m_exporter->Export(format, m_graphicsView);
VPExporter exporter;
exporter.Export(m_layout, format, m_graphicsView);
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -417,7 +417,6 @@ private:
QList<VPPiece *>m_selectedPieces{QList<VPPiece *>()};
VPTileFactory *m_tileFactory{nullptr};
VPExporter *m_exporter{nullptr};
/**
* @brief spin box with the scale factor of the graphic view