Skip to content

Commit

Permalink
[TD]fix bitmap hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Jul 3, 2022
1 parent 96cb9dc commit ac4fa39
Show file tree
Hide file tree
Showing 14 changed files with 391 additions and 261 deletions.
65 changes: 53 additions & 12 deletions src/Mod/TechDraw/App/DrawHatch.cpp
Expand Up @@ -43,9 +43,9 @@
#include <Base/Parameter.h>
#include <Base/UnitsApi.h>

#include "Preferences.h"
#include "DrawViewPart.h"
#include "DrawUtil.h"
#include "Preferences.h"
#include "DrawHatch.h"

#include <Mod/TechDraw/App/DrawHatchPy.h> // generated from DrawHatchPy.xml
Expand All @@ -65,8 +65,7 @@ DrawHatch::DrawHatch(void)
ADD_PROPERTY_TYPE(HatchPattern, (prefSvgHatch()), vgroup, App::Prop_None, "The hatch pattern file for this area");
ADD_PROPERTY_TYPE(SvgIncluded, (""), vgroup,App::Prop_None,
"Embedded SVG hatch file. System use only."); // n/a to end users

std::string svgFilter("SVG files (*.svg *.SVG);;All files (*)");
std::string svgFilter("SVG files (*.svg *.SVG);;Bitmap files(*.jpg *.jpeg *.png *.bmp);;All files (*)");
HatchPattern.setFilter(svgFilter);
}

Expand All @@ -84,13 +83,27 @@ void DrawHatch::onChanged(const App::Property* prop)
if ((prop == &HatchPattern) &&
(doc != nullptr) ) {
if (!HatchPattern.isEmpty()) {
replaceSvgIncluded(HatchPattern.getValue());
replaceFileIncluded(HatchPattern.getValue());
}
}
}
App::DocumentObject::onChanged(prop);
}

short DrawHatch::mustExecute() const
{
short result = 0;
if (!isRestoring()) {
result = (Source.isTouched() ||
HatchPattern.isTouched());
}

if (result) {
return result;
}
return App::DocumentObject::mustExecute();
}

App::DocumentObjectExecReturn *DrawHatch::execute(void)
{
DrawViewPart* parent = getSourceView();
Expand Down Expand Up @@ -187,11 +200,11 @@ bool DrawHatch::empty(void)
return sourceNames.empty();
}

void DrawHatch::replaceSvgIncluded(std::string newSvgFile)
void DrawHatch::replaceFileIncluded(std::string newSvgFile)
{
// Base::Console().Message("DH::replaceSvgHatch(%s)\n", newSvgFile.c_str());
if (SvgIncluded.isEmpty()) {
setupSvgIncluded();
setupFileIncluded();
} else {
std::string tempName = SvgIncluded.getExchangeTempFile();
DrawUtil::copyFile(newSvgFile, tempName);
Expand All @@ -211,28 +224,29 @@ void DrawHatch::onDocumentRestored()
Base::FileInfo tfi(svgFileName);
if (tfi.isReadable()) {
if (SvgIncluded.isEmpty()) {
setupSvgIncluded();
setupFileIncluded();
}
}
}
}

App::DocumentObject::onDocumentRestored();
}

void DrawHatch::setupObject()
{
//by this point DH should have a name and belong to a document
setupSvgIncluded();
setupFileIncluded();

App::DocumentObject::setupObject();
}

void DrawHatch::setupSvgIncluded(void)
void DrawHatch::setupFileIncluded(void)
{
// Base::Console().Message("DH::setupSvgIncluded()\n");
// Base::Console().Message("DH::setupFileIncluded()\n");
App::Document* doc = getDocument();
std::string special = getNameInDocument();
special += "SvgHatch.svg";
special += "Hatch.fill";
std::string dir = doc->TransientDir.getValue();
std::string svgName = dir + special;

Expand All @@ -259,6 +273,34 @@ void DrawHatch::unsetupObject(void)
App::DocumentObject::unsetupObject();
}

bool DrawHatch::isSvgHatch(void) const
{
bool result = false;
Base::FileInfo fi(HatchPattern.getValue());
if ((fi.extension() == "svg") ||
(fi.extension() == "SVG")) {
result = true;
}
return result;
}

bool DrawHatch::isBitmapHatch(void) const
{
bool result = false;
Base::FileInfo fi(HatchPattern.getValue());
if ((fi.extension() == "bmp") ||
(fi.extension() == "BMP") ||
(fi.extension() == "png") ||
(fi.extension() == "PNG") ||
(fi.extension() == "jpg") ||
(fi.extension() == "JPG") ||
(fi.extension() == "jpeg") ||
(fi.extension() == "JPEG") ) {
result = true;
}
return result;
}

//standard preference getters
std::string DrawHatch::prefSvgHatch(void)
{
Expand All @@ -274,7 +316,6 @@ App::Color DrawHatch::prefSvgHatchColor(void)
return fcColor;
}


// Python Drawing feature ---------------------------------------------------------

namespace App {
Expand Down
13 changes: 11 additions & 2 deletions src/Mod/TechDraw/App/DrawHatch.h
Expand Up @@ -23,11 +23,16 @@
#ifndef _TechDraw_DrawHatch_h_
#define _TechDraw_DrawHatch_h_

#include <Mod/TechDraw/TechDrawGlobal.h>

#include <App/DocumentObject.h>
#include <App/FeaturePython.h>
#include <App/PropertyFile.h>
#include <App/PropertyLinks.h>

namespace App {
class Color;
}

namespace TechDraw
{
Expand All @@ -46,6 +51,7 @@ class TechDrawExport DrawHatch : public App::DocumentObject
App::PropertyFileIncluded SvgIncluded;

virtual App::DocumentObjectExecReturn *execute(void) override;
virtual short mustExecute() const override;

virtual const char* getViewProviderName(void) const override {
return "TechDrawGui::ViewProviderHatch";
Expand All @@ -64,12 +70,15 @@ class TechDrawExport DrawHatch : public App::DocumentObject
static std::string prefSvgHatch(void);
static App::Color prefSvgHatchColor(void);

bool isSvgHatch(void) const;
bool isBitmapHatch(void) const;

protected:
void onChanged(const App::Property* prop) override;
virtual void onDocumentRestored() override;
virtual void setupObject() override;
void setupSvgIncluded(void);
void replaceSvgIncluded(std::string newSvgFile);
void setupFileIncluded(void);
void replaceFileIncluded(std::string newSvgFile);

private:

Expand Down
17 changes: 17 additions & 0 deletions src/Mod/TechDraw/App/Preferences.cpp
Expand Up @@ -302,3 +302,20 @@ std::string Preferences::patFile()
}
return result;
}

std::string Preferences::bitmapFill(void)
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Files");

std::string defaultDir = App::Application::getResourceDir() + "Mod/TechDraw/Patterns/";
std::string defaultFileName = defaultDir + "default.png";
std::string prefBitmapFile = hGrp->GetASCII("BitmapFill", defaultFileName.c_str());
std::string result = prefBitmapFile;
Base::FileInfo fi(result);
if (!fi.isReadable()) {
result = defaultFileName;
Base::Console().Warning("Bitmap Fill File: %s is not readable\n", prefBitmapFile.c_str());
}
return result;
}
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/App/Preferences.h
Expand Up @@ -73,7 +73,7 @@ static int mattingStyle();

static std::string svgFile();
static std::string patFile();

static std::string bitmapFill(void);
};

} //end namespace TechDraw
Expand Down
29 changes: 3 additions & 26 deletions src/Mod/TechDraw/Gui/CommandDecorate.cpp
Expand Up @@ -132,8 +132,8 @@ void CmdTechDrawHatch::activated(int iMsg)
}
}

openCommand(QT_TRANSLATE_NOOP("Command", "Create Hatch"));
if (removeOld) {
openCommand(QT_TRANSLATE_NOOP("Command", "Remove old Hatch"));
std::vector<std::pair< int, TechDraw::DrawHatch*> > toRemove;
for (auto& h: hatchObjs) { //all the hatch objects for selected DVP
std::vector<std::string> hatchSubs = h->Source.getSubValues();
Expand All @@ -154,33 +154,11 @@ void CmdTechDrawHatch::activated(int iMsg)
doCommand(Doc,"App.activeDocument().removeObject('%s')",r.second->getNameInDocument());
}
}
commitCommand();
}

std::string FeatName = getUniqueObjectName("Hatch");
std::stringstream featLabel;
featLabel << FeatName << "F" <<
TechDraw::DrawUtil::getIndexFromName(subNames.at(0)); //use 1st face# for label

doCommand(Doc,"App.activeDocument().addObject('TechDraw::DrawHatch','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Label = '%s'",FeatName.c_str(),featLabel.str().c_str());

auto hatch( static_cast<TechDraw::DrawHatch *>(getDocument()->getObject(FeatName.c_str())) );
hatch->Source.setValue(partFeat, subNames);

Gui::ViewProvider* vp = Gui::Application::Instance->getDocument(getDocument())->getViewProvider(hatch);
TechDrawGui::ViewProviderHatch* hvp = dynamic_cast<TechDrawGui::ViewProviderHatch*>(vp);
if (!hvp) {
Base::Console().Log("ERROR - CommandDecorate - Hatch has no ViewProvider\n");
return;
}

//should this be: doCommand(Doc,"App..Feat..Source = [(App...%s,%s),(App..%s,%s),...]",objs[0]->getNameInDocument(),subs[0],...);
//seems very unwieldy

// dialog to fill in hatch values
Gui::Control().showDialog(new TaskDlgHatch(hatch, hvp, true));

commitCommand();
Gui::Control().showDialog(new TaskDlgHatch(partFeat, subNames));

//Horrible hack to force Tree update ??still required??
//WF: yes. ViewProvider will not claim children without this!
Expand All @@ -189,7 +167,6 @@ void CmdTechDrawHatch::activated(int iMsg)
getDocument()->recompute();
}


bool CmdTechDrawHatch::isActive(void)
{
bool havePage = DrawGuiUtil::needPage(this);
Expand Down
71 changes: 31 additions & 40 deletions src/Mod/TechDraw/Gui/QGIFace.cpp
Expand Up @@ -136,41 +136,28 @@ void QGIFace::draw()
}
m_image->hide();
m_rect->hide();
} else if ((m_mode == FromFile) ||
(m_mode == SvgFill) ||
(m_mode == BitmapFill)) {
QFileInfo hfi(QString::fromUtf8(m_fileSpec.data(),m_fileSpec.size()));
if (hfi.isReadable()) {
QString ext = hfi.suffix();
if (ext.toUpper() == QString::fromUtf8("SVG")) {
setFillMode(SvgFill);
m_brush.setTexture(QPixmap());
m_styleNormal = m_styleDef;
m_fillStyleCurrent = m_styleNormal;
loadSvgHatch(m_fileSpec);
if (m_hideSvgTiles) {
//bitmap hatch doesn't need clipping
setFlag(QGraphicsItem::ItemClipsChildrenToShape,false);
buildPixHatch();
m_rect->hide();
m_image->show();
} else {
//SVG tiles need to be clipped
setFlag(QGraphicsItem::ItemClipsChildrenToShape,true);
buildSvgHatch();
m_image->hide();
m_rect->show();
}
} else if ((ext.toUpper() == QString::fromUtf8("JPG")) ||
(ext.toUpper() == QString::fromUtf8("PNG")) ||
(ext.toUpper() == QString::fromUtf8("JPEG")) ||
(ext.toUpper() == QString::fromUtf8("BMP")) ) {
setFillMode(BitmapFill);
m_fillStyleCurrent = Qt::TexturePattern;
m_texture = textureFromBitmap(m_fileSpec);
m_brush.setTexture(m_texture);
}
} else if (m_mode == SvgFill) {
m_brush.setTexture(QPixmap());
m_styleNormal = m_styleDef;
m_fillStyleCurrent = m_styleNormal;
loadSvgHatch(m_fileSpec);
if (m_hideSvgTiles) {
//bitmap hatch doesn't need clipping
setFlag(QGraphicsItem::ItemClipsChildrenToShape,false);
buildPixHatch();
m_rect->hide();
m_image->show();
} else {
//SVG tiles need to be clipped
setFlag(QGraphicsItem::ItemClipsChildrenToShape,true);
buildSvgHatch();
m_image->hide();
m_rect->show();
}
} else if (m_mode == BitmapFill) {
m_fillStyleCurrent = Qt::TexturePattern;
m_texture = textureFromBitmap(m_fileSpec);
m_brush.setTexture(m_texture);
} else if (m_mode == PlainFill) {
setFill(m_colNormalFill, m_styleNormal);
m_image->hide();
Expand All @@ -197,12 +184,14 @@ void QGIFace::setPrettyNormal() {

void QGIFace::setPrettyPre() {
// Base::Console().Message("QGIF::setPrettyPre()\n");
m_fillStyleCurrent = Qt::SolidPattern;
m_brush.setTexture(QPixmap());
QGIPrimPath::setPrettyPre();
}

void QGIFace::setPrettySel() {
// Base::Console().Message("QGIF::setPrettySel()\n");
m_fillStyleCurrent = Qt::SolidPattern;
m_brush.setTexture(QPixmap());
QGIPrimPath::setPrettySel();
}
Expand Down Expand Up @@ -699,13 +688,15 @@ void QGIFace::hideSvg(bool b)
QPixmap QGIFace::textureFromBitmap(std::string fileSpec)
{
QPixmap pix;
QString qs = QString::fromUtf8(fileSpec.data(),fileSpec.size());
QFileInfo ffi(qs);
if (ffi.isReadable()) {
QImage img = QImage(qs);
img = img.scaled(Rez::guiX(m_fillScale),Rez::guiX(m_fillScale));
pix = QPixmap::fromImage(img);

QString qfs(QString::fromUtf8(fileSpec.data(),fileSpec.size()));
QFile f(qfs);
if (!f.open(QFile::ReadOnly)) {
Base::Console().Error("QGIFace could not read %s\n",fileSpec.c_str());
return pix;
}
QByteArray bytes = f.readAll();
pix.loadFromData(bytes);
return pix;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Mod/TechDraw/Gui/QGIFace.h
Expand Up @@ -23,11 +23,16 @@
#ifndef DRAWINGGUI_QGRAPHICSITEMFACE_H
#define DRAWINGGUI_QGRAPHICSITEMFACE_H

#include <QByteArray>
#include <Qt>
#include <QGraphicsItem>
#include <QSvgRenderer>
#include <QByteArray>
#include <QBrush>
#include <QPixmap>
#include <QImage>

#include <Mod/TechDraw/App/HatchLine.h>
#include <Mod/TechDraw/App/Geometry.h>

#include "QGIPrimPath.h"

Expand Down

0 comments on commit ac4fa39

Please sign in to comment.