Seamly2D
Code documentation
vtoolarcwithlength.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vtoolarcwithlength.cpp
3  ** @author Douglas S Caskey
4  ** @date 21 Mar, 2023
5  **
6  ** @brief
7  ** @copyright
8  ** This source code is part of the Seamly2D project, a pattern making
9  ** program to create and model patterns of clothing.
10  ** Copyright (C) 2017-2023 Seamly2D project
11  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
12  **
13  ** Seamly2D is free software: you can redistribute it and/or modify
14  ** You should have received a copy of the GNU General Public License
15  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
16  **
17  *****************************************************************************/
18 
19 /************************************************************************
20  **
21  ** @file vtoolarcwithlength.cpp
22  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
23  ** @date 9 6, 2015
24  **
25  ** @brief
26  ** @copyright
27  ** This source code is part of the Valentine project, a pattern making
28  ** program, whose allow create and modeling patterns of clothing.
29  ** Copyright (C) 2015 Seamly2D project
30  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
31  **
32  ** Seamly2D is free software: you can redistribute it and/or modify
33  ** it under the terms of the GNU General Public License as published by
34  ** the Free Software Foundation, either version 3 of the License, or
35  ** (at your option) any later version.
36  **
37  ** Seamly2D is distributed in the hope that it will be useful,
38  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
39  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40  ** GNU General Public License for more details.
41  **
42  ** You should have received a copy of the GNU General Public License
43  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
44  **
45  *************************************************************************/
46 
47 #include "vtoolarcwithlength.h"
48 
49 #include <QPen>
50 #include <QSharedPointer>
51 #include <QStaticStringData>
52 #include <QStringData>
53 #include <QStringDataPtr>
54 #include <Qt>
55 #include <new>
56 
57 #include "../../../dialogs/tools/dialogtool.h"
58 #include "../../../dialogs/tools/dialogarcwithlength.h"
59 #include "../../../visualization/visualization.h"
60 #include "../../../visualization/path/vistoolarcwithlength.h"
61 #include "../ifc/exception/vexception.h"
62 #include "../ifc/xml/vdomdocument.h"
63 #include "../ifc/ifcdef.h"
64 #include "../vgeometry/varc.h"
65 #include "../vgeometry/vgobject.h"
66 #include "../vgeometry/vpointf.h"
67 #include "../vmisc/vabstractapplication.h"
68 #include "../vmisc/vcommonsettings.h"
69 #include "../vpatterndb/vcontainer.h"
70 #include "../vpatterndb/vformula.h"
71 #include "../vpatterndb/vtranslatevars.h"
72 #include "../vwidgets/vmaingraphicsscene.h"
73 #include "../../vabstracttool.h"
74 #include "../vdrawtool.h"
75 #include "vabstractspline.h"
76 
77 const QString VToolArcWithLength::ToolType = QStringLiteral("arcWithLength");
78 
79 //---------------------------------------------------------------------------------------------------------------------
80 VToolArcWithLength::VToolArcWithLength(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation,
81  QGraphicsItem *parent)
82  :VAbstractSpline(doc, data, id, parent)
83 {
85 
86  this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
87 
88  ToolCreation(typeCreation);
89 }
90 
91 //---------------------------------------------------------------------------------------------------------------------
93 {
94  SCASSERT(not m_dialog.isNull())
96  SCASSERT(not dialogTool.isNull())
98  dialogTool->setArc(*arc);
99  dialogTool->SetCenter(arc->GetCenter().id());
100  dialogTool->SetF1(arc->GetFormulaF1());
101  dialogTool->SetLength(arc->GetFormulaLength());
102  dialogTool->SetRadius(arc->GetFormulaRadius());
103  dialogTool->setLineColor(arc->getLineColor());
104  dialogTool->setLineWeight(arc->getLineWeight());
105  dialogTool->setPenStyle(arc->GetPenStyle());
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
110  VAbstractPattern *doc, VContainer *data)
111 {
112  SCASSERT(not dialog.isNull())
113  QSharedPointer<DialogArcWithLength> dialogTool = dialog.objectCast<DialogArcWithLength>();
114  SCASSERT(not dialogTool.isNull())
115  const quint32 center = dialogTool->GetCenter();
116  QString radius = dialogTool->GetRadius();
117  QString f1 = dialogTool->GetF1();
118  QString length = dialogTool->GetLength();
119  const QString color = dialogTool->getLineColor();
120  const QString penStyle = dialogTool->getPenStyle();
121  const QString lineWeight = dialogTool->getLineWeight();
122 
123  VToolArcWithLength* point = Create(0, center, radius, f1, length, color, penStyle, lineWeight, scene, doc, data,
125  if (point != nullptr)
126  {
127  point->m_dialog = dialogTool;
128  }
129  return point;
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
133 VToolArcWithLength *VToolArcWithLength::Create(const quint32 _id, const quint32 &center, QString &radius, QString &f1,
134  QString &length, const QString &color, const QString &penStyle,
135  const QString &lineWeight, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
136  const Document &parse, const Source &typeCreation)
137 {
138  qreal calcRadius = 0, calcF1 = 0, calcLength = 0;
139 
140  calcRadius = qApp->toPixel(CheckFormula(_id, radius, data));
141  calcLength = qApp->toPixel(CheckFormula(_id, length, data));
142  calcF1 = CheckFormula(_id, f1, data);
143 
144  const VPointF c = *data->GeometricObject<VPointF>(center);
145  VArc *arc = new VArc(calcLength, length, c, calcRadius, radius, calcF1, f1);
146  arc->setLineColor(color);
147  arc->SetPenStyle(penStyle);
148  arc->setLineWeight(lineWeight);
149  quint32 id = _id;
150  if (typeCreation == Source::FromGui)
151  {
152  id = data->AddGObject(arc);
153  data->AddArc(data->GeometricObject<VArc>(id), id);
154  }
155  else
156  {
157  data->UpdateGObject(id, arc);
158  data->AddArc(data->GeometricObject<VArc>(id), id);
159  if (parse != Document::FullParse)
160  {
161  doc->UpdateToolData(id, data);
162  }
163  }
164 
165  if (parse == Document::FullParse)
166  {
168  VToolArcWithLength *toolArc = new VToolArcWithLength(doc, data, id, typeCreation);
169  scene->addItem(toolArc);
170  InitArcToolConnections(scene, toolArc);
171  VAbstractPattern::AddTool(id, toolArc);
173  return toolArc;
174  }
175  return nullptr;
176 }
177 
178 //---------------------------------------------------------------------------------------------------------------------
180 {
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
186 {
187  return VAbstractTool::data.GetGObject(getCenter())->name();
188 }
189 
190 //---------------------------------------------------------------------------------------------------------------------
192 {
194  SCASSERT(arc.isNull() == false)
195 
196  return arc->GetCenter().id();
197 }
198 
199 //---------------------------------------------------------------------------------------------------------------------
200 void VToolArcWithLength::setCenter(const quint32 &value)
201 {
202  if (value != NULL_ID)
203  {
205  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
206 
208  arc->SetCenter(*point.data());
209  SaveOption(obj);
210  }
211 }
212 
213 //---------------------------------------------------------------------------------------------------------------------
215 {
217  SCASSERT(arc.isNull() == false)
218 
219  VFormula radius(arc->GetFormulaRadius(), getData());
220  radius.setCheckZero(true);
221  radius.setToolId(m_id);
222  radius.setPostfix(UnitsToStr(qApp->patternUnit()));
223  return radius;
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
228 {
229  if (value.error() == false)
230  {
231  if (value.getDoubleValue() > 0)// Formula don't check this, but radius can't be 0 or negative
232  {
234  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
235  arc->SetFormulaRadius(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
236  SaveOption(obj);
237  }
238  }
239 }
240 
241 //---------------------------------------------------------------------------------------------------------------------
243 {
245  SCASSERT(arc.isNull() == false)
246 
247  VFormula f1(arc->GetFormulaF1(), getData());
248  f1.setCheckZero(false);
249  f1.setToolId(m_id);
250  f1.setPostfix(degreeSymbol);
251  return f1;
252 }
253 
254 //---------------------------------------------------------------------------------------------------------------------
256 {
257  if (value.error() == false)
258  {
260  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
261 
262  if (not VFuzzyComparePossibleNulls(value.getDoubleValue(), arc->GetEndAngle()))// Angles can't be equal
263  {
264  arc->SetFormulaF1(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
265  SaveOption(obj);
266  }
267  }
268 }
269 
270 //---------------------------------------------------------------------------------------------------------------------
272 {
274  SCASSERT(arc.isNull() == false)
275 
276  VFormula radius(arc->GetFormulaLength(), getData());
277  radius.setCheckZero(true);
278  radius.setToolId(m_id);
279  radius.setPostfix(UnitsToStr(qApp->patternUnit()));
280  return radius;
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
285 {
286  if (value.error() == false)
287  {
289  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
290  arc->SetFormulaLength(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
291  SaveOption(obj);
292  }
293 }
294 
295 //---------------------------------------------------------------------------------------------------------------------
297 {
298  ShowToolVisualization<VisToolArcWithLength>(show);
299 }
300 
301 //---------------------------------------------------------------------------------------------------------------------
302 void VToolArcWithLength::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
303 {
304  Q_UNUSED(id)
305 
306  try
307  {
308  ContextMenu<DialogArcWithLength>(event);
309  }
310  catch(const VExceptionToolWasDeleted &e)
311  {
312  Q_UNUSED(e)
313  return;//Leave this method immediately!!!
314  }
315 }
316 
317 //---------------------------------------------------------------------------------------------------------------------
319 {
320  const auto arc = VAbstractTool::data.GeometricObject<VArc>(m_id);
321  doc->DecrementReferens(arc->GetCenter().getIdTool());
322 }
323 
324 //---------------------------------------------------------------------------------------------------------------------
325 void VToolArcWithLength::SaveDialog(QDomElement &domElement)
326 {
327  SCASSERT(not m_dialog.isNull())
329  SCASSERT(not dialogTool.isNull())
330  doc->SetAttribute(domElement, AttrCenter, QString().setNum(dialogTool->GetCenter()));
331  doc->SetAttribute(domElement, AttrRadius, dialogTool->GetRadius());
332  doc->SetAttribute(domElement, AttrAngle1, dialogTool->GetF1());
333  doc->SetAttribute(domElement, AttrLength, dialogTool->GetLength());
334  doc->SetAttribute(domElement, AttrColor, dialogTool->getLineColor());
335  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
336  doc->SetAttribute(domElement, AttrPenStyle, dialogTool->getPenStyle());
337 }
338 
339 //---------------------------------------------------------------------------------------------------------------------
341 {
343 
344  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
345  SCASSERT(arc.isNull() == false)
346 
348  doc->SetAttribute(tag, AttrCenter, arc->GetCenter().id());
349  doc->SetAttribute(tag, AttrRadius, arc->GetFormulaRadius());
350  doc->SetAttribute(tag, AttrAngle1, arc->GetFormulaF1());
351  doc->SetAttribute(tag, AttrLength, arc->GetFormulaLength());
352 }
353 
354 //---------------------------------------------------------------------------------------------------------------------
356 {
357  if (not vis.isNull())
358  {
360  VisToolArcWithLength *visual = qobject_cast<VisToolArcWithLength *>(vis);
361  SCASSERT(visual != nullptr)
362 
363  const VTranslateVars *trVars = qApp->TrVars();
364  visual->setObject1Id(arc->GetCenter().id());
365  visual->setRadius(trVars->FormulaToUser(arc->GetFormulaRadius(), qApp->Settings()->GetOsSeparator()));
366  visual->setF1(trVars->FormulaToUser(arc->GetFormulaF1(), qApp->Settings()->GetOsSeparator()));
367  visual->setLength(trVars->FormulaToUser(arc->GetFormulaLength(), qApp->Settings()->GetOsSeparator()));
368  visual->setLineStyle(lineTypeToPenStyle(arc->GetPenStyle()));
369  visual->setLineWeight(arc->getLineWeight());
370  visual->RefreshGeometry();
371  }
372 }
373 
374 //---------------------------------------------------------------------------------------------------------------------
376 {
378 
379  const QString toolTip = QString("<table>"
380  "<tr>"
381  "<td align ='right'><b>%12: </b></td>" // Tool name
382  "<td align ='left'>%13</td>"
383  "</tr>"
384  "<tr>"
385  "<td align ='right'><b>%10: </b></td>" // Point Name
386  "<td align ='left'>%11</td>"
387  "</tr>"
388  "<tr>"
389  "<td align ='right'><b>%1: </b></td>" // Length
390  "<td align ='left'>%2 %3</td>"
391  "</tr>"
392  "<tr>"
393  "<td align ='right'><b>%4: </b></td>" // Radius
394  "<td align ='left'>%5 %3</td>"
395  "</tr>"
396  "<tr>"
397  "<td align ='right'><b>%6: </b></td>" // Start angle
398  "<td align ='left'>%7</td>"
399  "</tr>"
400  "<tr>"
401  "<td align ='right'><b>%8: </b></td>" // End angle
402  "<td align ='left'>%9</td>"
403  "</tr>"
404  "</table>")
405  .arg(tr(" Length"))
406  .arg(qApp->fromPixel(arc->GetLength()))
407  .arg(UnitsToStr(qApp->patternUnit(), true))
408  .arg(tr(" Radius"))
409  .arg(qApp->fromPixel(arc->GetRadius()))
410  .arg(tr("Start angle"))
411  .arg(arc->GetStartAngle())
412  .arg(tr(" End angle"))
413  .arg(arc->GetEndAngle())
414  .arg(tr(" Name"))
415  .arg(arc->name())
416  .arg(tr(" Tool"))
417  .arg(tr("Arc - Radius and Length"));
418  return toolTip;
419 }
virtual void UpdateToolData(const quint32 &id, VContainer *data)=0
static const QString TagArc
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
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
SceneObject sceneType
static void InitArcToolConnections(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.
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
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
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
SetAttribute set attribute in pattern file. Replace "," by ".".
Definition: vdomdocument.h:185
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
bool error() const
Definition: vformula.cpp:238
qreal getDoubleValue() const
Definition: vformula.cpp:172
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
quint32 getIdTool() const
Definition: vgobject.cpp:221
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
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
VFormula GetFormulaLength() const
VFormula GetFormulaRadius() const
void SetFormulaLength(const VFormula &value)
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
VToolArcWithLength(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation, QGraphicsItem *parent=nullptr)
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
quint32 getCenter() const
static const QString ToolType
QString CenterPointName() const
virtual void SetVisualization() Q_DECL_OVERRIDE
void setCenter(const quint32 &value)
virtual QString getTagName() const Q_DECL_OVERRIDE
VFormula GetFormulaF1() const
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
static VToolArcWithLength * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual QString makeToolTip() const Q_DECL_OVERRIDE
void SetFormulaRadius(const VFormula &value)
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void SetFormulaF1(const VFormula &value)
QString FormulaToUser(const QString &formula, bool osSeparator) const
FormulaToUser replace all known tokens in formula to user look. Also change decimal separator in numb...
const QString degreeSymbol
Definition: def.cpp:196
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
static Q_REQUIRED_RESULT bool VFuzzyComparePossibleNulls(double p1, double p2)
Definition: def.h:490
@ ArcWithLength
Source
Definition: def.h:106
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrPenStyle
Definition: ifcdef.cpp:132
const QString AttrRadius
Definition: ifcdef.cpp:100
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrColor
Definition: ifcdef.cpp:131
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
const QString AttrAngle1
Definition: ifcdef.cpp:104
const QString AttrCenter
Definition: ifcdef.cpp:99
#define NULL_ID
Definition: ifcdef.h:76
Document
#define qApp
Definition: vapplication.h:67