Skip to content

Commit

Permalink
Add id=PageName to exported Svg
Browse files Browse the repository at this point in the history
- add id attribute to output of QSvgGenerator.
  • Loading branch information
WandererFan authored and yorikvanhavre committed Dec 7, 2018
1 parent 14b780c commit 0e26ce1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
62 changes: 57 additions & 5 deletions src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -34,7 +34,11 @@
# include <QPaintEvent>
# include <QSvgGenerator>
# include <QWheelEvent>
# include <cmath>
#include <QTemporaryFile>
#include <QDomDocument>
#include <QTextStream>
#include <QFile>
#include <cmath>
#endif

#include <App/Application.h>
Expand Down Expand Up @@ -578,15 +582,15 @@ void QGVPage::saveSvg(QString filename)
tr(" exported from FreeCAD document: ") +
docName;


QSvgGenerator svgGen;
svgGen.setFileName(filename);
QTemporaryFile* tempFile = new QTemporaryFile();;
svgGen.setOutputDevice(tempFile);
svgGen.setSize(QSize((int) Rez::guiX(page->getPageWidth()), (int) Rez::guiX(page->getPageHeight()))); //expects pixels, gets mm
//"By default this property is set to QSize(-1, -1), which indicates that the generator should not output
// the width and height attributes of the <svg> element." >> but Inkscape won't read it without size info??
svgGen.setViewBox(QRect(0, 0, Rez::guiX(page->getPageWidth()), Rez::guiX(page->getPageHeight())));
svgGen.setResolution(Rez::guiX(25.4)); // docs say this is DPI. 1dot/mm so 25.4dpi

svgGen.setResolution(Rez::guiX(25.4)); // docs say this is DPI. Rez::guiX(1dot/mm) so 254 dpi?

svgGen.setTitle(QObject::tr("FreeCAD SVG Export"));
svgGen.setDescription(svgDescription);
Expand Down Expand Up @@ -614,6 +618,54 @@ void QGVPage::saveSvg(QString filename)
toggleHatch(true);
scene()->update();
viewport()->repaint();

tempFile->close();
postProcessXml(tempFile, filename, pageName);
}

void QGVPage::postProcessXml(QTemporaryFile* tempFile, QString fileName, QString pageName)
{
QDomDocument doc(QString::fromUtf8("SvgDoc"));
QFile file(tempFile->fileName());
if (!file.open(QIODevice::ReadOnly)) {
Base::Console().Message("QGVPage::ppsvg - tempfile open error\n");
return;
}
if (!doc.setContent(&file)) {
Base::Console().Message("QGVPage::ppsvg - xml error\n");
file.close();
return;
}
file.close();

QDomElement docElem = doc.documentElement(); //root <svg>

QDomNode n = docElem.firstChild();
bool firstGroupFound = false;
QString groupTag = QString::fromUtf8("g");
QDomElement e;
while(!n.isNull()) {
e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
if (!firstGroupFound) {
if (e.tagName() == groupTag) {
firstGroupFound = true;
break;
}
}
}
n = n.nextSibling();
}
e.setAttribute(QString::fromUtf8("id"),pageName);

QFile outFile( fileName );
if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) {
Base::Console().Message("QGVP::ppxml - failed to open file for writing: %s\n.",qPrintable(fileName) );
}
QTextStream stream( &outFile );
stream << doc.toString();
outFile.close();
delete tempFile;
}

void QGVPage::paintEvent(QPaintEvent *event)
Expand Down
3 changes: 3 additions & 0 deletions src/Mod/TechDraw/Gui/QGVPage.h
Expand Up @@ -26,6 +26,8 @@
#include <QGraphicsView>
#include <QGraphicsScene>

class QTemporaryFile;

namespace TechDraw {
class DrawView;
class DrawViewPart;
Expand Down Expand Up @@ -100,6 +102,7 @@ class TechDrawGuiExport QGVPage : public QGraphicsView

/// Renders the page to SVG with filename.
void saveSvg(QString filename);
void postProcessXml(QTemporaryFile* tempFile, QString filename, QString pagename);

public Q_SLOTS:
void setHighQualityAntialiasing(bool highQualityAntialiasing);
Expand Down

0 comments on commit 0e26ce1

Please sign in to comment.