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