Seamly2D
Code documentation
vtoolendline.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 vtoolendline.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 "vtoolendline.h"
53 
54 #include <QLineF>
55 #include <QPointF>
56 #include <QSharedPointer>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <new>
61 
62 #include "../../../../../dialogs/tools/dialogendline.h"
63 #include "../../../../../dialogs/tools/dialogtool.h"
64 #include "../../../../../visualization/visualization.h"
65 #include "../../../../../visualization/line/vistoolendline.h"
66 #include "../ifc/exception/vexception.h"
67 #include "../ifc/ifcdef.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 VToolEndLine::ToolType = QStringLiteral("endLine");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolEndLine 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 formulaLength string with formula length of line.
92  * @param formulaAngle formula angle of line.
93  * @param basePointId id first point of line.
94  * @param typeCreation way we create this tool.
95  * @param parent parent object.
96  */
98  const QString &lineType, const QString &lineWeight,
99  const QString &lineColor, const QString &formulaLength, const QString &formulaAngle,
100  const quint32 &basePointId, const Source &typeCreation, QGraphicsItem *parent)
101  : VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, formulaLength, basePointId, 0, parent)
102  , formulaAngle(formulaAngle)
103 {
104  ToolCreation(typeCreation);
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 /**
109  * @brief setDialog set dialog when user want change tool option.
110  */
112 {
113  SCASSERT(not m_dialog.isNull())
114  m_dialog->setModal(true);
115  QSharedPointer<DialogEndLine> dialogTool = m_dialog.objectCast<DialogEndLine>();
116  SCASSERT(not dialogTool.isNull())
118  dialogTool->setLineColor(lineColor);
119  dialogTool->setLineType(m_lineType);
120  dialogTool->setLineWeight(m_lineWeight);
121  dialogTool->SetFormula(formulaLength);
122  dialogTool->SetAngle(formulaAngle);
123  dialogTool->SetBasePointId(basePointId);
124  dialogTool->SetPointName(p->name());
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
128 /**
129  * @brief Create help create tool from GUI.
130  * @param dialog dialog.
131  * @param scene pointer to scene.
132  * @param doc dom document container.
133  * @param data container with variables.
134  * @return the created tool
135  */
137  VContainer *data)
138 {
139  SCASSERT(not dialog.isNull())
140  QSharedPointer<DialogEndLine> dialogTool = dialog.objectCast<DialogEndLine>();
141  SCASSERT(not dialogTool.isNull())
142  const QString pointName = dialogTool->getPointName();
143  const QString lineColor = dialogTool->getLineColor();
144  const QString lineType = dialogTool->getLineType();
145  const QString lineWeight = dialogTool->getLineWeight();
146  QString formulaLength = dialogTool->GetFormula();
147  QString formulaAngle = dialogTool->GetAngle();
148  const quint32 basePointId = dialogTool->GetBasePointId();
149 
150  VToolEndLine *point = Create(0, pointName, lineType, lineWeight, lineColor, formulaLength, formulaAngle,
151  basePointId, 5, 10, true, scene, doc, data, Document::FullParse, Source::FromGui);
152  if (point != nullptr)
153  {
154  point->m_dialog = dialogTool;
155  }
156  return point;
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
160 /**
161  * @brief Create help create tool.
162  * @param _id tool id, 0 if tool doesn't exist yet.
163  * @param pointName point name.
164  * @param lineType line type.
165  * @param lineWeight line weight.
166  * @param lineColor line color.
167  * @param formulaLength string with formula length of line.
168  * @param formulaAngle formula angle of line.
169  * @param basePointId id first point of line.
170  * @param mx label bias x axis.
171  * @param my label bias y axis.
172  * @param showPointName show/hide point name text
173  * @param scene pointer to scene.
174  * @param doc dom document container.
175  * @param data container with variables.
176  * @param parse parser file mode.
177  * @param typeCreation way we create this tool.
178  * @return the created tool
179  */
180 VToolEndLine* VToolEndLine::Create(const quint32 _id, const QString &pointName,
181  const QString &lineType, const QString &lineWeight,
182  const QString &lineColor, QString &formulaLength, QString &formulaAngle,
183  quint32 basePointId, qreal mx, qreal my, bool showPointName,
184  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
185  const Document &parse,
186  const Source &typeCreation)
187 {
189  QLineF line = QLineF(static_cast<QPointF>(*basePoint), QPointF(basePoint->x()+100, basePoint->y()));
190 
191  line.setAngle(CheckFormula(_id, formulaAngle, data)); //First set angle.
192  line.setLength(qApp->toPixel(CheckFormula(_id, formulaLength, data)));
193  quint32 id = _id;
194  VPointF *p = new VPointF(line.p2(), pointName, mx, my);
195  p->setShowPointName(showPointName);
196 
197  if (typeCreation == Source::FromGui)
198  {
199  id = data->AddGObject(p);
200  data->AddLine(basePointId, id);
201  }
202  else
203  {
204  data->UpdateGObject(id, p);
205  data->AddLine(basePointId, id);
206  if (parse != Document::FullParse)
207  {
208  doc->UpdateToolData(id, data);
209  }
210  }
211 
212  if (parse == Document::FullParse)
213  {
215  VToolEndLine *point = new VToolEndLine(doc, data, id, lineType, lineWeight, lineColor, formulaLength, formulaAngle,
216  basePointId, typeCreation);
217  scene->addItem(point);
218  InitToolConnections(scene, point);
219  VAbstractPattern::AddTool(id, point);
220  doc->IncrementReferens(basePoint->getIdTool());
221  return point;
222  }
223  return nullptr;
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
227 /**
228  * @brief contextMenuEvent handle context menu events.
229  * @param event context menu event.
230  */
231 void VToolEndLine::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
232 {
233  try
234  {
235  ContextMenu<DialogEndLine>(event, id);
236  }
237  catch(const VExceptionToolWasDeleted &e)
238  {
239  Q_UNUSED(e)
240  return;//Leave this method immediately!!!
241  }
242 }
243 
244 //---------------------------------------------------------------------------------------------------------------------
245 /**
246  * @brief SaveDialog save options into file after change in dialog.
247  */
248 void VToolEndLine::SaveDialog(QDomElement &domElement)
249 {
250  SCASSERT(not m_dialog.isNull())
251  QSharedPointer<DialogEndLine> dialogTool = m_dialog.objectCast<DialogEndLine>();
252  SCASSERT(not dialogTool.isNull())
253  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
254  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
255  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
256  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
257  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
258  doc->SetAttribute(domElement, AttrAngle, dialogTool->GetAngle());
259  doc->SetAttribute(domElement, AttrBasePoint, QString().setNum(dialogTool->GetBasePointId()));
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
264 {
265  VToolLinePoint::SaveOptions(tag, obj);
266 
271 }
272 
273 //---------------------------------------------------------------------------------------------------------------------
274 void VToolEndLine::ReadToolAttributes(const QDomElement &domElement)
275 {
277  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
279  formulaLength = doc->GetParametrString(domElement, AttrLength, "");
281  formulaAngle = doc->GetParametrString(domElement, AttrAngle, "");
282 }
283 
284 //---------------------------------------------------------------------------------------------------------------------
286 {
287  if (not vis.isNull())
288  {
289  VisToolEndLine *visual = qobject_cast<VisToolEndLine *>(vis);
290  SCASSERT(visual != nullptr)
291 
292  visual->setObject1Id(basePointId);
293  visual->setLength(qApp->TrVars()->FormulaToUser(formulaLength, qApp->Settings()->GetOsSeparator()));
294  visual->SetAngle(qApp->TrVars()->FormulaToUser(formulaAngle, qApp->Settings()->GetOsSeparator()));
296  visual->setLineWeight(m_lineWeight);
297  visual->RefreshGeometry();
298  }
299 }
300 
301 //---------------------------------------------------------------------------------------------------------------------
303 {
304  VFormula fAngle(formulaAngle, getData());
305  fAngle.setCheckZero(false);
306  fAngle.setToolId(m_id);
307  fAngle.setPostfix(degreeSymbol);
308  return fAngle;
309 }
310 
311 //---------------------------------------------------------------------------------------------------------------------
313 {
314  if (value.error() == false)
315  {
317 
319  SaveOption(obj);
320  }
321 }
322 
323 //---------------------------------------------------------------------------------------------------------------------
325 {
326  ShowToolVisualization<VisToolEndLine>(show);
327 }
The DialogEndLine class dialog for ToolEndLine. Help create point and edit option.
Definition: dialogendline.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.
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
The VToolEndLine class tool for creation point on the line end.
Definition: vtoolendline.h:74
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
VToolEndLine(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType, const QString &lineWeight, const QString &lineColor, const QString &formulaLength, const QString &formulaAngle, const quint32 &basePointId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolEndLine constructor.
VFormula GetFormulaAngle() const
static VToolEndLine * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
QString formulaAngle
Definition: vtoolendline.h:110
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
static const QString ToolType
Definition: vtoolendline.h:90
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.
void SetFormulaAngle(const VFormula &value)
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
QString formulaLength
void SetAngle(const QString &expression)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLength(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
#define SCASSERT(cond)
Definition: def.h:317
@ EndLine
Source
Definition: def.h:106
const QString AttrBasePoint
Definition: ifcdef.cpp:95
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
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_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67