Seamly2D
Code documentation
vtoolcubicbezierpath.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 vtoolcubicbezierpath.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 18 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 "vtoolcubicbezierpath.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/dialogcubicbezierpath.h"
65 #include "../../../visualization/visualization.h"
66 #include "../../../visualization/path/vistoolcubicbezierpath.h"
67 #include "../ifc/exception/vexception.h"
68 #include "../ifc/xml/vdomdocument.h"
69 #include "../ifc/ifcdef.h"
70 #include "../vgeometry/vabstractcubicbezierpath.h"
71 #include "../vgeometry/vabstractcurve.h"
72 #include "../vgeometry/vcubicbezierpath.h"
73 #include "../vgeometry/vgobject.h"
74 #include "../vgeometry/vpointf.h"
75 #include "../vmisc/vabstractapplication.h"
76 #include "../vpatterndb/vcontainer.h"
77 #include "../vwidgets/vmaingraphicsscene.h"
78 #include "../../vabstracttool.h"
79 #include "../vdrawtool.h"
80 #include "vabstractspline.h"
81 
82 const QString VToolCubicBezierPath::ToolType = QStringLiteral("cubicBezierPath");
83 
84 //---------------------------------------------------------------------------------------------------------------------
86  const Source &typeCreation, QGraphicsItem *parent)
87  : VAbstractSpline(doc, data, id, parent)
88 {
90 
91  this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
92 
93  ToolCreation(typeCreation);
94 }
95 
96 //---------------------------------------------------------------------------------------------------------------------
98 {
99  SCASSERT(not m_dialog.isNull())
100  auto dialogTool = qobject_cast<DialogCubicBezierPath*>(m_dialog);
101  SCASSERT(dialogTool != nullptr)
103  dialogTool->SetPath(*splPath);
104  dialogTool->setLineColor(splPath->getLineColor());
105  dialogTool->setPenStyle(splPath->GetPenStyle());
106  dialogTool->setLineWeight(splPath->getLineWeight());
107 }
108 
109 //---------------------------------------------------------------------------------------------------------------------
111  VAbstractPattern *doc, VContainer *data)
112 {
113  SCASSERT(not dialog.isNull())
114  QSharedPointer<DialogCubicBezierPath> dialogTool = dialog.objectCast<DialogCubicBezierPath>();
115  SCASSERT(not dialogTool.isNull())
116  auto path = new VCubicBezierPath(dialogTool->GetPath());
117  const QString color = dialogTool->getLineColor();
118  const QString penStyle = dialogTool->getPenStyle();
119  const QString lineWeight = dialogTool->getLineWeight();
120  for (qint32 i = 0; i < path->CountPoints(); ++i)
121  {
122  doc->IncrementReferens((*path)[i].getIdTool());
123  }
124  path->setLineColor(color);
125  path->SetPenStyle(penStyle);
126  path->setLineWeight(lineWeight);
127 
129  if (spl != nullptr)
130  {
131  spl->m_dialog = dialogTool;
132  }
133  return spl;
134 }
135 
136 //---------------------------------------------------------------------------------------------------------------------
138  VAbstractPattern *doc, VContainer *data, const Document &parse,
139  const Source &typeCreation)
140 {
141  quint32 id = _id;
142  if (typeCreation == Source::FromGui)
143  {
144  id = data->AddGObject(path);
146  }
147  else
148  {
149  data->UpdateGObject(id, path);
151  if (parse != Document::FullParse)
152  {
153  doc->UpdateToolData(id, data);
154  }
155  }
156 
157  if (parse == Document::FullParse)
158  {
160  VToolCubicBezierPath *spl = new VToolCubicBezierPath(doc, data, id, typeCreation);
161  scene->addItem(spl);
162  InitSplinePathToolConnections(scene, spl);
163  VAbstractPattern::AddTool(id, spl);
164  return spl;
165  }
166  return nullptr;
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
170 void VToolCubicBezierPath::UpdatePathPoints(VAbstractPattern *doc, QDomElement &element, const VCubicBezierPath &path)
171 {
173  for (qint32 i = 0; i < path.CountPoints(); ++i)
174  {
175  AddPathPoint(doc, element, path.at(i));
176  }
177 }
178 
179 //---------------------------------------------------------------------------------------------------------------------
181 {
183  return *splPath.data();
184 }
185 
186 //---------------------------------------------------------------------------------------------------------------------
188 {
190  QSharedPointer<VCubicBezierPath> splinePath = qSharedPointerDynamicCast<VCubicBezierPath>(obj);
191  *splinePath.data() = splPath;
192  SaveOption(obj);
193 }
194 
195 //---------------------------------------------------------------------------------------------------------------------
197 {
198  ShowToolVisualization<VisToolCubicBezierPath>(show);
199 }
200 
201 //---------------------------------------------------------------------------------------------------------------------
202 void VToolCubicBezierPath::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
203 {
204  Q_UNUSED(id)
205 
206  try
207  {
208  ContextMenu<DialogCubicBezierPath>(event);
209  }
210  catch(const VExceptionToolWasDeleted &e)
211  {
212  Q_UNUSED(e)
213  return;//Leave this method immediately!!!
214  }
215 }
216 
217 //---------------------------------------------------------------------------------------------------------------------
219 {
221  for (qint32 i = 0; i < splPath->CountSubSpl(); ++i)
222  {
223  doc->DecrementReferens(splPath->at(i).getIdTool());
224  }
225 }
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 void VToolCubicBezierPath::SaveDialog(QDomElement &domElement)
229 {
230  SCASSERT(not m_dialog.isNull())
231  const auto dialogTool = qobject_cast<DialogCubicBezierPath*>(m_dialog);
232  SCASSERT(dialogTool != nullptr)
233 
234  doc->SetAttribute(domElement, AttrColor, dialogTool->getLineColor());
235  doc->SetAttribute(domElement, AttrPenStyle, dialogTool->getPenStyle());
236  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
237  SetSplinePathAttributes(domElement, dialogTool->GetPath());
238 }
239 
240 //---------------------------------------------------------------------------------------------------------------------
242 {
244 
245  QSharedPointer<VCubicBezierPath> splPath = qSharedPointerDynamicCast<VCubicBezierPath>(obj);
246  SCASSERT(splPath.isNull() == false)
247 
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  if (not vis.isNull())
255  {
256  auto visual = qobject_cast<VisToolCubicBezierPath *>(vis);
257  SCASSERT(visual != nullptr)
258 
260  visual->setPath(*splPath.data());
261  visual->setLineStyle(lineTypeToPenStyle(splPath->GetPenStyle()));
262  visual->SetMode(Mode::Show);
263  visual->RefreshGeometry();
264  }
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
269 {
271  setPath(splPath->GetPath());
272 
274 }
275 
276 //---------------------------------------------------------------------------------------------------------------------
277 void VToolCubicBezierPath::AddPathPoint(VAbstractPattern *doc, QDomElement &domElement, const VPointF &splPoint)
278 {
279  SCASSERT(doc != nullptr)
280  QDomElement pathPoint = doc->createElement(AttrPathPoint);
281  doc->SetAttribute(pathPoint, AttrPSpline, splPoint.id());
282  domElement.appendChild(pathPoint);
283 }
284 
285 //---------------------------------------------------------------------------------------------------------------------
286 void VToolCubicBezierPath::SetSplinePathAttributes(QDomElement &domElement, const VCubicBezierPath &path)
287 {
288  doc->SetAttribute(domElement, AttrType, ToolType);
289 
290  if (path.GetDuplicate() > 0)
291  {
292  doc->SetAttribute(domElement, AttrDuplicate, path.GetDuplicate());
293  }
294  else
295  {
296  if (domElement.hasAttribute(AttrDuplicate))
297  {
298  domElement.removeAttribute(AttrDuplicate);
299  }
300  }
301 
302  UpdatePathPoints(doc, domElement, path);
303 }
void setLineWeight(const QString &value)
setLineWeight set weight of the lines
void setLineColor(const QString &value)
void setLineColor(const QString &color)
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
static void InitSplinePathToolConnections(VMainGraphicsScene *scene, T *tool)
SceneObject sceneType
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 AddCurveWithSegments(const QSharedPointer< VAbstractCubicBezierPath > &curve, const quint32 &id, quint32 parentId=null_id)
Definition: vcontainer.cpp:452
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 qint32 CountPoints() const Q_DECL_OVERRIDE
const VPointF & at(int indx) const
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
static void RemoveAllChildren(QDomElement &domElement)
RemoveAllChildren remove all children from file.
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
quint32 id() const
id return id object.
Definition: vgobject.cpp:205
QSharedPointer< DialogTool > m_dialog
m_dialog tool's dialog options.
The VMainGraphicsScene class main scene.
The VPointF class keep data of point.
Definition: vpointf.h:75
static VToolCubicBezierPath * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
void SetSplinePathAttributes(QDomElement &domElement, const VCubicBezierPath &path)
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
void setSplinePath(const VCubicBezierPath &splPath)
VToolCubicBezierPath(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation, QGraphicsItem *parent=nullptr)
static void AddPathPoint(VAbstractPattern *doc, QDomElement &domElement, const VPointF &splPoint)
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
static const QString ToolType
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
static void UpdatePathPoints(VAbstractPattern *doc, QDomElement &element, const VCubicBezierPath &path)
VCubicBezierPath getSplinePath() const
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void RefreshGeometry() Q_DECL_OVERRIDE
RefreshGeometry refresh item on scene.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
#define SCASSERT(cond)
Definition: def.h:317
@ CubicBezierPath
Source
Definition: def.h:106
const QString splPath
Definition: ifcdef.cpp:419
const QString AttrDuplicate
Definition: ifcdef.cpp:122
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrPSpline
Definition: ifcdef.cpp:124
const QString AttrPenStyle
Definition: ifcdef.cpp:132
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrPathPoint
Definition: ifcdef.cpp:123
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