Seamly2D
Code documentation
vtoolpointfromarcandtangent.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 * @file vtoolpointfromarcandtangent.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 vtoolpointfromarcandtangent.cpp
31  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
32  ** @date 6 6, 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/dialogpointfromarcandtangent.h"
71 #include "../../../../visualization/visualization.h"
72 #include "../../../../visualization/line/vistoolpointfromarcandtangent.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 VToolPointFromArcAndTangent::ToolType = QStringLiteral("pointFromArcAndTangent");
85 
86 //---------------------------------------------------------------------------------------------------------------------
88  quint32 arcId, quint32 tangentPointId,
89  CrossCirclesPoint crossPoint, const Source &typeCreation,
90  QGraphicsItem *parent)
91  : VToolSinglePoint(doc, data, id, QColor(qApp->Settings()->getPointNameColor()), parent)
92  , arcId(arcId)
93  , tangentPointId(tangentPointId)
94  , crossPoint(crossPoint)
95 {
96  ToolCreation(typeCreation);
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
101 {
102  SCASSERT(not m_dialog.isNull())
104  SCASSERT(not dialogTool.isNull())
106  dialogTool->SetArcId(arcId);
107  dialogTool->setCirclesCrossPoint(crossPoint);
108  dialogTool->SetTangentPointId(tangentPointId);
109  dialogTool->SetPointName(p->name());
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
114  VMainGraphicsScene *scene,
115  VAbstractPattern *doc, VContainer *data)
116 {
117  SCASSERT(not dialog.isNull())
119  SCASSERT(not dialogTool.isNull())
120  const quint32 arcId = dialogTool->GetArcId();
121  const quint32 tangentPointId = dialogTool->GetTangentPointId();
122  const CrossCirclesPoint pType = dialogTool->GetCrossCirclesPoint();
123  const QString pointName = dialogTool->getPointName();
124  VToolPointFromArcAndTangent *point = Create(0, pointName, arcId, tangentPointId, pType, 5, 10, true, scene, doc,
126  if (point != nullptr)
127  {
128  point->m_dialog = dialogTool;
129  }
130  return point;
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135  const QString &pointName,
136  quint32 arcId,
137  quint32 tangentPointId,
138  CrossCirclesPoint crossPoint,
139  qreal mx, qreal my,
140  bool showPointName,
141  VMainGraphicsScene *scene,
142  VAbstractPattern *doc,
143  VContainer *data,
144  const Document &parse,
145  const Source &typeCreation)
146 {
147  const VArc arc = *data->GeometricObject<VArc>(arcId);
149 
150  const QPointF point = VToolPointFromArcAndTangent::FindPoint(static_cast<QPointF>(tPoint), &arc, crossPoint);
151 
152  if (point == QPointF())
153  {
154  const QString msg = tr("<b><big>Can't find intersection point %1 of</big></b><br>"
155  "<b><big>%2 and Tangent</big></b><br><br>"
156  "Using origin point as a place holder until pattern is corrected.")
157  .arg(pointName)
158  .arg(arc.name());
159 
160  QMessageBox msgBox(qApp->getMainWindow());
161  msgBox.setWindowTitle(tr("Intersect Arc and Tangent"));
162  msgBox.setWindowFlags(msgBox.windowFlags() & ~Qt::WindowContextHelpButtonHint);
163  msgBox.setWindowIcon(QIcon(":/toolicon/32x32/point_from_arc_and_tangent.png"));
164  msgBox.setIcon(QMessageBox::Warning);
165  msgBox.setText(msg);
166  msgBox.setStandardButtons(QMessageBox::Ok);
167  msgBox.exec();
168  }
169 
170  quint32 id = _id;
171 
172  VPointF *p = new VPointF(point, pointName, mx, my);
173  p->setShowPointName(showPointName);
174 
175  if (typeCreation == Source::FromGui)
176  {
177  id = data->AddGObject(p);
178  }
179  else
180  {
181  data->UpdateGObject(id, p);
182  if (parse != Document::FullParse)
183  {
184  doc->UpdateToolData(id, data);
185  }
186  }
187 
188  if (parse == Document::FullParse)
189  {
192  crossPoint, typeCreation);
193 
194  scene->addItem(point);
195  InitToolConnections(scene, point);
196  VAbstractPattern::AddTool(id, point);
198  doc->IncrementReferens(tPoint.getIdTool());
199  return point;
200  }
201  return nullptr;
202 }
203 
204 //---------------------------------------------------------------------------------------------------------------------
205 QPointF VToolPointFromArcAndTangent::FindPoint(const QPointF &p, const VArc *arc, const CrossCirclesPoint pType)
206 {
207  QPointF p1, p2;
208  const QPointF center = static_cast<QPointF>(arc->GetCenter());
209  const qreal radius = arc->GetRadius();
210  const int res = VGObject::ContactPoints (p, center, radius, p1, p2);
211 
212  QLineF r1Arc(center, p1);
213  r1Arc.setLength(radius+10);
214 
215  QLineF r2Arc(center, p2);
216  r2Arc.setLength(radius+10);
217 
218  switch(res)
219  {
220  case 2:
221  {
222  int localRes = 0;
223  bool flagP1 = false;
224 
225  if (arc->IsIntersectLine(r1Arc))
226  {
227  ++localRes;
228  flagP1 = true;
229  }
230 
231  if (arc->IsIntersectLine(r2Arc))
232  {
233  ++localRes;
234  }
235 
236  switch(localRes)
237  {
238  case 2:
239  if (pType == CrossCirclesPoint::FirstPoint)
240  {
241  return p1;
242  }
243  else
244  {
245  return p2;
246  }
247  case 1:
248  if (flagP1)
249  {
250  return p1;
251  }
252  else
253  {
254  return p2;
255  }
256  case 0:
257  default:
258  return QPointF();
259  }
260 
261  break;
262  }
263  case 1:
264  if (arc->IsIntersectLine(r1Arc))
265  {
266  return p1;
267  }
268  else
269  {
270  return QPointF();
271  }
272  case 3:
273  case 0:
274  default:
275  break;
276  }
277  return QPointF();
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
282 {
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
288 {
289  return VAbstractTool::data.GetGObject(arcId)->name();
290 }
291 
292 //---------------------------------------------------------------------------------------------------------------------
294 {
295  return tangentPointId;
296 }
297 
298 //---------------------------------------------------------------------------------------------------------------------
300 {
301  if (value != NULL_ID)
302  {
303  tangentPointId = value;
304 
306  SaveOption(obj);
307  }
308 }
309 
310 //---------------------------------------------------------------------------------------------------------------------
312 {
313  return arcId;
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
317 void VToolPointFromArcAndTangent::SetArcId(const quint32 &value)
318 {
319  if (value != NULL_ID)
320  {
321  arcId = value;
322 
324  SaveOption(obj);
325  }
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
331  return crossPoint;
332 }
333 
334 //---------------------------------------------------------------------------------------------------------------------
336 {
337  crossPoint = value;
338 
340  SaveOption(obj);
341 }
342 
343 //---------------------------------------------------------------------------------------------------------------------
345 {
346  ShowToolVisualization<VisToolPointFromArcAndTangent>(show);
347 }
348 
349 //---------------------------------------------------------------------------------------------------------------------
351 {
352  const auto arc = VAbstractTool::data.GetGObject(arcId);
354 
355  doc->DecrementReferens(arc->getIdTool());
356  doc->DecrementReferens(tP->getIdTool());
357 }
358 
359 //---------------------------------------------------------------------------------------------------------------------
360 void VToolPointFromArcAndTangent::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
361 {
362  try
363  {
364  ContextMenu<DialogPointFromArcAndTangent>(event, id);
365  }
366  catch(const VExceptionToolWasDeleted &e)
367  {
368  Q_UNUSED(e)
369  return;//Leave this method immediately!!!
370  }
371 }
372 
373 //---------------------------------------------------------------------------------------------------------------------
374 void VToolPointFromArcAndTangent::SaveDialog(QDomElement &domElement)
375 {
376  SCASSERT(not m_dialog.isNull())
378  SCASSERT(not dialogTool.isNull())
379  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
380  doc->SetAttribute(domElement, AttrArc, QString().setNum(dialogTool->GetArcId()));
381  doc->SetAttribute(domElement, AttrTangent, QString().setNum(dialogTool->GetTangentPointId()));
382  doc->SetAttribute(domElement, AttrCrossPoint,
383  QString().setNum(static_cast<int>(dialogTool->GetCrossCirclesPoint())));
384 }
385 
386 //---------------------------------------------------------------------------------------------------------------------
388 {
390 
392  doc->SetAttribute(tag, AttrArc, arcId);
394  doc->SetAttribute(tag, AttrCrossPoint, static_cast<int>(crossPoint));
395 }
396 
397 //---------------------------------------------------------------------------------------------------------------------
398 void VToolPointFromArcAndTangent::ReadToolAttributes(const QDomElement &domElement)
399 {
400  arcId = doc->GetParametrUInt(domElement, AttrArc, NULL_ID_STR);
402  crossPoint = static_cast<CrossCirclesPoint>(doc->GetParametrUInt(domElement, AttrCrossPoint, "1"));
403 }
404 
405 //---------------------------------------------------------------------------------------------------------------------
407 {
408  if (not vis.isNull())
409  {
410  VisToolPointFromArcAndTangent *visual = qobject_cast<VisToolPointFromArcAndTangent *>(vis);
411  SCASSERT(visual != nullptr)
412 
413  visual->setObject1Id(tangentPointId);
414  visual->setArcId(arcId);
415  visual->setCrossPoint(crossPoint);
416  visual->RefreshGeometry();
417  }
418 }
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
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
static int ContactPoints(const QPointF &p, const QPointF &center, qreal radius, QPointF &p1, QPointF &p2)
Definition: vgobject.cpp:286
quint32 getIdTool() const
Definition: vgobject.cpp:221
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 setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void setCirclesCrossPoint(const CrossCirclesPoint &value)
static VToolPointFromArcAndTangent * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void SetTangentPointId(const quint32 &value)
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void SetVisualization() Q_DECL_OVERRIDE
CrossCirclesPoint GetCrossCirclesPoint() const
VToolPointFromArcAndTangent(VAbstractPattern *doc, VContainer *data, const quint32 &id, quint32 arcId, quint32 tangentPointId, CrossCirclesPoint crossPoint, const Source &typeCreation, QGraphicsItem *parent=nullptr)
static QPointF FindPoint(const QPointF &p, const VArc *arc, const CrossCirclesPoint pType)
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
The VToolSinglePoint class parent for all tools what create points.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setCrossPoint(const CrossCirclesPoint &value)
void setObject1Id(const quint32 &value)
#define SCASSERT(cond)
Definition: def.h:317
@ PointFromArcAndTangent
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrCrossPoint
Definition: ifcdef.cpp:135
const QString AttrTangent
Definition: ifcdef.cpp:144
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrArc
Definition: ifcdef.cpp:146
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
CrossCirclesPoint
#define qApp
Definition: vapplication.h:67