Seamly2D
Code documentation
vtoolnormal.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 vtoolnormal.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 "vtoolnormal.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/dialognormal.h"
62 #include "../../../../../dialogs/tools/dialogtool.h"
63 #include "../../../../../visualization/visualization.h"
64 #include "../../../../../visualization/line/vistoolnormal.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 VToolNormal::ToolType = QStringLiteral("normal");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolNormal 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 normal length.
92  * @param angle additional angle.
93  * @param firstPointId id first line point.
94  * @param secondPointId id second line point.
95  * @param typeCreation way we create this tool.
96  * @param parent parent object.
97  */
98 VToolNormal::VToolNormal(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType,
99  const QString &lineWeight, const QString &lineColor, const QString &formula,
100  const qreal &angle,
101  const quint32 &firstPointId, const quint32 &secondPointId, const Source &typeCreation,
102  QGraphicsItem *parent)
103  :VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, formula, firstPointId, angle, parent),
104  secondPointId(secondPointId)
105 {
106 
107  ToolCreation(typeCreation);
108 }
109 
110 //---------------------------------------------------------------------------------------------------------------------
111 /**
112  * @brief setDialog set dialog when user want change tool option.
113  */
115 {
116  SCASSERT(not m_dialog.isNull())
117  QSharedPointer<DialogNormal> dialogTool = m_dialog.objectCast<DialogNormal>();
118  SCASSERT(not dialogTool.isNull())
120  dialogTool->setLineType(m_lineType);
121  dialogTool->setLineWeight(m_lineWeight);
122  dialogTool->setLineColor(lineColor);
123  dialogTool->SetFormula(formulaLength);
124  dialogTool->SetAngle(angle);
125  dialogTool->SetFirstPointId(basePointId);
126  dialogTool->SetSecondPointId(secondPointId);
127  dialogTool->SetPointName(p->name());
128 }
129 
130 //---------------------------------------------------------------------------------------------------------------------
131 /**
132  * @brief Create help create tool from GUI.
133  * @param dialog dialog.
134  * @param scene pointer to scene.
135  * @param doc dom document container.
136  * @param data container with variables.
137  */
139  VContainer *data)
140 {
141  SCASSERT(not dialog.isNull())
142  QSharedPointer<DialogNormal> dialogTool = dialog.objectCast<DialogNormal>();
143  SCASSERT(not dialogTool.isNull())
144  QString formula = dialogTool->GetFormula();
145  const quint32 firstPointId = dialogTool->GetFirstPointId();
146  const quint32 secondPointId = dialogTool->GetSecondPointId();
147  const QString lineType = dialogTool->getLineType();
148  const QString lineWeight = dialogTool->getLineWeight();
149  const QString lineColor = dialogTool->getLineColor();
150  const QString pointName = dialogTool->getPointName();
151  const qreal angle = dialogTool->GetAngle();
152  VToolNormal *point = Create(0, formula, firstPointId, secondPointId, lineType, lineWeight, lineColor, pointName, angle,
153  5, 10, true, scene, doc, data, Document::FullParse, Source::FromGui);
154  if (point != nullptr)
155  {
156  point->m_dialog = dialogTool;
157  }
158  return point;
159 }
160 
161 //---------------------------------------------------------------------------------------------------------------------
162 /**
163  * @brief Create help create tool.
164  * @param _id tool id, 0 if tool doesn't exist yet.
165  * @param formula string with formula normal length.
166  * @param firstPointId id first line point.
167  * @param secondPointId id second line point.
168  * @param lineType line type.
169  * @param lineWeight line weight.
170  * @param lineColor line color.
171  * @param pointName point name.
172  * @param angle additional angle.
173  * @param mx label bias x axis.
174  * @param my label bias y axis.
175  * @param showPointName show/hide point name text.
176  * @param scene pointer to scene.
177  * @param doc dom document container.
178  * @param data container with variables.
179  * @param parse parser file mode.
180  * @param typeCreation way we create this tool.
181  */
182 VToolNormal* VToolNormal::Create(const quint32 _id, QString &formula, quint32 firstPointId,
183  quint32 secondPointId, const QString &lineType, const QString &lineWeight,
184  const QString &lineColor,
185  const QString &pointName, qreal angle, qreal mx, qreal my, bool showPointName,
186  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
187  const Document &parse,
188  const Source &typeCreation)
189 {
190  const QSharedPointer<VPointF> firstPoint = data->GeometricObject<VPointF>(firstPointId);
192 
193  const qreal result = CheckFormula(_id, formula, data);
194 
195  QPointF fPoint = VToolNormal::FindPoint(static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*secondPoint),
196  qApp->toPixel(result), angle);
197  quint32 id = _id;
198  VPointF *p = new VPointF(fPoint, pointName, mx, my);
199  p->setShowPointName(showPointName);
200 
201  if (typeCreation == Source::FromGui)
202  {
203  id = data->AddGObject(p);
204  data->AddLine(firstPointId, id);
205  }
206  else
207  {
208  data->UpdateGObject(id, p);
209  data->AddLine(firstPointId, id);
210  if (parse != Document::FullParse)
211  {
212  doc->UpdateToolData(id, data);
213  }
214  }
215 
216  if (parse == Document::FullParse)
217  {
219  VToolNormal *point = new VToolNormal(doc, data, id, lineType, lineWeight, lineColor, formula, angle, firstPointId,
220  secondPointId, typeCreation);
221  scene->addItem(point);
222  InitToolConnections(scene, point);
223  VAbstractPattern::AddTool(id, point);
224  doc->IncrementReferens(firstPoint->getIdTool());
225  doc->IncrementReferens(secondPoint->getIdTool());
226  return point;
227  }
228  return nullptr;
229 }
230 
231 //---------------------------------------------------------------------------------------------------------------------
232 /**
233  * @brief FindPoint return normal point.
234  * @param firstPoint first line point.
235  * @param secondPoint second line point.
236  * @param length normal length.
237  * @param angle additional angle.
238  * @return normal point.
239  */
240 QPointF VToolNormal::FindPoint(const QPointF &firstPoint, const QPointF &secondPoint, const qreal &length,
241  const qreal &angle)
242 {
243  QLineF line(firstPoint, secondPoint);
244  QLineF normal = line.normalVector();
245  normal.setAngle(normal.angle()+angle);
246  normal.setLength(length);
247  return normal.p2();
248 }
249 
250 //---------------------------------------------------------------------------------------------------------------------
252 {
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
257 /**
258  * @brief contextMenuEvent handle context menu events.
259  * @param event context menu event.
260  */
261 void VToolNormal::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
262 {
263  try
264  {
265  ContextMenu<DialogNormal>(event, id);
266  }
267  catch(const VExceptionToolWasDeleted &e)
268  {
269  Q_UNUSED(e)
270  return;//Leave this method immediately!!!
271  }
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
275 /**
276  * @brief RemoveReferens decrement value of reference.
277  */
279 {
280  const auto secondPoint = VAbstractTool::data.GetGObject(secondPointId);
281  doc->DecrementReferens(secondPoint->getIdTool());
283 }
284 
285 //---------------------------------------------------------------------------------------------------------------------
286 /**
287  * @brief SaveDialog save options into file after change in dialog.
288  */
289 void VToolNormal::SaveDialog(QDomElement &domElement)
290 {
291  SCASSERT(not m_dialog.isNull())
292  QSharedPointer<DialogNormal> dialogTool = m_dialog.objectCast<DialogNormal>();
293  SCASSERT(not dialogTool.isNull())
294  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
295  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
296  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
297  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
298  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
299  doc->SetAttribute(domElement, AttrAngle, QString().setNum(dialogTool->GetAngle()));
300  doc->SetAttribute(domElement, AttrFirstPoint, QString().setNum(dialogTool->GetFirstPointId()));
301  doc->SetAttribute(domElement, AttrSecondPoint, QString().setNum(dialogTool->GetSecondPointId()));
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
306 {
307  VToolLinePoint::SaveOptions(tag, obj);
308 
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
317 void VToolNormal::ReadToolAttributes(const QDomElement &domElement)
318 {
320  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
322  formulaLength = doc->GetParametrString(domElement, AttrLength, "");
325  angle = doc->GetParametrDouble(domElement, AttrAngle, "0");
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
331  if (not vis.isNull())
332  {
333  VisToolNormal *visual = qobject_cast<VisToolNormal *>(vis);
334  SCASSERT(visual != nullptr)
335 
336  visual->setObject1Id(basePointId);
337  visual->setObject2Id(secondPointId);
338  visual->setLength(qApp->TrVars()->FormulaToUser(formulaLength, qApp->Settings()->GetOsSeparator()));
339  visual->SetAngle(angle);
341  visual->setLineWeight(m_lineWeight);
342  visual->RefreshGeometry();
343  }
344 }
345 
346 //---------------------------------------------------------------------------------------------------------------------
348 {
349  return secondPointId;
350 }
351 
352 //---------------------------------------------------------------------------------------------------------------------
353 void VToolNormal::SetSecondPointId(const quint32 &value)
354 {
355  if (value != NULL_ID)
356  {
357  secondPointId = value;
358 
360  SaveOption(obj);
361  }
362 }
363 
364 //---------------------------------------------------------------------------------------------------------------------
366 {
367  ShowToolVisualization<VisToolNormal>(show);
368 }
The DialogNormal class dialog for ToolNormal. Help create point and edit option.
Definition: dialognormal.h:73
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?
static qreal GetParametrDouble(const QDomElement &domElement, const QString &name, const QString &defValue)
Returns the double value of the given attribute.
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 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
The VToolNormal class tool for creation point on normal. Normal begin from first point of line.
Definition: vtoolnormal.h:74
static const QString ToolType
Definition: vtoolnormal.h:91
virtual void SetVisualization() 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.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
quint32 secondPointId
secondPointId id second line point.
Definition: vtoolnormal.h:116
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
QString SecondPointName() const
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
void SetSecondPointId(const quint32 &value)
VToolNormal(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &typeLine, const QString &lineWeight, const QString &lineColor, const QString &formula, const qreal &angle, const quint32 &firstPointId, const quint32 &secondPointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolNormal constructor.
Definition: vtoolnormal.cpp:98
static QPointF FindPoint(const QPointF &firstPoint, const QPointF &secondPoint, const qreal &length, const qreal &angle=0)
FindPoint return normal point.
quint32 GetSecondPointId() const
static VToolNormal * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void SetAngle(const qreal &value)
void setObject2Id(const quint32 &value)
void setLength(const QString &expression)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
#define SCASSERT(cond)
Definition: def.h:317
@ Normal
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 AttrAngle
Definition: ifcdef.cpp:103
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