diff --git a/src/Mod/TechDraw/App/DrawViewSection.cpp b/src/Mod/TechDraw/App/DrawViewSection.cpp index 55dbd10ce0..205fe01688 100644 --- a/src/Mod/TechDraw/App/DrawViewSection.cpp +++ b/src/Mod/TechDraw/App/DrawViewSection.cpp @@ -115,6 +115,7 @@ DrawViewSection::DrawViewSection() ADD_PROPERTY_TYPE(HatchCutSurface ,(false),fgroup,App::Prop_None,"Hatch the cut surface"); ADD_PROPERTY_TYPE(HatchPattern ,(""),fgroup,App::Prop_None,"The hatch pattern file for the cut surface"); ADD_PROPERTY_TYPE(HatchColor,(0.0,0.0,0.0),fgroup,App::Prop_None,"The color of the hatch pattern"); + ADD_PROPERTY_TYPE(HatchScale,(1.0),fgroup,App::Prop_None,"Hatch pattern size adjustment"); getParameters(); diff --git a/src/Mod/TechDraw/App/DrawViewSection.h b/src/Mod/TechDraw/App/DrawViewSection.h index 5096ef1d08..5f3f05fdde 100644 --- a/src/Mod/TechDraw/App/DrawViewSection.h +++ b/src/Mod/TechDraw/App/DrawViewSection.h @@ -67,6 +67,7 @@ public: App::PropertyBool HatchCutSurface; App::PropertyFile HatchPattern; App::PropertyColor HatchColor; + App::PropertyFloat HatchScale; App::PropertyString SectionSymbol; virtual short mustExecute() const; diff --git a/src/Mod/TechDraw/Gui/QGIFace.cpp b/src/Mod/TechDraw/Gui/QGIFace.cpp index 65801adcfe..63a6c205d8 100644 --- a/src/Mod/TechDraw/Gui/QGIFace.cpp +++ b/src/Mod/TechDraw/Gui/QGIFace.cpp @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include #endif #include @@ -45,6 +48,7 @@ #include #include +#include "Rez.h" #include "QGCustomSvg.h" #include "QGCustomRect.h" #include "QGIFace.h" @@ -57,15 +61,18 @@ QGIFace::QGIFace(int index) : m_styleDef(Qt::SolidPattern), m_styleSelect(Qt::SolidPattern) { + m_isHatched = false; + m_mode = 0; setFlag(QGraphicsItem::ItemClipsChildrenToShape,true); - //setFiltersChildEvents(true); - //setStyle(Qt::NoPen); //don't draw face lines, just fill + //setStyle(Qt::NoPen); //don't draw face lines, just fill for debugging setStyle(Qt::DashLine); m_styleNormal = m_styleDef; + m_fillStyle = m_styleDef; m_colNormalFill = m_colDefFill; setPrettyNormal(); + m_texture = QPixmap(); m_svg = new QGCustomSvg(); @@ -81,8 +88,38 @@ QGIFace::~QGIFace() //nothing to do. every item is a child of QGIFace & will get removed/deleted when QGIF is deleted } +void QGIFace::draw() +{ + if (isHatched()) { + QFileInfo hfi(QString::fromUtf8(m_fileSpec.data(),m_fileSpec.size())); + if (hfi.isReadable()) { + QString ext = hfi.suffix(); + if (ext.toUpper() == QString::fromUtf8("SVG")) { + m_mode = 1; + loadSvgHatch(m_fileSpec); + buildSvgHatch(); + toggleSvg(true); + } else if ((ext.toUpper() == QString::fromUtf8("JPG")) || + (ext.toUpper() == QString::fromUtf8("PNG")) || + (ext.toUpper() == QString::fromUtf8("JPEG")) || + (ext.toUpper() == QString::fromUtf8("BMP")) ) { + m_mode = 2; + toggleSvg(false); + m_texture = textureFromBitmap(m_fileSpec); + } + } + } + show(); +} + void QGIFace::setPrettyNormal() { - m_fillStyle = m_styleNormal; + if (isHatched() && + (m_mode == 2) ) { //hatch with bitmap fill + m_fillStyle = Qt::TexturePattern; + m_brush.setTexture(m_texture); + } else { + m_fillStyle = m_styleNormal; + } m_fillColor = m_colNormalFill; QGIPrimPath::setPrettyNormal(); } @@ -99,16 +136,6 @@ void QGIFace::setPrettySel() { QGIPrimPath::setPrettySel(); } -void QGIFace::setFill(QColor c, Qt::BrushStyle s) { - m_colNormalFill = c; - m_styleNormal = s; -} - -void QGIFace::setFill(QBrush b) { - m_colNormalFill = b.color(); - m_styleNormal = b.style(); -} - void QGIFace::setDrawEdges(bool b) { if (b) { setStyle(Qt::DashLine); @@ -117,14 +144,14 @@ void QGIFace::setDrawEdges(bool b) { } } -void QGIFace::resetFill() { - m_colNormalFill = m_colDefFill; - m_styleNormal = m_styleDef; -} - -void QGIFace::setHatch(std::string fileSpec) +void QGIFace::setHatchFile(std::string fileSpec) { - QString qfs(QString::fromStdString(fileSpec)); + m_fileSpec = fileSpec; +} + +void QGIFace::loadSvgHatch(std::string fileSpec) +{ + QString qfs(QString::fromUtf8(fileSpec.data(),fileSpec.size())); QFile f(qfs); if (!f.open(QFile::ReadOnly | QFile::Text)) { Base::Console().Error("QGIFace could not read %s\n",fileSpec.c_str()); @@ -135,21 +162,18 @@ void QGIFace::setHatch(std::string fileSpec) Base::Console().Error("Error - Could not load hatch into SVG renderer for %s\n", fileSpec.c_str()); return; } - - buildHatch(); } void QGIFace::setPath(const QPainterPath & path) { QGraphicsPathItem::setPath(path); - if (!m_svgXML.isEmpty()) { - buildHatch(); + if ((m_mode == 1) && !m_svgXML.isEmpty()) { // svg hatch mode and have svg hatch info loded + buildSvgHatch(); } } -void QGIFace::buildHatch() +void QGIFace::buildSvgHatch() { - m_styleNormal = Qt::NoBrush; double wTile = SVGSIZEW * m_svgScale; double hTile = SVGSIZEH * m_svgScale; double w = boundingRect().width(); @@ -177,7 +201,28 @@ void QGIFace::buildHatch() } } } +} +void QGIFace::clearSvg() +{ + toggleSvg(false); +} + +//this isn't used currently +QPixmap QGIFace::textureFromSvg(std::string fileSpec) +{ + QPixmap result; + QString qs(QString::fromStdString(fileSpec)); + QFileInfo ffi(qs); + if (ffi.isReadable()) { + QSvgRenderer renderer(qs); + QPixmap pixMap(renderer.defaultSize()); + pixMap.fill(Qt::white); //try Qt::transparent? + QPainter painter(&pixMap); + renderer.render(&painter); //svg texture -> bitmap + result = pixMap.scaled(m_svgScale,m_svgScale); + } //else return empty pixmap + return result; } //c is a CSS color ie "#000000" @@ -203,6 +248,35 @@ void QGIFace::toggleSvg(bool b) update(); } +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_svgScale),Rez::guiX(m_svgScale)); + pix = QPixmap::fromImage(img); + } + return pix; +} + +void QGIFace::setFill(QColor c, Qt::BrushStyle s) { + m_colNormalFill = c; + m_styleNormal = s; +} + +void QGIFace::setFill(QBrush b) { + m_colNormalFill = b.color(); + m_styleNormal = b.style(); +} + +void QGIFace::resetFill() { + m_colNormalFill = m_colDefFill; + m_styleNormal = m_styleDef; +} + + QRectF QGIFace::boundingRect() const { return shape().controlPointRect(); diff --git a/src/Mod/TechDraw/Gui/QGIFace.h b/src/Mod/TechDraw/Gui/QGIFace.h index 9ee2593371..e28fb4a753 100644 --- a/src/Mod/TechDraw/Gui/QGIFace.h +++ b/src/Mod/TechDraw/Gui/QGIFace.h @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include "QGIPrimPath.h" @@ -55,22 +57,35 @@ public: public: int getProjIndex() const { return projIndex; } + void draw(); void setPrettyNormal(); void setPrettyPre(); void setPrettySel(); + void setPath(const QPainterPath & path); + void setDrawEdges(bool b); + void setFill(QColor c, Qt::BrushStyle s); void setFill(QBrush b); - void setHatch(std::string fileSpec); - void resetFill(void); - void setPath(const QPainterPath & path); - void buildHatch(void); + void resetFill(); + + void setHatchFile(std::string fileSpec); void setHatchColor(std::string c); void setHatchScale(double s); - void setDrawEdges(bool b); + + void loadSvgHatch(std::string fileSpec); + void buildSvgHatch(void); void toggleSvg(bool b); + void clearSvg(void); + + QPixmap textureFromBitmap(std::string fileSpec); + QPixmap textureFromSvg(std::string fillSpec); + + void isHatched(bool s) {m_isHatched = s; } + bool isHatched(void) {return m_isHatched;} + protected: - bool load(QByteArray *svgBytes); +// bool load(QByteArray *svgBytes); protected: int projIndex; //index of face in Projection. -1 for SectionFace. @@ -79,6 +94,9 @@ protected: QByteArray m_svgXML; std::string m_svgCol; double m_svgScale; + std::string m_fileSpec; + bool m_isHatched; + int m_mode; private: QBrush m_brush; @@ -90,6 +108,7 @@ private: Qt::BrushStyle m_styleDef; //default Normal fill style Qt::BrushStyle m_styleNormal; //current Normal fill style Qt::BrushStyle m_styleSelect; //Select/preSelect fill style + QPixmap m_texture; // }; } diff --git a/src/Mod/TechDraw/Gui/QGIViewPart.cpp b/src/Mod/TechDraw/Gui/QGIViewPart.cpp index 22af898bc9..b02301fa5c 100644 --- a/src/Mod/TechDraw/Gui/QGIViewPart.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewPart.cpp @@ -377,14 +377,16 @@ void QGIViewPart::drawViewPart() TechDraw::DrawHatch* fHatch = faceIsHatched(i,hatchObjs); if (fHatch) { if (!fHatch->HatchPattern.isEmpty()) { + newFace->setHatchFile(fHatch->HatchPattern.getValue()); App::Color hColor = fHatch->HatchColor.getValue(); newFace->setHatchColor(hColor.asCSSString()); newFace->setHatchScale(fHatch->HatchScale.getValue()); - newFace->setHatch(fHatch->HatchPattern.getValue()); + newFace->isHatched(true); } } newFace->setDrawEdges(false); newFace->setZValue(ZVALUE::FACE); + newFace->draw(); newFace->setPrettyNormal(); } } diff --git a/src/Mod/TechDraw/Gui/QGIViewSection.cpp b/src/Mod/TechDraw/Gui/QGIViewSection.cpp index 32b20de4fe..dcb52620be 100644 --- a/src/Mod/TechDraw/Gui/QGIViewSection.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewSection.cpp @@ -86,9 +86,12 @@ void QGIViewSection::drawSectionFace() if (section->HatchCutSurface.getValue()) { App::Color hColor = section->HatchColor.getValue(); newFace->setHatchColor(hColor.asCSSString()); - newFace->setHatch(section->HatchPattern.getValue()); + newFace->setHatchFile(section->HatchPattern.getValue()); + newFace->setHatchScale(section->HatchScale.getValue()); + newFace->isHatched(true); } newFace->setFill(faceColor, Qt::SolidPattern); + newFace->draw(); newFace->setPrettyNormal(); newFace->setAcceptHoverEvents(false); newFace->setFlag(QGraphicsItem::ItemIsSelectable, false); diff --git a/src/Mod/TechDraw/Gui/Rez.cpp b/src/Mod/TechDraw/Gui/Rez.cpp index fb6d875e6c..e4a1b59058 100644 --- a/src/Mod/TechDraw/Gui/Rez.cpp +++ b/src/Mod/TechDraw/Gui/Rez.cpp @@ -25,15 +25,30 @@ #endif +#include +#include +#include + #include "Rez.h" using namespace TechDrawGui; +//*** initial static var outside methods! +double Rez::m_rezFactor = Rez::getParameter(); +//*** + + double Rez::getRezFactor() { - return 10.0; // 1/10 mm + return Rez::m_rezFactor; } +void Rez::setRezFactor(double f) +{ + Rez::m_rezFactor = f; +} + + //turn App side value to Gui side value double Rez::guiX(double x) { @@ -86,3 +101,11 @@ QSize Rez::appSize(QSize s) return result; } +double Rez::getParameter() +{ + Base::Reference hGrp = App::GetApplication().GetUserParameter() + .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Rez"); + double rezFactor = hGrp->GetFloat("Resolution", 12.0); + return rezFactor; +} + diff --git a/src/Mod/TechDraw/Gui/Rez.h b/src/Mod/TechDraw/Gui/Rez.h index 09b3ed7fde..3409a33954 100644 --- a/src/Mod/TechDraw/Gui/Rez.h +++ b/src/Mod/TechDraw/Gui/Rez.h @@ -36,7 +36,9 @@ namespace TechDrawGui class TechDrawGuiExport Rez { public: - static double getRezFactor(); + static double getParameter(void); + static double getRezFactor(void); + static void setRezFactor(double f); //turn App side value to Gui side value static double guiX(double x); static Base::Vector2d guiX(Base::Vector2d v); @@ -48,6 +50,9 @@ public: static QRectF guiRect(QRectF r); static QSize guiSize(QSize s); static QSize appSize(QSize s); + +private: + static double m_rezFactor; }; } //end namespace TechDrawGui