Seamly2D
Code documentation
vtoollineintersect.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 vtoollineintersect.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 "vtoollineintersect.h"
53 
54 #include <QLine>
55 #include <QLineF>
56 #include <QPointF>
57 #include <QSharedPointer>
58 #include <QStaticStringData>
59 #include <QStringData>
60 #include <QStringDataPtr>
61 #include <new>
62 
63 #include "../../../../dialogs/tools/dialoglineintersect.h"
64 #include "../../../../visualization/line/vistoollineintersect.h"
65 #include "../ifc/exception/vexception.h"
66 #include "../ifc/ifcdef.h"
67 #include "../vgeometry/vgobject.h"
68 #include "../vgeometry/vpointf.h"
69 #include "../vmisc/vabstractapplication.h"
70 #include "../vpatterndb/vcontainer.h"
71 #include "../vwidgets/vmaingraphicsscene.h"
72 #include "../../../../dialogs/tools/dialogtool.h"
73 #include "../../../../visualization/visualization.h"
74 #include "../../../vabstracttool.h"
75 #include "../../vdrawtool.h"
76 #include "vtoolsinglepoint.h"
77 
78 template <class T> class QSharedPointer;
79 
80 const QString VToolLineIntersect::ToolType = QStringLiteral("lineIntersect");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolLineIntersect constructor.
85  * @param doc dom document container.
86  * @param data container with variables.
87  * @param id object id in container.
88  * @param p1Line1 id first point first line.
89  * @param p2Line1 id second point first line.
90  * @param p1Line2 id first point second line.
91  * @param p2Line2 id second point second line.
92  * @param typeCreation way we create this tool.
93  * @param parent parent object.
94  */
96  const quint32 &p1Line1, const quint32 &p2Line1, const quint32 &p1Line2,
97  const quint32 &p2Line2, const Source &typeCreation,
98  QGraphicsItem *parent)
99  :VToolSinglePoint(doc, data, id, QColor(qApp->Settings()->getPointNameColor()), parent)
100  , p1Line1(p1Line1)
101  , p2Line1(p2Line1)
102  , p1Line2(p1Line2)
103  , p2Line2(p2Line2)
104 {
105  ToolCreation(typeCreation);
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
109 /**
110  * @brief setDialog set dialog when user want change tool option.
111  */
113 {
114  SCASSERT(not m_dialog.isNull())
116  SCASSERT(not dialogTool.isNull())
118  dialogTool->SetP1Line1(p1Line1);
119  dialogTool->SetP2Line1(p2Line1);
120  dialogTool->SetP1Line2(p1Line2);
121  dialogTool->SetP2Line2(p2Line2);
122  dialogTool->SetPointName(p->name());
123 }
124 
125 //---------------------------------------------------------------------------------------------------------------------
126 /**
127  * @brief Create help create tool from GUI.
128  * @param dialog dialog.
129  * @param scene pointer to scene.
130  * @param doc dom document container.
131  * @param data container with variables.
132  * @return the created tool
133  */
135  VAbstractPattern *doc, VContainer *data)
136 {
137  SCASSERT(not dialog.isNull())
138  QSharedPointer<DialogLineIntersect> dialogTool = dialog.objectCast<DialogLineIntersect>();
139  SCASSERT(not dialogTool.isNull())
140  const quint32 p1Line1Id = dialogTool->GetP1Line1();
141  const quint32 p2Line1Id = dialogTool->GetP2Line1();
142  const quint32 p1Line2Id = dialogTool->GetP1Line2();
143  const quint32 p2Line2Id = dialogTool->GetP2Line2();
144  const QString pointName = dialogTool->getPointName();
145  VToolLineIntersect* point = Create(0, p1Line1Id, p2Line1Id, p1Line2Id, p2Line2Id, pointName, 5, 10, true, scene, doc,
147  if (point != nullptr)
148  {
149  point->m_dialog = dialogTool;
150  }
151  return point;
152 }
153 
154 //---------------------------------------------------------------------------------------------------------------------
155 /**
156  * @brief Create help create tool.
157  * @param _id tool id, 0 if tool doesn't exist yet.
158  * @param p1Line1Id id first point first line.
159  * @param p2Line1Id id second point first line.
160  * @param p1Line2Id id first point second line.
161  * @param p2Line2Id id second point second line.
162  * @param pointName point name.
163  * @param mx label bias x axis.
164  * @param my label bias y axis.
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 VToolLineIntersect* VToolLineIntersect::Create(const quint32 _id, const quint32 &p1Line1Id, const quint32 &p2Line1Id,
173  const quint32 &p1Line2Id, const quint32 &p2Line2Id,
174  const QString &pointName, qreal mx, qreal my, bool showPointName,
175  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
176  const Document &parse, const Source &typeCreation)
177 {
182 
183  QLineF line1(static_cast<QPointF>(*p1Line1), static_cast<QPointF>(*p2Line1));
184  QLineF line2(static_cast<QPointF>(*p1Line2), static_cast<QPointF>(*p2Line2));
185  QPointF fPoint;
186  QLineF::IntersectType intersect = line1.intersects(line2, &fPoint);
187  if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection)
188  {
189  quint32 id = _id;
190 
191  VPointF *p = new VPointF(fPoint, pointName, mx, my);
192  p->setShowPointName(showPointName);
193 
194  if (typeCreation == Source::FromGui)
195  {
196  id = data->AddGObject(p);
197  data->AddLine(p1Line1Id, id);
198  data->AddLine(id, p2Line1Id);
199  data->AddLine(p1Line2Id, id);
200  data->AddLine(id, p2Line2Id);
201  }
202  else
203  {
204  data->UpdateGObject(id, p);
205  data->AddLine(p1Line1Id, id);
206  data->AddLine(id, p2Line1Id);
207  data->AddLine(p1Line2Id, id);
208  data->AddLine(id, p2Line2Id);
209  if (parse != Document::FullParse)
210  {
211  doc->UpdateToolData(id, data);
212  }
213  }
214 
215  if (parse == Document::FullParse)
216  {
218  VToolLineIntersect *point = new VToolLineIntersect(doc, data, id, p1Line1Id, p2Line1Id, p1Line2Id,
219  p2Line2Id, typeCreation);
220  scene->addItem(point);
221  InitToolConnections(scene, point);
222  VAbstractPattern::AddTool(id, point);
223  doc->IncrementReferens(p1Line1->getIdTool());
224  doc->IncrementReferens(p2Line1->getIdTool());
225  doc->IncrementReferens(p1Line2->getIdTool());
226  doc->IncrementReferens(p2Line2->getIdTool());
227  return point;
228  }
229  }
230  return nullptr;
231 }
232 
233 //---------------------------------------------------------------------------------------------------------------------
235 {
236  return VAbstractTool::data.GetGObject(p1Line1)->name();
237 }
238 
239 //---------------------------------------------------------------------------------------------------------------------
241 {
242  return VAbstractTool::data.GetGObject(p2Line1)->name();
243 }
244 
245 //---------------------------------------------------------------------------------------------------------------------
247 {
248  return VAbstractTool::data.GetGObject(p1Line2)->name();
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  return VAbstractTool::data.GetGObject(p2Line2)->name();
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
258 /**
259  * @brief contextMenuEvent handle context menu events.
260  * @param event context menu event.
261  */
262 void VToolLineIntersect::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
263 {
264  try
265  {
266  ContextMenu<DialogLineIntersect>(event, id);
267  }
268  catch(const VExceptionToolWasDeleted &e)
269  {
270  Q_UNUSED(e)
271  return;//Leave this method immediately!!!
272  }
273 }
274 
275 //---------------------------------------------------------------------------------------------------------------------
276 /**
277  * @brief RemoveReferens decrement value of reference.
278  */
280 {
281  const auto p1L1 = VAbstractTool::data.GetGObject(p1Line1);
282  const auto p2L1 = VAbstractTool::data.GetGObject(p2Line1);
283  const auto p1L2 = VAbstractTool::data.GetGObject(p1Line2);
284  const auto p2L2 = VAbstractTool::data.GetGObject(p2Line2);
285 
286  doc->DecrementReferens(p1L1->getIdTool());
287  doc->DecrementReferens(p2L1->getIdTool());
288  doc->DecrementReferens(p1L2->getIdTool());
289  doc->DecrementReferens(p2L2->getIdTool());
290 }
291 
292 //---------------------------------------------------------------------------------------------------------------------
293 /**
294  * @brief SaveDialog save options into file after change in dialog.
295  */
296 void VToolLineIntersect::SaveDialog(QDomElement &domElement)
297 {
298  SCASSERT(not m_dialog.isNull())
300  SCASSERT(not dialogTool.isNull())
301  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
302  doc->SetAttribute(domElement, AttrP1Line1, QString().setNum(dialogTool->GetP1Line1()));
303  doc->SetAttribute(domElement, AttrP2Line1, QString().setNum(dialogTool->GetP2Line1()));
304  doc->SetAttribute(domElement, AttrP1Line2, QString().setNum(dialogTool->GetP1Line2()));
305  doc->SetAttribute(domElement, AttrP2Line2, QString().setNum(dialogTool->GetP2Line2()));
306 }
307 
308 //---------------------------------------------------------------------------------------------------------------------
310 {
312 
318 }
319 
320 //---------------------------------------------------------------------------------------------------------------------
321 void VToolLineIntersect::ReadToolAttributes(const QDomElement &domElement)
322 {
327 }
328 
329 //---------------------------------------------------------------------------------------------------------------------
331 {
332  if (not vis.isNull())
333  {
334  VisToolLineIntersect *visual = qobject_cast<VisToolLineIntersect *>(vis);
335  SCASSERT(visual != nullptr)
336 
337  visual->setObject1Id(p1Line1);
338  visual->setLine1P2Id(p2Line1);
339  visual->setLine2P1Id(p1Line2);
340  visual->setLine2P2Id(p2Line2);
341  visual->RefreshGeometry();
342  }
343 }
344 
345 //---------------------------------------------------------------------------------------------------------------------
347 {
353 
354  const QLineF p1L1ToCur(static_cast<QPointF>(*p1L1), static_cast<QPointF>(*current));
355  const QLineF curToP2L1(static_cast<QPointF>(*current), static_cast<QPointF>(*p2L1));
356  const QLineF p1L2ToCur(static_cast<QPointF>(*p1L2), static_cast<QPointF>(*current));
357  const QLineF curToP2L2(static_cast<QPointF>(*current), static_cast<QPointF>(*p2L2));
358 
359  const QString toolTip = QString("<table>"
360  "<tr> <td><b>%10:</b> %11</td> </tr>"
361  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
362  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
363  "<tr> <td><b>%6:</b> %7 %3</td> </tr>"
364  "<tr> <td><b>%8:</b> %9 %3</td> </tr>"
365  "</table>")
366  .arg(QString("%1->%2").arg(p1L1->name(), current->name()))
367  .arg(qApp->fromPixel(p1L1ToCur.length()))
368  .arg(UnitsToStr(qApp->patternUnit(), true))
369  .arg(QString("%1->%2").arg(current->name(), p2L1->name()))
370  .arg(qApp->fromPixel(curToP2L1.length()))
371  .arg(QString("%1->%2").arg(p1L2->name(), current->name()))
372  .arg(qApp->fromPixel(p1L2ToCur.length()))
373  .arg(QString("%1->%2").arg(current->name(), p2L2->name()))
374  .arg(qApp->fromPixel(curToP2L2.length()))
375  .arg(tr("Name"))
376  .arg(current->name());
377 
378  return toolTip;
379 }
380 
381 //---------------------------------------------------------------------------------------------------------------------
383 {
384  return p2Line2;
385 }
386 
387 //---------------------------------------------------------------------------------------------------------------------
388 void VToolLineIntersect::SetP2Line2(const quint32 &value)
389 {
390  if (value != NULL_ID)
391  {
392  p2Line2 = value;
393 
395  SaveOption(obj);
396  }
397 }
398 
399 //---------------------------------------------------------------------------------------------------------------------
401 {
402  ShowToolVisualization<VisToolLineIntersect>(show);
403 }
404 
405 //---------------------------------------------------------------------------------------------------------------------
407 {
408  return p1Line2;
409 }
410 
411 //---------------------------------------------------------------------------------------------------------------------
412 void VToolLineIntersect::SetP1Line2(const quint32 &value)
413 {
414  if (value != NULL_ID)
415  {
416  p1Line2 = value;
417 
419  SaveOption(obj);
420  }
421 }
422 
423 //---------------------------------------------------------------------------------------------------------------------
425 {
426  return p2Line1;
427 }
428 
429 //---------------------------------------------------------------------------------------------------------------------
430 void VToolLineIntersect::SetP2Line1(const quint32 &value)
431 {
432  if (value != NULL_ID)
433  {
434  p2Line1 = value;
435 
437  SaveOption(obj);
438  }
439 }
440 
441 //---------------------------------------------------------------------------------------------------------------------
443 {
444  return p1Line1;
445 }
446 
447 //---------------------------------------------------------------------------------------------------------------------
448 void VToolLineIntersect::SetP1Line1(const quint32 &value)
449 {
450  if (value != NULL_ID)
451  {
452  p1Line1 = value;
453 
455  SaveOption(obj);
456  }
457 }
The DialogLineIntersect class dialog for ToolLineIntersect. 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
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 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 VToolLineIntersect class help find point intersection lines.
quint32 GetP2Line2() const
static VToolLineIntersect * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
static const QString ToolType
QString Line2P1Name() const
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
virtual QString makeToolTip() const Q_DECL_OVERRIDE
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
QString Line2P2Name() const
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
quint32 GetP1Line2() const
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
quint32 p1Line2
p1Line2 id first point second line.
QString Line1P1Name() const
quint32 p1Line1
p1Line1 id first point first line.
quint32 GetP1Line1() const
virtual void SetVisualization() Q_DECL_OVERRIDE
QString Line1P2Name() const
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
void SetP1Line2(const quint32 &value)
VToolLineIntersect(VAbstractPattern *doc, VContainer *data, const quint32 &id, const quint32 &p1Line1, const quint32 &p2Line1, const quint32 &p1Line2, const quint32 &p2Line2, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolLineIntersect constructor.
quint32 GetP2Line1() const
void SetP2Line2(const quint32 &value)
quint32 p2Line1
p2Line1 id second point first line.
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
quint32 p2Line2
p2Line2 id second point second line.
void SetP2Line1(const quint32 &value)
void SetP1Line1(const quint32 &value)
The VToolSinglePoint class parent for all tools what create points.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setLine2P2Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLine2P1Id(const quint32 &value)
void setLine1P2Id(const quint32 &value)
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
@ LineIntersect
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrP1Line1
Definition: ifcdef.cpp:110
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrP2Line2
Definition: ifcdef.cpp:113
const QString AttrP1Line2
Definition: ifcdef.cpp:112
const QString AttrP2Line1
Definition: ifcdef.cpp:111
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67