Hatch using svgItem

This commit is contained in:
WandererFan 2016-06-13 19:36:52 -04:00 committed by wmayer
parent ba71e074ce
commit 731f76f52d
11 changed files with 112 additions and 40 deletions

View File

@ -103,6 +103,7 @@ MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget*
m_exportSVGAction = new QAction(tr("&Export SVG"), this); m_exportSVGAction = new QAction(tr("&Export SVG"), this);
connect(m_exportSVGAction, SIGNAL(triggered()), this, SLOT(saveSVG())); connect(m_exportSVGAction, SIGNAL(triggered()), this, SLOT(saveSVG()));
//m_outlineAction is obs? never set to Enabled?
m_outlineAction = new QAction(tr("&Outline"), this); m_outlineAction = new QAction(tr("&Outline"), this);
m_outlineAction->setEnabled(false); m_outlineAction->setEnabled(false);
m_outlineAction->setCheckable(true); m_outlineAction->setCheckable(true);

View File

@ -37,6 +37,13 @@ QGCustomSvg::QGCustomSvg()
setAcceptHoverEvents(false); setAcceptHoverEvents(false);
setFlag(QGraphicsItem::ItemIsSelectable, false); setFlag(QGraphicsItem::ItemIsSelectable, false);
setFlag(QGraphicsItem::ItemIsMovable, false); setFlag(QGraphicsItem::ItemIsMovable, false);
m_svgRender = new QSvgRenderer();
}
QGCustomSvg::~QGCustomSvg()
{
delete m_svgRender;
} }
void QGCustomSvg::centerAt(QPointF centerPos) void QGCustomSvg::centerAt(QPointF centerPos)
@ -59,10 +66,16 @@ void QGCustomSvg::centerAt(double cX, double cY)
setPos(newX,newY); setPos(newX,newY);
} }
bool QGCustomSvg::load(QByteArray *svgBytes)
{
bool success = m_svgRender->load(*svgBytes);
setSharedRenderer(m_svgRender);
return(success);
}
void QGCustomSvg::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { void QGCustomSvg::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option); QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected; myOption.state &= ~QStyle::State_Selected;
QGraphicsSvgItem::paint (painter, &myOption, widget); QGraphicsSvgItem::paint (painter, &myOption, widget);
} }

View File

@ -26,6 +26,8 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QGraphicsSvgItem> #include <QGraphicsSvgItem>
#include <QPointF> #include <QPointF>
#include <QByteArray>
#include <QSvgRenderer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPainter; class QPainter;
@ -39,7 +41,7 @@ class TechDrawGuiExport QGCustomSvg : public QGraphicsSvgItem
{ {
public: public:
explicit QGCustomSvg(void); explicit QGCustomSvg(void);
~QGCustomSvg() {} ~QGCustomSvg();
enum {Type = QGraphicsItem::UserType + 131}; enum {Type = QGraphicsItem::UserType + 131};
int type() const { return Type;} int type() const { return Type;}
@ -47,8 +49,10 @@ public:
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ); virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
virtual void centerAt(QPointF centerPos); virtual void centerAt(QPointF centerPos);
virtual void centerAt(double cX, double cY); virtual void centerAt(double cX, double cY);
virtual bool load(QByteArray *svgString);
protected: protected:
QSvgRenderer *m_svgRender;
private: private:
@ -57,4 +61,3 @@ private:
} // namespace MDIViewPageGui } // namespace MDIViewPageGui
#endif // DRAWINGGUI_QGCUSTOMSVG_H #endif // DRAWINGGUI_QGCUSTOMSVG_H

View File

@ -33,6 +33,13 @@
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#endif #endif
#include <QFile>
#include <QTextStream>
#include <QRectF>
#include <QPointF>
#include <cmath>
#include <App/Application.h> #include <App/Application.h>
#include <App/Material.h> #include <App/Material.h>
#include <Base/Console.h> #include <Base/Console.h>
@ -54,6 +61,8 @@ QGIFace::QGIFace(int index) :
setFlag(QGraphicsItem::ItemIsMovable, false); setFlag(QGraphicsItem::ItemIsMovable, false);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true); setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
setFlag(QGraphicsItem::ItemSendsGeometryChanges,true); setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
setFlag(QGraphicsItem::QGraphicsItem::ItemClipsChildrenToShape,true);
//setFiltersChildEvents(true);
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
isHighlighted = false; isHighlighted = false;
@ -80,6 +89,16 @@ QGIFace::QGIFace(int index) :
m_brushPre.setStyle(m_styleSelect); m_brushPre.setStyle(m_styleSelect);
m_brushSel.setColor(m_colSel); m_brushSel.setColor(m_colSel);
m_brushSel.setStyle(m_styleSelect); m_brushSel.setStyle(m_styleSelect);
m_svg = new QGCustomSvg();
m_rect = new QGCustomRect();
m_rect->setParentItem(this);
}
QGIFace::~QGIFace()
{
//nothing to do. every item is a child of QGIFace & will get removed/deleted when QGIF is deleted
} }
QVariant QGIFace::itemChange(GraphicsItemChange change, const QVariant &value) QVariant QGIFace::itemChange(GraphicsItemChange change, const QVariant &value)
@ -163,6 +182,58 @@ void QGIFace::setFill(QBrush b) {
m_brushNormal = b; m_brushNormal = b;
} }
void QGIFace::setHatch(std::string fileSpec)
{
QString qfs(QString::fromStdString(fileSpec));
QFile f(qfs);
if (!f.open(QFile::ReadOnly | QFile::Text)) {
Base::Console().Error("QGIFace could not read %s\n",fileSpec.c_str());
return;
}
m_qba = f.readAll();
if (!m_svg->load(&m_qba)) {
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_qba.isEmpty()) {
buildHatch();
}
}
void QGIFace::buildHatch()
{
m_brushNormal.setStyle(Qt::NoBrush );
double w = boundingRect().width();
double h = boundingRect().height();
QRectF r = boundingRect();
QPointF fCenter = r.center();
double nw = ceil(w / SVGSIZEW);
double nh = ceil(h / SVGSIZEH);
w = nw * SVGSIZEW;
h = nh * SVGSIZEW;
m_rect->setRect(0.,0.,w,-h);
m_rect->centerAt(fCenter);
QPointF rPos = m_rect->pos();
r = m_rect->rect();
for (int iw = 0; iw < int(nw); iw++) {
for (int ih = 0; ih < int(nh); ih++) {
QGCustomSvg* tile = new QGCustomSvg();
if (tile->load(&m_qba)) {
tile->setParentItem(m_rect);
tile->setPos(iw*SVGSIZEW,-h + ih*SVGSIZEH);
}
}
}
}
void QGIFace::resetFill() { void QGIFace::resetFill() {
m_colNormalFill = m_colDefFill; m_colNormalFill = m_colDefFill;
//m_styleCurr = m_styleDef; //m_styleCurr = m_styleDef;

View File

@ -25,6 +25,11 @@
#include <Qt> #include <Qt>
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QSvgRenderer>
#include <QByteArray>
#include "QGCustomSvg.h"
#include "QGCustomRect.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPainter; class QPainter;
@ -38,11 +43,14 @@ class BaseGeom;
namespace TechDrawGui namespace TechDrawGui
{ {
const double SVGSIZEW = 64.0; //width and height of standard FC SVG pattern
const double SVGSIZEH = 64.0;
class QGIFace : public QGraphicsPathItem class QGIFace : public QGraphicsPathItem
{ {
public: public:
explicit QGIFace(int index = -1); explicit QGIFace(int index = -1);
~QGIFace() {} ~QGIFace();
enum {Type = QGraphicsItem::UserType + 104}; enum {Type = QGraphicsItem::UserType + 104};
int type() const { return Type;} int type() const { return Type;}
@ -59,7 +67,10 @@ public:
void setPrettySel(); void setPrettySel();
void setFill(QColor c, Qt::BrushStyle s); void setFill(QColor c, Qt::BrushStyle s);
void setFill(QBrush b); void setFill(QBrush b);
void setHatch(std::string fileSpec);
void resetFill(void); void resetFill(void);
void setPath(const QPainterPath & path);
void buildHatch(void);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event); virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
@ -70,10 +81,14 @@ protected:
// Selection detection // Selection detection
QVariant itemChange(GraphicsItemChange change, const QVariant &value); QVariant itemChange(GraphicsItemChange change, const QVariant &value);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * event); virtual void mousePressEvent(QGraphicsSceneMouseEvent * event);
bool load(QByteArray *svgBytes);
protected: protected:
int projIndex; //index of face in Projection. -1 for SectionFace. int projIndex; //index of face in Projection. -1 for SectionFace.
bool isHighlighted; bool isHighlighted;
QGCustomRect *m_rect;
QGCustomSvg *m_svg;
QByteArray m_qba;
private: private:
QPen m_pen; QPen m_pen;

View File

@ -313,9 +313,7 @@ void QGIViewPart::drawViewPart()
TechDraw::DrawHatch* fHatch = faceIsHatched(i,hatchObjs); TechDraw::DrawHatch* fHatch = faceIsHatched(i,hatchObjs);
if (fHatch) { if (fHatch) {
if (!fHatch->HatchPattern.isEmpty()) { if (!fHatch->HatchPattern.isEmpty()) {
QBrush fBrush = brushFromFile(fHatch->HatchPattern.getValue()); newFace->setHatch(fHatch->HatchPattern.getValue());
fBrush.setColor(fHatch->HatchColor.getValue().asValue<QColor>());
newFace->setFill(fBrush);
} }
} }
newFace->setZValue(ZVALUE::FACE); newFace->setZValue(ZVALUE::FACE);
@ -407,7 +405,6 @@ QGIFace* QGIViewPart::drawFace(TechDrawGeometry::Face* f, int idx)
//faceId << "facePath " << idx; //faceId << "facePath " << idx;
//_dumpPath(faceId.str().c_str(),facePath); //_dumpPath(faceId.str().c_str(),facePath);
//gFace->setFlag(QGraphicsItem::ItemIsSelectable, true); ???
return gFace; return gFace;
} }
@ -565,19 +562,6 @@ void QGIViewPart::toggleVertices(bool state)
} }
} }
QBrush QGIViewPart::brushFromFile(std::string fillSpec)
{
QBrush result;
QString qs(QString::fromStdString(fillSpec));
QSvgRenderer renderer(qs);
QBitmap pixMap(renderer.defaultSize());
pixMap.fill(Qt::white); //try Qt::transparent?
QPainter painter(&pixMap);
renderer.render(&painter); //svg texture -> bitmap
result.setTexture(pixMap);
return result;
}
TechDraw::DrawHatch* QGIViewPart::faceIsHatched(int i,std::vector<TechDraw::DrawHatch*> hatchObjs) const TechDraw::DrawHatch* QGIViewPart::faceIsHatched(int i,std::vector<TechDraw::DrawHatch*> hatchObjs) const
{ {
TechDraw::DrawHatch* result = nullptr; TechDraw::DrawHatch* result = nullptr;

View File

@ -85,8 +85,6 @@ protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
//hatching routines
QBrush brushFromFile(std::string fillSpec);
TechDraw::DrawHatch* faceIsHatched(int i,std::vector<TechDraw::DrawHatch*> hatchObjs) const; TechDraw::DrawHatch* faceIsHatched(int i,std::vector<TechDraw::DrawHatch*> hatchObjs) const;
QColor m_colHid; QColor m_colHid;

View File

@ -53,8 +53,6 @@ QGIViewSymbol::QGIViewSymbol()
setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsSelectable, true);
m_svgRender = new QSvgRenderer();
m_svgItem = new QGCustomSvg(); m_svgItem = new QGCustomSvg();
addToGroup(m_svgItem); addToGroup(m_svgItem);
m_svgItem->setPos(0.,0.); m_svgItem->setPos(0.,0.);
@ -63,7 +61,6 @@ QGIViewSymbol::QGIViewSymbol()
QGIViewSymbol::~QGIViewSymbol() QGIViewSymbol::~QGIViewSymbol()
{ {
// m_svgItem belongs to this group and will be deleted by Qt // m_svgItem belongs to this group and will be deleted by Qt
delete(m_svgRender);
} }
QVariant QGIViewSymbol::itemChange(GraphicsItemChange change, const QVariant &value) QVariant QGIViewSymbol::itemChange(GraphicsItemChange change, const QVariant &value)
@ -131,7 +128,7 @@ void QGIViewSymbol::symbolToSvg(QString qs)
QByteArray qba; QByteArray qba;
qba.append(qs); qba.append(qs);
if (!load(&qba)) { if (!m_svgItem->load(&qba)) {
Base::Console().Error("Error - Could not load Symbol into SVG renderer for %s\n", getViewObject()->getNameInDocument()); Base::Console().Error("Error - Could not load Symbol into SVG renderer for %s\n", getViewObject()->getNameInDocument());
} }
m_svgItem->setPos(0.,0.); m_svgItem->setPos(0.,0.);
@ -141,11 +138,3 @@ QRectF QGIViewSymbol::boundingRect() const
{ {
return childrenBoundingRect(); return childrenBoundingRect();
} }
bool QGIViewSymbol::load(QByteArray *svgBytes)
{
bool success = m_svgRender->load(*svgBytes);
m_svgItem->setSharedRenderer(m_svgRender);
return(success);
}

View File

@ -57,15 +57,12 @@ public:
virtual QRectF boundingRect() const override; virtual QRectF boundingRect() const override;
protected: protected:
bool load(QByteArray *svgString);
virtual void drawSvg(); virtual void drawSvg();
void symbolToSvg(QString qs); void symbolToSvg(QString qs);
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
QGCustomSvg *m_svgItem; QGCustomSvg *m_svgItem;
QSvgRenderer *m_svgRender;
}; };
} // namespace MDIViewPageGui } // namespace
#endif // DRAWINGGUI_QGRAPHICSITEMVIEWSYMBOL_H #endif // DRAWINGGUI_QGRAPHICSITEMVIEWSYMBOL_H

View File

@ -123,6 +123,7 @@ QGVPage::~QGVPage()
void QGVPage::drawBackground(QPainter *p, const QRectF &) void QGVPage::drawBackground(QPainter *p, const QRectF &)
{ {
//Note: Background is not part of scene()
if(!drawBkg) if(!drawBkg)
return; return;

View File

@ -302,7 +302,7 @@ void ViewProviderPage::onSelectionChanged(const Gui::SelectionChanges& msg)
} }
} }
} }
} } //else (Gui::SelectionChanges::SetPreselect)
} }
} }