Seamly2D
Code documentation
vtoolcutsplinepath.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 vtoolcutsplinepath.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 15 12, 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 "vtoolcutsplinepath.h"
53 
54 #include <QPointF>
55 #include <QSharedPointer>
56 #include <QStaticStringData>
57 #include <QStringData>
58 #include <QStringDataPtr>
59 #include <QVector>
60 #include <new>
61 
62 #include "../../../../../dialogs/tools/dialogcutsplinepath.h"
63 #include "../../../../../dialogs/tools/dialogtool.h"
64 #include "../../../../../visualization/visualization.h"
65 #include "../../../../../visualization/path/vistoolcutsplinepath.h"
66 #include "../ifc/exception/vexception.h"
67 #include "../ifc/ifcdef.h"
68 #include "../vgeometry/vabstractcubicbezierpath.h"
69 #include "../vgeometry/vabstractcurve.h"
70 #include "../vgeometry/vpointf.h"
71 #include "../vgeometry/vspline.h"
72 #include "../vgeometry/vsplinepath.h"
73 #include "../vgeometry/vsplinepoint.h"
74 #include "../vmisc/vabstractapplication.h"
75 #include "../vmisc/vcommonsettings.h"
76 #include "../vpatterndb/vcontainer.h"
77 #include "../vpatterndb/vtranslatevars.h"
78 #include "../vwidgets/vmaingraphicsscene.h"
79 #include "../../../../vabstracttool.h"
80 #include "../../../vdrawtool.h"
81 #include "vtoolcut.h"
82 
83 template <class T> class QSharedPointer;
84 
85 const QString VToolCutSplinePath::ToolType = QStringLiteral("cutSplinePath");
86 const QString VToolCutSplinePath::AttrSplinePath = QStringLiteral("splinePath");
87 
88 //---------------------------------------------------------------------------------------------------------------------
89 /**
90  * @brief VToolCutSplinePath constructor.
91  * @param doc dom document container.
92  * @param data container with variables.
93  * @param id object id in container.
94  * @param formula string with formula length first splinePath.
95  * @param splinePathId id splinePath (we cut this splinePath) in data container.
96  * @param typeCreation way we create this tool.
97  * @param parent parent object.
98  */
100  const QString &formula, const quint32 &splinePathId,
101  const Source &typeCreation, QGraphicsItem *parent)
102  :VToolCut(doc, data, id, formula, splinePathId, parent)
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())
115  SCASSERT(not dialogTool.isNull())
117  dialogTool->SetFormula(formula);
118  dialogTool->setSplinePathId(curveCutId);
119  dialogTool->SetPointName(point->name());
120 }
121 
122 //---------------------------------------------------------------------------------------------------------------------
123 /**
124  * @brief Create help create tool form GUI.
125  * @param dialog dialog.
126  * @param scene pointer to scene.
127  * @param doc dom document container.
128  * @param data container with variables.
129  */
131  VAbstractPattern *doc, VContainer *data)
132 {
133  SCASSERT(not dialog.isNull())
134  QSharedPointer<DialogCutSplinePath> dialogTool = dialog.objectCast<DialogCutSplinePath>();
135  SCASSERT(not dialogTool.isNull())
136  const QString pointName = dialogTool->getPointName();
137  QString formula = dialogTool->GetFormula();
138  const quint32 splinePathId = dialogTool->getSplinePathId();
139  VToolCutSplinePath* point = Create(0, pointName, formula, splinePathId, 5, 10, true, scene, doc, data,
141  if (point != nullptr)
142  {
143  point->m_dialog = dialogTool;
144  }
145  return point;
146 }
147 
148 //---------------------------------------------------------------------------------------------------------------------
149 /**
150  * @brief Create help create tool.
151  * @param _id tool id, 0 if tool doesn't exist yet.
152  * @param pointName point name.
153  * @param formula string with formula length first splinePath.
154  * @param splinePathId id of splinePath in data container.
155  * @param mx label bias x axis.
156  * @param my label bias y axis.
157  * @param scene pointer to scene.
158  * @param doc dom document container.
159  * @param data container with variables.
160  * @param parse parser file mode.
161  * @param typeCreation way we create this tool.
162  */
163 VToolCutSplinePath* VToolCutSplinePath::Create(const quint32 _id, const QString &pointName, QString &formula,
164  quint32 splinePathId, qreal mx, qreal my, bool showPointName,
166  VContainer *data, const Document &parse, const Source &typeCreation)
167 {
168  const auto splPath = data->GeometricObject<VAbstractCubicBezierPath>(splinePathId);
169  SCASSERT(splPath != nullptr)
170 
171  const qreal result = CheckFormula(_id, formula, data);
172 
173  quint32 id = _id;
174  VSplinePath *splPath1 = nullptr;
175  VSplinePath *splPath2 = nullptr;
176 
177  VPointF *p = VToolCutSplinePath::CutSplinePath(qApp->toPixel(result), splPath, pointName, &splPath1, &splPath2);
178  p->setShowPointName(showPointName);
179 
180  SCASSERT(splPath1 != nullptr)
181  SCASSERT(splPath2 != nullptr)
182  SCASSERT(p != nullptr)
183 
184  p->setMx(mx);
185  p->setMy(my);
186 
187  if (typeCreation == Source::FromGui)
188  {
189  id = data->AddGObject(p);
190 
193  }
194  else
195  {
196  data->UpdateGObject(id, p);
197 
200 
201  if (parse != Document::FullParse)
202  {
203  doc->UpdateToolData(id, data);
204  }
205  }
206 
207  if (parse == Document::FullParse)
208  {
210  VToolCutSplinePath *point = new VToolCutSplinePath(doc, data, id, formula, splinePathId, typeCreation);
211  scene->addItem(point);
212  InitToolConnections(scene, point);
213  VAbstractPattern::AddTool(id, point);
214  doc->IncrementReferens(splPath->getIdTool());
215  return point;
216  }
217  return nullptr;
218 }
219 
220 //---------------------------------------------------------------------------------------------------------------------
222 {
223  ShowToolVisualization<VisToolCutSplinePath>(show);
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
228  const QString &pName, VSplinePath **splPath1, VSplinePath **splPath2)
229 {
230  SCASSERT(splPath != nullptr)
231 
232  QPointF spl1p2, spl1p3, spl2p2, spl2p3;
233  qint32 p1 = 0, p2 = 0;
234 
235  const QPointF point = splPath->CutSplinePath(length, p1, p2, spl1p2, spl1p3, spl2p2, spl2p3);
236  VPointF *p = new VPointF(point);
237  p->setName(pName);
238 
239  const QVector<VSplinePoint> points = splPath->GetSplinePath();
240 
241  const VSplinePoint splP1 = points.at(p1);
242  const VSplinePoint splP2 = points.at(p2);
243  const VSpline spl1 = VSpline(splP1.P(), spl1p2, spl1p3, *p);
244  const VSpline spl2 = VSpline(*p, spl2p2, spl2p3, splP2.P());
245 
246  *splPath1 = new VSplinePath();
247  *splPath2 = new VSplinePath();
248 
249  for (qint32 i = 0; i < points.size(); i++)
250  {
251  if (i <= p1 && i < p2)
252  {
253  if (i == p1)
254  {
255  const qreal angle1 = spl1.GetStartAngle()+180;
256  const QString angle1F = QString().number(angle1);
257 
258  (*splPath1)->append(VSplinePoint(splP1.P(), angle1, angle1F, spl1.GetStartAngle(),
259  spl1.GetStartAngleFormula(), splP1.Length1(), splP1.Length1Formula(),
260  spl1.GetC1Length(), spl1.GetC1LengthFormula()));
261 
262  const qreal angle2 = spl1.GetEndAngle()+180;
263  const QString angle2F = QString().number(angle2);
264 
265  const auto cutPoint = VSplinePoint(*p, spl1.GetEndAngle(), spl1.GetEndAngleFormula(), angle2, angle2F,
266  spl1.GetC2Length(), spl1.GetC2LengthFormula(), spl2.GetC1Length(),
267  spl2.GetC1LengthFormula());
268  (*splPath1)->append(cutPoint);
269  continue;
270  }
271  (*splPath1)->append(points.at(i));
272  }
273  else
274  {
275  if (i == p2)
276  {
277  const qreal angle1 = spl2.GetStartAngle()+180;
278  const QString angle1F = QString().number(angle1);
279 
280  const auto cutPoint = VSplinePoint(*p, angle1, angle1F, spl2.GetStartAngle(),
281  spl2.GetStartAngleFormula(), spl1.GetC2Length(),
282  spl1.GetC2LengthFormula(), spl2.GetC1Length(),
283  spl2.GetC1LengthFormula());
284 
285  (*splPath2)->append(cutPoint);
286 
287  const qreal angle2 = spl2.GetEndAngle()+180;
288  const QString angle2F = QString().number(angle2);
289 
290  (*splPath2)->append(VSplinePoint(splP2.P(), spl2.GetEndAngle(), spl2.GetEndAngleFormula(), angle2,
291  angle2F, spl2.GetC2Length(), spl2.GetC2LengthFormula(),
292  splP2.Length2(), splP2.Length2Formula()));
293  continue;
294  }
295  (*splPath2)->append(points.at(i));
296  }
297  }
298 
299  return p;
300 }
301 
302 //---------------------------------------------------------------------------------------------------------------------
303 /**
304  * @brief contextMenuEvent handle context menu events.
305  * @param event context menu event.
306  */
307 void VToolCutSplinePath::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
308 {
309  try
310  {
311  ContextMenu<DialogCutSplinePath>(event, id);
312  }
313  catch(const VExceptionToolWasDeleted &e)
314  {
315  Q_UNUSED(e)
316  return;//Leave this method immediately!!!
317  }
318 }
319 
320 //---------------------------------------------------------------------------------------------------------------------
321 /**
322  * @brief SaveDialog save options into file after change in dialog.
323  */
324 void VToolCutSplinePath::SaveDialog(QDomElement &domElement)
325 {
326  SCASSERT(not m_dialog.isNull())
328  SCASSERT(not dialogTool.isNull())
329  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
330  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
331  doc->SetAttribute(domElement, AttrSplinePath, QString().setNum(dialogTool->getSplinePathId()));
332 }
333 
334 //---------------------------------------------------------------------------------------------------------------------
336 {
337  VToolCut::SaveOptions(tag, obj);
338 
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
345 void VToolCutSplinePath::ReadToolAttributes(const QDomElement &domElement)
346 {
347  formula = doc->GetParametrString(domElement, AttrLength, "");
349 }
350 
351 //---------------------------------------------------------------------------------------------------------------------
353 {
354  if (not vis.isNull())
355  {
356  VisToolCutSplinePath *visual = qobject_cast<VisToolCutSplinePath *>(vis);
357  SCASSERT(visual != nullptr)
358 
359  visual->setObject1Id(curveCutId);
360  visual->setLength(qApp->TrVars()->FormulaToUser(formula, qApp->Settings()->GetOsSeparator()));
361 
363  visual->setLineStyle(lineTypeToPenStyle(curve->GetPenStyle()));
364 
365  visual->RefreshGeometry();
366  }
367 }
368 
369 //---------------------------------------------------------------------------------------------------------------------
371 {
373 
374  const QString expression = qApp->TrVars()->FormulaToUser(formula, qApp->Settings()->GetOsSeparator());
375  const qreal length = Visualization::FindVal(expression, VAbstractTool::data.DataVariables());
376 
377  VSplinePath *splPath1 = nullptr;
378  VSplinePath *splPath2 = nullptr;
379  VPointF *p = VToolCutSplinePath::CutSplinePath(qApp->toPixel(length), splPath, "X", &splPath1, &splPath2);
380  delete p; // Don't need this point
381 
382  const QString curveStr = tr("Curve");
383  const QString lengthStr = tr("length");
384 
385  const QString toolTip = QString("<table>"
386  "<tr> <td><b>%6:</b> %7</td> </tr>"
387  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
388  "<tr> <td><b>%8:</b> %9</td> </tr>"
389  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
390  "</table>")
391  .arg(curveStr + QLatin1String("1 ") + lengthStr)
392  .arg(qApp->fromPixel(splPath1->GetLength()))
393  .arg(UnitsToStr(qApp->patternUnit(), true))
394  .arg(curveStr + QLatin1String("2 ") + lengthStr)
395  .arg(qApp->fromPixel(splPath2->GetLength()))
396  .arg(curveStr + QLatin1String(" 1") + tr("label"))
397  .arg(splPath1->name())
398  .arg(curveStr + QLatin1String(" 2") + tr("label"))
399  .arg(splPath2->name());
400 
401  delete splPath1;
402  delete splPath2;
403 
404  return toolTip;
405 }
The DialogCutSplinePath class dialog for ToolCutSplinePath.
virtual qreal GetLength() const Q_DECL_OVERRIDE
GetLength return length of spline path.
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
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
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
void AddSpline(const QSharedPointer< VAbstractBezier > &curve, quint32 id, quint32 parentId=null_id)
Definition: vcontainer.cpp:440
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
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
void setName(const QString &name)
setName set name graphical object.
Definition: vgobject.cpp:158
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 setMx(qreal mx)
setMx set offset name respect to x
Definition: vpointf.cpp:197
void setMy(qreal my)
setMy set offset name respect to y
Definition: vpointf.cpp:207
void setShowPointName(bool show)
Definition: vpointf.cpp:265
The VSplinePath class keep information about splinePath.
Definition: vsplinepath.h:72
The VSplinePoint class keep information about point in spline path. Each point have two angles and tw...
Definition: vsplinepoint.h:108
QString Length1Formula() const
qreal Length2() const
QString Length2Formula() const
VPointF P() const
qreal Length1() const
VSpline class that implements the spline.
Definition: vspline.h:75
virtual qreal GetC2Length() const Q_DECL_OVERRIDE
Definition: vspline.cpp:385
QString GetC1LengthFormula() const
Definition: vspline.cpp:391
virtual qreal GetStartAngle() const Q_DECL_OVERRIDE
GetAngle1 return first angle control line.
Definition: vspline.cpp:337
QString GetStartAngleFormula() const
Definition: vspline.cpp:353
QString GetEndAngleFormula() const
Definition: vspline.cpp:359
virtual qreal GetC1Length() const Q_DECL_OVERRIDE
Definition: vspline.cpp:379
QString GetC2LengthFormula() const
Definition: vspline.cpp:397
virtual qreal GetEndAngle() const Q_DECL_OVERRIDE
GetAngle2 return second angle control line.
Definition: vspline.cpp:347
The VToolCutSplinePath class for tool CutSplinePath. This tool find point on splinePath and cut splin...
static VToolCutSplinePath * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool form GUI.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
static const QString AttrSplinePath
virtual QString makeToolTip() const Q_DECL_OVERRIDE
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.
static const QString ToolType
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
VToolCutSplinePath(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &formula, const quint32 &splinePathId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolCutSplinePath constructor.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void SetVisualization() Q_DECL_OVERRIDE
static Q_REQUIRED_RESULT VPointF * CutSplinePath(qreal length, const QSharedPointer< VAbstractCubicBezierPath > &splPath, const QString &pName, VSplinePath **splPath1, VSplinePath **splPath2)
QString formula
formula keep formula of length
Definition: vtoolcut.h:96
quint32 curveCutId
Definition: vtoolcut.h:98
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLength(const QString &expression)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
static qreal FindVal(const QString &expression, const QHash< QString, QSharedPointer< VInternalVariable > > *vars)
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
@ CutSplinePath
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString splPath
Definition: ifcdef.cpp:419
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrType
Definition: ifcdef.cpp:73
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