Seamly2D
Code documentation
vtooltruedarts.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 vtooltruedarts.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 23 6, 2015
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) 2015 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 "vtooltruedarts.h"
53 
54 #include <QLine>
55 #include <QLineF>
56 #include <QSharedPointer>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <new>
61 
62 #include "../../../../dialogs/tools/dialogtruedarts.h"
63 #include "../../../../dialogs/tools/dialogtool.h"
64 #include "../../../../visualization/visualization.h"
65 #include "../../../../visualization/line/vistooltruedarts.h"
66 #include "../ifc/exception/vexception.h"
67 #include "../ifc/ifcdef.h"
68 #include "../vgeometry/vgobject.h"
69 #include "../vgeometry/vpointf.h"
70 #include "../vpatterndb/vcontainer.h"
71 #include "../vwidgets/vmaingraphicsscene.h"
72 #include "../../../vabstracttool.h"
73 #include "../../vdrawtool.h"
74 #include "vtooldoublepoint.h"
75 
76 template <class T> class QSharedPointer;
77 
78 const QString VToolTrueDarts::ToolType = QStringLiteral("trueDarts");
79 
80 //---------------------------------------------------------------------------------------------------------------------
82  VContainer *data,
83  const quint32 &id,
84  const quint32 &p1id,
85  const quint32 &p2id,
86  const quint32 &baseLineP1Id,
87  const quint32 &baseLineP2Id,
88  const quint32 &dartP1Id,
89  const quint32 &dartP2Id,
90  const quint32 &dartP3Id,
91  const Source &typeCreation,
92  QGraphicsItem *parent)
93  : VToolDoublePoint(doc, data, id, p1id, p2id, parent)
94  , baseLineP1Id (baseLineP1Id)
95  , baseLineP2Id(baseLineP2Id)
96  , dartP1Id(dartP1Id)
97  , dartP2Id(dartP2Id)
98  , dartP3Id(dartP3Id)
99 {
100  ToolCreation(typeCreation);
101 }
102 
103 //---------------------------------------------------------------------------------------------------------------------
104 void VToolTrueDarts::FindPoint(const QPointF &baseLineP1, const QPointF &baseLineP2, const QPointF &dartP1,
105  const QPointF &dartP2, const QPointF &dartP3, QPointF &p1, QPointF &p2)
106 {
107  const QLineF d2d1(dartP2, dartP1);
108  const QLineF d2d3(dartP2, dartP3);
109 
110  const qreal degrees = d2d3.angleTo(d2d1);
111 
112  QLineF d2blP2(dartP2, baseLineP2);
113  d2blP2.setAngle(d2blP2.angle()+degrees);
114 
115  if (QLineF(baseLineP1, d2blP2.p2()).intersects(d2d1, &p1) == QLineF::NoIntersection)
116  {
117  p1 = QPointF(0, 0);
118  p2 = QPointF(0, 0);
119  return;
120  }
121 
122  QLineF d2p1(dartP2, p1);
123  d2p1.setAngle(d2p1.angle()-degrees);
124  p2 = d2p1.p2();
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
129 {
130  SCASSERT(not m_dialog.isNull())
131  QSharedPointer<DialogTrueDarts> dialogTool = m_dialog.objectCast<DialogTrueDarts>();
132  SCASSERT(not dialogTool.isNull())
133 
136 
137  dialogTool->SetChildrenId(p1id, p2id);
138  dialogTool->SetNewDartPointNames(p1->name(), p2->name());
139  dialogTool->SetFirstBasePointId(baseLineP1Id);
140  dialogTool->SetSecondBasePointId(baseLineP2Id);
141  dialogTool->SetFirstDartPointId(dartP1Id);
142  dialogTool->SetSecondDartPointId(dartP2Id);
143  dialogTool->SetThirdDartPointId(dartP3Id);
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
148  VAbstractPattern *doc, VContainer *data)
149 {
150  SCASSERT(not dialog.isNull())
151  QSharedPointer<DialogTrueDarts> dialogTool = dialog.objectCast<DialogTrueDarts>();
152  SCASSERT(not dialogTool.isNull())
153 
154  const QString point1Name = dialogTool->GetFirstNewDartPointName();
155  const QString point2Name = dialogTool->GetSecondNewDartPointName();
156  const quint32 baseLineP1Id = dialogTool->GetFirstBasePointId();
157  const quint32 baseLineP2Id = dialogTool->GetSecondBasePointId();
158  const quint32 dartP1Id = dialogTool->GetFirstDartPointId();
159  const quint32 dartP2Id = dialogTool->GetSecondDartPointId();
160  const quint32 dartP3Id = dialogTool->GetThirdDartPointId();
161 
163  point1Name, 5, 10, true, point2Name, 5, 10, true, scene, doc, data, Document::FullParse,
165  if (point != nullptr)
166  {
167  point->m_dialog = dialogTool;
168  }
169  return point;
170 }
171 
172 //---------------------------------------------------------------------------------------------------------------------
174  const quint32 &_p1id, const quint32 &_p2id,
175  const quint32 &baseLineP1Id,
176  const quint32 &baseLineP2Id,
177  const quint32 &dartP1Id,
178  const quint32 &dartP2Id,
179  const quint32 &dartP3Id,
180  const QString &point1Name,
181  qreal mx1, qreal my1, bool showPointName1,
182  const QString &point2Name,
183  qreal mx2, qreal my2, bool showPointName2,
184  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
185  const Document &parse, const Source &typeCreation)
186 {
192 
193  QPointF fPoint1;
194  QPointF fPoint2;
195  VToolTrueDarts::FindPoint(static_cast<QPointF>(*baseLineP1), static_cast<QPointF>(*baseLineP2),
196  static_cast<QPointF>(*dartP1), static_cast<QPointF>(*dartP2),
197  static_cast<QPointF>(*dartP3), fPoint1, fPoint2);
198 
199  quint32 id = _id;
200  quint32 p1id = _p1id;
201  quint32 p2id = _p2id;
202 
203  VPointF *p1 = new VPointF(fPoint1, point1Name, mx1, my1, id);
204  p1->setShowPointName(showPointName1);
205 
206  VPointF *p2 = new VPointF(fPoint2, point2Name, mx2, my2, id);
207  p2->setShowPointName(showPointName2);
208 
209 
210  if (typeCreation == Source::FromGui)
211  {
212  id = VContainer::getNextId(); //Just reserve id for tool
213  p1id = data->AddGObject(p1);
214  p2id = data->AddGObject(p2);
215  }
216  else
217  {
218  data->UpdateGObject(p1id, p1);
219  data->UpdateGObject(p2id, p2);
220  if (parse != Document::FullParse)
221  {
222  doc->UpdateToolData(id, data);
223  }
224  }
225 
226  if (parse == Document::FullParse)
227  {
230  dartP1Id, dartP2Id, dartP3Id, typeCreation);
231  scene->addItem(points);
232  InitToolConnections(scene, points);
233  VAbstractPattern::AddTool(id, points);
234  doc->IncrementReferens(baseLineP1->getIdTool());
235  doc->IncrementReferens(baseLineP2->getIdTool());
236  doc->IncrementReferens(dartP1->getIdTool());
237  doc->IncrementReferens(dartP2->getIdTool());
238  doc->IncrementReferens(dartP3->getIdTool());
239  return points;
240  }
241  return nullptr;
242 }
243 
244 //---------------------------------------------------------------------------------------------------------------------
246 {
247  ShowToolVisualization<VisToolTrueDarts>(show);
248 }
249 
250 //---------------------------------------------------------------------------------------------------------------------
252 {
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
258 {
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
264 {
265  return VAbstractTool::data.GetGObject(dartP1Id)->name();
266 }
267 
268 //---------------------------------------------------------------------------------------------------------------------
270 {
271  return VAbstractTool::data.GetGObject(dartP2Id)->name();
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
276 {
277  return VAbstractTool::data.GetGObject(dartP3Id)->name();
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
282 {
283  return baseLineP1Id;
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
287 void VToolTrueDarts::SetBaseLineP1Id(const quint32 &value)
288 {
289  if (value != NULL_ID)
290  {
291  baseLineP1Id = value;
292 
294  SaveOption(obj);
295  }
296 }
297 
298 //---------------------------------------------------------------------------------------------------------------------
300 {
301  return baseLineP2Id;
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
305 void VToolTrueDarts::SetBaseLineP2Id(const quint32 &value)
306 {
307  if (value != NULL_ID)
308  {
309  baseLineP2Id = value;
310 
312  SaveOption(obj);
313  }
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
318 {
319  return dartP1Id;
320 }
321 
322 //---------------------------------------------------------------------------------------------------------------------
323 void VToolTrueDarts::SetDartP1Id(const quint32 &value)
324 {
325  if (value != NULL_ID)
326  {
327  dartP1Id = value;
328 
330  SaveOption(obj);
331  }
332 }
333 
334 //---------------------------------------------------------------------------------------------------------------------
336 {
337  return dartP2Id;
338 }
339 
340 //---------------------------------------------------------------------------------------------------------------------
341 void VToolTrueDarts::SetDartP2Id(const quint32 &value)
342 {
343  if (value != NULL_ID)
344  {
345  dartP2Id = value;
346 
348  SaveOption(obj);
349  }
350 }
351 
352 //---------------------------------------------------------------------------------------------------------------------
354 {
355  return dartP3Id;
356 }
357 
358 //---------------------------------------------------------------------------------------------------------------------
359 void VToolTrueDarts::SetDartP3Id(const quint32 &value)
360 {
361  if (value != NULL_ID)
362  {
363  dartP3Id = value;
364 
366  SaveOption(obj);
367  }
368 }
369 
370 //---------------------------------------------------------------------------------------------------------------------
371 void VToolTrueDarts::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
372 {
373  try
374  {
375  ContextMenu<DialogTrueDarts>(event, id);
376  }
377  catch(const VExceptionToolWasDeleted &e)
378  {
379  Q_UNUSED(e)
380  return;//Leave this method immediately!!!
381  }
382 }
383 
384 //---------------------------------------------------------------------------------------------------------------------
386 {
387  const auto baseLineP1 = VAbstractTool::data.GetGObject(baseLineP1Id);
388  const auto baseLineP2 = VAbstractTool::data.GetGObject(baseLineP2Id);
389  const auto dartP1 = VAbstractTool::data.GetGObject(dartP1Id);
390  const auto dartP2 = VAbstractTool::data.GetGObject(dartP2Id);
391  const auto dartP3 = VAbstractTool::data.GetGObject(dartP3Id);
392 
393  doc->DecrementReferens(baseLineP1->getIdTool());
394  doc->DecrementReferens(baseLineP2->getIdTool());
395  doc->DecrementReferens(dartP1->getIdTool());
396  doc->DecrementReferens(dartP2->getIdTool());
397  doc->DecrementReferens(dartP3->getIdTool());
399 }
400 
401 //---------------------------------------------------------------------------------------------------------------------
402 void VToolTrueDarts::SaveDialog(QDomElement &domElement)
403 {
404  SCASSERT(not m_dialog.isNull())
405  QSharedPointer<DialogTrueDarts> dialogTool = m_dialog.objectCast<DialogTrueDarts>();
406  SCASSERT(not dialogTool.isNull())
407 
408  doc->SetAttribute(domElement, AttrName1, dialogTool->GetFirstNewDartPointName());
409  doc->SetAttribute(domElement, AttrName2, dialogTool->GetSecondNewDartPointName());
410  doc->SetAttribute(domElement, AttrBaseLineP1, QString().setNum(dialogTool->GetFirstBasePointId()));
411  doc->SetAttribute(domElement, AttrBaseLineP2, QString().setNum(dialogTool->GetSecondBasePointId()));
412  doc->SetAttribute(domElement, AttrDartP1, QString().setNum(dialogTool->GetFirstDartPointId()));
413  doc->SetAttribute(domElement, AttrDartP2, QString().setNum(dialogTool->GetSecondDartPointId()));
414  doc->SetAttribute(domElement, AttrDartP3, QString().setNum(dialogTool->GetThirdDartPointId()));
415 }
416 
417 //---------------------------------------------------------------------------------------------------------------------
419 {
421 
428 }
429 
430 //---------------------------------------------------------------------------------------------------------------------
431 void VToolTrueDarts::ReadToolAttributes(const QDomElement &domElement)
432 {
438 }
439 
440 //---------------------------------------------------------------------------------------------------------------------
442 {
443  if (not vis.isNull())
444  {
445  VisToolTrueDarts *visual = qobject_cast<VisToolTrueDarts *>(vis);
446  SCASSERT(visual != nullptr)
447 
448  visual->setObject1Id(baseLineP1Id);
449  visual->setObject2Id(baseLineP2Id);
450  visual->setD1PointId(dartP1Id);
451  visual->setD2PointId(dartP2Id);
452  visual->setD3PointId(dartP3Id);
453  visual->RefreshGeometry();
454  }
455 }
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
static void InitToolConnections(VMainGraphicsScene *scene, T *tool)
VAbstractPattern * doc
doc dom document container
QPointer< Visualization > vis
virtual void RemoveReferens()
RemoveReferens decrement value of reference.
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
static quint32 getNextId()
getNextId generate next unique id
Definition: vcontainer.cpp:251
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
quint32 AddGObject(VGObject *obj)
AddGObject add new GObject to container.
Definition: vcontainer.cpp:216
static const QSharedPointer< VGObject > GetFakeGObject(quint32 id)
Definition: vcontainer.cpp:156
void UpdateGObject(quint32 id, T *obj)
UpdateGObject update GObject by id.
Definition: vcontainer.h:374
VContainer data
data container with data
Definition: vdatatool.h:84
static quint32 GetParametrUInt(const QDomElement &domElement, const QString &name, const QString &defValue)
Returns the long long value of the given attribute. RENAME: GetParameterLongLong?
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
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
void setShowPointName(bool show)
Definition: vpointf.cpp:265
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
quint32 GetDartP3Id() const
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
quint32 baseLineP1Id
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void SetDartP3Id(const quint32 &value)
quint32 baseLineP2Id
quint32 GetBaseLineP1Id() const
QString DartP2Name() const
QString DartP3Name() const
void SetBaseLineP2Id(const quint32 &value)
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
quint32 GetDartP2Id() const
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
static VToolTrueDarts * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
void SetDartP2Id(const quint32 &value)
QString BaseLineP2Name() const
static void FindPoint(const QPointF &baseLineP1, const QPointF &baseLineP2, const QPointF &dartP1, const QPointF &dartP2, const QPointF &dartP3, QPointF &p1, QPointF &p2)
QString DartP1Name() const
virtual void SetVisualization() Q_DECL_OVERRIDE
QString BaseLineP1Name() const
void SetDartP1Id(const quint32 &value)
quint32 GetBaseLineP2Id() const
void SetBaseLineP1Id(const quint32 &value)
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
static const QString ToolType
VToolTrueDarts(VAbstractPattern *doc, VContainer *data, const quint32 &id, const quint32 &p1id, const quint32 &p2id, const quint32 &baseLineP1Id, const quint32 &baseLineP2Id, const quint32 &dartP1Id, const quint32 &dartP2Id, const quint32 &dartP3Id, const Source &typeCreation, QGraphicsItem *parent=nullptr)
quint32 GetDartP1Id() const
void setD3PointId(const quint32 &value)
void setD1PointId(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setObject2Id(const quint32 &value)
void setD2PointId(const quint32 &value)
void setObject1Id(const quint32 &value)
#define SCASSERT(cond)
Definition: def.h:317
@ TrueDarts
Source
Definition: def.h:106
const QString AttrName1
Definition: ifcdef.cpp:79
const QString AttrBaseLineP1
Definition: ifcdef.cpp:83
const QString AttrDartP2
Definition: ifcdef.cpp:86
const QString AttrBaseLineP2
Definition: ifcdef.cpp:84
const QString AttrDartP1
Definition: ifcdef.cpp:85
const QString AttrName2
Definition: ifcdef.cpp:82
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrDartP3
Definition: ifcdef.cpp:87
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document