Seamly2D
Code documentation
vtoolshoulderpoint.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 vtoolshoulderpoint.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 "vtoolshoulderpoint.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/dialogshoulderpoint.h"
64 #include "../../../../../dialogs/tools/dialogtool.h"
65 #include "../../../../../visualization/visualization.h"
66 #include "../../../../../visualization/line/vistoolshoulderpoint.h"
67 #include "../ifc/exception/vexception.h"
68 #include "../ifc/ifcdef.h"
69 #include "../vgeometry/vgobject.h"
70 #include "../vgeometry/vpointf.h"
71 #include "../vmisc/vabstractapplication.h"
72 #include "../vmisc/vcommonsettings.h"
73 #include "../vpatterndb/vcontainer.h"
74 #include "../vpatterndb/vtranslatevars.h"
75 #include "../vwidgets/vmaingraphicsscene.h"
76 #include "../../../../vabstracttool.h"
77 #include "../../../vdrawtool.h"
78 #include "vtoollinepoint.h"
79 
80 template <class T> class QSharedPointer;
81 
82 const QString VToolShoulderPoint::ToolType = QStringLiteral("shoulder");
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 /**
86  * @brief VToolShoulderPoint constructor.
87  * @param doc dom document container.
88  * @param data container with variables.
89  * @param id object id in container.
90  * @param lineType line type.
91  * @param lineWeight line weight.
92  * @param lineColor line color.
93  * @param formula string with formula length.
94  * @param p1Line id first line point.
95  * @param p2Line id second line point.
96  * @param pShoulder id shoulder point.
97  * @param typeCreation way we create this tool.
98  * @param parent parent object.
99  */
101  const QString &lineType, const QString &lineWeight,
102  const QString &lineColor, const QString &formula, const quint32 &p1Line,
103  const quint32 &p2Line, const quint32 &pShoulder, const Source &typeCreation,
104  QGraphicsItem * parent)
105  : VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, formula, p1Line, 0, parent), p2Line(p2Line)
106  , pShoulder(pShoulder)
107 {
108  ToolCreation(typeCreation);
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
112 /**
113  * @brief setDialog set dialog when user want change tool option.
114  */
116 {
117  SCASSERT(not m_dialog.isNull())
119  SCASSERT(not dialogTool.isNull())
121  dialogTool->setLineType(m_lineType);
122  dialogTool->setLineColor(lineColor);
123  dialogTool->SetFormula(formulaLength);
124  dialogTool->SetP1Line(basePointId);
125  dialogTool->SetP2Line(p2Line);
126  dialogTool->SetP3(pShoulder);
127  dialogTool->SetPointName(p->name());
128 }
129 
130 //---------------------------------------------------------------------------------------------------------------------
131 /**
132  * @brief FindPoint find point.
133  * @param p1Line first line point.
134  * @param p2Line second line point.
135  * @param pShoulder shoulder point.
136  * @param length length form shoulder point to our.
137  * @return point.
138  */
139 QPointF VToolShoulderPoint::FindPoint(const QPointF &p1Line, const QPointF &p2Line, const QPointF &pShoulder,
140  const qreal &length)
141 {
142  QPointF shoulderPoint = p2Line; // Position if result was not found
143  if (length <= 0)
144  {
145  return shoulderPoint;
146  }
147 
148  QLineF line = QLineF(p1Line, p2Line);
149  const qreal baseLength = line.length();
150  const int baseAngle = qRound(line.angle());
151  line.setLength(length*2);
152 
153  QPointF p1;
154  QPointF p2;
155 
156  const qint32 res = VGObject::LineIntersectCircle(pShoulder, length, line, p1, p2);
157 
158  if (res == 1 || res == 2)
159  {
160  const QLineF line1 = QLineF(p1Line, p1);
161  const QLineF line2 = QLineF(p1Line, p2);
162  if (line1.length() > baseLength && baseAngle == qRound(line1.angle()))
163  {
164  shoulderPoint = p1;
165  }
166  else if (res == 2 && line2.length() > baseLength && baseAngle == qRound(line2.angle()))
167  {
168  shoulderPoint = p2;
169  }
170  }
171 
172  return shoulderPoint;
173 }
174 
175 //---------------------------------------------------------------------------------------------------------------------
176 /**
177  * @brief Create help create tool from GUI.
178  * @param dialog dialog.
179  * @param scene pointer to scene.
180  * @param doc dom document container.
181  * @param data container with variables.
182  * @return the created tool
183  */
185  VAbstractPattern *doc, VContainer *data)
186 {
187  SCASSERT(not dialog.isNull())
188  QSharedPointer<DialogShoulderPoint> dialogTool = dialog.objectCast<DialogShoulderPoint>();
189  SCASSERT(not dialogTool.isNull())
190  QString formula = dialogTool->GetFormula();
191  const quint32 p1Line = dialogTool->GetP1Line();
192  const quint32 p2Line = dialogTool->GetP2Line();
193  const quint32 pShoulder = dialogTool->GetP3();
194  const QString lineType = dialogTool->getLineType();
195  const QString lineWeight = dialogTool->getLineWeight();
196  const QString lineColor = dialogTool->getLineColor();
197  const QString pointName = dialogTool->getPointName();
198  VToolShoulderPoint * point = Create(0, formula, p1Line, p2Line, pShoulder, lineType, lineWeight, lineColor, pointName,
199  5, 10, true, scene, doc, data, Document::FullParse, Source::FromGui);
200  if (point != nullptr)
201  {
202  point->m_dialog = dialogTool;
203  }
204  return point;
205 }
206 
207 //---------------------------------------------------------------------------------------------------------------------
208 /**
209  * @brief Create help create tool.
210  * @param _id tool id, 0 if tool doesn't exist yet.
211  * @param formula string with formula length.
212  * @param p1Line id first line point.
213  * @param p2Line id second line point.
214  * @param pShoulder id shoulder point.
215  * @param lineType line type.
216  * @param lineWeight line weight.
217  * @param lineColor line color.
218  * @param pointName point name.
219  * @param mx label bias x axis.
220  * @param my label bias y axis.
221  * @param showPointName show/hide point name text.
222  * @param scene pointer to scene.
223  * @param doc dom document container.
224  * @param data container with variables.
225  * @param parse parser file mode.
226  * @param typeCreation way we create this tool.
227  * @return the created tool
228  */
229 VToolShoulderPoint* VToolShoulderPoint::Create(const quint32 _id, QString &formula, quint32 p1Line,
230  quint32 p2Line, quint32 pShoulder, const QString &lineType,
231  const QString &lineWeight,
232  const QString &lineColor, const QString &pointName, qreal mx, qreal my,
233  bool showPointName, VMainGraphicsScene *scene, VAbstractPattern *doc,
234  VContainer *data, const Document &parse, const Source &typeCreation)
235 {
236  const QSharedPointer<VPointF> firstPoint = data->GeometricObject<VPointF>(p1Line);
239 
240  const qreal result = CheckFormula(_id, formula, data);
241 
242  QPointF fPoint = VToolShoulderPoint::FindPoint(static_cast<QPointF>(*firstPoint),
243  static_cast<QPointF>(*secondPoint),
244  static_cast<QPointF>(*shoulderPoint), qApp->toPixel(result));
245  quint32 id = _id;
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(p1Line, id);
253  data->AddLine(p2Line, id);
254  }
255  else
256  {
257  data->UpdateGObject(id, p);
258  data->AddLine(p1Line, id);
259  data->AddLine(p2Line, id);
260  if (parse != Document::FullParse)
261  {
262  doc->UpdateToolData(id, data);
263  }
264  }
265 
266  if (parse == Document::FullParse)
267  {
269  VToolShoulderPoint *point = new VToolShoulderPoint(doc, data, id, lineType, lineWeight, lineColor, formula,
270  p1Line, p2Line, pShoulder,
271  typeCreation);
272  scene->addItem(point);
273  InitToolConnections(scene, point);
274  VAbstractPattern::AddTool(id, point);
275  doc->IncrementReferens(firstPoint->getIdTool());
276  doc->IncrementReferens(secondPoint->getIdTool());
277  doc->IncrementReferens(shoulderPoint->getIdTool());
278  return point;
279  }
280  return nullptr;
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
285 {
286  return VAbstractTool::data.GetGObject(p2Line)->name();
287 }
288 
289 //---------------------------------------------------------------------------------------------------------------------
291 {
292  return VAbstractTool::data.GetGObject(pShoulder)->name();
293 }
294 
295 //---------------------------------------------------------------------------------------------------------------------
296 /**
297  * @brief contextMenuEvent handle context menu events.
298  * @param event context menu event.
299  */
300 void VToolShoulderPoint::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
301 {
302  try
303  {
304  ContextMenu<DialogShoulderPoint>(event, id);
305  }
306  catch(const VExceptionToolWasDeleted &e)
307  {
308  Q_UNUSED(e)
309  return;//Leave this method immediately!!!
310  }
311 }
312 
313 //---------------------------------------------------------------------------------------------------------------------
314 /**
315  * @brief RemoveReferens decrement value of reference.
316  */
318 {
319  const auto p2 = VAbstractTool::data.GetGObject(p2Line);
320  const auto pS = VAbstractTool::data.GetGObject(pShoulder);
321 
322  doc->DecrementReferens(p2->getIdTool());
323  doc->DecrementReferens(pS->getIdTool());
325 }
326 
327 //---------------------------------------------------------------------------------------------------------------------
328 /**
329  * @brief SaveDialog save options into file after change in dialog.
330  */
331 void VToolShoulderPoint::SaveDialog(QDomElement &domElement)
332 {
333  SCASSERT(not m_dialog.isNull())
335  SCASSERT(not dialogTool.isNull())
336  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
337  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
338  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
339  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
340  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
341  doc->SetAttribute(domElement, AttrP1Line, QString().setNum(dialogTool->GetP1Line()));
342  doc->SetAttribute(domElement, AttrP2Line, QString().setNum(dialogTool->GetP2Line()));
343  doc->SetAttribute(domElement, AttrPShoulder, QString().setNum(dialogTool->GetP3()));
344 }
345 
346 //---------------------------------------------------------------------------------------------------------------------
348 {
349  VToolLinePoint::SaveOptions(tag, obj);
350 
356 }
357 
358 //---------------------------------------------------------------------------------------------------------------------
359 void VToolShoulderPoint::ReadToolAttributes(const QDomElement &domElement)
360 {
362  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
364  formulaLength = doc->GetParametrString(domElement, AttrLength, "");
368 }
369 
370 //---------------------------------------------------------------------------------------------------------------------
372 {
373  if (not vis.isNull())
374  {
375  VisToolShoulderPoint *visual = qobject_cast<VisToolShoulderPoint *>(vis);
376  SCASSERT(visual != nullptr)
377 
378  visual->setObject1Id(pShoulder);
379  visual->setLineP1Id(basePointId);
380  visual->setLineP2Id(p2Line);
381  visual->setLength(qApp->TrVars()->FormulaToUser(formulaLength, qApp->Settings()->GetOsSeparator()));
383  visual->setLineWeight(m_lineWeight);
384  visual->RefreshGeometry();
385  }
386 }
387 
388 //---------------------------------------------------------------------------------------------------------------------
390 {
394 
395  const QLineF firstToCur(static_cast<QPointF>(*first), static_cast<QPointF>(*current));
396  const QLineF secondToCur(static_cast<QPointF>(*second), static_cast<QPointF>(*current));
397 
398  const QString toolTip = QString("<table>"
399  "<tr> <td><b> %8:</b> %9</td> </tr>"
400  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
401  "<tr> <td><b> %4:</b> %5°</td> </tr>"
402  "<tr> <td><b>%6:</b> %7 %3</td> </tr>"
403  "</table>")
404  .arg(tr("Length"))
405  .arg(qApp->fromPixel(firstToCur.length()))
406  .arg(UnitsToStr(qApp->patternUnit(), true))
407  .arg(tr("Angle"))
408  .arg(firstToCur.angle())
409  .arg(QString("%1->%2").arg(second->name(), current->name()))
410  .arg(qApp->fromPixel(secondToCur.length()))
411  .arg(tr("Name"))
412  .arg(current->name());
413 
414  return toolTip;
415 }
416 
417 //---------------------------------------------------------------------------------------------------------------------
418 // cppcheck-suppress unusedFunction
420 {
421  return pShoulder;
422 }
423 
424 //---------------------------------------------------------------------------------------------------------------------
425 // cppcheck-suppress unusedFunction
426 void VToolShoulderPoint::setPShoulder(const quint32 &value)
427 {
428  if (value != NULL_ID)
429  {
430  pShoulder = value;
431 
433  SaveOption(obj);
434  }
435 }
436 
437 //---------------------------------------------------------------------------------------------------------------------
439 {
440  ShowToolVisualization<VisToolShoulderPoint>(show);
441 }
442 
443 //---------------------------------------------------------------------------------------------------------------------
445 {
446  return p2Line;
447 }
448 
449 //---------------------------------------------------------------------------------------------------------------------
450 void VToolShoulderPoint::SetP2Line(const quint32 &value)
451 {
452  if (value != NULL_ID)
453  {
454  p2Line = value;
455 
457  SaveOption(obj);
458  }
459 }
The DialogShoulderPoint class dialog for ToolShoulderPoint. 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.
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
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 VToolLinePoint class parent for all tools what create point with line.
quint32 basePointId
angle line angle.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
QString lineColor
mainLine line item.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
QString formulaLength
The VToolShoulderPoint class tool for creation of point Length to Line. This tool for special situati...
static VToolShoulderPoint * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
QString ShoulderPointName() const
void SetP2Line(const quint32 &value)
quint32 getPShoulder() const
QString SecondPointName() const
static const QString ToolType
quint32 pShoulder
p2Line id second line point.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
static QPointF FindPoint(const QPointF &p1Line, const QPointF &p2Line, const QPointF &pShoulder, const qreal &length)
FindPoint find point.
VToolShoulderPoint(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &typeLine, const QString &lineWeight, const QString &lineColor, const QString &formula, const quint32 &p1Line, const quint32 &p2Line, const quint32 &pShoulder, const Source &typeCreation, QGraphicsItem *parent=nullptr)
pShoulder id shoulder line point.
void setPShoulder(const quint32 &value)
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
quint32 GetP2Line() const
virtual QString makeToolTip() const Q_DECL_OVERRIDE
void setLength(const QString &expression)
void setLineP1Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLineP2Id(const quint32 &value)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &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
@ ShoulderPoint
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrPShoulder
Definition: ifcdef.cpp:114
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 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