Seamly2D
Code documentation
vtoolcutspline.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 vtoolcutspline.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 "vtoolcutspline.h"
53 
54 #include <QPointF>
55 #include <QSharedPointer>
56 #include <QStaticStringData>
57 #include <QStringData>
58 #include <QStringDataPtr>
59 #include <new>
60 
61 #include "../../../../../dialogs/tools/dialogcutspline.h"
62 #include "../../../../../dialogs/tools/dialogtool.h"
63 #include "../../../../../visualization/visualization.h"
64 #include "../../../../../visualization/path/vistoolcutspline.h"
65 #include "../ifc/exception/vexception.h"
66 #include "../ifc/ifcdef.h"
67 #include "../vgeometry/vabstractcubicbezier.h"
68 #include "../vgeometry/vabstractcurve.h"
69 #include "../vgeometry/vpointf.h"
70 #include "../vgeometry/vspline.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 "vtoolcut.h"
79 
80 template <class T> class QSharedPointer;
81 
82 const QString VToolCutSpline::ToolType = QStringLiteral("cutSpline");
83 const QString VToolCutSpline::AttrSpline = QStringLiteral("spline");
84 
85 //---------------------------------------------------------------------------------------------------------------------
86 /**
87  * @brief VToolCutSpline constructor.
88  * @param doc dom document container.
89  * @param data container with variables.
90  * @param id object id in container.
91  * @param formula string with formula length first spline.
92  * @param splineId id spline in data container.
93  * @param typeCreation way we create this tool.
94  * @param parent parent object.
95  */
96 VToolCutSpline::VToolCutSpline(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &formula,
97  const quint32 &splineId, const Source &typeCreation, QGraphicsItem *parent)
98  :VToolCut(doc, data, id, formula, splineId, parent)
99 {
100  ToolCreation(typeCreation);
101 }
102 
103 //---------------------------------------------------------------------------------------------------------------------
104 /**
105  * @brief setDialog set dialog when user want change tool option.
106  */
108 {
109  SCASSERT(not m_dialog.isNull())
110  QSharedPointer<DialogCutSpline> dialogTool = m_dialog.objectCast<DialogCutSpline>();
111  SCASSERT(not dialogTool.isNull())
113  dialogTool->SetFormula(formula);
114  dialogTool->setSplineId(curveCutId);
115  dialogTool->SetPointName(point->name());
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 /**
120  * @brief Create help create tool from GUI.
121  * @param dialog dialog.
122  * @param scene pointer to scene.
123  * @param doc dom document container.
124  * @param data container with variables.
125  */
127  VAbstractPattern *doc, VContainer *data)
128 {
129  SCASSERT(not dialog.isNull())
130  QSharedPointer<DialogCutSpline> dialogTool = dialog.objectCast<DialogCutSpline>();
131  SCASSERT(not dialogTool.isNull())
132  const QString pointName = dialogTool->getPointName();
133  QString formula = dialogTool->GetFormula();
134  const quint32 splineId = dialogTool->getSplineId();
135  VToolCutSpline* point = Create(0, pointName, formula, splineId, 5, 10, true, scene, doc, data, Document::FullParse,
137  if (point != nullptr)
138  {
139  point->m_dialog = dialogTool;
140  }
141  return point;
142 }
143 
144 //---------------------------------------------------------------------------------------------------------------------
145 /**
146  * @brief Create help create tool.
147  * @param _id tool id, 0 if tool doesn't exist yet.
148  * @param pointName point name.
149  * @param formula string with formula length first spline.
150  * @param splineId id spline in data container.
151  * @param mx label bias x axis.
152  * @param my label bias y axis.
153  * @param scene pointer to scene.
154  * @param doc dom document container.
155  * @param data container with variables.
156  * @param parse parser file mode.
157  * @param typeCreation way we create this tool.
158  */
159 VToolCutSpline* VToolCutSpline::Create(const quint32 _id, const QString &pointName, QString &formula,
160  const quint32 &splineId, qreal mx, qreal my, bool showPointName,
161  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
162  const Document &parse, const Source &typeCreation)
163 {
164  const auto spl = data->GeometricObject<VAbstractCubicBezier>(splineId);
165 
166  const qreal result = CheckFormula(_id, formula, data);
167 
168  QPointF spl1p2, spl1p3, spl2p2, spl2p3;
169  QPointF point = spl->CutSpline(qApp->toPixel(result), spl1p2, spl1p3, spl2p2, spl2p3);
170 
171  quint32 id = _id;
172  VPointF *p = new VPointF(point, pointName, mx, my);
173  p->setShowPointName(showPointName);
174 
175  auto spline1 = QSharedPointer<VAbstractBezier>(new VSpline(spl->GetP1(), spl1p2, spl1p3, *p));
176  auto spline2 = QSharedPointer<VAbstractBezier>(new VSpline(*p, spl2p2, spl2p3, spl->GetP4()));
177 
178  if (typeCreation == Source::FromGui)
179  {
180  id = data->AddGObject(p);
181  data->AddSpline(spline1, NULL_ID, id);
182  data->AddSpline(spline2, NULL_ID, id);
183  }
184  else
185  {
186  data->UpdateGObject(id, p);
187  data->AddSpline(spline1, NULL_ID, id);
188  data->AddSpline(spline2, NULL_ID, id);
189 
190  if (parse != Document::FullParse)
191  {
192  doc->UpdateToolData(id, data);
193  }
194  }
195 
196  if (parse == Document::FullParse)
197  {
199  VToolCutSpline *point = new VToolCutSpline(doc, data, id, formula, splineId, typeCreation);
200  scene->addItem(point);
201  InitToolConnections(scene, point);
202  VAbstractPattern::AddTool(id, point);
203  doc->IncrementReferens(spl->getIdTool());
204  return point;
205  }
206  return nullptr;
207 }
208 
209 //---------------------------------------------------------------------------------------------------------------------
211 {
212  ShowToolVisualization<VisToolCutSpline>(show);
213 }
214 
215 //---------------------------------------------------------------------------------------------------------------------
216 /**
217  * @brief contextMenuEvent handle context menu events.
218  * @param event context menu event.
219  */
220 void VToolCutSpline::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
221 {
222  try
223  {
224  ContextMenu<DialogCutSpline>(event, id);
225  }
226  catch(const VExceptionToolWasDeleted &e)
227  {
228  Q_UNUSED(e)
229  return;//Leave this method immediately!!!
230  }
231 }
232 
233 //---------------------------------------------------------------------------------------------------------------------
234 /**
235  * @brief SaveDialog save options into file after change in dialog.
236  */
237 void VToolCutSpline::SaveDialog(QDomElement &domElement)
238 {
239  SCASSERT(not m_dialog.isNull())
240  QSharedPointer<DialogCutSpline> dialogTool = m_dialog.objectCast<DialogCutSpline>();
241  SCASSERT(not dialogTool.isNull())
242  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
243  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
244  doc->SetAttribute(domElement, AttrSpline, QString().setNum(dialogTool->getSplineId()));
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
249 {
250  VToolCut::SaveOptions(tag, obj);
251 
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
258 void VToolCutSpline::ReadToolAttributes(const QDomElement &domElement)
259 {
260  formula = doc->GetParametrString(domElement, AttrLength, "");
262 }
263 
264 //---------------------------------------------------------------------------------------------------------------------
266 {
267  if (not vis.isNull())
268  {
269  VisToolCutSpline *visual = qobject_cast<VisToolCutSpline *>(vis);
270  SCASSERT(visual != nullptr)
271 
272  visual->setObject1Id(curveCutId);
273  visual->setLength(qApp->TrVars()->FormulaToUser(formula, qApp->Settings()->GetOsSeparator()));
274 
276  visual->setLineStyle(lineTypeToPenStyle(curve->GetPenStyle()));
277 
278  visual->RefreshGeometry();
279  }
280 }
281 
282 //---------------------------------------------------------------------------------------------------------------------
284 {
286 
287  const QString expression = qApp->TrVars()->FormulaToUser(formula, qApp->Settings()->GetOsSeparator());
288  const qreal length = Visualization::FindVal(expression, VAbstractTool::data.DataVariables());
289 
290  QPointF spl1p2, spl1p3, spl2p2, spl2p3;
291  QPointF point = spl->CutSpline(qApp->toPixel(length), spl1p2, spl1p3, spl2p2, spl2p3);
292 
293  VSpline spline1 = VSpline(spl->GetP1(), spl1p2, spl1p3, VPointF(point));
294  VSpline spline2 = VSpline(VPointF(point), spl2p2, spl2p3, spl->GetP4());
295 
296  const QString curveStr = tr("Curve");
297  const QString lengthStr = tr("length");
298 
299  const QString toolTip = QString("<table>"
300  "<tr> <td><b>%6:</b> %7</td> </tr>"
301  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
302  "<tr> <td><b>%8:</b> %9</td> </tr>"
303  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
304  "</table>")
305  .arg(curveStr + QLatin1String("1 ") + lengthStr)
306  .arg(qApp->fromPixel(spline1.GetLength()))
307  .arg(UnitsToStr(qApp->patternUnit(), true))
308  .arg(curveStr + QLatin1String("2 ") + lengthStr)
309  .arg(qApp->fromPixel(spline2.GetLength()))
310  .arg(curveStr + QLatin1String(" 1") + tr("label"))
311  .arg(spline1.name())
312  .arg(curveStr + QLatin1String(" 2") + tr("label"))
313  .arg(spline2.name());
314 
315  return toolTip;
316 }
The DialogCutSpline class dialog for ToolCutSpline.
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
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
VSpline class that implements the spline.
Definition: vspline.h:75
virtual qreal GetLength() const Q_DECL_OVERRIDE
GetLength return length of spline.
Definition: vspline.cpp:203
The VToolCutSpline class for tool CutSpline. This tool find point on spline and cut spline on two.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
static const QString AttrSpline
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
static VToolCutSpline * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
static const QString ToolType
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
VToolCutSpline(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &formula, const quint32 &splineId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolCutSpline constructor.
virtual QString makeToolTip() const Q_DECL_OVERRIDE
virtual void SetVisualization() Q_DECL_OVERRIDE
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
@ CutSpline
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
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