From c867a9a56edaa0459745fe38a363f596c6e92dc8 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 28 Jun 2021 20:05:19 +0300 Subject: [PATCH] Export the Draw mode. Closes #83 --- ChangeLog.txt | 1 + src/app/valentina/mainwindow.cpp | 84 ++++++++++++++++++++++++ src/app/valentina/mainwindow.h | 3 + src/app/valentina/mainwindow.ui | 24 ++++++- src/libs/ifc/xml/vabstractpattern.cpp | 2 +- src/libs/vlayout/vlayoutexporter.cpp | 13 ++++ src/libs/vlayout/vlayoutexporter.h | 4 ++ src/libs/vwidgets/vmaingraphicsscene.cpp | 4 +- 8 files changed, 130 insertions(+), 5 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 571d86ced..25f72e269 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -3,6 +3,7 @@ - Parsing formulas with not canonical math symbols. - [smart-pattern/valentina#133] Incorrect seam allowance. - Fix regression. Fix correct handle export format for exporting details in the Detail mode. +- [smart-pattern/valentina#83] Export the Draw mode. # Version 0.7.47 May 13, 2021 - [smart-pattern/valentina#118] Incorrect seam allowance. diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 86febb45b..d4fe15014 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -67,6 +67,8 @@ #include "../vformat/vpatternrecipe.h" #include "watermarkwindow.h" #include "../vmisc/backport/qoverload.h" +#include "../vlayout/vlayoutexporter.h" +#include "../vwidgets/vgraphicssimpletextitem.h" #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) #include "../vmisc/backport/qscopeguard.h" @@ -2244,6 +2246,62 @@ void MainWindow::StoreDimensions() } } +//--------------------------------------------------------------------------------------------------------------------- +void MainWindow::ExportDraw(const QString &fileName) +{ + QT_WARNING_PUSH + QT_WARNING_DISABLE_GCC("-Wnoexcept") + + VLayoutExporter exporter; + + QT_WARNING_POP + + exporter.SetFileName(fileName); + + int verticalScrollBarValue = ui->view->verticalScrollBar()->value(); + int horizontalScrollBarValue = ui->view->horizontalScrollBar()->value(); + + QTransform viewTransform = ui->view->transform(); + ui->view->ZoomFitBest(); // Resize all labels + ui->view->repaint(); + ui->view->ZoomOriginal(); // Set to original scale + + // Enable all items on scene + const QList qItems = sceneDraw->items(); + for (auto *item : qItems) + { + item->setEnabled(true); + if (item->type() == VGraphicsSimpleTextItem::Type) + { + auto *text = dynamic_cast(item); + text->setBrush(text->BaseColor()); // Regular update doesn't work on labels + } + } + + ui->view->repaint(); + + sceneDraw->SetOriginsVisible(false); + + const QRectF rect = sceneDraw->VisibleItemsBoundingRect(); + sceneDraw->update(rect); + exporter.SetImageRect(rect); + exporter.SetOffset(rect.topLeft()); // Correct positions to fit SVG view rect + + exporter.ExportToSVG(sceneDraw); + + sceneDraw->SetOriginsVisible(true); + + // Restore scale, scrollbars and current active pattern piece + ui->view->setTransform(viewTransform); + VMainGraphicsView::NewSceneRect(ui->view->scene(), ui->view); + emit ScaleChanged(ui->view->transform().m11()); + + ui->view->verticalScrollBar()->setValue(verticalScrollBarValue); + ui->view->horizontalScrollBar()->setValue(horizontalScrollBarValue); + + doc->ChangeActivPP(doc->GetNameActivPP(), Document::FullParse); +} + //--------------------------------------------------------------------------------------------------------------------- #if defined(Q_OS_MAC) void MainWindow::OpenAt(QAction *where) @@ -2517,6 +2575,7 @@ void MainWindow::InitToolButtons() connect(ui->toolButtonFlippingByAxis, &QToolButton::clicked, this, &MainWindow::ToolFlippingByAxis); connect(ui->toolButtonMove, &QToolButton::clicked, this, &MainWindow::ToolMove); connect(ui->toolButtonMidpoint, &QToolButton::clicked, this, &MainWindow::ToolMidpoint); + connect(ui->toolButtonExportDraw, &QToolButton::clicked, this, &MainWindow::ExportDrawAs); connect(ui->toolButtonLayoutExportAs, &QToolButton::clicked, this, &MainWindow::ExportLayoutAs); connect(ui->toolButtonDetailExportAs, &QToolButton::clicked, this, &MainWindow::ExportDetailsAs); connect(ui->toolButtonEllipticalArc, &QToolButton::clicked, this, &MainWindow::ToolEllipticalArc); @@ -4226,6 +4285,7 @@ QT_WARNING_POP ui->toolButtonPin->setEnabled(drawTools); ui->toolButtonInsertNode->setEnabled(drawTools); ui->toolButtonPlaceLabel->setEnabled(drawTools); + ui->toolButtonExportDraw->setEnabled(drawTools); ui->actionLast_tool->setEnabled(drawTools); @@ -5480,6 +5540,30 @@ void MainWindow::CreateMeasurements() } #endif +//--------------------------------------------------------------------------------------------------------------------- +void MainWindow::ExportDrawAs() +{ + auto Uncheck = qScopeGuard([this] {ui->toolButtonExportDraw->setChecked(false);}); + + QString filters(tr("Scalable Vector Graphics files") + QLatin1String("(*.svg)")); + QString dir = QDir::homePath() + QLatin1String("/") + FileName() + QLatin1String(".svg"); + QString fileName = QFileDialog::getSaveFileName(this, tr("Save draw"), dir, filters, nullptr, + VAbstractApplication::VApp()->NativeFileDialog()); + + if (fileName.isEmpty()) + { + return; + } + + QFileInfo f( fileName ); + if (f.suffix().isEmpty() || f.suffix() != QLatin1String("svg")) + { + fileName += QLatin1String(".svg"); + } + + ExportDraw(fileName); +} + //--------------------------------------------------------------------------------------------------------------------- void MainWindow::ExportLayoutAs() { diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 357cded20..b6a9136be 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -130,6 +130,7 @@ private slots: #if defined(Q_OS_MAC) void CreateMeasurements(); #endif + void ExportDrawAs(); void ExportLayoutAs(); void ExportDetailsAs(); @@ -415,6 +416,8 @@ private: void SetDimensionBases(); void StoreDimensions(); + + void ExportDraw(const QString &fileName); }; #endif // MAINWINDOW_H diff --git a/src/app/valentina/mainwindow.ui b/src/app/valentina/mainwindow.ui index ca14002f7..feb3b4d85 100644 --- a/src/app/valentina/mainwindow.ui +++ b/src/app/valentina/mainwindow.ui @@ -1119,9 +1119,9 @@ 0 - 0 + -53 126 - 192 + 237 @@ -1317,6 +1317,26 @@ + + + + false + + + ... + + + + :/icon/32x32/export_to_picture_document.png:/icon/32x32/export_to_picture_document.png + + + + 32 + 32 + + + + diff --git a/src/libs/ifc/xml/vabstractpattern.cpp b/src/libs/ifc/xml/vabstractpattern.cpp index 21ddcac5b..fa6b96b96 100644 --- a/src/libs/ifc/xml/vabstractpattern.cpp +++ b/src/libs/ifc/xml/vabstractpattern.cpp @@ -291,7 +291,7 @@ QStringList VAbstractPattern::ListMeasurements() const void VAbstractPattern::ChangeActivPP(const QString &name, const Document &parse) { Q_ASSERT_X(not name.isEmpty(), Q_FUNC_INFO, "name pattern piece is empty"); - if (CheckExistNamePP(name) && this->nameActivPP != name) + if (CheckExistNamePP(name)) { this->nameActivPP = name; if (parse == Document::FullParse) diff --git a/src/libs/vlayout/vlayoutexporter.cpp b/src/libs/vlayout/vlayoutexporter.cpp index 42c168cd5..c2daf0ef0 100644 --- a/src/libs/vlayout/vlayoutexporter.cpp +++ b/src/libs/vlayout/vlayoutexporter.cpp @@ -139,6 +139,7 @@ void VLayoutExporter::ExportToSVG(QGraphicsScene *scene) const QPainter painter; painter.begin(&generator); + painter.translate(-m_offset.x(), -m_offset.y()); painter.translate(m_margins.left(), m_margins.top()); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(m_pen); @@ -374,6 +375,18 @@ auto VLayoutExporter::SupportPDFConversion() -> bool return res; } +//--------------------------------------------------------------------------------------------------------------------- +QPointF VLayoutExporter::offset() const +{ + return m_offset; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VLayoutExporter::SetOffset(QPointF newOffset) +{ + m_offset = newOffset; +} + //--------------------------------------------------------------------------------------------------------------------- /** * @brief PdfToPs use external tool "pdftops" for converting pdf too eps or ps format. diff --git a/src/libs/vlayout/vlayoutexporter.h b/src/libs/vlayout/vlayoutexporter.h index af7f8b85d..ed8246c44 100644 --- a/src/libs/vlayout/vlayoutexporter.h +++ b/src/libs/vlayout/vlayoutexporter.h @@ -90,6 +90,9 @@ public: static auto SupportPDFConversion() -> bool; + auto offset() const -> QPointF; + void SetOffset(QPointF newOffset); + private: QString m_fileName{}; QMarginsF m_margins{}; @@ -102,6 +105,7 @@ private: bool m_ignorePrinterMargins{false}; bool m_binaryDxfFormat{false}; int m_dxfVersion{0}; + QPointF m_offset{}; static void PdfToPs(const QStringList ¶ms); }; diff --git a/src/libs/vwidgets/vmaingraphicsscene.cpp b/src/libs/vwidgets/vmaingraphicsscene.cpp index 8e43d0c26..e15d474e2 100644 --- a/src/libs/vwidgets/vmaingraphicsscene.cpp +++ b/src/libs/vwidgets/vmaingraphicsscene.cpp @@ -221,9 +221,9 @@ void VMainGraphicsScene::InitOrigins() //--------------------------------------------------------------------------------------------------------------------- void VMainGraphicsScene::SetOriginsVisible(bool visible) { - for (auto item : qAsConst(origins)) + for (auto *item : qAsConst(origins)) { - if (item) + if (item != nullptr) { item->setVisible(visible); }