Seamly2D
Code documentation
vtoolcubicbezier.cpp
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 vtoolcubicbezier.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 10 3, 2016
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) 2016 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 #include "vtoolcubicbezier.h"
53 
54 #include <QDomElement>
55 #include <QPen>
56 #include <QSharedPointer>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <Qt>
61 #include <new>
62 
63 #include "../../../dialogs/tools/dialogtool.h"
64 #include "../../../dialogs/tools/dialogcubicbezier.h"
65 #include "../../../visualization/visualization.h"
66 #include "../../../visualization/path/vistoolcubicbezier.h"
67 #include "../ifc/exception/vexception.h"
68 #include "../vgeometry/../ifc/ifcdef.h"
69 #include "../vgeometry/vabstractcurve.h"
70 #include "../vgeometry/vcubicbezier.h"
71 #include "../vgeometry/vgobject.h"
72 #include "../vgeometry/vpointf.h"
73 #include "../vmisc/vabstractapplication.h"
74 #include "../vpatterndb/vcontainer.h"
75 #include "../vwidgets/vmaingraphicsscene.h"
76 #include "../../vabstracttool.h"
77 #include "../vdrawtool.h"
78 #include "vabstractspline.h"
79 
80 const QString VToolCubicBezier::ToolType = QStringLiteral("cubicBezier");
81 
82 //---------------------------------------------------------------------------------------------------------------------
84  const Source &typeCreation, QGraphicsItem *parent)
85  :VAbstractSpline(doc, data, id, parent)
86 {
88 
89  this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
90 
91  ToolCreation(typeCreation);
92 }
93 
94 //---------------------------------------------------------------------------------------------------------------------
96 {
97  SCASSERT(not m_dialog.isNull())
98  auto dialogTool = qobject_cast<DialogCubicBezier*>(m_dialog);
99  SCASSERT(dialogTool != nullptr)
101  dialogTool->SetSpline(*spl);
102  dialogTool->setLineColor(spl->getLineColor());
103  dialogTool->setPenStyle(spl->GetPenStyle());
104  dialogTool->setLineWeight(spl->getLineWeight());
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
109  VAbstractPattern *doc, VContainer *data)
110 {
111  SCASSERT(not dialog.isNull())
112  QSharedPointer<DialogCubicBezier> dialogTool = dialog.objectCast<DialogCubicBezier>();
113  SCASSERT(not dialogTool.isNull())
114 
115  VCubicBezier *spline = new VCubicBezier(dialogTool->GetSpline());
116  spline->setLineColor(dialogTool->getLineColor());
117  spline->SetPenStyle(dialogTool->getPenStyle());
118  spline->setLineWeight(dialogTool->getLineWeight());
119 
120  auto spl = Create(0, spline, scene, doc, data, Document::FullParse, Source::FromGui);
121 
122  if (spl != nullptr)
123  {
124  spl->m_dialog = dialogTool;
125  }
126  return spl;
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
131  VAbstractPattern *doc, VContainer *data, const Document &parse,
132  const Source &typeCreation)
133 {
134  quint32 id = _id;
135  if (typeCreation == Source::FromGui)
136  {
137  id = data->AddGObject(spline);
139  }
140  else
141  {
142  data->UpdateGObject(id, spline);
144  if (parse != Document::FullParse)
145  {
146  doc->UpdateToolData(id, data);
147  }
148  }
149 
150  if (parse == Document::FullParse)
151  {
153  auto _spl = new VToolCubicBezier(doc, data, id, typeCreation);
154  scene->addItem(_spl);
155  InitSplineToolConnections(scene, _spl);
156  VAbstractPattern::AddTool(id, _spl);
157  doc->IncrementReferens(spline->GetP1().getIdTool());
158  doc->IncrementReferens(spline->GetP1().getIdTool());
159  doc->IncrementReferens(spline->GetP1().getIdTool());
160  doc->IncrementReferens(spline->GetP4().getIdTool());
161  return _spl;
162  }
163  return nullptr;
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
168 {
170  return spline->GetP1().name();
171 }
172 
173 //---------------------------------------------------------------------------------------------------------------------
175 {
177  return spline->GetP2().name();
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
182 {
184  return spline->GetP3().name();
185 }
186 
187 //---------------------------------------------------------------------------------------------------------------------
189 {
191  return spline->GetP4().name();
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
196 {
198  return *spline.data();
199 }
200 
201 //---------------------------------------------------------------------------------------------------------------------
203 {
205  QSharedPointer<VCubicBezier> spline = qSharedPointerDynamicCast<VCubicBezier>(obj);
206  *spline.data() = spl;
207  SaveOption(obj);
208 }
209 
210 //---------------------------------------------------------------------------------------------------------------------
212 {
213  ShowToolVisualization<VisToolCubicBezier>(show);
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------------
217 void VToolCubicBezier::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
218 {
219  Q_UNUSED(id)
220 
221  try
222  {
223  ContextMenu<DialogCubicBezier>(event);
224  }
225  catch(const VExceptionToolWasDeleted &e)
226  {
227  Q_UNUSED(e)
228  return;//Leave this method immediately!!!
229  }
230 }
231 
232 //---------------------------------------------------------------------------------------------------------------------
234 {
236  doc->DecrementReferens(spl->GetP1().getIdTool());
237  doc->DecrementReferens(spl->GetP2().getIdTool());
238  doc->DecrementReferens(spl->GetP3().getIdTool());
239  doc->DecrementReferens(spl->GetP4().getIdTool());
240 }
241 
242 //---------------------------------------------------------------------------------------------------------------------
243 void VToolCubicBezier::SaveDialog(QDomElement &domElement)
244 {
245  SCASSERT(not m_dialog.isNull())
246  auto dialogTool = qobject_cast<DialogCubicBezier*>(m_dialog);
247  SCASSERT(dialogTool != nullptr)
248 
249  const VCubicBezier spl = dialogTool->GetSpline();
250 
251  SetSplineAttributes(domElement, spl);
252  doc->SetAttribute(domElement, AttrColor, dialogTool->getLineColor());
253  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
254  doc->SetAttribute(domElement, AttrPenStyle, dialogTool->getPenStyle());
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
259 {
261 
262  auto spl = qSharedPointerDynamicCast<VCubicBezier>(obj);
263  SCASSERT(spl.isNull() == false)
264  SetSplineAttributes(tag, *spl);
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
269 {
270  if (not vis.isNull())
271  {
272  auto visual = qobject_cast<VisToolCubicBezier *>(vis);
273  SCASSERT(visual != nullptr)
274 
276  visual->setObject1Id(spl->GetP1().id());
277  visual->setObject2Id(spl->GetP2().id());
278  visual->setObject3Id(spl->GetP3().id());
279  visual->setObject4Id(spl->GetP4().id());
280  visual->setLineStyle(lineTypeToPenStyle(spl->GetPenStyle()));
281  visual->setLineWeight(spl->getLineWeight());
282  visual->SetMode(Mode::Show);
283  visual->RefreshGeometry();
284  }
285 }
286 
287 //---------------------------------------------------------------------------------------------------------------------
289 {
291  this->setPath(spl->GetPath());
292 
294 }
295 
296 //---------------------------------------------------------------------------------------------------------------------
297 void VToolCubicBezier::SetSplineAttributes(QDomElement &domElement, const VCubicBezier &spl)
298 {
299  SCASSERT(doc != nullptr)
300 
301  doc->SetAttribute(domElement, AttrType, ToolType);
302  doc->SetAttribute(domElement, AttrPoint1, spl.GetP1().id());
303  doc->SetAttribute(domElement, AttrPoint2, spl.GetP2().id());
304  doc->SetAttribute(domElement, AttrPoint3, spl.GetP3().id());
305  doc->SetAttribute(domElement, AttrPoint4, spl.GetP4().id());
306 
307  if (spl.GetDuplicate() > 0)
308  {
309  doc->SetAttribute(domElement, AttrDuplicate, spl.GetDuplicate());
310  }
311  else
312  {
313  if (domElement.hasAttribute(AttrDuplicate))
314  {
315  domElement.removeAttribute(AttrDuplicate);
316  }
317  }
318 }
void setLineColor(const QString &color)
void setLineWeight(const QString &lineWeight)
setLineWeight set weight of the lines
quint32 GetDuplicate() const
virtual void UpdateToolData(const quint32 &id, VContainer *data)=0
virtual void IncrementReferens(quint32 id) const =0
static void AddTool(quint32 id, VDataTool *tool)
AddTool add tool to list tools.
virtual void DecrementReferens(quint32 id) const =0
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
SceneObject sceneType
static void InitSplineToolConnections(VMainGraphicsScene *scene, T *tool)
VAbstractPattern * doc
doc dom document container
QPointer< Visualization > vis
virtual void ToolCreation(const Source &typeCreation)
static void AddRecord(const quint32 id, const Tool &toolType, VAbstractPattern *doc)
AddRecord add record about tool in history.
const quint32 m_id
id object id.
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< VGObject > GetGObject(quint32 id) const
GetGObject returns a point by id.
Definition: vcontainer.cpp:150
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
void AddSpline(const QSharedPointer< VAbstractBezier > &curve, quint32 id, quint32 parentId=null_id)
Definition: vcontainer.cpp:440
quint32 AddGObject(VGObject *obj)
AddGObject add new GObject to container.
Definition: vcontainer.cpp:216
void UpdateGObject(quint32 id, T *obj)
UpdateGObject update GObject by id.
Definition: vcontainer.h:374
virtual VPointF GetP1() const Q_DECL_OVERRIDE
virtual VPointF GetP3() const Q_DECL_OVERRIDE
virtual VPointF GetP2() const Q_DECL_OVERRIDE
virtual VPointF GetP4() const Q_DECL_OVERRIDE
VContainer data
data container with data
Definition: vdatatool.h:84
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
SetAttribute set attribute in pattern file. Replace "," by ".".
Definition: vdomdocument.h:185
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
quint32 id() const
id return id object.
Definition: vgobject.cpp:205
quint32 getIdTool() const
Definition: vgobject.cpp:221
QSharedPointer< DialogTool > m_dialog
m_dialog tool's dialog options.
The VMainGraphicsScene class main scene.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
VToolCubicBezier(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation, QGraphicsItem *parent=nullptr)
void setSpline(const VCubicBezier &spl)
void SetSplineAttributes(QDomElement &domElement, const VCubicBezier &spl)
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void RefreshGeometry() Q_DECL_OVERRIDE
RefreshGeometry refresh item on scene.
QString ThirdPointName() const
VCubicBezier getSpline() const
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
static VToolCubicBezier * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
QString ForthPointName() const
QString FirstPointName() const
static const QString ToolType
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
QString SecondPointName() const
#define SCASSERT(cond)
Definition: def.h:317
@ CubicBezier
Source
Definition: def.h:106
const QString AttrPoint2
Definition: ifcdef.cpp:116
const QString AttrDuplicate
Definition: ifcdef.cpp:122
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrPenStyle
Definition: ifcdef.cpp:132
const QString AttrPoint3
Definition: ifcdef.cpp:117
const QString AttrPoint1
Definition: ifcdef.cpp:115
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrPoint4
Definition: ifcdef.cpp:118
const QString AttrColor
Definition: ifcdef.cpp:131
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
Document