Seamly2D
Code documentation
vtoolcutarc.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 vtoolcutarc.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 7 1, 2014
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 "vtoolcutarc.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/dialogcutarc.h"
62 #include "../../../../../dialogs/tools/dialogtool.h"
63 #include "../../../../../visualization/visualization.h"
64 #include "../../../../../visualization/path/vistoolcutarc.h"
65 #include "../ifc/exception/vexception.h"
66 #include "../ifc/ifcdef.h"
67 #include "../vgeometry/varc.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 "vtoolcut.h"
77 
78 template <class T> class QSharedPointer;
79 
80 const QString VToolCutArc::ToolType = QStringLiteral("cutArc");
81 
82 //---------------------------------------------------------------------------------------------------------------------
83 /**
84  * @brief VToolCutArc constructor.
85  * @param doc dom document container.
86  * @param data container with variables.
87  * @param id object id in container.
88  * @param formula string with formula length first arc.
89  * @param arcId id arc in data container.
90  * @param typeCreation way we create this tool.
91  * @param parent parent object.
92  */
93 VToolCutArc::VToolCutArc(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &formula,
94  const quint32 &arcId, const Source &typeCreation, QGraphicsItem * parent)
95  :VToolCut(doc, data, id, formula, arcId, parent)
96 {
97  ToolCreation(typeCreation);
98 }
99 
100 //---------------------------------------------------------------------------------------------------------------------
101 /**
102  * @brief setDialog set dialog when user want change tool option.
103  */
105 {
106  SCASSERT(not m_dialog.isNull())
107  QSharedPointer<DialogCutArc> dialogTool = m_dialog.objectCast<DialogCutArc>();
108  SCASSERT(not dialogTool.isNull())
110  dialogTool->SetFormula(formula);
111  dialogTool->setArcId(curveCutId);
112  dialogTool->SetPointName(point->name());
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
116 /**
117  * @brief Create help create tool form GUI.
118  * @param dialog dialog.
119  * @param scene pointer to scene.
120  * @param doc dom document container.
121  * @param data container with variables.
122  */
124  VContainer *data)
125 {
126  SCASSERT(not dialog.isNull())
127  QSharedPointer<DialogCutArc> dialogTool = dialog.objectCast<DialogCutArc>();
128  SCASSERT(not dialogTool.isNull())
129  const QString pointName = dialogTool->getPointName();
130  QString formula = dialogTool->GetFormula();
131  const quint32 arcId = dialogTool->getArcId();
132  VToolCutArc* point = Create(0, pointName, formula, arcId, 5, 10, true, scene, doc, data, Document::FullParse,
134  if (point != nullptr)
135  {
136  point->m_dialog = dialogTool;
137  }
138  return point;
139 }
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 /**
143  * @brief Create help create tool.
144  * @param _id tool id, 0 if tool doesn't exist yet.
145  * @param pointName point name.
146  * @param formula string with formula length first arc.
147  * @param arcId id arc in data container.
148  * @param mx label bias x axis.
149  * @param my label bias y axis.
150  * @param scene pointer to scene.
151  * @param doc dom document container.
152  * @param data container with variables.
153  * @param parse parser file mode.
154  * @param typeCreation way we create this tool.
155  */
156 VToolCutArc* VToolCutArc::Create(const quint32 _id, const QString &pointName, QString &formula, quint32 arcId,
157  qreal mx, qreal my, bool showPointName, VMainGraphicsScene *scene, VAbstractPattern *doc,
158  VContainer *data, const Document &parse, const Source &typeCreation)
159 {
160  const QSharedPointer<VArc> arc = data->GeometricObject<VArc>(arcId);
161 
162  const qreal result = CheckFormula(_id, formula, data);
163 
164  VArc arc1;
165  VArc arc2;
166  QPointF point = arc->CutArc(qApp->toPixel(result), arc1, arc2);
167 
168  quint32 id = _id;
169  VPointF *p = new VPointF(point, pointName, mx, my);
170  p->setShowPointName(showPointName);
171 
172  auto a1 = QSharedPointer<VArc>(new VArc(arc1));
173  auto a2 = QSharedPointer<VArc>(new VArc(arc2));
174  if (typeCreation == Source::FromGui)
175  {
176  id = data->AddGObject(p);
177  a1->setId(VContainer::getNextId());
178  a2->setId(VContainer::getNextId());
179  data->AddArc(a1, a1->id(), id);
180  data->AddArc(a2, a2->id(), id);
181  }
182  else
183  {
184  data->UpdateGObject(id, p);
185  a1->setId(id + 1);
186  a2->setId(id + 2);
187  data->AddArc(a1, a1->id(), id);
188  data->AddArc(a2, a2->id(), id);
189 
190  if (parse != Document::FullParse)
191  {
192  doc->UpdateToolData(id, data);
193  }
194  }
195 
196  if (parse == Document::FullParse)
197  {
199  VToolCutArc *point = new VToolCutArc(doc, data, id, formula, arcId, typeCreation);
200  scene->addItem(point);
201  InitToolConnections(scene, point);
202  VAbstractPattern::AddTool(id, point);
203  doc->IncrementReferens(arc->getIdTool());
204  return point;
205  }
206  return nullptr;
207 }
208 
209 //---------------------------------------------------------------------------------------------------------------------
211 {
212  ShowToolVisualization<VisToolCutArc>(show);
213 }
214 
215 //---------------------------------------------------------------------------------------------------------------------
216 /**
217  * @brief contextMenuEvent handle context menu events.
218  * @param event context menu event.
219  */
220 void VToolCutArc::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
221 {
222  try
223  {
224  ContextMenu<DialogCutArc>(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 VToolCutArc::SaveDialog(QDomElement &domElement)
238 {
239  SCASSERT(not m_dialog.isNull())
240  QSharedPointer<DialogCutArc> dialogTool = m_dialog.objectCast<DialogCutArc>();
241  SCASSERT(not dialogTool.isNull())
242  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
243  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
244  doc->SetAttribute(domElement, AttrArc, QString().setNum(dialogTool->getArcId()));
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
249 {
250  VToolCut::SaveOptions(tag, obj);
251 
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
258 void VToolCutArc::ReadToolAttributes(const QDomElement &domElement)
259 {
260  formula = doc->GetParametrString(domElement, AttrLength, "");
262 }
263 
264 //---------------------------------------------------------------------------------------------------------------------
266 {
267  if (not vis.isNull())
268  {
269  VisToolCutArc *visual = qobject_cast<VisToolCutArc *>(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  const QString arcStr = tr("Arc");
291  const QString lengthStr = tr("length");
292  const QString startAngleStr = tr("start angle");
293  const QString endAngleStr = tr("end angle");
294  const QString radiusStr = tr("radius");
295 
296  VArc ar1;
297  VArc ar2;
298  arc->CutArc(qApp->toPixel(length), ar1, ar2);
299 
300  auto ArcToolTip = [arcStr, lengthStr, startAngleStr, endAngleStr, radiusStr](QString toolTip, const VArc &arc,
301  const QString &arcNumber)
302  {
303  toolTip += QString("<tr> <td><b>%10:</b> %11</td> </tr>"
304  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
305  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
306  "<tr> <td><b>%6:</b> %7°</td> </tr>"
307  "<tr> <td><b>%8:</b> %9°</td> </tr>")
308  .arg(arcStr + arcNumber + QLatin1String(" ") + lengthStr)
309  .arg(qApp->fromPixel(arc.GetLength()))
310  .arg(UnitsToStr(qApp->patternUnit(), true))
311  .arg(arcStr + arcNumber + QLatin1String(" ") + radiusStr)
312  .arg(qApp->fromPixel(arc.GetRadius()))
313  .arg(arcStr + arcNumber + QLatin1String(" ") + startAngleStr)
314  .arg(arc.GetStartAngle())
315  .arg(arcStr + arcNumber + QLatin1String(" ") + endAngleStr)
316  .arg(arc.GetEndAngle())
317  .arg(arcStr + arcNumber + QLatin1String(" ") + tr("label"))
318  .arg(arc.name());
319  return toolTip;
320  };
321 
322  return ArcToolTip(ArcToolTip("<table>", ar1, "1"), ar2, "2") + QLatin1String("</table>");
323 }
The DialogCutArc class dialog for ToolCutArc.
Definition: dialogcutarc.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
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.
VArc class for anticlockwise arc.
Definition: varc.h:74
The VContainer class container of all variables.
Definition: vcontainer.h:141
void AddArc(const QSharedPointer< VAbstractCurve > &arc, const quint32 &id, const quint32 &parentId=null_id)
Definition: vcontainer.cpp:395
static quint32 getNextId()
getNextId generate next unique id
Definition: vcontainer.cpp:251
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
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 VToolCutArc class tool for cutting arc.
Definition: vtoolcutarc.h:73
static const QString ToolType
Definition: vtoolcutarc.h:85
static VToolCutArc * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool form GUI.
VToolCutArc(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &formula, const quint32 &arcId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolCutArc constructor.
Definition: vtoolcutarc.cpp:93
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
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual QString makeToolTip() const 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
@ CutArc
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
const QString AttrArc
Definition: ifcdef.cpp:146
#define NULL_ID_STR
Definition: ifcdef.h:77
Document
#define qApp
Definition: vapplication.h:67