Skip to content

Commit

Permalink
[TD]Export Svg hatch as bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed May 30, 2020
1 parent 08854bc commit a9b718d
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/Mod/TechDraw/App/DrawViewPart.cpp
Expand Up @@ -625,8 +625,7 @@ std::vector<TechDraw::DrawHatch*> DrawViewPart::getHatches() const
std::vector<TechDraw::DrawHatch*> result;
std::vector<App::DocumentObject*> children = getInList();
for (std::vector<App::DocumentObject*>::iterator it = children.begin(); it != children.end(); ++it) {
if ( ((*it)->getTypeId().isDerivedFrom(DrawHatch::getClassTypeId())) &&
(!(*it)->isRemoving()) ) {
if ((*it)->getTypeId().isDerivedFrom(DrawHatch::getClassTypeId())) {
TechDraw::DrawHatch* hatch = dynamic_cast<TechDraw::DrawHatch*>(*it);
result.push_back(hatch);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Mod/TechDraw/Gui/QGCustomImage.cpp
Expand Up @@ -74,6 +74,15 @@ bool QGCustomImage::load(QString fileSpec)
return(success);
}

bool QGCustomImage::load(QPixmap map)
{
bool success = true;
m_px = map;
prepareGeometryChange();
setPixmap(m_px);
return(success);
}

QSize QGCustomImage::imageSize(void)
{
QSize result = m_px.size();
Expand Down
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/QGCustomImage.h
Expand Up @@ -53,6 +53,7 @@ class TechDrawGuiExport QGCustomImage : public QGraphicsPixmapItem
virtual void centerAt(QPointF centerPos);
virtual void centerAt(double cX, double cY);
virtual bool load(QString fileSpec);
virtual bool load(QPixmap map);
virtual QSize imageSize(void);

protected:
Expand Down
93 changes: 92 additions & 1 deletion src/Mod/TechDraw/Gui/QGIFace.cpp
Expand Up @@ -58,7 +58,9 @@
#include "ZVALUE.h"
//
#include "Rez.h"
#include "DrawGuiUtil.h"
#include "QGCustomSvg.h"
#include "QGCustomImage.h"
#include "QGCustomRect.h"
#include "QGIViewPart.h"
#include "QGIPrimPath.h"
Expand All @@ -84,6 +86,9 @@ QGIFace::QGIFace(int index) :
setPrettyNormal();
m_texture = QPixmap(); //empty texture

m_image = new QGCustomImage();
m_image->setParentItem(this);

m_svg = new QGCustomSvg();

m_rect = new QGCustomRect();
Expand Down Expand Up @@ -139,10 +144,13 @@ void QGIFace::draw()
m_styleNormal = m_styleDef;
m_fillStyleCurrent = m_styleNormal;
loadSvgHatch(m_fileSpec);
buildSvgHatch();
if (m_hideSvgTiles) {
buildPixHatch();
m_rect->hide();
m_image->show();
} else {
buildSvgHatch();
m_image->hide();
m_rect->show();
}
} else if ((ext.toUpper() == QString::fromUtf8("JPG")) ||
Expand Down Expand Up @@ -553,6 +561,89 @@ void QGIFace::clearSvg()
hideSvg(true);
}

void QGIFace::buildPixHatch()
{
double wTile = SVGSIZEW * m_fillScale;
double hTile = SVGSIZEH * m_fillScale;
double w = m_outline.boundingRect().width();
double h = m_outline.boundingRect().height();
QRectF r = m_outline.boundingRect();
QPointF fCenter = r.center();
double nw = ceil(w / wTile);
double nh = ceil(h / hTile);
w = nw * wTile;
h = nh * hTile;

m_rect->setRect(0.,0.,w,-h);
m_rect->centerAt(fCenter);

r = m_rect->rect();
QByteArray before,after;
before.append(QString::fromStdString(SVGCOLPREFIX + SVGCOLDEFAULT));
after.append(QString::fromStdString(SVGCOLPREFIX + m_svgCol));
QByteArray colorXML = m_svgXML.replace(before,after);
QSvgRenderer renderer;
bool success = renderer.load(colorXML);
if (!success) {
Base::Console().Error("QGIF::buildPixHatch - renderer failed to load\n");
}

QImage imageIn(64, 64, QImage::Format_ARGB32);
imageIn.fill(Qt::transparent);
QPainter painter(&imageIn);

renderer.render(&painter);
if (imageIn.isNull()) {
Base::Console().Error("QGIF::buildPixHatch - imageIn is null\n");
return;
}

QPixmap pm(64, 64);
pm = QPixmap::fromImage(imageIn);
pm = pm.scaled(wTile, hTile);
if (pm.isNull()) {
Base::Console().Error("QGIF::buildPixHatch - pm is null\n");
return;
}

QImage tileField(w, h, QImage::Format_ARGB32);
QPointF fieldCenter(w / 2.0, h / 2.0);

tileField.fill(Qt::transparent);
QPainter painter2(&tileField);
QPainter::RenderHints hints = painter2.renderHints();
hints = hints & QPainter::Antialiasing;
painter2.setRenderHints(hints);
QPainterPath clipper = path();
QPointF offset = (fieldCenter - fCenter);
clipper.translate(offset);
painter2.setClipPath(clipper);

long int tileCount = 0;
for (int iw = 0; iw < int(nw); iw++) {
for (int ih = 0; ih < int(nh); ih++) {
painter2.drawPixmap(QRectF(iw*wTile, ih*hTile, wTile, hTile), //target rect
pm, //map
QRectF(0, 0, wTile, hTile)); //source rect
tileCount++;
if (tileCount > m_maxTile) {
Base::Console().Warning("Pixmap tile count exceeded: %ld\n",tileCount);
break;
}
}
if (tileCount > m_maxTile) {
break;
}
}
QPixmap bigMap(fabs(r.width()), fabs(r.height()));
bigMap = QPixmap::fromImage(tileField);

QPixmap nothing;
m_image->setPixmap(nothing);
m_image->load(bigMap);
m_image->centerAt(fCenter);
}

//this isn't used currently
QPixmap QGIFace::textureFromSvg(std::string fileSpec)
{
Expand Down
9 changes: 8 additions & 1 deletion src/Mod/TechDraw/Gui/QGIFace.h
Expand Up @@ -29,6 +29,7 @@
#include <QByteArray>
#include <QBrush>
#include <QPixmap>
#include <QImage>

#include <Mod/TechDraw/App/HatchLine.h>
#include <Mod/TechDraw/App/Geometry.h>
Expand All @@ -39,6 +40,7 @@ namespace TechDrawGui
{
class QGCustomSvg;
class QGCustomRect;
class QGCustomImage;

const double SVGSIZEW = 64.0; //width and height of standard FC SVG pattern
const double SVGSIZEH = 64.0;
Expand Down Expand Up @@ -92,7 +94,10 @@ class QGIFace : public QGIPrimPath
void buildSvgHatch(void);
void hideSvg(bool b);
void clearSvg(void);


//tiled pixmap fill from svg
void buildPixHatch();

//PAT fill parms & methods
void setGeomHatchWeight(double w) { m_geomWeight = w; }
void setLineWeight(double w);
Expand Down Expand Up @@ -128,6 +133,8 @@ class QGIFace : public QGIPrimPath
std::string m_svgCol;
std::string m_fileSpec; //for svg & bitmaps

QGCustomImage* m_image;

double m_fillScale;
bool m_isHatched;
QGIFace::fillMode m_mode;
Expand Down
25 changes: 11 additions & 14 deletions src/Mod/TechDraw/Gui/QGIViewPart.cpp
Expand Up @@ -424,7 +424,6 @@ void QGIViewPart::updateView(bool update)
}

void QGIViewPart::draw() {
// Base::Console().Message("QGIVP::draw()\n");
if (!isVisible()) {
return;
}
Expand Down Expand Up @@ -507,22 +506,20 @@ void QGIViewPart::drawViewPart()
if (!fHatch->SvgIncluded.isEmpty()) {
if (getExporting()) {
newFace->hideSvg(true);
newFace->isHatched(false);
newFace->setFillMode(QGIFace::PlainFill);
} else {
newFace->hideSvg(false);
newFace->isHatched(true);
newFace->setFillMode(QGIFace::FromFile);
newFace->setHatchFile(fHatch->SvgIncluded.getValue());
Gui::ViewProvider* gvp = QGIView::getViewProvider(fHatch);
ViewProviderHatch* hatchVp = dynamic_cast<ViewProviderHatch*>(gvp);
if (hatchVp != nullptr) {
double hatchScale = hatchVp->HatchScale.getValue();
if (hatchScale > 0.0) {
newFace->setHatchScale(hatchVp->HatchScale.getValue());
}
newFace->setHatchColor(hatchVp->HatchColor.getValue());
}
newFace->isHatched(true);
newFace->setFillMode(QGIFace::SvgFill);
newFace->setHatchFile(fHatch->SvgIncluded.getValue());
Gui::ViewProvider* gvp = QGIView::getViewProvider(fHatch);
ViewProviderHatch* hatchVp = dynamic_cast<ViewProviderHatch*>(gvp);
if (hatchVp != nullptr) {
double hatchScale = hatchVp->HatchScale.getValue();
if (hatchScale > 0.0) {
newFace->setHatchScale(hatchVp->HatchScale.getValue());
}
newFace->setHatchColor(hatchVp->HatchColor.getValue());
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/Mod/TechDraw/Gui/QGIViewSection.cpp
Expand Up @@ -115,18 +115,15 @@ void QGIViewSection::drawSectionFace()
} else if (section->CutSurfaceDisplay.isValue("SvgHatch")) {
if (getExporting()) {
newFace->hideSvg(true);
newFace->isHatched(false);
newFace->setFillMode(QGIFace::PlainFill);
} else {
newFace->hideSvg(false);
newFace->isHatched(true);
newFace->setFillMode(QGIFace::SvgFill);
newFace->setHatchColor(sectionVp->HatchColor.getValue());
newFace->setHatchScale(section->HatchScale.getValue());
// std::string hatchSpec = section->FileHatchPattern.getValue();
std::string hatchSpec = section->SvgIncluded.getValue();
newFace->setHatchFile(hatchSpec);
}
newFace->setFillMode(QGIFace::SvgFill);
newFace->setHatchColor(sectionVp->HatchColor.getValue());
newFace->setHatchScale(section->HatchScale.getValue());
// std::string hatchSpec = section->FileHatchPattern.getValue();
std::string hatchSpec = section->SvgIncluded.getValue();
newFace->setHatchFile(hatchSpec);
} else if (section->CutSurfaceDisplay.isValue("PatHatch")) {
newFace->isHatched(true);
newFace->setFillMode(QGIFace::GeomHatchFill);
Expand Down
8 changes: 6 additions & 2 deletions src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -773,18 +773,22 @@ void QGVPage::refreshViews(void)

void QGVPage::setExporting(bool enable)
{
// Base::Console().Message("QGVP::setExporting(%d)\n", enable);
QList<QGraphicsItem*> sceneItems = scene()->items();
std::vector<QGIViewPart*> dvps;
for (auto& qgi:sceneItems) {
QGIViewPart* qgiPart = dynamic_cast<QGIViewPart *>(qgi);
QGIRichAnno* qgiRTA = dynamic_cast<QGIRichAnno *>(qgi);
if(qgiPart) {
qgiPart->setExporting(enable);
dvps.push_back(qgiPart);
}
if (qgiRTA) {
qgiRTA->setExporting(enable);
}
}
for (auto& v: dvps) {
v->draw();
}
}

void QGVPage::saveSvg(QString filename)
Expand Down Expand Up @@ -848,7 +852,7 @@ void QGVPage::saveSvg(QString filename)
QPainter p;

p.begin(&svgGen);
scene()->render(&p, targetRect,sourceRect);
scene()->render(&p, targetRect,sourceRect); //note: scene render, not item render!
p.end();

m_vpPage->setFrameState(saveState);
Expand Down

0 comments on commit a9b718d

Please sign in to comment.