1 /************************************************************************
2  **
3  **  @file   vtoolsinglepoint.h
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   November 15, 2013
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 VTOOLSINGLEPOINT_H
30 #define VTOOLSINGLEPOINT_H
31 
32 #include <qcompilerdetection.h>
33 #include <QGraphicsItem>
34 #include <QMetaObject>
35 #include <QObject>
36 #include <QPointF>
37 #include <QString>
38 #include <QVariant>
39 #include <QtGlobal>
40 
41 #include "../vabstractpoint.h"
42 #include "../vmisc/def.h"
43 #include "../vwidgets/vscenepoint.h"
44 
45 template <class T> class QSharedPointer;
46 
47 struct VToolSinglePointInitData : VDrawToolInitData
48 {
VToolSinglePointInitDataVToolSinglePointInitData49     VToolSinglePointInitData()
50         : VDrawToolInitData(),
51           name(),
52           mx(labelMX),
53           my(labelMY),
54           showLabel(true)
55     {}
56 
57     QString name;
58     qreal   mx;
59     qreal   my;
60     bool    showLabel;
61 };
62 
63 /**
64  * @brief The VToolSinglePoint class parent for all tools what create points.
65  */
66 class VToolSinglePoint: public VAbstractPoint, public VScenePoint
67 {
68     Q_OBJECT
69 public:
70     VToolSinglePoint(VAbstractPattern *doc, VContainer *data, quint32 id, const QString &notes,
71                      QGraphicsItem * parent = nullptr);
72     virtual ~VToolSinglePoint() Q_DECL_EQ_DEFAULT;
73 
type()74     virtual int type() const override {return Type;}
75     enum { Type = UserType + static_cast<int>(Tool::SinglePoint)};
76 
77     QString name() const;
78     void    setName(const QString &name);
79 
80     void SetEnabled(bool enabled);
81 
82     virtual void GroupVisibility(quint32 object, bool visible) override;
83     virtual void ChangeLabelPosition(quint32 id, const QPointF &pos) override;
84 
85     virtual bool IsLabelVisible(quint32 id) const override;
86     virtual void SetLabelVisible(quint32 id, bool visible) override;
87 public slots:
88     void         NameChangePosition(const QPointF &pos);
89     virtual void Disable(bool disable, const QString &namePP) override;
90     virtual void EnableToolMove(bool move) override;
91     void         PointChoosed();
92     void         PointSelected(bool selected);
93     virtual void FullUpdateFromFile() override;
94     virtual void AllowHover(bool enabled) override;
95     virtual void AllowSelecting(bool enabled) override;
96     void         AllowLabelHover(bool enabled);
97     void         AllowLabelSelecting(bool enabled);
98     virtual void ToolSelectionType(const SelectionType &type) override;
99 protected:
100     virtual void     UpdateNamePosition(quint32 id, const QPointF &pos) override;
101     virtual void     mousePressEvent(QGraphicsSceneMouseEvent *event) override;
102     virtual void     mouseReleaseEvent ( QGraphicsSceneMouseEvent * event ) override;
103     virtual void     hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
104     virtual QVariant itemChange ( GraphicsItemChange change, const QVariant &value ) override;
105     virtual void     keyReleaseEvent(QKeyEvent * event) override;
106     virtual void     contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ) override;
107     virtual void     SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj) override;
108     virtual void     ChangeLabelVisibility(quint32 id, bool visible) override;
109 
110     template <class Item>
111     static QPair<QString, QString> InitArc(VContainer *data, qreal segLength, const VPointF *p, quint32 curveId,
112                                            const QString &alias1, const QString &alias2);
113     static QPair<QString, QString> InitSegments(GOType curveType, qreal segLength, const VPointF *p, quint32 curveId,
114                                                 VContainer *data, const QString &alias1, const QString &alias2);
115 private:
116     Q_DISABLE_COPY(VToolSinglePoint)
117 };
118 
119 //---------------------------------------------------------------------------------------------------------------------
120 template <class Item>
121 inline auto VToolSinglePoint::InitArc(VContainer *data, qreal segLength, const VPointF *p,
122                                       quint32 curveId, const QString &alias1,
123                                       const QString &alias2) -> QPair<QString, QString>
124 {
125     QSharedPointer<Item> a1;
126     QSharedPointer<Item> a2;
127 
128     const QSharedPointer<Item> arc = data->GeometricObject<Item>(curveId);
129     Item arc1;
130     Item arc2;
131 
132     if (not VFuzzyComparePossibleNulls(segLength, -1))
133     {
134         arc->CutArc(segLength, arc1, arc2);
135     }
136     else
137     {
138         arc->CutArc(0, arc1, arc2);
139     }
140 
141     // Arc highly depend on id. Need for creating the name.
142     arc1.setId(p->id() + 1);
143     arc2.setId(p->id() + 2);
144 
145     arc1.SetAliasSuffix(alias1);
146     arc2.SetAliasSuffix(alias2);
147 
148     if (not VFuzzyComparePossibleNulls(segLength, -1))
149     {
150         a1 = QSharedPointer<Item>(new Item(arc1));
151         a2 = QSharedPointer<Item>(new Item(arc2));
152     }
153     else
154     {
155         a1 = QSharedPointer<Item>(new Item());
156         a2 = QSharedPointer<Item>(new Item());
157 
158         // Take names for empty arcs from donors.
159         a1->setName(arc1.name());
160         a2->setName(arc2.name());
161     }
162 
163     data->AddArc(a1, arc1.id(), p->id());
164     data->AddArc(a2, arc2.id(), p->id());
165 
166     // Because we don't store segments, but only data about them we must register the names manually
167     data->RegisterUniqueName(a1);
168     data->RegisterUniqueName(a2);
169 
170     return qMakePair(arc1.ObjectName(), arc2.ObjectName());
171 }
172 
173 #endif // VTOOLSINGLEPOINT_H
174