Seamly2D
Code documentation
vtooltriangle.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 vtooltriangle.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
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) 2013-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 "vtooltriangle.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/dialogtriangle.h"
63 #include "../../../../dialogs/tools/dialogtool.h"
64 #include "../../../../visualization/visualization.h"
65 #include "../../../../visualization/line/vistooltriangle.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 "vtoolsinglepoint.h"
75 #include "../vmisc/vmath.h"
76 #include "../vmisc/vabstractapplication.h"
77 
78 template <class T> class QSharedPointer;
79 
80 const QString VToolTriangle::ToolType = QStringLiteral("triangle");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolTriangle constructor.
85  * @param doc dom document container.
86  * @param data container with variables.
87  * @param id object id in container.
88  * @param axisP1Id id first axis point.
89  * @param axisP2Id id second axis point.
90  * @param firstPointId id first triangle point, what lies on the hypotenuse.
91  * @param secondPointId id second triangle point, what lies on the hypotenuse.
92  * @param typeCreation way we create this tool.
93  * @param parent parent object.
94  */
95 VToolTriangle::VToolTriangle(VAbstractPattern *doc, VContainer *data, const quint32 &id, const quint32 &axisP1Id,
96  const quint32 &axisP2Id, const quint32 &firstPointId, const quint32 &secondPointId,
97  const Source &typeCreation, QGraphicsItem *parent)
98  : VToolSinglePoint(doc, data, id, QColor(qApp->Settings()->getPointNameColor()), parent)
99  , axisP1Id(axisP1Id)
100  , axisP2Id(axisP2Id)
101  , firstPointId(firstPointId)
102  , secondPointId(secondPointId)
103 {
104  ToolCreation(typeCreation);
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 /**
109  * @brief setDialog set dialog when user want change tool option.
110  */
112 {
113  SCASSERT(not m_dialog.isNull())
114  QSharedPointer<DialogTriangle> dialogTool = m_dialog.objectCast<DialogTriangle>();
115  SCASSERT(not dialogTool.isNull())
117  dialogTool->SetAxisP1Id(axisP1Id);
118  dialogTool->SetAxisP2Id(axisP2Id);
119  dialogTool->SetFirstPointId(firstPointId);
120  dialogTool->SetSecondPointId(secondPointId);
121  dialogTool->SetPointName(p->name());
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
125 /**
126  * @brief Create help create tool from GUI.
127  * @param dialog dialog.
128  * @param scene pointer to scene.
129  * @param doc dom document container.
130  * @param data container with variables.
131  * @return the created tool
132  */
134  VAbstractPattern *doc, VContainer *data)
135 {
136  SCASSERT(not dialog.isNull())
137  QSharedPointer<DialogTriangle> dialogTool = dialog.objectCast<DialogTriangle>();
138  SCASSERT(not dialogTool.isNull())
139  const quint32 axisP1Id = dialogTool->GetAxisP1Id();
140  const quint32 axisP2Id = dialogTool->GetAxisP2Id();
141  const quint32 firstPointId = dialogTool->GetFirstPointId();
142  const quint32 secondPointId = dialogTool->GetSecondPointId();
143  const QString pointName = dialogTool->getPointName();
144  VToolTriangle* point = Create(0, pointName, axisP1Id, axisP2Id, firstPointId, secondPointId, 5, 10,
145  true, scene, doc, data, Document::FullParse, Source::FromGui);
146  if (point != nullptr)
147  {
148  point->m_dialog = dialogTool;
149  }
150  return point;
151 }
152 
153 //---------------------------------------------------------------------------------------------------------------------
154 /**
155  * @brief Create help create tool.
156  * @param _id tool id, 0 if tool doesn't exist yet.
157  * @param pointName point name.
158  * @param axisP1Id id first axis point.
159  * @param axisP2Id id second axis point.
160  * @param firstPointId id first triangle point, what lies on the hypotenuse.
161  * @param secondPointId id second triangle point, what lies on the hypotenuse.
162  * @param mx label bias x axis.
163  * @param my label bias y axis.
164  * @param showPointName show/hide point name text.
165  * @param scene pointer to scene.
166  * @param doc dom document container.
167  * @param data container with variables.
168  * @param parse parser file mode.
169  * @param typeCreation way we create this tool.
170  * @return the created tool
171  */
172 VToolTriangle* VToolTriangle::Create(const quint32 _id, const QString &pointName, quint32 axisP1Id,
173  quint32 axisP2Id, quint32 firstPointId, quint32 secondPointId,
174  qreal mx, qreal my, bool showPointName, VMainGraphicsScene *scene, VAbstractPattern *doc,
175  VContainer *data, const Document &parse, const Source &typeCreation)
176 {
181 
182  QPointF point = FindPoint(static_cast<QPointF>(*axisP1), static_cast<QPointF>(*axisP2),
183  static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*secondPoint));
184  quint32 id = _id;
185  VPointF *p = new VPointF(point, pointName, mx, my);
186  p->setShowPointName(showPointName);
187 
188  if (typeCreation == Source::FromGui)
189  {
190  id = data->AddGObject(p);
191  }
192  else
193  {
194  data->UpdateGObject(id, p);
195  if (parse != Document::FullParse)
196  {
197  doc->UpdateToolData(id, data);
198  }
199  }
200 
201  if (parse == Document::FullParse)
202  {
205  secondPointId, typeCreation);
206  scene->addItem(point);
207  InitToolConnections(scene, point);
208  VAbstractPattern::AddTool(id, point);
209  doc->IncrementReferens(axisP1->getIdTool());
210  doc->IncrementReferens(axisP2->getIdTool());
211  doc->IncrementReferens(firstPoint->getIdTool());
212  doc->IncrementReferens(secondPoint->getIdTool());
213  return point;
214  }
215  return nullptr;
216 }
217 
218 //---------------------------------------------------------------------------------------------------------------------
219 /**
220  * @brief FindPoint find point intersection two foots right triangle.
221  * @param axisP1 first axis point.
222  * @param axisP2 second axis point.
223  * @param firstPoint first triangle point, what lies on the hypotenuse.
224  * @param secondPoint second triangle point, what lies on the hypotenuse.
225  * @return point intersection two foots right triangle.
226  */
227 QPointF VToolTriangle::FindPoint(const QPointF &axisP1, const QPointF &axisP2, const QPointF &firstPoint,
228  const QPointF &secondPoint)
229 {
230  QLineF axis(axisP1, axisP2);
231  QLineF hypotenuse(firstPoint, secondPoint);
232 
233  QPointF startPoint;
234  QLineF::IntersectType intersect = axis.intersects(hypotenuse, &startPoint);
235  if (intersect != QLineF::UnboundedIntersection && intersect != QLineF::BoundedIntersection)
236  {
237  return QPointF();
238  }
239  if (VFuzzyComparePossibleNulls(axis.angle(), hypotenuse.angle())
240  || VFuzzyComparePossibleNulls(qAbs(axis.angle() - hypotenuse.angle()), 180))
241  {
242  return QPointF();
243  }
244 
245  qreal step = 1;
246 
247  QLineF line;
248  line.setP1(startPoint);
249  line.setAngle(axis.angle());
250  line.setLength(step);
251 
252  qint64 c = qFloor(hypotenuse.length());
253  while (1)
254  {
255  line.setLength(line.length()+step);
256  qint64 a = qFloor(QLineF(line.p2(), firstPoint).length());
257  qint64 b = qFloor(QLineF(line.p2(), secondPoint).length());
258  if (c*c <= (a*a + b*b))
259  {
260  return line.p2();
261  }
262  }
263  return QPointF();
264 }
265 
266 //---------------------------------------------------------------------------------------------------------------------
268 {
269  return VAbstractTool::data.GetGObject(axisP1Id)->name();
270 }
271 
272 //---------------------------------------------------------------------------------------------------------------------
274 {
275  return VAbstractTool::data.GetGObject(axisP2Id)->name();
276 }
277 
278 //---------------------------------------------------------------------------------------------------------------------
280 {
282 }
283 
284 //---------------------------------------------------------------------------------------------------------------------
286 {
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
291 /**
292  * @brief RemoveReferens decrement value of reference.
293  */
295 {
296  const auto axisP1 = VAbstractTool::data.GetGObject(axisP1Id);
297  const auto axisP2 = VAbstractTool::data.GetGObject(axisP2Id);
298  const auto firstPoint = VAbstractTool::data.GetGObject(firstPointId);
299  const auto secondPoint = VAbstractTool::data.GetGObject(secondPointId);
300 
301  doc->DecrementReferens(axisP1->getIdTool());
302  doc->DecrementReferens(axisP2->getIdTool());
303  doc->DecrementReferens(firstPoint->getIdTool());
304  doc->DecrementReferens(secondPoint->getIdTool());
305 }
306 
307 //---------------------------------------------------------------------------------------------------------------------
308 /**
309  * @brief contextMenuEvent handle context menu events.
310  * @param event context menu event.
311  */
312 void VToolTriangle::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
313 {
314  try
315  {
316  ContextMenu<DialogTriangle>(event, id);
317  }
318  catch(const VExceptionToolWasDeleted &e)
319  {
320  Q_UNUSED(e)
321  return;//Leave this method immediately!!!
322  }
323 }
324 
325 //---------------------------------------------------------------------------------------------------------------------
326 /**
327  * @brief SaveDialog save options into file after change in dialog.
328  */
329 void VToolTriangle::SaveDialog(QDomElement &domElement)
330 {
331  SCASSERT(not m_dialog.isNull())
332  QSharedPointer<DialogTriangle> dialogTool = m_dialog.objectCast<DialogTriangle>();
333  SCASSERT(not dialogTool.isNull())
334  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
335  doc->SetAttribute(domElement, AttrAxisP1, QString().setNum(dialogTool->GetAxisP1Id()));
336  doc->SetAttribute(domElement, AttrAxisP2, QString().setNum(dialogTool->GetAxisP2Id()));
337  doc->SetAttribute(domElement, AttrFirstPoint, QString().setNum(dialogTool->GetFirstPointId()));
338  doc->SetAttribute(domElement, AttrSecondPoint, QString().setNum(dialogTool->GetSecondPointId()));
339 }
340 
341 //---------------------------------------------------------------------------------------------------------------------
343 {
345 
351 }
352 
353 //---------------------------------------------------------------------------------------------------------------------
354 void VToolTriangle::ReadToolAttributes(const QDomElement &domElement)
355 {
360 }
361 
362 //---------------------------------------------------------------------------------------------------------------------
364 {
365  if (not vis.isNull())
366  {
367  VisToolTriangle * visual = qobject_cast<VisToolTriangle *>(vis);
368  SCASSERT(visual != nullptr)
369 
370  visual->setObject1Id(axisP1Id);
371  visual->setObject2Id(axisP2Id);
374  visual->RefreshGeometry();
375  }
376 }
377 
378 //---------------------------------------------------------------------------------------------------------------------
380 {
381  return secondPointId;
382 }
383 
384 //---------------------------------------------------------------------------------------------------------------------
385 void VToolTriangle::SetSecondPointId(const quint32 &value)
386 {
387  if (value != NULL_ID)
388  {
389  secondPointId = value;
390 
392  SaveOption(obj);
393  }
394 }
395 
396 //---------------------------------------------------------------------------------------------------------------------
398 {
399  ShowToolVisualization<VisToolTriangle>(show);
400 }
401 
402 //---------------------------------------------------------------------------------------------------------------------
404 {
405  return firstPointId;
406 }
407 
408 //---------------------------------------------------------------------------------------------------------------------
409 void VToolTriangle::SetFirstPointId(const quint32 &value)
410 {
411  if (value != NULL_ID)
412  {
413  firstPointId = value;
414 
416  SaveOption(obj);
417  }
418 }
419 
420 //---------------------------------------------------------------------------------------------------------------------
422 {
423  return axisP2Id;
424 }
425 
426 //---------------------------------------------------------------------------------------------------------------------
427 void VToolTriangle::SetAxisP2Id(const quint32 &value)
428 {
429  if (value != NULL_ID)
430  {
431  axisP2Id = value;
432 
434  SaveOption(obj);
435  }
436 }
437 
438 //---------------------------------------------------------------------------------------------------------------------
440 {
441  return axisP1Id;
442 }
443 
444 //---------------------------------------------------------------------------------------------------------------------
445 void VToolTriangle::SetAxisP1Id(const quint32 &value)
446 {
447  if (value != NULL_ID)
448  {
449  axisP1Id = value;
450 
452  SaveOption(obj);
453  }
454 }
The DialogTriangle class dialog for ToolTriangle. Help create point and edit option.
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 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
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
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
The VToolSinglePoint class parent for all tools what create points.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
The VToolTriangle class for tool that find point intersection two foots right triangle (triangle with...
Definition: vtooltriangle.h:75
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
quint32 axisP2Id
axisP2Id id second axis point.
quint32 GetSecondPointId() const
quint32 axisP1Id
axisP1Id id first axis point.
void SetAxisP1Id(const quint32 &value)
quint32 secondPointId
secondPointId id second triangle point, what lies on the hypotenuse.
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void SetVisualization() Q_DECL_OVERRIDE
static const QString ToolType
Definition: vtooltriangle.h:90
QString SecondPointName() const
QString AxisP2Name() const
quint32 firstPointId
firstPointId id first triangle point, what lies on the hypotenuse.
void SetSecondPointId(const quint32 &value)
static VToolTriangle * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
static QPointF FindPoint(const QPointF &axisP1, const QPointF &axisP2, const QPointF &firstPoint, const QPointF &secondPoint)
FindPoint find point intersection two foots right triangle.
quint32 GetAxisP2Id() const
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
QString FirstPointName() const
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void SetAxisP2Id(const quint32 &value)
quint32 GetAxisP1Id() const
quint32 GetFirstPointId() const
VToolTriangle(VAbstractPattern *doc, VContainer *data, const quint32 &id, const quint32 &axisP1Id, const quint32 &axisP2Id, const quint32 &firstPointId, const quint32 &secondPointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolTriangle constructor.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
void SetFirstPointId(const quint32 &value)
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
QString AxisP1Name() const
void setObject2Id(const quint32 &value)
void setHypotenuseP1Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setHypotenuseP2Id(const quint32 &value)
void setObject1Id(const quint32 &value)
#define SCASSERT(cond)
Definition: def.h:317
static Q_REQUIRED_RESULT bool VFuzzyComparePossibleNulls(double p1, double p2)
Definition: def.h:490
@ Triangle
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrSecondPoint
Definition: ifcdef.cpp:97
const QString AttrFirstPoint
Definition: ifcdef.cpp:96
const QString AttrAxisP2
Definition: ifcdef.cpp:126
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrAxisP1
Definition: ifcdef.cpp:125
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67