Seamly2D
Code documentation
vtoollineintersectaxis.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 vtoollineintersectaxis.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 19 10, 2014
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 "vtoollineintersectaxis.h"
53 
54 #include "vtoollinepoint.h"
55 #include "../ifc/ifcdef.h"
56 #include "../ifc/exception/vexception.h"
57 #include "../vgeometry/vpointf.h"
58 #include "../vmisc/vabstractapplication.h"
59 #include "../vmisc/vcommonsettings.h"
60 #include "../vpatterndb/vcontainer.h"
61 #include "../vpatterndb/vtranslatevars.h"
62 #include "../vwidgets/vmaingraphicsscene.h"
63 #include "../../../vdrawtool.h"
64 #include "../../../../vabstracttool.h"
65 #include "../../../../../dialogs/tools/dialogtool.h"
66 #include "../../../../../dialogs/tools/dialoglineintersectaxis.h"
67 #include "../../../../../visualization/visualization.h"
68 #include "../../../../../visualization/line/vistoollineintersectaxis.h"
69 
70 #include <QLine>
71 #include <QMessageBox>
72 #include <QSharedPointer>
73 #include <QStaticStringData>
74 #include <QStringData>
75 #include <QStringDataPtr>
76 #include <new>
77 
78 template <class T> class QSharedPointer;
79 
80 const QString VToolLineIntersectAxis::ToolType = QStringLiteral("lineIntersectAxis");
81 
82 //---------------------------------------------------------------------------------------------------------------------
84  const QString &lineType, const QString &lineWeight,
85  const QString &lineColor,
86  const QString &formulaAngle, const quint32 &basePointId,
87  const quint32 &firstPointId, const quint32 &secondPointId,
88  const Source &typeCreation, QGraphicsItem *parent)
89  : VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, QString()
90  , basePointId, 0, parent)
91  , formulaAngle(formulaAngle)
92  , firstPointId(firstPointId)
93  , secondPointId(secondPointId)
94 {
95  ToolCreation(typeCreation);
96 }
97 
98 //---------------------------------------------------------------------------------------------------------------------
100 {
101  SCASSERT(not m_dialog.isNull())
102  m_dialog->setModal(true);
104  SCASSERT(not dialogTool.isNull())
106  dialogTool->setLineType(m_lineType);
107  dialogTool->setLineWeight(m_lineWeight);
108  dialogTool->setLineColor(lineColor);
109  dialogTool->SetAngle(formulaAngle);
110  dialogTool->SetBasePointId(basePointId);
111  dialogTool->SetFirstPointId(firstPointId);
112  dialogTool->SetSecondPointId(secondPointId);
113  dialogTool->SetPointName(p->name());
114 }
115 
116 //---------------------------------------------------------------------------------------------------------------------
118  VAbstractPattern *doc, VContainer *data)
119 {
120  SCASSERT(not dialog.isNull())
121  QSharedPointer<DialogLineIntersectAxis> dialogTool = dialog.objectCast<DialogLineIntersectAxis>();
122  SCASSERT(not dialogTool.isNull())
123  const QString pointName = dialogTool->getPointName();
124  const QString lineType = dialogTool->getLineType();
125  const QString lineWeight = dialogTool->getLineWeight();
126  const QString lineColor = dialogTool->getLineColor();
127  QString formulaAngle = dialogTool->GetAngle();
128  const quint32 basePointId = dialogTool->GetBasePointId();
129  const quint32 firstPointId = dialogTool->GetFirstPointId();
130  const quint32 secondPointId = dialogTool->GetSecondPointId();
131 
132  VToolLineIntersectAxis *point = Create(0, pointName, lineType, lineWeight, lineColor, formulaAngle,
133  basePointId, firstPointId, secondPointId, 5, 10, true,
135  if (point != nullptr)
136  {
137  point->m_dialog = dialogTool;
138  }
139  return point;
140 }
141 
142 //---------------------------------------------------------------------------------------------------------------------
143 VToolLineIntersectAxis *VToolLineIntersectAxis::Create(const quint32 _id, const QString &pointName,
144  const QString &lineType, const QString &lineWeight,
145  const QString &lineColor,
146  QString &formulaAngle, quint32 basePointId,
147  quint32 firstPointId, quint32 secondPointId,
148  qreal mx, qreal my, bool showPointName, VMainGraphicsScene *scene,
149  VAbstractPattern *doc, VContainer *data, const Document &parse,
150  const Source &typeCreation)
151 {
153  QLineF axis = QLineF(static_cast<QPointF>(*basePoint), QPointF(basePoint->x()+100, basePoint->y()));
154  axis.setAngle(CheckFormula(_id, formulaAngle, data));
155 
158  QLineF line(static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*secondPoint));
159 
160  QPointF fPoint = FindPoint(axis, line);
161 
162  if (fPoint == QPointF())
163  {
164  const QString msg = tr("<b><big>Can't find intersection point %1 of</big></b><br>"
165  "<b><big>Line and Axis</big></b><br><br>"
166  "Using origin point as a place holder until pattern is corrected.")
167  .arg(pointName);
168 
169  QMessageBox msgBox(qApp->getMainWindow());
170  msgBox.setWindowTitle(tr("Intersect Line and Axis"));
171  msgBox.setWindowFlags(msgBox.windowFlags() & ~Qt::WindowContextHelpButtonHint);
172  msgBox.setWindowIcon(QIcon(":/toolicon/32x32/line_intersect_axis.png"));
173  msgBox.setIcon(QMessageBox::Warning);
174  msgBox.setText(msg);
175  msgBox.setStandardButtons(QMessageBox::Ok);
176  msgBox.exec();
177  }
178 
179  quint32 id = _id;
180  VPointF *p = new VPointF(fPoint, pointName, mx, my);
181  p->setShowPointName(showPointName);
182 
183  if (typeCreation == Source::FromGui)
184  {
185  id = data->AddGObject(p);
186  data->AddLine(basePointId, id);
187  data->AddLine(firstPointId, id);
188  data->AddLine(id, secondPointId);
189  }
190  else
191  {
192  data->UpdateGObject(id, p);
193  data->AddLine(basePointId, id);
194  data->AddLine(firstPointId, id);
195  data->AddLine(id, secondPointId);
196  if (parse != Document::FullParse)
197  {
198  doc->UpdateToolData(id, data);
199  }
200  }
201 
202  if (parse == Document::FullParse)
203  {
205  VToolLineIntersectAxis *point = new VToolLineIntersectAxis(doc, data, id, lineType, lineWeight, lineColor, formulaAngle,
207  typeCreation);
208  scene->addItem(point);
209  InitToolConnections(scene, point);
210  VAbstractPattern::AddTool(id, point);
211  doc->IncrementReferens(basePoint->getIdTool());
212  doc->IncrementReferens(firstPoint->getIdTool());
213  doc->IncrementReferens(secondPoint->getIdTool());
214  return point;
215  }
216  return nullptr;
217 }
218 
219 //---------------------------------------------------------------------------------------------------------------------
220 QPointF VToolLineIntersectAxis::FindPoint(const QLineF &axis, const QLineF &line)
221 {
222  QPointF fPoint;
223  QLineF::IntersectType intersect = axis.intersects(line, &fPoint);
224  if (intersect == QLineF::UnboundedIntersection || intersect == QLineF::BoundedIntersection)
225  {
226  if(VFuzzyComparePossibleNulls(axis.angle(), line.angle())
227  || VFuzzyComparePossibleNulls(qAbs(axis.angle() - line.angle()), 180))
228  {
229  return QPointF();
230  }
231  else
232  {
233  return fPoint;
234  }
235  }
236  else
237  {
238  return QPointF();
239  }
240 }
241 
242 //---------------------------------------------------------------------------------------------------------------------
244 {
246 }
247 
248 //---------------------------------------------------------------------------------------------------------------------
250 {
252 }
253 
254 //---------------------------------------------------------------------------------------------------------------------
256 {
257  VFormula fAngle(formulaAngle, getData());
258  fAngle.setCheckZero(false);
259  fAngle.setToolId(m_id);
260  fAngle.setPostfix(degreeSymbol);
261  return fAngle;
262 }
263 
264 //---------------------------------------------------------------------------------------------------------------------
266 {
267  if (value.error() == false)
268  {
270 
272  SaveOption(obj);
273  }
274 }
275 
276 //---------------------------------------------------------------------------------------------------------------------
278 {
279  return firstPointId;
280 }
281 
282 //---------------------------------------------------------------------------------------------------------------------
283 void VToolLineIntersectAxis::SetFirstPointId(const quint32 &value)
284 {
285  if (value != NULL_ID)
286  {
287  firstPointId = value;
288 
290  SaveOption(obj);
291  }
292 }
293 
294 //---------------------------------------------------------------------------------------------------------------------
296 {
297  return secondPointId;
298 }
299 
300 //---------------------------------------------------------------------------------------------------------------------
302 {
303  if (value != NULL_ID)
304  {
305  secondPointId = value;
306 
308  SaveOption(obj);
309  }
310 }
311 
312 //---------------------------------------------------------------------------------------------------------------------
314 {
315  ShowToolVisualization<VisToolLineIntersectAxis>(show);
316 }
317 
318 //---------------------------------------------------------------------------------------------------------------------
319 void VToolLineIntersectAxis::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
320 {
321  try
322  {
323  ContextMenu<DialogLineIntersectAxis>(event, id);
324  }
325  catch(const VExceptionToolWasDeleted &e)
326  {
327  Q_UNUSED(e)
328  return;//Leave this method immediately!!!
329  }
330 }
331 
332 //---------------------------------------------------------------------------------------------------------------------
333 void VToolLineIntersectAxis::SaveDialog(QDomElement &domElement)
334 {
335  SCASSERT(not m_dialog.isNull())
337  SCASSERT(not dialogTool.isNull())
338  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
339  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
340  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
341  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
342  doc->SetAttribute(domElement, AttrAngle, dialogTool->GetAngle());
343  doc->SetAttribute(domElement, AttrBasePoint, QString().setNum(dialogTool->GetBasePointId()));
344  doc->SetAttribute(domElement, AttrP1Line, QString().setNum(dialogTool->GetFirstPointId()));
345  doc->SetAttribute(domElement, AttrP2Line, QString().setNum(dialogTool->GetSecondPointId()));
346 }
347 
348 //---------------------------------------------------------------------------------------------------------------------
350 {
351  VToolLinePoint::SaveOptions(tag, obj);
352 
358 }
359 
360 //---------------------------------------------------------------------------------------------------------------------
361 void VToolLineIntersectAxis::ReadToolAttributes(const QDomElement &domElement)
362 {
364  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
369  formulaAngle = doc->GetParametrString(domElement, AttrAngle, "");
370 }
371 
372 //---------------------------------------------------------------------------------------------------------------------
374 {
375  if (not vis.isNull())
376  {
377  VisToolLineIntersectAxis *visual = qobject_cast<VisToolLineIntersectAxis *>(vis);
378  SCASSERT(visual != nullptr)
379 
380  visual->setObject1Id(firstPointId);
381  visual->setPoint2Id(secondPointId);
382  visual->setAxisPointId(basePointId);
383  visual->SetAngle(qApp->TrVars()->FormulaToUser(formulaAngle, qApp->Settings()->GetOsSeparator()));
385  visual->setLineWeight(m_lineWeight);
386  visual->RefreshGeometry();
387  }
388 }
389 
390 //---------------------------------------------------------------------------------------------------------------------
392 {
397 
398  const QLineF curLine(static_cast<QPointF>(*basePoint), static_cast<QPointF>(*current));
399  const QLineF firstToCur(static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*current));
400  const QLineF curToSecond(static_cast<QPointF>(*current), static_cast<QPointF>(*secondPoint));
401 
402  const QString toolTip = QString("<table>"
403  "<tr> <td><b> %10:</b> %11</td> </tr>"
404  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
405  "<tr> <td><b> %4:</b> %5°</td> </tr>"
406  "<tr> <td><b>%6:</b> %7 %3</td> </tr>"
407  "<tr> <td><b>%8:</b> %9 %3</td> </tr>"
408  "</table>")
409  .arg(tr("Length"))
410  .arg(qApp->fromPixel(curLine.length()))
411  .arg(UnitsToStr(qApp->patternUnit(), true))
412  .arg(tr("Angle"))
413  .arg(curLine.angle())
414  .arg(QString("%1->%2").arg(firstPoint->name(), current->name()))
415  .arg(qApp->fromPixel(firstToCur.length()))
416  .arg(QString("%1->%2").arg(current->name(), secondPoint->name()))
417  .arg(qApp->fromPixel(curToSecond.length()))
418  .arg(tr("Name"))
419  .arg(current->name());
420 
421  return toolTip;
422 }
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.
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
QString m_lineWeight
typeLine line type.
Definition: vdrawtool.h:104
QString m_lineType
activeBlockName name of tool's pattern peace.
Definition: vdrawtool.h:103
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
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
void SetSecondPointId(const quint32 &value)
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
static QPointF FindPoint(const QLineF &axis, const QLineF &line)
VToolLineIntersectAxis(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType, const QString &lineWeight, const QString &lineColor, const QString &formulaAngle, const quint32 &basePointId, const quint32 &firstPointId, const quint32 &secondPointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
virtual void SetVisualization() Q_DECL_OVERRIDE
static const QString ToolType
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void SetFirstPointId(const quint32 &value)
virtual QString makeToolTip() const Q_DECL_OVERRIDE
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
void SetFormulaAngle(const VFormula &value)
static VToolLineIntersectAxis * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
The VToolLinePoint class parent for all tools what create point with line.
quint32 basePointId
angle line angle.
QString lineColor
mainLine line item.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setPoint2Id(const quint32 &value)
void setAxisPointId(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void SetAngle(const QString &expression)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
const QString degreeSymbol
Definition: def.cpp:196
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
static Q_REQUIRED_RESULT bool VFuzzyComparePossibleNulls(double p1, double p2)
Definition: def.h:490
@ LineIntersectAxis
Source
Definition: def.h:106
const QString AttrBasePoint
Definition: ifcdef.cpp:95
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrP1Line
Definition: ifcdef.cpp:108
const QString AttrAngle
Definition: ifcdef.cpp:103
const QString AttrP2Line
Definition: ifcdef.cpp:109
const QString ColorBlack
Definition: ifcdef.cpp:373
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrLineColor
Definition: ifcdef.cpp:130
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67