QElectroTech  0.8-dev
qetshapeitem.h
Go to the documentation of this file.
1 /*
2  Copyright 2006-2020 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef QETSHAPEITEM_H
19 #define QETSHAPEITEM_H
20 
21 #include <QPen>
22 #include "qetgraphicsitem.h"
24 
25 class QDomElement;
26 class QDomDocument;
28 class QAction;
29 
36 {
37  Q_OBJECT
38 
39  Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
40  Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
41  Q_PROPERTY(QRectF rect READ rect WRITE setRect)
42  Q_PROPERTY(QLineF line READ line WRITE setLine)
43  Q_PROPERTY(QPolygonF polygon READ polygon WRITE setPolygon)
44  Q_PROPERTY(bool close READ isClosed WRITE setClosed NOTIFY closeChanged)
45  Q_PROPERTY(qreal xRadius READ XRadius WRITE setXRadius NOTIFY XRadiusChanged)
46  Q_PROPERTY(qreal yRadius READ YRadius WRITE setYRadius NOTIFY YRadiusChanged)
47 
48  signals:
49  void penChanged();
50  void brushChanged();
51  void closeChanged();
54 
55 
56  public:
57  enum ShapeType {Line =1,
59  Ellipse =4,
60  Polygon =8 };
61  Q_ENUM (ShapeType)
62 
63  enum { Type = UserType + 1008 };
64 
65  QetShapeItem(QPointF, QPointF = QPointF(0,0), ShapeType = Line, QGraphicsItem *parent = nullptr);
66  ~QetShapeItem() override;
67 
68  //Enable the use of qgraphicsitem_cast to safely cast a
69  //QGraphicsItem into a QetShapeItem return the QGraphicsItem type
70  int type() const override { return Type; }
71 
73  QPen pen() const {return m_pen;}
74  void setPen(const QPen &pen);
75  QBrush brush() const {return m_brush;}
76  void setBrush(const QBrush &brush);
77  ShapeType shapeType() const {return m_shapeType;}
78 
79  virtual bool fromXml (const QDomElement &);
80  virtual QDomElement toXml (QDomDocument &document) const;
81  virtual bool toDXF (const QString &filepath,const QPen &pen);
82 
83  void editProperty() override;
84  QString name() const override;
85 
86  void setP2 (const QPointF &P2);
87  QLineF line() const{return QLineF(m_P1, m_P2);}
88  bool setLine (const QLineF &line);
89  QRectF rect() const{return QRectF(m_P1, m_P2);}
90  bool setRect (const QRectF &rect);
91  QPolygonF polygon() const {return m_polygon;}
92  bool setPolygon (const QPolygonF &polygon);
93  bool isClosed() const {return m_closed;}
94  void setClosed (bool close);
95  qreal XRadius() const {return m_xRadius;}
96  void setXRadius(qreal X);
97  qreal YRadius() const {return m_yRadius;}
98  void setYRadius(qreal Y);
99 
100  //Methods available for polygon shape
101  int pointsCount () const;
102  void setNextPoint (QPointF P);
103  void removePoints (int number = 1);
104 
105  QRectF boundingRect() const override;
106  QPainterPath shape() const override;
107 
108  protected:
109  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
110  void hoverEnterEvent (QGraphicsSceneHoverEvent *event) override;
111  void hoverLeaveEvent (QGraphicsSceneHoverEvent *event) override;
112  void mousePressEvent (QGraphicsSceneMouseEvent *event) override;
113  QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
114  bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
115  void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
116 
117  private:
118  void switchResizeMode();
119  void addHandler();
120  void adjusteHandlerPos();
121  void insertPoint();
122  void removePoint();
123 
124  void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
125  void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
126  void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
127 
129  private:
131  QPen m_pen;
132  QBrush m_brush;
133  QPointF m_P1,
139  bool m_hovered;
141  bool m_closed = false,
143  int m_resize_mode = 1;
144  QVector<QetGraphicsHandlerItem *> m_handler_vector;
145  QAction *m_insert_point,
147  qreal m_xRadius = 0,
151 };
152 #endif // QETSHAPEITEM_H
qetgraphicshandlerutility.h
QetShapeItem::removePoints
void removePoints(int number=1)
QetShapeItem::removePoints Number of point to remove on the polygon If number is superior to number o...
Definition: qetshapeitem.cpp:228
QetShapeItem::editProperty
void editProperty() override
QetShapeItem::editProperty Edit the property of this item.
Definition: qetshapeitem.cpp:1004
QetShapeItem::Rectangle
@ Rectangle
Definition: qetshapeitem.h:58
QetShapeItem::setBrush
void setBrush(const QBrush &brush)
QetShapeItem::setBrush Set the brush to use for the fill the shape.
Definition: qetshapeitem.cpp:89
QetGraphicsHandlerItem::Type
@ Type
Definition: qetgraphicshandleritem.h:42
QetShapeItem::hoverEnterEvent
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverEnterEvent Handle hover enter event.
Definition: qetshapeitem.cpp:341
QetShapeItem::m_vector_index
int m_vector_index
Definition: qetshapeitem.h:140
QetShapeItem::shapeType
ShapeType shapeType() const
Definition: qetshapeitem.h:77
QetShapeItem::m_resize_mode
int m_resize_mode
Definition: qetshapeitem.h:143
QetShapeItem::m_shapeType
ShapeType m_shapeType
ATTRIBUTES.
Definition: qetshapeitem.h:130
QetGraphicsItem
Definition: qetgraphicsitem.h:27
QetShapeItem::penChanged
void penChanged()
qetgraphicsitem.h
propertieseditordialog.h
QetShapeItem::addHandler
void addHandler()
Definition: qetshapeitem.cpp:561
QetShapeItem::sceneEventFilter
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override
QetShapeItem::sceneEventFilter.
Definition: qetshapeitem.cpp:414
QetShapeItem::m_xRadius
qreal m_xRadius
Definition: qetshapeitem.h:147
QetShapeItem::setYRadius
void setYRadius(qreal Y)
Definition: qetshapeitem.cpp:194
QetGraphicsItem::diagram
Diagram * diagram() const
Definition: qetgraphicsitem.cpp:40
QetShapeItem::m_handler_vector
QVector< QetGraphicsHandlerItem * > m_handler_vector
Definition: qetshapeitem.h:144
QETXML::brushFromXml
QBrush brushFromXml(const QDomElement &element)
QETXML::brushFromXml Build a QBrush from a xml description.
Definition: qetxml.cpp:135
QetGraphicsHandlerUtility::pointForRadiusRect
static QVector< QPointF > pointForRadiusRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::pointForRadiusRect.
Definition: qetgraphicshandlerutility.cpp:276
QetShapeItem::hoverLeaveEvent
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverLeaveEvent Handle hover leave event.
Definition: qetshapeitem.cpp:352
QetShapeItem::close
bool close
Definition: qetshapeitem.h:44
QetShapeItem::fromXml
virtual bool fromXml(const QDomElement &)
QetShapeItem::fromXml Build this item from the xml description.
Definition: qetshapeitem.cpp:849
QetShapeItem::Type
@ Type
Definition: qetshapeitem.h:63
QetShapeItem::setNextPoint
void setNextPoint(QPointF P)
QetShapeItem::setNextPoint Add a new point to the curent polygon.
Definition: qetshapeitem.cpp:215
QetShapeItem::m_closed
bool m_closed
Definition: qetshapeitem.h:141
QetShapeItem::shape
QPainterPath shape() const override
QetShapeItem::shape.
Definition: qetshapeitem.cpp:257
QetShapeItem::isClosed
bool isClosed() const
Definition: qetshapeitem.h:93
shapegraphicsitempropertieswidget.h
QETXML::brushToXml
QDomElement brushToXml(QDomDocument &parent_document, const QBrush &brush)
QETXML::brushToXml Write attribute of a QBrush in xml element.
Definition: qetxml.cpp:97
QetGraphicsHandlerUtility::radiusForPosAtIndex
static qreal radiusForPosAtIndex(const QRectF &rect, const QPointF &pos, int index, Qt::SizeMode mode=Qt::AbsoluteSize)
QetGraphicsHandlerUtility::radiusForPosAtIndex.
Definition: qetgraphicshandlerutility.cpp:316
QetShapeItem::ShapeType
ShapeType
Definition: qetshapeitem.h:57
QetShapeItem::brushChanged
void brushChanged()
QetShapeItem::XRadiusChanged
void XRadiusChanged()
QetShapeItem::m_P1
QPointF m_P1
Definition: qetshapeitem.h:133
QETXML::penToXml
QDomElement penToXml(QDomDocument &parent_document, const QPen &pen)
QETXML::penToXml Write attribute of a QPen in xml element.
Definition: qetxml.cpp:33
QetShapeItem::yRadius
qreal yRadius
Definition: qetshapeitem.h:46
QetShapeItem::setPen
void setPen(const QPen &pen)
QetShapeItem::setPen Set the pen to use for draw the shape.
Definition: qetshapeitem.cpp:76
QET::findInDomElement
QList< QDomElement > findInDomElement(const QDomElement &, const QString &)
Definition: qet.cpp:333
QetShapeItem::m_old_xRadius
qreal m_old_xRadius
Definition: qetshapeitem.h:149
QetShapeItem::boundingRect
QRectF boundingRect() const override
QetShapeItem::boundingRect.
Definition: qetshapeitem.cpp:249
ShapeGraphicsItemPropertiesWidget
The ShapeGraphicsItemPropertiesWidget class Provide a widget to edit the properties of a QetShapeItem...
Definition: shapegraphicsitempropertieswidget.h:34
QetShapeItem::toXml
virtual QDomElement toXml(QDomDocument &document) const
QetShapeItem::toXml Save this item to xml element.
Definition: qetshapeitem.cpp:906
QetShapeItem::polygon
QPolygonF polygon
Definition: qetshapeitem.h:43
QetShapeItem::brush
QBrush brush
Definition: qetshapeitem.h:40
QetShapeItem::polygon
QPolygonF polygon() const
Definition: qetshapeitem.h:91
QetShapeItem::toDXF
virtual bool toDXF(const QString &filepath, const QPen &pen)
QetShapeItem::toDXF Draw this element to the dxf document.
Definition: qetshapeitem.cpp:967
QetShapeItem::~QetShapeItem
~QetShapeItem() override
Definition: qetshapeitem.cpp:65
QetShapeItem::m_remove_point
QAction * m_remove_point
Definition: qetshapeitem.h:146
QetGraphicsHandlerUtility::mirrorRectForPosAtIndex
static QRectF mirrorRectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::mirrorRectForPosAtIndex Return a rectangle after modification of the point...
Definition: qetgraphicshandlerutility.cpp:139
QetShapeItem::Ellipse
@ Ellipse
Definition: qetshapeitem.h:59
QetShapeItem::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Definition: qetshapeitem.cpp:358
QetShapeItem::pen
QPen pen() const
METHODS.
Definition: qetshapeitem.h:73
QetShapeItem::m_brush
QBrush m_brush
Definition: qetshapeitem.h:132
QetShapeItem::setRect
bool setRect(const QRectF &rect)
QetShapeItem::setRect Set this item geometry to rect (only available if shape is a rectangle or an el...
Definition: qetshapeitem.cpp:140
QetShapeItem::name
QString name() const override
QetShapeItem::name.
Definition: qetshapeitem.cpp:1016
qeticons.h
qet.h
QetShapeItem::m_modifie_radius_equaly
bool m_modifie_radius_equaly
Definition: qetshapeitem.h:142
QetShapeItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
QetShapeItem::paint Paint this item.
Definition: qetshapeitem.cpp:298
diagramview.h
QetGraphicsHandlerUtility::pointsForRect
static QVector< QPointF > pointsForRect(const QRectF &rect)
QetGraphicsHandlerUtility::pointsForRect Return the keys points of the rectangle, stored in a vector....
Definition: qetgraphicshandlerutility.cpp:36
QetShapeItem::rect
QRectF rect() const
Definition: qetshapeitem.h:89
QetShapeItem::m_context_menu_pos
QPointF m_context_menu_pos
Definition: qetshapeitem.h:137
Createdxf::getcolorCode
static int getcolorCode(const long red, const long green, const long blue)
Createdxf::getcolorCode This function returns the ACI color which is the "nearest" color to the color...
Definition: createdxf.cpp:389
QetShapeItem::XRadius
qreal XRadius() const
Definition: qetshapeitem.h:95
Createdxf::drawLine
static void drawLine(const QString &filapath, double, double, double, double, const int &clorcode)
Definition: createdxf.cpp:290
QetShapeItem::adjusteHandlerPos
void adjusteHandlerPos()
QetShapeItem::adjusteHandlerPos Adjust the position of the handler item.
Definition: qetshapeitem.cpp:606
QetGraphicsItem::is_movable_
bool is_movable_
Definition: qetgraphicsitem.h:56
QET::Icons::de
QIcon de
Definition: qeticons.cpp:188
QetShapeItem::Polygon
@ Polygon
Definition: qetshapeitem.h:60
qpropertyundocommand.h
QetShapeItem::contextMenuEvent
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
QetShapeItem::contextMenuEvent.
Definition: qetshapeitem.cpp:452
QetShapeItem::line
QLineF line() const
Definition: qetshapeitem.h:87
QetShapeItem::setP2
void setP2(const QPointF &P2)
QetShapeItem::setP2 Set the second point of this item. If this item is a polyline,...
Definition: qetshapeitem.cpp:104
QetShapeItem::YRadius
qreal YRadius() const
Definition: qetshapeitem.h:97
QetGraphicsHandlerUtility::rectForPosAtIndex
static QRectF rectForPosAtIndex(const QRectF &old_rect, const QPointF &pos, int index)
QetGraphicsHandlerUtility::rectForPosAtIndex Return a rectangle after modification of the point 'pos'...
Definition: qetgraphicshandlerutility.cpp:106
qetgraphicshandleritem.h
QetShapeItem::m_old_P1
QPointF m_old_P1
Definition: qetshapeitem.h:135
Diagram::undoStack
QUndoStack & undoStack()
Diagram::undoStack.
Definition: diagram.h:397
QetShapeItem::rect
QRectF rect
Definition: qetshapeitem.h:41
QetGraphicsItem::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Definition: qetgraphicsitem.cpp:79
QetGraphicsHandlerItem
The QetGraphicsHandlerItem class This graphics item represents a point, destined to be used as an han...
Definition: qetgraphicshandleritem.h:37
Createdxf::drawRectangle
static void drawRectangle(const QString &filepath, double, double, double, double, const int &colorcode)
Definition: createdxf.cpp:565
QPropertyUndoCommand::setAnimated
void setAnimated(bool animate=true, bool first_time=true)
QPropertyUndoCommand::setAnimated.
Definition: qpropertyundocommand.cpp:103
QetShapeItem::m_pen
QPen m_pen
Definition: qetshapeitem.h:131
QetShapeItem::brush
QBrush brush() const
Definition: qetshapeitem.h:75
QetShapeItem::QetShapeItem
QetShapeItem(QPointF, QPointF=QPointF(0, 0), ShapeType=Line, QGraphicsItem *parent=nullptr)
QetShapeItem::QetShapeItem Constructor of shape item. point 1 and 2 must be in scene coordinate.
Definition: qetshapeitem.cpp:38
QetShapeItem::m_polygon
QPolygonF m_polygon
Definition: qetshapeitem.h:138
QetShapeItem::m_yRadius
qreal m_yRadius
Definition: qetshapeitem.h:148
QETXML::penFromXml
QPen penFromXml(const QDomElement &element)
QETXML::penFromXml Build a QPen from a xml description.
Definition: qetxml.cpp:62
QetShapeItem::removePoint
void removePoint()
Definition: qetshapeitem.cpp:668
QetShapeItem::setPolygon
bool setPolygon(const QPolygonF &polygon)
QetShapeItem::setPolygon Set this item geometry to polygon (only available if shape is a polyline)
Definition: qetshapeitem.cpp:160
QetShapeItem::m_old_polygon
QPolygonF m_old_polygon
Definition: qetshapeitem.h:138
Createdxf::drawEllipse
static void drawEllipse(const QString &filepath, const QRectF &rect, const int &colorcode)
Createdxf::drawEllipse Conveniance function for draw ellipse.
Definition: createdxf.cpp:556
QetShapeItem::switchResizeMode
void switchResizeMode()
QetShapeItem::switchResizeMode.
Definition: qetshapeitem.cpp:510
QetShapeItem::m_insert_point
QAction * m_insert_point
Definition: qetshapeitem.h:145
QetShapeItem::m_hovered
bool m_hovered
Definition: qetshapeitem.h:139
QetShapeItem::m_P2
QPointF m_P2
Definition: qetshapeitem.h:134
QetShapeItem::setLine
bool setLine(const QLineF &line)
QetShapeItem::setLine Set item geometry to line (only available for line shape)
Definition: qetshapeitem.cpp:124
QetShapeItem::pointsCount
int pointsCount() const
QetShapeItem::pointCount.
Definition: qetshapeitem.cpp:206
QetShapeItem::handlerMousePressEvent
void handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMousePressEvent.
Definition: qetshapeitem.cpp:706
QetShapeItem::insertPoint
void insertPoint()
Definition: qetshapeitem.cpp:652
qetxml.h
QetShapeItem::Line
@ Line
Definition: qetshapeitem.h:57
QetShapeItem::xRadius
qreal xRadius
Definition: qetshapeitem.h:45
PropertiesEditorDialog
The PropertiesEditorDialog class Create a dialog to edit some properties of a thing....
Definition: propertieseditordialog.h:42
QetShapeItem::closeChanged
void closeChanged()
Diagram::snapToGrid
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:2240
QetShapeItem
The QetShapeItem class this class is used to draw a basic shape (line, rectangle, ellipse) into a dia...
Definition: qetshapeitem.h:36
QetShapeItem::handlerMouseMoveEvent
void handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseMoveEvent.
Definition: qetshapeitem.cpp:726
QetShapeItem::m_old_yRadius
qreal m_old_yRadius
Definition: qetshapeitem.h:150
QetShapeItem::m_old_P2
QPointF m_old_P2
Definition: qetshapeitem.h:136
qetshapeitem.h
DiagramView
Definition: diagramview.h:38
QetShapeItem::itemChange
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
QetShapeItem::itemChange.
Definition: qetshapeitem.cpp:375
createdxf.h
QetGraphicsHandlerUtility::polygonForInsertPoint
static QPolygonF polygonForInsertPoint(const QPolygonF &old_polygon, bool closed, const QPointF &pos)
QetGraphicsHandlerUtility::polygonForInsertPoint.
Definition: qetgraphicshandlerutility.cpp:213
QetShapeItem::type
int type() const override
Definition: qetshapeitem.h:70
QPropertyUndoCommand
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
Definition: qpropertyundocommand.h:34
QetShapeItem::setClosed
void setClosed(bool close)
QetShapeItem::setClosed Close this item, have effect only if this item is a polygon.
Definition: qetshapeitem.cpp:176
QetShapeItem::pen
QPen pen
Definition: qetshapeitem.h:39
QetGraphicsHandlerItem::handlerForPoint
static QVector< QetGraphicsHandlerItem * > handlerForPoint(const QVector< QPointF > &points, int size=10)
QetGraphicsHandlerItem::handlerForPoint.
Definition: qetgraphicshandleritem.cpp:87
QET::Icons::Add
QIcon Add
Definition: qeticons.cpp:24
QetShapeItem::line
QLineF line
Definition: qetshapeitem.h:42
QetShapeItem::handlerMouseReleaseEvent
void handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseReleaseEvent.
Definition: qetshapeitem.cpp:790
QET::Icons::tr
QIcon tr
Definition: qeticons.cpp:206
DiagramView::contextMenuActions
QList< QAction * > contextMenuActions() const
DiagramView::contextMenuActions.
Definition: diagramview.cpp:1152
QetShapeItem::setXRadius
void setXRadius(qreal X)
Definition: qetshapeitem.cpp:186
QetShapeItem::YRadiusChanged
void YRadiusChanged()
diagram.h
QET::Icons::Remove
QIcon Remove
Definition: qeticons.cpp:155