Seamly2D
Code documentation
vtoolbisector.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 vtoolbisector.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 "vtoolbisector.h"
53 
54 #include <QLineF>
55 #include <QSharedPointer>
56 #include <QStaticStringData>
57 #include <QStringData>
58 #include <QStringDataPtr>
59 #include <new>
60 
61 #include "../../../../../dialogs/tools/dialogbisector.h"
62 #include "../../../../../dialogs/tools/dialogtool.h"
63 #include "../../../../../visualization/visualization.h"
64 #include "../../../../../visualization/line/vistoolbisector.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 "../vmisc/vcommonsettings.h"
71 #include "../vpatterndb/vcontainer.h"
72 #include "../vpatterndb/vtranslatevars.h"
73 #include "../vwidgets/vmaingraphicsscene.h"
74 #include "../../../../vabstracttool.h"
75 #include "../../../vdrawtool.h"
76 #include "vtoollinepoint.h"
77 
78 template <class T> class QSharedPointer;
79 
80 const QString VToolBisector::ToolType = QStringLiteral("bisector");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolBisector constructor.
85  * @param doc dom document container.
86  * @param data container with variables.
87  * @param id object id in container.
88  * @param lineType line type.
89  * @param lineWeight line weight.
90  * @param lineColor line color.
91  * @param formula string with formula length of bisector.
92  * @param firstPointId id first point of angle.
93  * @param secondPointId id second point of angle.
94  * @param thirdPointId id third point of angle.
95  * @param typeCreation way we create this tool.
96  * @param parent parent object.
97  */
99  const QString &lineType, const QString &lineWeight,
100  const QString &lineColor, const QString &formula, const quint32 &firstPointId,
101  const quint32 &secondPointId, const quint32 &thirdPointId, const Source &typeCreation,
102  QGraphicsItem *parent)
103  : VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, formula, secondPointId, 0, parent)
104  , firstPointId(firstPointId)
105  , thirdPointId(thirdPointId)
106 {
107  ToolCreation(typeCreation);
108 }
109 
110 //---------------------------------------------------------------------------------------------------------------------
111 qreal VToolBisector::BisectorAngle(const QPointF &firstPoint, const QPointF &secondPoint, const QPointF &thirdPoint)
112 {
113  QLineF line1(secondPoint, firstPoint);
114  QLineF line2(secondPoint, thirdPoint);
115  qreal angle = line1.angleTo(line2);
116  if (angle>180)
117  {
118  angle = 360 - angle;
119  return line1.angle()-angle/2;
120  }
121  else
122  {
123  return line1.angle()+angle/2;
124  }
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
128 /**
129  * @brief FindPoint find bisector point.
130  * @param firstPoint first point of angle.
131  * @param secondPoint second point of angle.
132  * @param thirdPoint third point of angle.
133  * @param length bisector length.
134  * @return bisector point.
135  */
136 QPointF VToolBisector::FindPoint(const QPointF &firstPoint, const QPointF &secondPoint,
137  const QPointF &thirdPoint, const qreal &length)
138 {
139  QLineF line1(secondPoint, firstPoint);
140  line1.setAngle(BisectorAngle(firstPoint, secondPoint, thirdPoint));
141  line1.setLength(length);
142  return line1.p2();
143 }
144 
145 //---------------------------------------------------------------------------------------------------------------------
146 /**
147  * @brief setDialog set dialog when user want change tool option.
148  */
150 {
151  SCASSERT(not m_dialog.isNull())
152  QSharedPointer<DialogBisector> dialogTool = m_dialog.objectCast<DialogBisector>();
153  SCASSERT(not dialogTool.isNull())
155  dialogTool->setLineType(m_lineType);
156  dialogTool->setLineWeight(m_lineWeight);
157  dialogTool->setLineColor(lineColor);
158  dialogTool->SetFormula(formulaLength);
159  dialogTool->SetFirstPointId(firstPointId);
160  dialogTool->SetSecondPointId(basePointId);
161  dialogTool->SetThirdPointId(thirdPointId);
162  dialogTool->SetPointName(p->name());
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
166 /**
167  * @brief Create help create tool form GUI.
168  * @param dialog dialog.
169  * @param scene pointer to scene.
170  * @param doc dom document container.
171  * @param data container with variables.
172  */
174  VAbstractPattern *doc, VContainer *data)
175 {
176  SCASSERT(not dialog.isNull())
177  QSharedPointer<DialogBisector> dialogTool = dialog.objectCast<DialogBisector>();
178  SCASSERT(not dialogTool.isNull())
179  QString formula = dialogTool->GetFormula();
180  const quint32 firstPointId = dialogTool->GetFirstPointId();
181  const quint32 secondPointId = dialogTool->GetSecondPointId();
182  const quint32 thirdPointId = dialogTool->GetThirdPointId();
183  const QString lineType = dialogTool->getLineType();
184  const QString lineWeight = dialogTool->getLineWeight();
185  const QString lineColor = dialogTool->getLineColor();
186  const QString pointName = dialogTool->getPointName();
187  VToolBisector *point = Create(0, formula, firstPointId, secondPointId, thirdPointId, lineType, lineWeight, lineColor,
188  pointName, 5, 10, true, scene, doc, data, Document::FullParse, Source::FromGui);
189  if (point != nullptr)
190  {
191  point->m_dialog = dialogTool;
192  }
193  return point;
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
197 /**
198  * @brief Create help create tool.
199  * @param _id tool id, 0 if tool doesn't exist yet.
200  * @param formula string with formula.
201  * @param firstPointId id first point of angle.
202  * @param secondPointId id second point of angle.
203  * @param thirdPointId id third point of angle.
204  * @param lineType line type.
205  * @param lineWeight line weight.
206  * @param lineColor line color.
207  * @param pointName point name.
208  * @param mx label bias x axis.
209  * @param my label bias y axis.
210  * @param showPointName show/hide point name text.
211  * @param scene pointer to scene.
212  * @param doc dom document container.
213  * @param data container with variables.
214  * @param parse parser file mode.
215  * @param typeCreation way we create this tool.
216  */
217 VToolBisector* VToolBisector::Create(const quint32 _id, QString &formula, quint32 firstPointId, quint32 secondPointId,
218  quint32 thirdPointId, const QString &lineType,
219  const QString &lineWeight, const QString &lineColor,
220  const QString &pointName, qreal mx, qreal my, bool showPointName,
222  VContainer *data, const Document &parse, const Source &typeCreation)
223 {
225  const QSharedPointer<VPointF> secondPoint = data->GeometricObject<VPointF>(secondPointId);
227 
228  const qreal result = CheckFormula(_id, formula, data);
229 
230  QPointF fPoint = VToolBisector::FindPoint(static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*secondPoint),
231  static_cast<QPointF>(*thirdPoint), qApp->toPixel(result));
232  quint32 id = _id;
233  VPointF *p = new VPointF(fPoint, pointName, mx, my);
234  p->setShowPointName(showPointName);
235 
236  if (typeCreation == Source::FromGui)
237  {
238  id = data->AddGObject(p);
239  data->AddLine(secondPointId, id);
240  }
241  else
242  {
243  data->UpdateGObject(id, p);
244  data->AddLine(secondPointId, id);
245  if (parse != Document::FullParse)
246  {
247  doc->UpdateToolData(id, data);
248  }
249  }
250 
251  if (parse == Document::FullParse)
252  {
254  VToolBisector *point = new VToolBisector(doc, data, id, lineType, lineWeight, lineColor, formula, firstPointId,
255  secondPointId, thirdPointId, typeCreation);
256  scene->addItem(point);
257  InitToolConnections(scene, point);
258  VAbstractPattern::AddTool(id, point);
259  doc->IncrementReferens(firstPoint->getIdTool());
260  doc->IncrementReferens(secondPoint->getIdTool());
261  doc->IncrementReferens(thirdPoint->getIdTool());
262  return point;
263  }
264  return nullptr;
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
269 {
271 }
272 
273 //---------------------------------------------------------------------------------------------------------------------
275 {
277 }
278 
279 //---------------------------------------------------------------------------------------------------------------------
280 /**
281  * @brief contextMenuEvent handle context menu events.
282  * @param event context menu event.
283  */
284 void VToolBisector::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
285 {
286  try
287  {
288  ContextMenu<DialogBisector>(event, id);
289  }
290  catch(const VExceptionToolWasDeleted &e)
291  {
292  Q_UNUSED(e)
293  return;//Leave this method immediately!!!
294  }
295 }
296 
297 //---------------------------------------------------------------------------------------------------------------------
298 /**
299  * @brief RemoveReferens decrement value of reference.
300  */
302 {
303  const auto firstPoint = VAbstractTool::data.GetGObject(firstPointId);
304  const auto thirdPoint = VAbstractTool::data.GetGObject(thirdPointId);
305 
306  doc->DecrementReferens(firstPoint->getIdTool());
307  doc->DecrementReferens(thirdPoint->getIdTool());
309 }
310 
311 //---------------------------------------------------------------------------------------------------------------------
312 /**
313  * @brief SaveDialog save options into file after change in dialog.
314  */
315 void VToolBisector::SaveDialog(QDomElement &domElement)
316 {
317  SCASSERT(not m_dialog.isNull())
318  QSharedPointer<DialogBisector> dialogTool = m_dialog.objectCast<DialogBisector>();
319  SCASSERT(not dialogTool.isNull())
320  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
321  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
322  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
323  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
324  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
325  doc->SetAttribute(domElement, AttrFirstPoint, QString().setNum(dialogTool->GetFirstPointId()));
326  doc->SetAttribute(domElement, AttrSecondPoint, QString().setNum(dialogTool->GetSecondPointId()));
327  doc->SetAttribute(domElement, AttrThirdPoint, QString().setNum(dialogTool->GetThirdPointId()));
328 }
329 
330 //---------------------------------------------------------------------------------------------------------------------
332 {
333  VToolLinePoint::SaveOptions(tag, obj);
334 
340 }
341 
342 //---------------------------------------------------------------------------------------------------------------------
343 void VToolBisector::ReadToolAttributes(const QDomElement &domElement)
344 {
346  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
348  formulaLength = doc->GetParametrString(domElement, AttrLength, "");
352 }
353 
354 //---------------------------------------------------------------------------------------------------------------------
356 {
357  if (not vis.isNull())
358  {
359  VisToolBisector *visual = qobject_cast<VisToolBisector *>(vis);
360  SCASSERT(visual != nullptr)
361 
362  visual->setObject1Id(firstPointId);
363  visual->setObject2Id(basePointId);
364  visual->setObject3Id(thirdPointId);
365  visual->setLength(qApp->TrVars()->FormulaToUser(formulaLength, qApp->Settings()->GetOsSeparator()));
367  visual->setLineWeight(m_lineWeight);
368  visual->RefreshGeometry();
369  }
370 }
371 
372 //---------------------------------------------------------------------------------------------------------------------
374 {
375  return thirdPointId;
376 }
377 
378 //---------------------------------------------------------------------------------------------------------------------
379 void VToolBisector::SetThirdPointId(const quint32 &value)
380 {
381  if (value != NULL_ID)
382  {
383  thirdPointId = value;
384 
386  SaveOption(obj);
387  }
388 }
389 
390 //---------------------------------------------------------------------------------------------------------------------
392 {
393  ShowToolVisualization<VisToolBisector>(show);
394 }
395 
396 //---------------------------------------------------------------------------------------------------------------------
398 {
399  return firstPointId;
400 }
401 
402 //---------------------------------------------------------------------------------------------------------------------
403 void VToolBisector::SetFirstPointId(const quint32 &value)
404 {
405  if (value != NULL_ID)
406  {
407  firstPointId = value;
408 
410  SaveOption(obj);
411  }
412 }
The DialogBisector class dialog for ToolBisector. 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
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 VToolBisector class tool for creation bisector point.
Definition: vtoolbisector.h:74
quint32 GetFirstPointId() const
static const QString ToolType
Definition: vtoolbisector.h:91
void SetFirstPointId(const quint32 &value)
static VToolBisector * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool form GUI.
QString FirstPointName() const
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
VToolBisector(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType, const QString &lineWeight, const QString &lineColor, const QString &formula, const quint32 &firstPointId, const quint32 &secondPointId, const quint32 &thirdPointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolBisector constructor.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
quint32 firstPointId
firstPointId id first point of angle.
quint32 thirdPointId
thirdPointId id third point of angle.
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
static QPointF FindPoint(const QPointF &firstPoint, const QPointF &secondPoint, const QPointF &thirdPoint, const qreal &length)
FindPoint find bisector point.
static qreal BisectorAngle(const QPointF &firstPoint, const QPointF &secondPoint, const QPointF &thirdPoint)
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
void SetThirdPointId(const quint32 &value)
quint32 GetThirdPointId() const
QString ThirdPointName() const
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void SetVisualization() Q_DECL_OVERRIDE
The VToolLinePoint class parent for all tools what create point with line.
qreal angle
formula string with length formula.
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
void setLength(const QString &expression)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setObject3Id(const quint32 &value)
void setObject2Id(const quint32 &value)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
#define SCASSERT(cond)
Definition: def.h:317
@ Bisector
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString AttrSecondPoint
Definition: ifcdef.cpp:97
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString AttrFirstPoint
Definition: ifcdef.cpp:96
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString ColorBlack
Definition: ifcdef.cpp:373
const QString AttrThirdPoint
Definition: ifcdef.cpp:98
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