Hatch using svgItem
This commit is contained in:
parent
ba71e074ce
commit
731f76f52d
|
@ -103,6 +103,7 @@ MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget*
|
|||
m_exportSVGAction = new QAction(tr("&Export SVG"), this);
|
||||
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->setEnabled(false);
|
||||
m_outlineAction->setCheckable(true);
|
||||
|
|
|
@ -37,6 +37,13 @@ QGCustomSvg::QGCustomSvg()
|
|||
setAcceptHoverEvents(false);
|
||||
setFlag(QGraphicsItem::ItemIsSelectable, false);
|
||||
setFlag(QGraphicsItem::ItemIsMovable, false);
|
||||
|
||||
m_svgRender = new QSvgRenderer();
|
||||
}
|
||||
|
||||
QGCustomSvg::~QGCustomSvg()
|
||||
{
|
||||
delete m_svgRender;
|
||||
}
|
||||
|
||||
void QGCustomSvg::centerAt(QPointF centerPos)
|
||||
|
@ -59,10 +66,16 @@ void QGCustomSvg::centerAt(double cX, double cY)
|
|||
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) {
|
||||
QStyleOptionGraphicsItem myOption(*option);
|
||||
myOption.state &= ~QStyle::State_Selected;
|
||||
|
||||
QGraphicsSvgItem::paint (painter, &myOption, widget);
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
#include <QGraphicsItem>
|
||||
#include <QGraphicsSvgItem>
|
||||
#include <QPointF>
|
||||
#include <QByteArray>
|
||||
#include <QSvgRenderer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPainter;
|
||||
|
@ -39,7 +41,7 @@ class TechDrawGuiExport QGCustomSvg : public QGraphicsSvgItem
|
|||
{
|
||||
public:
|
||||
explicit QGCustomSvg(void);
|
||||
~QGCustomSvg() {}
|
||||
~QGCustomSvg();
|
||||
|
||||
enum {Type = QGraphicsItem::UserType + 131};
|
||||
int type() const { return Type;}
|
||||
|
@ -47,8 +49,10 @@ public:
|
|||
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 );
|
||||
virtual void centerAt(QPointF centerPos);
|
||||
virtual void centerAt(double cX, double cY);
|
||||
virtual bool load(QByteArray *svgString);
|
||||
|
||||
protected:
|
||||
QSvgRenderer *m_svgRender;
|
||||
|
||||
private:
|
||||
|
||||
|
@ -57,4 +61,3 @@ private:
|
|||
} // namespace MDIViewPageGui
|
||||
|
||||
#endif // DRAWINGGUI_QGCUSTOMSVG_H
|
||||
|
||||
|
|
|
@ -33,6 +33,13 @@
|
|||
#include <QStyleOptionGraphicsItem>
|
||||
#endif
|
||||
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
#include <QRectF>
|
||||
#include <QPointF>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <App/Application.h>
|
||||
#include <App/Material.h>
|
||||
#include <Base/Console.h>
|
||||
|
@ -54,6 +61,8 @@ QGIFace::QGIFace(int index) :
|
|||
setFlag(QGraphicsItem::ItemIsMovable, false);
|
||||
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, true);
|
||||
setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
|
||||
setFlag(QGraphicsItem::QGraphicsItem::ItemClipsChildrenToShape,true);
|
||||
//setFiltersChildEvents(true);
|
||||
setAcceptHoverEvents(true);
|
||||
|
||||
isHighlighted = false;
|
||||
|
@ -80,6 +89,16 @@ QGIFace::QGIFace(int index) :
|
|||
m_brushPre.setStyle(m_styleSelect);
|
||||
m_brushSel.setColor(m_colSel);
|
||||
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)
|
||||
|
@ -163,6 +182,58 @@ void QGIFace::setFill(QBrush 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() {
|
||||
m_colNormalFill = m_colDefFill;
|
||||
//m_styleCurr = m_styleDef;
|
||||
|
|
|
@ -25,6 +25,11 @@
|
|||
|
||||
#include <Qt>
|
||||
#include <QGraphicsItem>
|
||||
#include <QSvgRenderer>
|
||||
#include <QByteArray>
|
||||
|
||||
#include "QGCustomSvg.h"
|
||||
#include "QGCustomRect.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QPainter;
|
||||
|
@ -38,11 +43,14 @@ class BaseGeom;
|
|||
namespace TechDrawGui
|
||||
{
|
||||
|
||||
const double SVGSIZEW = 64.0; //width and height of standard FC SVG pattern
|
||||
const double SVGSIZEH = 64.0;
|
||||
|
||||
class QGIFace : public QGraphicsPathItem
|
||||
{
|
||||
public:
|
||||
explicit QGIFace(int index = -1);
|
||||
~QGIFace() {}
|
||||
~QGIFace();
|
||||
|
||||
enum {Type = QGraphicsItem::UserType + 104};
|
||||
int type() const { return Type;}
|
||||
|
@ -59,7 +67,10 @@ public:
|
|||
void setPrettySel();
|
||||
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);
|
||||
|
||||
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
|
||||
|
||||
|
@ -70,10 +81,14 @@ protected:
|
|||
// Selection detection
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
|
||||
virtual void mousePressEvent(QGraphicsSceneMouseEvent * event);
|
||||
bool load(QByteArray *svgBytes);
|
||||
|
||||
protected:
|
||||
int projIndex; //index of face in Projection. -1 for SectionFace.
|
||||
bool isHighlighted;
|
||||
QGCustomRect *m_rect;
|
||||
QGCustomSvg *m_svg;
|
||||
QByteArray m_qba;
|
||||
|
||||
private:
|
||||
QPen m_pen;
|
||||
|
|
|
@ -313,9 +313,7 @@ void QGIViewPart::drawViewPart()
|
|||
TechDraw::DrawHatch* fHatch = faceIsHatched(i,hatchObjs);
|
||||
if (fHatch) {
|
||||
if (!fHatch->HatchPattern.isEmpty()) {
|
||||
QBrush fBrush = brushFromFile(fHatch->HatchPattern.getValue());
|
||||
fBrush.setColor(fHatch->HatchColor.getValue().asValue<QColor>());
|
||||
newFace->setFill(fBrush);
|
||||
newFace->setHatch(fHatch->HatchPattern.getValue());
|
||||
}
|
||||
}
|
||||
newFace->setZValue(ZVALUE::FACE);
|
||||
|
@ -407,7 +405,6 @@ QGIFace* QGIViewPart::drawFace(TechDrawGeometry::Face* f, int idx)
|
|||
//faceId << "facePath " << idx;
|
||||
//_dumpPath(faceId.str().c_str(),facePath);
|
||||
|
||||
//gFace->setFlag(QGraphicsItem::ItemIsSelectable, true); ???
|
||||
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* result = nullptr;
|
||||
|
|
|
@ -85,8 +85,6 @@ protected:
|
|||
|
||||
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;
|
||||
|
||||
QColor m_colHid;
|
||||
|
|
|
@ -53,8 +53,6 @@ QGIViewSymbol::QGIViewSymbol()
|
|||
setFlag(QGraphicsItem::ItemIsMovable, true);
|
||||
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
||||
|
||||
m_svgRender = new QSvgRenderer();
|
||||
|
||||
m_svgItem = new QGCustomSvg();
|
||||
addToGroup(m_svgItem);
|
||||
m_svgItem->setPos(0.,0.);
|
||||
|
@ -63,7 +61,6 @@ QGIViewSymbol::QGIViewSymbol()
|
|||
QGIViewSymbol::~QGIViewSymbol()
|
||||
{
|
||||
// m_svgItem belongs to this group and will be deleted by Qt
|
||||
delete(m_svgRender);
|
||||
}
|
||||
|
||||
QVariant QGIViewSymbol::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
|
@ -131,7 +128,7 @@ void QGIViewSymbol::symbolToSvg(QString qs)
|
|||
|
||||
QByteArray qba;
|
||||
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());
|
||||
}
|
||||
m_svgItem->setPos(0.,0.);
|
||||
|
@ -141,11 +138,3 @@ QRectF QGIViewSymbol::boundingRect() const
|
|||
{
|
||||
return childrenBoundingRect();
|
||||
}
|
||||
|
||||
bool QGIViewSymbol::load(QByteArray *svgBytes)
|
||||
{
|
||||
bool success = m_svgRender->load(*svgBytes);
|
||||
m_svgItem->setSharedRenderer(m_svgRender);
|
||||
return(success);
|
||||
}
|
||||
|
||||
|
|
|
@ -57,15 +57,12 @@ public:
|
|||
virtual QRectF boundingRect() const override;
|
||||
|
||||
protected:
|
||||
bool load(QByteArray *svgString);
|
||||
virtual void drawSvg();
|
||||
void symbolToSvg(QString qs);
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
|
||||
|
||||
QGCustomSvg *m_svgItem;
|
||||
QSvgRenderer *m_svgRender;
|
||||
};
|
||||
|
||||
} // namespace MDIViewPageGui
|
||||
|
||||
} // namespace
|
||||
#endif // DRAWINGGUI_QGRAPHICSITEMVIEWSYMBOL_H
|
||||
|
|
|
@ -123,6 +123,7 @@ QGVPage::~QGVPage()
|
|||
|
||||
void QGVPage::drawBackground(QPainter *p, const QRectF &)
|
||||
{
|
||||
//Note: Background is not part of scene()
|
||||
if(!drawBkg)
|
||||
return;
|
||||
|
||||
|
|
|
@ -302,7 +302,7 @@ void ViewProviderPage::onSelectionChanged(const Gui::SelectionChanges& msg)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} //else (Gui::SelectionChanges::SetPreselect)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user