Seamly2D
Code documentation
vtoolpointofcontact.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 vtoolpointofcontact.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 "vtoolpointofcontact.h"
53 
54 #include <QLineF>
55 #include <QMessageLogger>
56 #include <QSharedPointer>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <QtDebug>
61 #include <new>
62 
63 #include "../../../../dialogs/tools/dialogpointofcontact.h"
64 #include "../../../../dialogs/tools/dialogtool.h"
65 #include "../../../../visualization/visualization.h"
66 #include "../../../../visualization/line/vistoolpointofcontact.h"
67 #include "../ifc/exception/vexception.h"
68 #include "../ifc/xml/vdomdocument.h"
69 #include "../ifc/ifcdef.h"
70 #include "../vgeometry/vgobject.h"
71 #include "../vgeometry/vpointf.h"
72 #include "../vmisc/vabstractapplication.h"
73 #include "../vmisc/vcommonsettings.h"
74 #include "../vpatterndb/vcontainer.h"
75 #include "../vpatterndb/vformula.h"
76 #include "../vpatterndb/vtranslatevars.h"
77 #include "../vwidgets/vmaingraphicsscene.h"
78 #include "../../../vabstracttool.h"
79 #include "../../vdrawtool.h"
80 #include "vtoolsinglepoint.h"
81 
82 template <class T> class QSharedPointer;
83 
84 const QString VToolPointOfContact::ToolType = QStringLiteral("pointOfContact");
85 
86 //---------------------------------------------------------------------------------------------------------------------
87 /**
88  * @brief VToolPointOfContact constructor.
89  * @param doc dom document container.
90  * @param data container with variables.
91  * @param id object id in container.
92  * @param radius string with formula radius arc.
93  * @param center id center arc point.
94  * @param firstPointId id first line point.
95  * @param secondPointId id second line point.
96  * @param typeCreation way we create this tool.
97  * @param parent parent object.
98  */
100  const QString &radius, const quint32 &center,
101  const quint32 &firstPointId, const quint32 &secondPointId,
102  const Source &typeCreation, QGraphicsItem *parent)
103  : VToolSinglePoint(doc, data, id, QColor(qApp->Settings()->getPointNameColor()), parent)
104  , arcRadius(radius)
105  , center(center)
106  , firstPointId(firstPointId)
107  , secondPointId(secondPointId)
108 {
109  ToolCreation(typeCreation);
110 }
111 
112 //---------------------------------------------------------------------------------------------------------------------
113 /**
114  * @brief setDialog set dialog when user want change tool option.
115  */
117 {
118  SCASSERT(not m_dialog.isNull())
120  SCASSERT(not dialogTool.isNull())
122  dialogTool->setRadius(arcRadius);
123  dialogTool->setCenter(center);
124  dialogTool->SetFirstPoint(firstPointId);
125  dialogTool->SetSecondPoint(secondPointId);
126  dialogTool->SetPointName(p->name());
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
130 /**
131  * @brief FindPoint return point intersection arc and line.
132  * @param radius string with formula arc radius.
133  * @param center center arc point.
134  * @param firstPoint first line point.
135  * @param secondPoint second line point.
136  * @return point intersection.
137  */
138 QPointF VToolPointOfContact::FindPoint(const qreal &radius, const QPointF &center, const QPointF &firstPoint,
139  const QPointF &secondPoint)
140 {
141  QPointF p1, p2;
142  qint32 res = VGObject::LineIntersectCircle(center, radius, QLineF(firstPoint, secondPoint), p1, p2);
143  switch (res)
144  {
145  case 0:
146  return QPointF();
147  case 1:
148  return p1;
149  case 2:
150  {
151  const bool flagP1 = VGObject::IsPointOnLineSegment (p1, firstPoint, secondPoint);
152  const bool flagP2 = VGObject::IsPointOnLineSegment (p2, firstPoint, secondPoint);
153  if ((flagP1 == true && flagP2 == true) ||
154  (flagP1 == false && flagP2 == false)/*In case we have something wrong*/)
155  {
156  // We don't have options for choosing correct point. Use closest to segment first point.
157  if (QLineF(firstPoint, p1).length() <= QLineF(firstPoint, p2).length())
158  {
159  return p1;
160  }
161  else
162  {
163  return p2;
164  }
165  }
166  else
167  { // In this case we have one real and one theoretical intersection.
168  if (flagP1)
169  {
170  return p1;
171  }
172  else
173  {
174  return p2;
175  }
176  }
177  }
178  default:
179  qDebug() << "Unxpected value" << res;
180  break;
181  }
182  return QPointF();
183 }
184 
185 //---------------------------------------------------------------------------------------------------------------------
186 /**
187  * @brief Create help create tool from GUI.
188  * @param dialog dialog.
189  * @param scene pointer to scene.
190  * @param doc dom document container.
191  * @param data container with variables.
192  */
194  VAbstractPattern *doc, VContainer *data)
195 {
196  SCASSERT(not dialog.isNull())
197  QSharedPointer<DialogPointOfContact> dialogTool = dialog.objectCast<DialogPointOfContact>();
198  SCASSERT(not dialogTool.isNull())
199  QString radius = dialogTool->getRadius();
200  const quint32 center = dialogTool->getCenter();
201  const quint32 firstPointId = dialogTool->GetFirstPoint();
202  const quint32 secondPointId = dialogTool->GetSecondPoint();
203  const QString pointName = dialogTool->getPointName();
204  VToolPointOfContact *point = Create(0, radius, center, firstPointId, secondPointId, pointName, 5, 10, true, scene, doc,
206  if (point != nullptr)
207  {
208  point->m_dialog = dialogTool;
209  }
210  return point;
211 }
212 
213 //---------------------------------------------------------------------------------------------------------------------
214 /**
215  * @brief Create help create tool.
216  * @param _id tool id, 0 if tool doesn't exist yet.
217  * @param radius string with formula radius arc.
218  * @param center id center arc point.
219  * @param firstPointId id first line point.
220  * @param secondPointId id second line point.
221  * @param pointName point name.
222  * @param mx label bias x axis.
223  * @param my label bias y axis.
224  * @param scene pointer to scene.
225  * @param doc dom document container.
226  * @param data container with variables.
227  * @param parse parser file mode.
228  * @param typeCreation way we create this tool.
229  */
230 VToolPointOfContact* VToolPointOfContact::Create(const quint32 _id, QString &radius, const quint32 &center,
231  const quint32 &firstPointId, const quint32 &secondPointId,
232  const QString &pointName, qreal mx, qreal my, bool showPointName,
233  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
234  const Document &parse, const Source &typeCreation)
235 {
239 
240  const qreal result = CheckFormula(_id, radius, data);
241 
242  QPointF fPoint = VToolPointOfContact::FindPoint(qApp->toPixel(result), static_cast<QPointF>(*centerP),
243  static_cast<QPointF>(*firstP), static_cast<QPointF>(*secondP));
244  quint32 id = _id;
245 
246  VPointF *p = new VPointF(fPoint, pointName, mx, my);
247  p->setShowPointName(showPointName);
248 
249  if (typeCreation == Source::FromGui)
250  {
251  id = data->AddGObject(p);
252  data->AddLine(firstPointId, id);
253  data->AddLine(secondPointId, id);
254  data->AddLine(center, id);
255  }
256  else
257  {
258  data->UpdateGObject(id, p);
259  data->AddLine(firstPointId, id);
260  data->AddLine(secondPointId, id);
261  data->AddLine(center, id);
262  if (parse != Document::FullParse)
263  {
264  doc->UpdateToolData(id, data);
265  }
266  }
267 
268  if (parse == Document::FullParse)
269  {
271  VToolPointOfContact *point = new VToolPointOfContact(doc, data, id, radius, center,
272  firstPointId, secondPointId, typeCreation);
273  scene->addItem(point);
274  InitToolConnections(scene, point);
275  VAbstractPattern::AddTool(id, point);
276  doc->IncrementReferens(centerP->getIdTool());
277  doc->IncrementReferens(firstP->getIdTool());
278  doc->IncrementReferens(secondP->getIdTool());
279  return point;
280  }
281  return nullptr;
282 }
283 
284 //---------------------------------------------------------------------------------------------------------------------
286 {
287  return VAbstractTool::data.GetGObject(center)->name();
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
292 {
294 }
295 
296 //---------------------------------------------------------------------------------------------------------------------
298 {
300 }
301 
302 //---------------------------------------------------------------------------------------------------------------------
303 /**
304  * @brief contextMenuEvent handle context menu events.
305  * @param event context menu event.
306  */
307 void VToolPointOfContact::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
308 {
309  try
310  {
311  ContextMenu<DialogPointOfContact>(event, id);
312  }
313  catch(const VExceptionToolWasDeleted &e)
314  {
315  Q_UNUSED(e)
316  return;//Leave this method immediately!!!
317  }
318 }
319 
320 //---------------------------------------------------------------------------------------------------------------------
321 /**
322  * @brief RemoveReferens decrement value of reference.
323  */
325 {
326  const auto c = VAbstractTool::data.GetGObject(center);
327  const auto firstPoint = VAbstractTool::data.GetGObject(firstPointId);
328  const auto secondPoint = VAbstractTool::data.GetGObject(secondPointId);
329 
330  doc->DecrementReferens(c->getIdTool());
331  doc->DecrementReferens(firstPoint->getIdTool());
332  doc->DecrementReferens(secondPoint->getIdTool());
333 }
334 
335 //---------------------------------------------------------------------------------------------------------------------
336 /**
337  * @brief SaveDialog save options into file after change in dialog.
338  */
339 void VToolPointOfContact::SaveDialog(QDomElement &domElement)
340 {
341  SCASSERT(not m_dialog.isNull())
343  SCASSERT(not dialogTool.isNull())
344  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
345  doc->SetAttribute(domElement, AttrRadius, dialogTool->getRadius());
346  doc->SetAttribute(domElement, AttrCenter, QString().setNum(dialogTool->getCenter()));
347  doc->SetAttribute(domElement, AttrFirstPoint, QString().setNum(dialogTool->GetFirstPoint()));
348  doc->SetAttribute(domElement, AttrSecondPoint, QString().setNum(dialogTool->GetSecondPoint()));
349 }
350 
351 //---------------------------------------------------------------------------------------------------------------------
353 {
355 
361 }
362 
363 //---------------------------------------------------------------------------------------------------------------------
364 void VToolPointOfContact::ReadToolAttributes(const QDomElement &domElement)
365 {
366  arcRadius = doc->GetParametrString(domElement, AttrRadius, "");
370 }
371 
372 //---------------------------------------------------------------------------------------------------------------------
374 {
375  if (not vis.isNull())
376  {
377  VisToolPointOfContact *visual = qobject_cast<VisToolPointOfContact *>(vis);
378  SCASSERT(visual != nullptr)
379 
380  visual->setObject1Id(firstPointId);
381  visual->setLineP2Id(secondPointId);
382  visual->setRadiusId(center);
383  visual->setRadius(qApp->TrVars()->FormulaToUser(arcRadius, qApp->Settings()->GetOsSeparator()));
384  visual->RefreshGeometry();
385  }
386 }
387 
388 //---------------------------------------------------------------------------------------------------------------------
390 {
395 
396  const QLineF p1ToCur(static_cast<QPointF>(*p1), static_cast<QPointF>(*current));
397  const QLineF p2ToCur(static_cast<QPointF>(*p2), static_cast<QPointF>(*current));
398  const QLineF centerToCur(static_cast<QPointF>(*centerP), static_cast<QPointF>(*current));
399 
400  const QString toolTip = QString("<table>"
401  "<tr> <td><b>%10:</b> %11</td> </tr>"
402  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
403  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
404  "<tr> <td><b>%6:</b> %7 %3</td> </tr>"
405  "<tr> <td><b>%8:</b> %9°</td> </tr>"
406  "</table>")
407  .arg(QString("%1->%2").arg(p1->name(), current->name()))
408  .arg(qApp->fromPixel(p1ToCur.length()))
409  .arg(UnitsToStr(qApp->patternUnit(), true))
410  .arg(QString("%1->%2").arg(p2->name(), current->name()))
411  .arg(qApp->fromPixel(p2ToCur.length()))
412  .arg(QString("%1 %2->%3").arg(tr("Length"), centerP->name(), current->name()))
413  .arg(qApp->fromPixel(centerToCur.length()))
414  .arg(QString("%1 %2->%3").arg(tr("Angle"), centerP->name(), current->name()))
415  .arg(centerToCur.angle())
416  .arg(tr("Name"))
417  .arg(current->name());
418 
419  return toolTip;
420 }
421 
422 //---------------------------------------------------------------------------------------------------------------------
424 {
425  return secondPointId;
426 }
427 
428 //---------------------------------------------------------------------------------------------------------------------
429 void VToolPointOfContact::SetSecondPointId(const quint32 &value)
430 {
431  if (value != NULL_ID)
432  {
433  secondPointId = value;
434 
436  SaveOption(obj);
437  }
438 }
439 
440 //---------------------------------------------------------------------------------------------------------------------
442 {
443  ShowToolVisualization<VisToolPointOfContact>(show);
444 }
445 
446 //---------------------------------------------------------------------------------------------------------------------
448 {
449  return firstPointId;
450 }
451 
452 //---------------------------------------------------------------------------------------------------------------------
453 void VToolPointOfContact::SetFirstPointId(const quint32 &value)
454 {
455  if (value != NULL_ID)
456  {
457  firstPointId = value;
458 
460  SaveOption(obj);
461  }
462 }
463 
464 //---------------------------------------------------------------------------------------------------------------------
466 {
467  return center;
468 }
469 
470 //---------------------------------------------------------------------------------------------------------------------
471 void VToolPointOfContact::setCenter(const quint32 &value)
472 {
473  if (value != NULL_ID)
474  {
475  center = value;
476 
478  SaveOption(obj);
479  }
480 }
481 
482 //---------------------------------------------------------------------------------------------------------------------
484 {
485  VFormula radius(arcRadius, this->getData());
486  radius.setCheckZero(true);
487  radius.setToolId(m_id);
488  radius.setPostfix(UnitsToStr(qApp->patternUnit()));
489 
490  return radius;
491 }
492 
493 //---------------------------------------------------------------------------------------------------------------------
495 {
496  if (value.error() == false)
497  {
499 
501  SaveOption(obj);
502  }
503 }
The DialogPointOfContact class dialog for ToolPointOfContact. 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
const VContainer * getData() const
getData return pointer to data container.
virtual void ToolCreation(const Source &typeCreation)
static void AddRecord(const quint32 id, const Tool &toolType, VAbstractPattern *doc)
AddRecord add record about tool in history.
static qreal CheckFormula(const quint32 &toolId, QString &formula, VContainer *data)
CheckFormula check formula.
const quint32 m_id
id object id.
The VContainer class container of all variables.
Definition: vcontainer.h:141
void AddLine(const quint32 &firstPointId, const quint32 &secondPointId)
AddLine add line to container.
Definition: vcontainer.cpp:382
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 QString GetParametrString(const QDomElement &domElement, const QString &name, const QString &defValue=QString())
Returns the string value of the given attribute. RENAME: see above.
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
bool error() const
Definition: vformula.cpp:238
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
void setToolId(const quint32 &value)
Definition: vformula.cpp:216
void setCheckZero(bool value)
Definition: vformula.cpp:184
void setPostfix(const QString &value)
Definition: vformula.cpp:228
static bool IsPointOnLineSegment(const QPointF &t, const QPointF &p1, const QPointF &p2)
IsPointOnLineSegment Check if the point is on the line segment.
Definition: vgobject.cpp:484
static qint32 LineIntersectCircle(const QPointF &center, qreal radius, const QLineF &line, QPointF &p1, QPointF &p2)
LineIntersectCircle find point intersection line and circle.
Definition: vgobject.cpp:391
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 VToolPointOfContact class tool for creation point intersection arc ad line.
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
void SetSecondPointId(const quint32 &value)
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
static QPointF FindPoint(const qreal &radius, const QPointF &center, const QPointF &firstPoint, const QPointF &secondPoint)
FindPoint return point intersection arc and line.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
quint32 secondPointId
secondPointId id second line point.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void SetFirstPointId(const quint32 &value)
quint32 center
center id center arc point.
QString arcRadius
radius string with formula radius arc.
VToolPointOfContact(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &radius, const quint32 &center, const quint32 &firstPointId, const quint32 &secondPointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolPointOfContact constructor.
void setArcRadius(const VFormula &value)
quint32 firstPointId
firstPointId id first line point.
quint32 GetFirstPointId() const
VFormula getArcRadius() const
QString FirstPointName() const
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
static const QString ToolType
QString SecondPointName() const
void setCenter(const quint32 &value)
virtual QString makeToolTip() const Q_DECL_OVERRIDE
QString ArcCenterPointName() const
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
quint32 GetSecondPointId() const
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
static VToolPointOfContact * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
The VToolSinglePoint class parent for all tools what create points.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setLineP2Id(const quint32 &value)
void setRadiusId(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setRadius(const QString &expression)
void setObject1Id(const quint32 &value)
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
@ PointOfContact
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 AttrRadius
Definition: ifcdef.cpp:100
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrCenter
Definition: ifcdef.cpp:99
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67