Seamly2D
Code documentation
vabstractspline.h
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2017 Seamly, LLC *
4  * *
5  * https://github.com/fashionfreedom/seamly2d *
6  * *
7  ***************************************************************************
8  **
9  ** Seamly2D is free software: you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation, either version 3 of the License, or
12  ** (at your option) any later version.
13  **
14  ** Seamly2D is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** You should have received a copy of the GNU General Public License
20  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
21  **
22  **************************************************************************
23 
24  ************************************************************************
25  **
26  ** @file vabstractspline.h
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 4 3, 2014
29  **
30  ** @brief
31  ** @copyright
32  ** This source code is part of the Valentine project, a pattern making
33  ** program, whose allow create and modeling patterns of clothing.
34  ** Copyright (C) 2013-2015 Seamly2D project
35  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
36  **
37  ** Seamly2D is free software: you can redistribute it and/or modify
38  ** it under the terms of the GNU General Public License as published by
39  ** the Free Software Foundation, either version 3 of the License, or
40  ** (at your option) any later version.
41  **
42  ** Seamly2D is distributed in the hope that it will be useful,
43  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
44  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45  ** GNU General Public License for more details.
46  **
47  ** You should have received a copy of the GNU General Public License
48  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
49  **
50  *************************************************************************/
51 
52 #ifndef VABSTRACTSPLINE_H
53 #define VABSTRACTSPLINE_H
54 
55 #include <qcompilerdetection.h>
56 #include <QDomElement>
57 #include <QGraphicsItem>
58 #include <QGraphicsPathItem>
59 #include <QMetaObject>
60 #include <QObject>
61 #include <QPainterPath>
62 #include <QPointF>
63 #include <QString>
64 #include <QVariant>
65 #include <QVector>
66 #include <QtGlobal>
67 
68 #include "../../../visualization/line/visline.h"
69 #include "../vdrawtool.h"
70 #include "../vgeometry/vabstractcurve.h"
71 #include "../vgeometry/vgeometrydef.h"
72 #include "../vmisc/vabstractapplication.h"
73 #include "../vmisc/def.h"
74 #include "../vwidgets/vmaingraphicsscene.h"
75 #include "../vwidgets/vmaingraphicsview.h"
76 
78 template <class T> class QSharedPointer;
79 
80 class VAbstractSpline:public VDrawTool, public QGraphicsPathItem
81 {
82  Q_OBJECT
83 public:
85  QGraphicsItem * parent = nullptr);
86  virtual ~VAbstractSpline() Q_DECL_EQ_DEFAULT;
87 
88  virtual QPainterPath shape() const Q_DECL_OVERRIDE;
89  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE;
90  virtual int type() const Q_DECL_OVERRIDE {return Type;}
91 
92  enum { Type = UserType + static_cast<int>(Tool::AbstractSpline)};
93 
94  virtual QString getTagName() const Q_DECL_OVERRIDE;
95  void ShowHandles(bool show);
96 
97  QString getLineColor() const;
98  void setLineColor(const QString &value);
99 
100  QString GetPenStyle() const;
101  void SetPenStyle(const QString &value);
102 
103  QString getLineWeight() const;
104  void setLineWeight(const QString &value);
105 
106  QString name() const;
107 
108  virtual void GroupVisibility(quint32 object, bool visible) Q_DECL_OVERRIDE;
109 
110 public slots:
111  virtual void FullUpdateFromFile () Q_DECL_OVERRIDE;
112  virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE;
113  virtual void piecesMode(bool mode) Q_DECL_OVERRIDE;
114  virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE;
115  virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE;
116  virtual QString makeToolTip() const Q_DECL_OVERRIDE;
117 
118 signals:
119  /**
120  * @brief setEnabledPoint disable control points.
121  * @param enable enable or diasable points.
122  */
123  void setEnabledPoint(bool enable);
124 
125 protected:
126  /**
127  * @brief controlPoints list pointers of control points.
128  */
130 
134 
135  /**
136  * @brief RefreshGeometry refresh item on scene.
137  */
138  virtual void RefreshGeometry();
139 
140  virtual void ShowTool(quint32 id, bool enable) Q_DECL_OVERRIDE;
141  virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event ) Q_DECL_OVERRIDE;
142  virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event ) Q_DECL_OVERRIDE;
143  virtual QVariant itemChange ( GraphicsItemChange change, const QVariant &value ) Q_DECL_OVERRIDE;
144  virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE;
145  virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE;
146  virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) Q_DECL_OVERRIDE;
147  virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE;
148  virtual void SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj) Q_DECL_OVERRIDE;
149  virtual void refreshCtrlPoints();
150  virtual void contextMenuEvent (QGraphicsSceneContextMenuEvent * event ) Q_DECL_OVERRIDE;
151 
152  VSpline CorrectedSpline(const VSpline &spline, const SplinePointPosition &position,
153  const QPointF &pos) const;
154 
155  template <typename T>
156  void ShowToolVisualization(bool show);
157 
158  template <typename T>
159  static void InitSplineToolConnections(VMainGraphicsScene *scene, T *tool);
160 
161  template <typename T>
162  static void InitSplinePathToolConnections(VMainGraphicsScene *scene, T *tool);
163 
164  template <typename T>
165  static void InitArcToolConnections(VMainGraphicsScene *scene, T *tool);
166 
167  template <typename T>
168  static void InitElArcToolConnections(VMainGraphicsScene *scene, T *tool);
169 
170 private:
171  Q_DISABLE_COPY(VAbstractSpline)
172 
173  void InitDefShape();
174 };
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 template <typename T>
178 inline void VAbstractSpline::ShowToolVisualization(bool show)
179 {
180  if (show)
181  {
182  if (vis.isNull())
183  {
184  AddVisualization<T>();
186  }
187  else
188  {
189  if (T *visual = qobject_cast<T *>(vis))
190  {
191  visual->show();
192  }
193  }
194  }
195  else
196  {
197  delete vis;
198  }
199 
200  if (m_piecesMode)
201  {
203  }
204  else
205  {
206  ShowHandles(show);
207  }
208 
209  if (QGraphicsScene *sc = scene())
210  { // Showing/hiding control points require recalculation scene size.
211  VMainGraphicsView::NewSceneRect(sc, qApp->getSceneView(), this);
212  }
213 }
214 
215 //---------------------------------------------------------------------------------------------------------------------
216 template <typename T>
218 {
219  SCASSERT(scene != nullptr)
220  SCASSERT(tool != nullptr)
221 
222  InitDrawToolConnections(scene, tool);
223  QObject::connect(scene, &VMainGraphicsScene::EnableSplineItemHover, tool, &T::AllowHover);
224  QObject::connect(scene, &VMainGraphicsScene::EnableSplineItemSelection, tool, &T::AllowSelecting);
225 }
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 template <typename T>
230 {
231  SCASSERT(scene != nullptr)
232  SCASSERT(tool != nullptr)
233 
234  InitDrawToolConnections(scene, tool);
235  QObject::connect(scene, &VMainGraphicsScene::EnableSplinePathItemHover, tool, &T::AllowHover);
236  QObject::connect(scene, &VMainGraphicsScene::EnableSplinePathItemSelection, tool, &T::AllowSelecting);
237 }
238 
239 //---------------------------------------------------------------------------------------------------------------------
240 template <typename T>
242 {
243  SCASSERT(scene != nullptr)
244  SCASSERT(tool != nullptr)
245 
246  InitDrawToolConnections(scene, tool);
247  QObject::connect(scene, &VMainGraphicsScene::EnableArcItemHover, tool, &T::AllowHover);
248  QObject::connect(scene, &VMainGraphicsScene::EnableArcItemSelection, tool, &T::AllowSelecting);
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
252 template <typename T>
254 {
255  SCASSERT(scene != nullptr)
256  SCASSERT(tool != nullptr)
257 
258  InitDrawToolConnections(scene, tool);
259  QObject::connect(scene, &VMainGraphicsScene::EnableElArcItemHover, tool, &T::AllowHover);
260  QObject::connect(scene, &VMainGraphicsScene::EnableElArcItemSelection, tool, &T::AllowSelecting);
261 }
262 
263 #endif // VABSTRACTSPLINE_H
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
static void InitElArcToolConnections(VMainGraphicsScene *scene, T *tool)
virtual QPainterPath shape() const Q_DECL_OVERRIDE
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
mouseReleaseEvent handle mouse release events.
static void InitSplinePathToolConnections(VMainGraphicsScene *scene, T *tool)
void setLineColor(const QString &value)
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void SetPenStyle(const QString &value)
virtual void ShowTool(quint32 id, bool enable) Q_DECL_OVERRIDE
ShowTool highlight tool.
virtual void piecesMode(bool mode) Q_DECL_OVERRIDE
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
void setEnabledPoint(bool enable)
setEnabledPoint disable control points.
virtual void RefreshGeometry()
RefreshGeometry refresh item on scene.
virtual void refreshCtrlPoints()
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
virtual ~VAbstractSpline() Q_DECL_EQ_DEFAULT
virtual QString getTagName() const Q_DECL_OVERRIDE
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
hoverEnterEvent handle hover enter events.
QString name() const
QString getLineColor() const
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
void setLineWeight(const QString &value)
setLineWeight set line weight of the spline
SceneObject sceneType
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
FullUpdateFromFile update tool data form file.
static void InitSplineToolConnections(VMainGraphicsScene *scene, T *tool)
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
hoverLeaveEvent handle hover leave events.
static void InitArcToolConnections(VMainGraphicsScene *scene, T *tool)
void ShowToolVisualization(bool show)
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
keyReleaseEvent handle key release events.
virtual QString makeToolTip() const Q_DECL_OVERRIDE
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE
itemChange hadle item change.
QVector< VControlPointSpline * > controlPoints
controlPoints list pointers of control points.
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
QString GetPenStyle() const
VAbstractSpline(VAbstractPattern *doc, VContainer *data, quint32 id, QGraphicsItem *parent=nullptr)
void ShowHandles(bool show)
virtual void GroupVisibility(quint32 object, bool visible) Q_DECL_OVERRIDE
QString getLineWeight() const
getLineWeight return line weight of the spline
virtual int type() const Q_DECL_OVERRIDE
VSpline CorrectedSpline(const VSpline &spline, const SplinePointPosition &position, const QPointF &pos) const
VAbstractPattern * doc
doc dom document container
QPointer< Visualization > vis
virtual void SetVisualization()=0
The VContainer class container of all variables.
Definition: vcontainer.h:141
The VControlPointSpline class control spline point.
VContainer data
data container with data
Definition: vdatatool.h:84
The VDrawTool abstract class for all draw tool.
Definition: vdrawtool.h:68
static void InitDrawToolConnections(VMainGraphicsScene *scene, T *tool)
Definition: vdrawtool.h:364
The VGObject class keep information graphical objects.
Definition: vgobject.h:74
The VMainGraphicsScene class main scene.
void EnableSplinePathItemHover(bool enable)
void EnableArcItemHover(bool enable)
void EnableElArcItemHover(bool enable)
void EnableSplineItemSelection(bool enable)
void EnableArcItemSelection(bool enable)
void EnableElArcItemSelection(bool enable)
void EnableSplineItemHover(bool enable)
void EnableSplinePathItemSelection(bool enable)
static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item=nullptr)
NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
VSpline class that implements the spline.
Definition: vspline.h:75
#define SCASSERT(cond)
Definition: def.h:317
SceneObject
Definition: def.h:103
@ AbstractSpline
#define qApp
Definition: vapplication.h:67
SplinePointPosition
Definition: vgeometrydef.h:58