1 /************************************************************************
2  **
3  **  @file   vabstracttool.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 VABSTRACTTOOL_H
30 #define VABSTRACTTOOL_H
31 
32 #include <qcompilerdetection.h>
33 #include <QMap>
34 #include <QMetaObject>
35 #include <QObject>
36 #include <QString>
37 #include <QStringList>
38 #include <Qt>
39 #include <QtGlobal>
40 
41 #include "../ifc/xml/vabstractpattern.h"
42 #include "../ifc/xml/vabstractpattern.h"
43 #include "../vtools/visualization/visualization.h"
44 #include "../vmisc/vabstractapplication.h"
45 #include "../vmisc/def.h"
46 #include "../vwidgets/vmaingraphicsscene.h"
47 #include "../vmisc/diagnostic.h"
48 #include "vdatatool.h"
49 
50 class VGraphicsSimpleTextItem;
51 class VAbstractNode;
52 
53 QT_WARNING_PUSH
54 QT_WARNING_DISABLE_GCC("-Weffc++")
55 QT_WARNING_DISABLE_CLANG("-Wdeprecated")
56 
57 struct VAbstractToolInitData
58 {
VAbstractToolInitDataVAbstractToolInitData59     VAbstractToolInitData()
60         : id(NULL_ID),
61           scene(nullptr),
62           doc(nullptr),
63           data(nullptr),
64           parse(Document::FullParse),
65           typeCreation(Source::FromFile)
66     {}
67 
68     virtual ~VAbstractToolInitData()=default;
69 
70     /** @brief id tool id, 0 if tool doesn't exist yet.*/
71     quint32             id;
72     VMainGraphicsScene *scene;
73     VAbstractPattern   *doc;
74     VContainer         *data;
75     Document            parse;
76     Source              typeCreation;
77 };
78 
79 QT_WARNING_POP
80 
81 /**
82  * @brief The VAbstractTool abstract class for all tools.
83  */
84 class VAbstractTool: public VDataTool
85 {
86     Q_OBJECT
87 public:
88     VAbstractTool(VAbstractPattern *doc, VContainer *data, quint32 id, QObject *parent = nullptr);
89     virtual ~VAbstractTool() override;
90     quint32                 getId() const;
91 
92     static bool m_suppressContextMenu;
93     static const QString AttrInUse;
94 
95     static qreal CheckFormula(const quint32 &toolId, QString &formula, VContainer *data);
96 
97     static const QStringList      Colors();
98     static QMap<QString, QString> ColorsList();
99 
100     static VToolRecord GetRecord(const quint32 id, const Tool &toolType, VAbstractPattern *doc);
101     static void RemoveRecord(const VToolRecord &record, VAbstractPattern *doc);
102     static void AddRecord(const VToolRecord &record, VAbstractPattern *doc);
103     static void AddRecord(const quint32 id, const Tool &toolType, VAbstractPattern *doc);
104     static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiecePath &path);
105     static void AddNodes(VAbstractPattern *doc, QDomElement &domElement, const VPiece &piece);
106 
107     const VContainer        *getData() const;
108 
109     QMap<QString, quint32>  PointsList() const;
110     virtual QString         getTagName() const =0;
111     virtual void            ShowVisualization(bool show) =0;
112     virtual void            ChangeLabelPosition(quint32 id, const QPointF &pos);
113     virtual void            SetLabelVisible(quint32 id, bool visible);
114 public slots:
115     /**
116      * @brief FullUpdateFromFile update tool data form file.
117      */
118     virtual void            FullUpdateFromFile()=0;
119     virtual void            AllowHover(bool enabled)=0;
120     virtual void            AllowSelecting(bool enabled)=0;
121     virtual void            ToolSelectionType(const SelectionType &type);
122 signals:
123     /**
124      * @brief toolhaveChange emit if tool create change that need save.
125      */
126     void                    toolhaveChange();
127     /**
128      * @brief ChoosedTool emit if object was clicked.
129      * @param id object id in container.
130      * @param type type of scene object.
131      */
132     void                    ChoosedTool(quint32 id, SceneObject type);
133     /**
134      * @brief FullUpdateTree emit if need reparse pattern file.
135      */
136     void                    LiteUpdateTree(const Document &parse);
137 
138     void                    ToolTip(const QString &toolTip);
139 protected:
140     /** @brief doc dom document container */
141     VAbstractPattern         *doc;
142 
143     /** @brief id object id. */
144     const quint32            m_id;
145 
146     QPointer<Visualization> vis;
147     SelectionType           selectionType;
148 
149     /**
150      * @brief AddToFile add tag with informations about tool into file.
151      */
152     virtual void            AddToFile()=0;
153     /**
154      * @brief RefreshDataInFile refresh attributes in file. If attributes don't exist create them.
155      */
156     virtual void            RefreshDataInFile();
157     /**
158      * @brief RemoveReferens decrement value of reference.
159      */
RemoveReferens()160     virtual void            RemoveReferens() {}
161     virtual void            DeleteToolWithConfirm(bool ask = true);
162     virtual void            PerformDelete();
163     static int              ConfirmDeletion();
164 
165     template<typename T>
166     static quint32 CreateNode(VContainer *data, quint32 id);
167     static quint32 CreateNodeSpline(VContainer *data, quint32 id);
168     static quint32 CreateNodeSplinePath(VContainer *data, quint32 id);
169     static quint32 CreateNodePoint(VContainer *data, quint32 id, const QSharedPointer<VPointF> &point);
170 
171     template <typename T>
172     void AddVisualization();
173 
174     virtual void SetVisualization()=0;
175     virtual void ToolCreation(const Source &typeCreation);
176 
177     static QDomElement AddSANode(VAbstractPattern *doc, const QString &tagName, const VPieceNode &node);
178     static void        AddNode(VAbstractPattern *doc, QDomElement &domElement, const VPieceNode &node);
179 
180     static QVector<VPieceNode> PrepareNodes(const VPiecePath &path, VMainGraphicsScene *scene, VAbstractPattern *doc,
181                                             VContainer *data);
182     static quint32 PrepareNode(const VPieceNode &node, VMainGraphicsScene *scene, VAbstractPattern *doc,
183                                VContainer *data);
184 private:
185     Q_DISABLE_COPY(VAbstractTool)
186 };
187 
188 //---------------------------------------------------------------------------------------------------------------------
189 /**
190  * @brief getId return object id.
191  * @return id.
192  */
getId()193 inline quint32 VAbstractTool::getId() const
194 {
195     return m_id;
196 }
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 /**
200  * @brief getData return pointer to data container.
201  * @return container.
202  */
getData()203 inline const VContainer *VAbstractTool::getData() const
204 {
205     return &data;
206 }
207 
208 //---------------------------------------------------------------------------------------------------------------------
209 template <typename T>
AddVisualization()210 inline void VAbstractTool::AddVisualization()
211 {
212     T *visual = new T(getData());
213     VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(VAbstractValApplication::VApp()->getCurrentScene());
214     SCASSERT(scene != nullptr)
215     scene->addItem(visual);
216 
217     connect(visual, &Visualization::ToolTip, this, &VAbstractTool::ToolTip);
218     vis = visual;
219 }
220 
221 //---------------------------------------------------------------------------------------------------------------------
222 template<typename T>
223 /**
224  * @brief CreateNode create new node for detail.
225  * @param data container.
226  * @param id id parent object.
227  * @return id for new object.
228  */
CreateNode(VContainer * data,quint32 id)229 quint32 VAbstractTool::CreateNode(VContainer *data, quint32 id)
230 {
231     //We can't use exist object. Need create new.
232     T *node = new T(*data->GeometricObject<T>(id).data());
233     node->setMode(Draw::Modeling);
234     node->setIdObject(id);
235     return data->AddGObject(node);
236 }
237 
238 #endif // VABSTRACTTOOL_H
239