Seamly2D
Code documentation
vtoolpointofintersectionarcs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 * @file vtoolpointofintersectionarcs.cpp
3 ** @author Douglas S Caskey
4 ** @date 30 Apr, 2023
5 **
6 ** @brief
7 ** @copyright
8 ** This source code is part of the Seamly2D project, a pattern making
9 ** program to create and model patterns of clothing.
10 ** Copyright (C) 2017-2023 Seamly2D project
11 ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
12 **
13 ** Seamly2D is free software: you can redistribute it and/or modify
14 ** it under the terms of the GNU General Public License as published by
15 ** the Free Software Foundation, either version 3 of the License, or
16 ** (at your option) any later version.
17 **
18 ** Seamly2D is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
22 **
23 ** You should have received a copy of the GNU General Public License
24 ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
25 **
26 *************************************************************************/
27 
28 /************************************************************************
29  **
30  ** @file vtoolpointofintersectionarcs.cpp
31  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
32  ** @date 25 5, 2015
33  **
34  ** @brief
35  ** @copyright
36  ** This source code is part of the Valentine project, a pattern making
37  ** program, whose allow create and modeling patterns of clothing.
38  ** Copyright (C) 2015 Seamly2D project
39  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
40  **
41  ** Seamly2D is free software: you can redistribute it and/or modify
42  ** it under the terms of the GNU General Public License as published by
43  ** the Free Software Foundation, either version 3 of the License, or
44  ** (at your option) any later version.
45  **
46  ** Seamly2D is distributed in the hope that it will be useful,
47  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
48  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49  ** GNU General Public License for more details.
50  **
51  ** You should have received a copy of the GNU General Public License
52  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
53  **
54  *************************************************************************/
55 
57 
58 #include "vtoolsinglepoint.h"
59 #include "../ifc/ifcdef.h"
60 #include "../ifc/exception/vexception.h"
61 #include "../vgeometry/varc.h"
62 #include "../vgeometry/vgobject.h"
63 #include "../vgeometry/vpointf.h"
64 #include "../vmisc/vabstractapplication.h"
65 #include "../vpatterndb/vcontainer.h"
66 #include "../vwidgets/vmaingraphicsscene.h"
67 #include "../../vdrawtool.h"
68 #include "../../../vabstracttool.h"
69 #include "../../../../dialogs/tools/dialogtool.h"
70 #include "../../../../dialogs/tools/dialogpointofintersectionarcs.h"
71 #include "../../../../visualization/visualization.h"
72 #include "../../../../visualization/line/vistoolpointofintersectionarcs.h"
73 
74 #include <QLineF>
75 #include <QMessageBox>
76 #include <QSharedPointer>
77 #include <QStaticStringData>
78 #include <QStringData>
79 #include <QStringDataPtr>
80 #include <new>
81 
82 template <class T> class QSharedPointer;
83 
84 const QString VToolPointOfIntersectionArcs::ToolType = QStringLiteral("pointOfIntersectionArcs");
85 
86 //---------------------------------------------------------------------------------------------------------------------
88  const quint32 &firstArcId, const quint32 &secondArcId,
89  CrossCirclesPoint pType, const Source &typeCreation,
90  QGraphicsItem *parent)
91  : VToolSinglePoint(doc, data, id, QColor(qApp->Settings()->getPointNameColor()), parent)
92  , firstArcId(firstArcId)
93  , secondArcId(secondArcId)
94  , crossPoint(pType)
95 {
96  ToolCreation(typeCreation);
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
101 {
102  SCASSERT(not m_dialog.isNull())
104  SCASSERT(not dialogTool.isNull())
106  dialogTool->SetFirstArcId(firstArcId);
107  dialogTool->SetSecondArcId(secondArcId);
108  dialogTool->SetCrossArcPoint(crossPoint);
109  dialogTool->SetPointName(p->name());
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
115  VContainer *data)
116 {
117  SCASSERT(not dialog.isNull())
119  SCASSERT(not dialogTool.isNull())
120  const quint32 firstArcId = dialogTool->GetFirstArcId();
121  const quint32 secondArcId = dialogTool->GetSecondArcId();
122  const CrossCirclesPoint pType = dialogTool->GetCrossArcPoint();
123  const QString pointName = dialogTool->getPointName();
124  VToolPointOfIntersectionArcs *point = Create(0, pointName, firstArcId, secondArcId, pType, 5, 10, scene, doc,
126  if (point != nullptr)
127  {
128  point->m_dialog = dialogTool;
129  }
130  return point;
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
134 VToolPointOfIntersectionArcs *VToolPointOfIntersectionArcs::Create(const quint32 _id, const QString &pointName,
135  const quint32 &firstArcId,
136  const quint32 &secondArcId, CrossCirclesPoint pType,
137  qreal mx, qreal my,
139  VContainer *data, const Document &parse,
140  const Source &typeCreation)
141 {
144 
145  const QPointF point = FindPoint(firstArc.data(), secondArc.data(), pType);
146 
147  if (point == QPointF())
148  {
149  const QString msg = tr("<b><big>Can't find intersection point %1 of Arcs</big></b><br>"
150  "Using origin point as a place holder until pattern is corrected.")
151  .arg(pointName);
152 
153  QMessageBox msgBox(qApp->getMainWindow());
154  msgBox.setWindowTitle(tr("Point Intersect Arcs"));
155  msgBox.setWindowFlags(msgBox.windowFlags() & ~Qt::WindowContextHelpButtonHint);
156  msgBox.setWindowIcon(QIcon(":/toolicon/32x32/point_of_intersection_arcs.png"));
157  msgBox.setIcon(QMessageBox::Warning);
158  msgBox.setText(msg);
159  msgBox.setStandardButtons(QMessageBox::Ok);
160  msgBox.exec();
161  }
162 
163  quint32 id = _id;
164  if (typeCreation == Source::FromGui)
165  {
166  id = data->AddGObject(new VPointF(point, pointName, mx, my));
167  }
168  else
169  {
170  data->UpdateGObject(id, new VPointF(point, pointName, mx, my));
171  if (parse != Document::FullParse)
172  {
173  doc->UpdateToolData(id, data);
174  }
175  }
176 
177  if (parse == Document::FullParse)
178  {
181  secondArcId, pType, typeCreation);
182  scene->addItem(point);
183  InitToolConnections(scene, point);
184  VAbstractPattern::AddTool(id, point);
185  doc->IncrementReferens(firstArc->getIdTool());
186  doc->IncrementReferens(secondArc->getIdTool());
187  return point;
188  }
189  return nullptr;
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
193 QPointF VToolPointOfIntersectionArcs::FindPoint(const VArc *arc1, const VArc *arc2, const CrossCirclesPoint pType)
194 {
195  QPointF p1, p2;
196  const QPointF centerArc1 = static_cast<QPointF>(arc1->GetCenter());
197  const QPointF centerArc2 = static_cast<QPointF>(arc2->GetCenter());
198  const int res = VGObject::IntersectionCircles(centerArc1, arc1->GetRadius(), centerArc2, arc2->GetRadius(), p1, p2);
199 
200  QLineF r1Arc1(centerArc1, p1);
201  r1Arc1.setLength(r1Arc1.length()+10);
202 
203  QLineF r1Arc2(centerArc2, p1);
204  r1Arc2.setLength(r1Arc2.length()+10);
205 
206  QLineF r2Arc1(centerArc1, p2);
207  r2Arc1.setLength(r2Arc1.length()+10);
208 
209  QLineF r2Arc2(centerArc2, p2);
210  r2Arc2.setLength(r2Arc2.length()+10);
211 
212  switch(res)
213  {
214  case 2:
215  {
216  int localRes = 0;
217  bool flagP1 = false;
218 
219  if (arc1->IsIntersectLine(r1Arc1) && arc2->IsIntersectLine(r1Arc2))
220  {
221  ++localRes;
222  flagP1 = true;
223  }
224 
225  if (arc1->IsIntersectLine(r2Arc1) && arc2->IsIntersectLine(r2Arc2))
226  {
227  ++localRes;
228  }
229 
230  switch(localRes)
231  {
232  case 2:
233  if (pType == CrossCirclesPoint::FirstPoint)
234  {
235  return p1;
236  }
237  else
238  {
239  return p2;
240  }
241  case 1:
242  if (flagP1)
243  {
244  return p1;
245  }
246  else
247  {
248  return p2;
249  }
250  case 0:
251  default:
252  return QPointF();
253  }
254 
255  break;
256  }
257  case 1:
258  if (arc1->IsIntersectLine(r1Arc1) && arc2->IsIntersectLine(r1Arc2))
259  {
260  return p1;
261  }
262  else
263  {
264  return QPointF();
265  }
266  case 3:
267  case 0:
268  default:
269  break;
270  }
271  return QPointF();
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
276 {
277  return VAbstractTool::data.GetGObject(firstArcId)->name();
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
282 {
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
288 {
289  return firstArcId;
290 }
291 
292 //---------------------------------------------------------------------------------------------------------------------
294 {
295  if (value != NULL_ID)
296  {
297  firstArcId = value;
298 
300  SaveOption(obj);
301  }
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
306 {
307  return secondArcId;
308 }
309 
310 //---------------------------------------------------------------------------------------------------------------------
312 {
313  if (value != NULL_ID)
314  {
315  secondArcId = value;
316 
318  SaveOption(obj);
319  }
320 }
321 
322 //---------------------------------------------------------------------------------------------------------------------
324 {
325  return crossPoint;
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
331  crossPoint = value;
332 
334  SaveOption(obj);
335 }
336 
337 //---------------------------------------------------------------------------------------------------------------------
339 {
340  ShowToolVisualization<VisToolPointOfIntersectionArcs>(show);
341 }
342 
343 //---------------------------------------------------------------------------------------------------------------------
345 {
346  const auto firstArc = VAbstractTool::data.GetGObject(firstArcId);
347  const auto secondArc = VAbstractTool::data.GetGObject(secondArcId);
348 
349  doc->DecrementReferens(firstArc->getIdTool());
350  doc->DecrementReferens(secondArc->getIdTool());
351 }
352 
353 //---------------------------------------------------------------------------------------------------------------------
354 void VToolPointOfIntersectionArcs::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
355 {
356  try
357  {
358  ContextMenu<DialogPointOfIntersectionArcs>(event, id);
359  }
360  catch(const VExceptionToolWasDeleted &e)
361  {
362  Q_UNUSED(e)
363  return;//Leave this method immediately!!!
364  }
365 }
366 
367 //---------------------------------------------------------------------------------------------------------------------
368 void VToolPointOfIntersectionArcs::SaveDialog(QDomElement &domElement)
369 {
370  SCASSERT(not m_dialog.isNull())
372  SCASSERT(not dialogTool.isNull())
373  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
374  doc->SetAttribute(domElement, AttrFirstArc, QString().setNum(dialogTool->GetFirstArcId()));
375  doc->SetAttribute(domElement, AttrSecondArc, QString().setNum(dialogTool->GetSecondArcId()));
376  doc->SetAttribute(domElement, AttrCrossPoint, QString().setNum(static_cast<int>(dialogTool->GetCrossArcPoint())));
377 }
378 
379 //---------------------------------------------------------------------------------------------------------------------
381 {
383 
387  doc->SetAttribute(tag, AttrCrossPoint, static_cast<int>(crossPoint));
388 }
389 
390 //---------------------------------------------------------------------------------------------------------------------
391 void VToolPointOfIntersectionArcs::ReadToolAttributes(const QDomElement &domElement)
392 {
395  crossPoint = static_cast<CrossCirclesPoint>(doc->GetParametrUInt(domElement, AttrCrossPoint, "1"));
396 }
397 
398 //---------------------------------------------------------------------------------------------------------------------
400 {
401  if (not vis.isNull())
402  {
403  VisToolPointOfIntersectionArcs *visual = qobject_cast<VisToolPointOfIntersectionArcs *>(vis);
404  SCASSERT(visual != nullptr)
405 
406  visual->setArc1Id(firstArcId);
407  visual->setArc2Id(secondArcId);
408  visual->setCrossPoint(crossPoint);
409  visual->RefreshGeometry();
410  }
411 }
virtual VPointF GetCenter() const
virtual bool IsIntersectLine(const QLineF &line) 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
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.
VArc class for anticlockwise arc.
Definition: varc.h:74
qreal GetRadius() const
GetRadius return formula for radius.
Definition: varc.cpp:482
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
static int IntersectionCircles(const QPointF &c1, double r1, const QPointF &c2, double r2, QPointF &p1, QPointF &p2)
Definition: vgobject.cpp:342
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 VToolPointOfIntersectionArcs * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
void setCirclesCrossPoint(const CrossCirclesPoint &value)
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
static QPointF FindPoint(const VArc *arc1, const VArc *arc2, const CrossCirclesPoint pType)
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
VToolPointOfIntersectionArcs(VAbstractPattern *doc, VContainer *data, const quint32 &id, const quint32 &firstArcId, const quint32 &secondArcId, CrossCirclesPoint pType, const Source &typeCreation, QGraphicsItem *parent=nullptr)
quint32 secondArcId
secondArcId id second arc.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
quint32 firstArcId
firstArcId id first arc.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
CrossCirclesPoint GetCrossCirclesPoint() const
The VToolSinglePoint class parent for all tools what create points.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setCrossPoint(const CrossCirclesPoint &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
#define SCASSERT(cond)
Definition: def.h:317
@ PointOfIntersectionArcs
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrSecondArc
Definition: ifcdef.cpp:134
const QString AttrFirstArc
Definition: ifcdef.cpp:133
const QString AttrCrossPoint
Definition: ifcdef.cpp:135
const QString AttrType
Definition: ifcdef.cpp:73
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
CrossCirclesPoint
#define qApp
Definition: vapplication.h:67