QElectroTech  0.70
qetshapeitem.h
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 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();
52  void XRadiusChanged();
53  void YRadiusChanged();
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 QGraphicsItem into a QetShapeItem @return the QGraphicsItem type
69  int type() const override { return Type; }
70 
72  QPen pen() const {return m_pen;}
73  void setPen(const QPen &pen);
74  QBrush brush() const {return m_brush;}
75  void setBrush(const QBrush &brush);
76  ShapeType shapeType() const {return m_shapeType;}
77 
78  virtual bool fromXml (const QDomElement &);
79  virtual QDomElement toXml (QDomDocument &document) const;
80  virtual bool toDXF (const QString &filepath,const QPen &pen);
81 
82  void editProperty() override;
83  QString name() const override;
84 
85  void setP2 (const QPointF &P2);
86  QLineF line() const{return QLineF(m_P1, m_P2);}
87  bool setLine (const QLineF &line);
88  QRectF rect() const{return QRectF(m_P1, m_P2);}
89  bool setRect (const QRectF &rect);
90  QPolygonF polygon() const {return m_polygon;}
91  bool setPolygon (const QPolygonF &polygon);
92  bool isClosed() const {return m_closed;}
93  void setClosed (bool close);
94  qreal XRadius() const {return m_xRadius;}
95  void setXRadius(qreal X);
96  qreal YRadius() const {return m_yRadius;}
97  void setYRadius(qreal Y);
98 
99  //Methods available for polygon shape
100  int pointsCount () const;
101  void setNextPoint (QPointF P);
102  void removePoints (int number = 1);
103 
104  QRectF boundingRect() const override;
105  QPainterPath shape() const override;
106 
107  protected:
108  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
109  void hoverEnterEvent (QGraphicsSceneHoverEvent *event) override;
110  void hoverLeaveEvent (QGraphicsSceneHoverEvent *event) override;
111  void mousePressEvent (QGraphicsSceneMouseEvent *event) override;
112  QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
113  bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
114  void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
115 
116  private:
117  void switchResizeMode();
118  void addHandler();
119  void adjusteHandlerPos();
120  void insertPoint();
121  void removePoint();
122 
123  void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
124  void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
125  void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
126 
128  private:
130  QPen m_pen;
131  QBrush m_brush;
132  QPointF m_P1,
133  m_P2,
134  m_old_P1,
135  m_old_P2,
138  bool m_hovered;
140  bool m_closed = false,
142  int m_resize_mode = 1;
143  QVector<QetGraphicsHandlerItem *> m_handler_vector;
144  QAction *m_insert_point,
146  qreal m_xRadius = 0,
150 };
151 #endif // QETSHAPEITEM_H
bool isClosed() const
Definition: qetshapeitem.h:92
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override
QetShapeItem::sceneEventFilter.
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
QetShapeItem::contextMenuEvent.
bool setPolygon(const QPolygonF &polygon)
QetShapeItem::setPolygon Set this item geometry to polygon (only available if shape is a polyline) ...
QLineF line() const
Definition: qetshapeitem.h:86
QPen pen() const
METHODS.
Definition: qetshapeitem.h:72
QPointF m_P2
Definition: qetshapeitem.h:132
QVector< QetGraphicsHandlerItem * > m_handler_vector
Definition: qetshapeitem.h:143
QRectF boundingRect() const override
QetShapeItem::boundingRect.
virtual bool fromXml(const QDomElement &)
QetShapeItem::fromXml Build this item from the xml description.
void removePoints(int number=1)
QetShapeItem::removePoints Number of point to remove on the polygon If is superior to number of poly...
QBrush m_brush
Definition: qetshapeitem.h:131
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
QetShapeItem::itemChange.
The QetGraphicsHandlerItem class This graphics item represents a point, destined to be used as an han...
QPolygonF polygon() const
Definition: qetshapeitem.h:90
void handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMousePressEvent.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverLeaveEvent Handle hover leave event.
void insertPoint()
qreal YRadius() const
Definition: qetshapeitem.h:96
qreal m_old_yRadius
Definition: qetshapeitem.h:147
QPointF m_P1
Definition: qetshapeitem.h:132
void setYRadius(qreal Y)
void adjusteHandlerPos()
QetShapeItem::adjusteHandlerPos Adjust the position of the handler item.
void XRadiusChanged()
QPointF m_old_P2
Definition: qetshapeitem.h:132
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...
void brushChanged()
void handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseMoveEvent.
void YRadiusChanged()
void setP2(const QPointF &P2)
QetShapeItem::setP2 Set the second point of this item. If this item is a polyline, the last point of the polyline is replaced by P2.
QAction * m_insert_point
Definition: qetshapeitem.h:144
QPainterPath shape() const override
QetShapeItem::shape.
QAction * m_remove_point
Definition: qetshapeitem.h:144
int type() const override
Definition: qetshapeitem.h:69
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
virtual QDomElement toXml(QDomDocument &document) const
QetShapeItem::toXml Save this item to xml element.
void closeChanged()
void removePoint()
QPointF m_old_P1
Definition: qetshapeitem.h:132
QRectF rect() const
Definition: qetshapeitem.h:88
int pointsCount() const
QetShapeItem::pointCount.
ShapeType m_shapeType
ATTRIBUTES.
Definition: qetshapeitem.h:129
QPolygonF m_old_polygon
Definition: qetshapeitem.h:137
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
QetShapeItem::paint Paint this item.
void editProperty() override
QetShapeItem::editProperty Edit the property of this item.
void setClosed(bool close)
QetShapeItem::setClosed Close this item, have effect only if this item is a polygon.
bool setRect(const QRectF &rect)
QetShapeItem::setRect Set this item geometry to rect (only available if shape is a rectangle or an el...
QBrush brush() const
Definition: qetshapeitem.h:74
qreal XRadius() const
Definition: qetshapeitem.h:94
bool m_modifie_radius_equaly
Definition: qetshapeitem.h:141
~QetShapeItem() override
The QetShapeItem class this class is used to draw a basic shape (line, rectangle, ellipse) into a dia...
Definition: qetshapeitem.h:35
void setPen(const QPen &pen)
QetShapeItem::setPen Set the pen to use for draw the shape.
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
QetShapeItem::hoverEnterEvent Handle hover enter event.
QPolygonF m_polygon
Definition: qetshapeitem.h:137
void handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
QetShapeItem::handlerMouseReleaseEvent.
QPointF m_context_menu_pos
Definition: qetshapeitem.h:132
bool setLine(const QLineF &line)
QetShapeItem::setLine Set item geometry to line (only available for line shape)
ShapeType shapeType() const
Definition: qetshapeitem.h:76
void setBrush(const QBrush &brush)
QetShapeItem::setBrush Set the brush to use for the fill the shape.
void penChanged()
void setXRadius(qreal X)
QString name() const override
QetShapeItem::name.
void setNextPoint(QPointF P)
QetShapeItem::setNextPoint Add a new point to the curent polygon.
virtual bool toDXF(const QString &filepath, const QPen &pen)
QetShapeItem::toDXF Draw this element to the dxf document.
qreal m_old_xRadius
Definition: qetshapeitem.h:147
void switchResizeMode()
QetShapeItem::switchResizeMode.