Seamly2D
Code documentation
vtoolarc.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * @file vtoolarc.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 vtoolarc.cpp
22  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
23  ** @date November 15, 2013
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) 2013-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 "vtoolarc.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/dialogarc.h"
59 #include "../../../visualization/path/vistoolarc.h"
60 #include "../../../visualization/visualization.h"
61 #include "../ifc/exception/vexception.h"
62 #include "../ifc/xml/vdomdocument.h"
63 #include "../ifc/ifcdef.h"
64 #include "../vgeometry/vgobject.h"
65 #include "../vgeometry/vpointf.h"
66 #include "../vmisc/vabstractapplication.h"
67 #include "../vmisc/vcommonsettings.h"
68 #include "../vpatterndb/vcontainer.h"
69 #include "../vpatterndb/vformula.h"
70 #include "../vpatterndb/vtranslatevars.h"
71 #include "../vwidgets/vmaingraphicsscene.h"
72 #include "../../vabstracttool.h"
73 #include "../vdrawtool.h"
74 #include "vabstractspline.h"
75 
76 const QString VToolArc::ToolType = QStringLiteral("simple");
77 
78 //---------------------------------------------------------------------------------------------------------------------
79 /**
80  * @brief VToolArc constuctor.
81  * @param doc dom document container
82  * @param data container with variables
83  * @param id object id in container
84  * @param typeCreation way we create this tool.
85  * @param parent parent object
86  */
87 VToolArc::VToolArc(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation,
88  QGraphicsItem *parent)
89  : VAbstractSpline(doc, data, id, parent)
90 {
92 
93  this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
94 
95  ToolCreation(typeCreation);
96 }
97 
98 //---------------------------------------------------------------------------------------------------------------------
99 /**
100  * @brief setDialog set dialog when user want change tool option.
101  */
103 {
104  SCASSERT(!m_dialog.isNull())
105  QSharedPointer<DialogArc> dialogTool = m_dialog.objectCast<DialogArc>();
106  SCASSERT(!dialogTool.isNull())
108  dialogTool->setArc(*arc);
109  dialogTool->setCenter(arc->GetCenter().id());
110  dialogTool->setF1(arc->GetFormulaF1());
111  dialogTool->setF2(arc->GetFormulaF2());
112  dialogTool->setRadius(arc->GetFormulaRadius());
113  dialogTool->setLineColor(arc->getLineColor());
114  dialogTool->setLineWeight(arc->getLineWeight());
115  dialogTool->setPenStyle(arc->GetPenStyle());
116 }
117 
118 //---------------------------------------------------------------------------------------------------------------------
119 /**
120  * @brief Create help create tool
121  * @param dialog dialog options.
122  * @param scene pointer to scene.
123  * @param doc dom document container
124  * @param data container with variables
125  */
127  VContainer *data)
128 {
129  SCASSERT(not dialog.isNull())
130  QSharedPointer<DialogArc> dialogTool = dialog.objectCast<DialogArc>();
131  SCASSERT(not dialogTool.isNull())
132  const quint32 center = dialogTool->getCenter();
133  QString radius = dialogTool->getRadius();
134  QString f1 = dialogTool->getF1();
135  QString f2 = dialogTool->getF2();
136  const QString color = dialogTool->getLineColor();
137  const QString penStyle = dialogTool->getPenStyle();
138  const QString lineWeight = dialogTool->getLineWeight();
139  VToolArc* point = Create(0, center, radius, f1, f2, color, penStyle, lineWeight, 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 form GUI.
151  * @param _id tool id, 0 if tool doesn't exist yet.
152  * @param center id arc center point.
153  * @param radius arc radius.
154  * @param f1 start angle of arc.
155  * @param f2 end angle of arc.
156  * @param scene pointer to scene.
157  * @param doc dom document container.
158  * @param data container with variables.
159  * @param parse parser file mode.
160  * @param typeCreation way we create this tool.
161  */
162 VToolArc* VToolArc::Create(const quint32 _id, const quint32 &center, QString &radius, QString &f1, QString &f2,
163  const QString &color, const QString &penStyle, const QString &lineWeight,
164  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
165  const Document &parse, const Source &typeCreation)
166 {
167  qreal calcRadius = 0, calcF1 = 0, calcF2 = 0;
168 
169  calcRadius = qApp->toPixel(CheckFormula(_id, radius, data));
170 
171  calcF1 = CheckFormula(_id, f1, data);
172  calcF2 = CheckFormula(_id, f2, data);
173 
174  const VPointF c = *data->GeometricObject<VPointF>(center);
175  VArc *arc = new VArc(c, calcRadius, radius, calcF1, f1, calcF2, f2 );
176  arc->setLineColor(color);
177  arc->SetPenStyle(penStyle);
178  arc->setLineWeight(lineWeight);
179  quint32 id = _id;
180  if (typeCreation == Source::FromGui)
181  {
182  id = data->AddGObject(arc);
183  data->AddArc(data->GeometricObject<VArc>(id), id);
184  }
185  else
186  {
187  data->UpdateGObject(id, arc);
188  data->AddArc(data->GeometricObject<VArc>(id), id);
189  if (parse != Document::FullParse)
190  {
191  doc->UpdateToolData(id, data);
192  }
193  }
194 
195  if (parse == Document::FullParse)
196  {
198  VToolArc *toolArc = new VToolArc(doc, data, id, typeCreation);
199  scene->addItem(toolArc);
200  InitArcToolConnections(scene, toolArc);
201  VAbstractPattern::AddTool(id, toolArc);
203  return toolArc;
204  }
205  return nullptr;
206 }
207 
208 //---------------------------------------------------------------------------------------------------------------------
209 QString VToolArc::getTagName() const
210 {
212 }
213 
214 //---------------------------------------------------------------------------------------------------------------------
216 {
217  return VAbstractTool::data.GetGObject(getCenter())->name();
218 }
219 
220 //---------------------------------------------------------------------------------------------------------------------
221 quint32 VToolArc::getCenter() const
222 {
224  SCASSERT(arc.isNull() == false)
225 
226  return arc->GetCenter().id();
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
230 void VToolArc::setCenter(const quint32 &value)
231 {
232  if (value != NULL_ID)
233  {
235  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
236 
238  arc->SetCenter(*point.data());
239  SaveOption(obj);
240  }
241 }
242 
243 //---------------------------------------------------------------------------------------------------------------------
245 {
247  SCASSERT(arc.isNull() == false)
248 
249  VFormula radius(arc->GetFormulaRadius(), getData());
250  radius.setCheckZero(true);
251  radius.setToolId(m_id);
252  radius.setPostfix(UnitsToStr(qApp->patternUnit()));
253  return radius;
254 }
255 
256 //---------------------------------------------------------------------------------------------------------------------
258 {
259  if (value.error() == false)
260  {
261  if (value.getDoubleValue() > 0)// Formula don't check this, but radius can't be 0 or negative
262  {
264  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
265  arc->SetFormulaRadius(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
266  SaveOption(obj);
267  }
268  }
269 }
270 
271 //---------------------------------------------------------------------------------------------------------------------
273 {
275  SCASSERT(arc.isNull() == false)
276 
277  VFormula f1(arc->GetFormulaF1(), getData());
278  f1.setCheckZero(false);
279  f1.setToolId(m_id);
280  f1.setPostfix(degreeSymbol);
281  return f1;
282 }
283 
284 //---------------------------------------------------------------------------------------------------------------------
286 {
287  if (value.error() == false)
288  {
290  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
291 
292  if (not VFuzzyComparePossibleNulls(value.getDoubleValue(), arc->GetEndAngle()))// Angles can't be equal
293  {
294  arc->SetFormulaF1(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
295  SaveOption(obj);
296  }
297  }
298 }
299 
300 //---------------------------------------------------------------------------------------------------------------------
302 {
304  SCASSERT(arc.isNull() == false)
305 
306  VFormula f2(arc->GetFormulaF2(), getData());
307  f2.setCheckZero(false);
308  f2.setToolId(m_id);
309  f2.setPostfix(degreeSymbol);
310  return f2;
311 }
312 
313 //---------------------------------------------------------------------------------------------------------------------
315 {
316  if (value.error() == false)
317  {
319  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
320  if (not VFuzzyComparePossibleNulls(value.getDoubleValue(), arc->GetStartAngle()))// Angles can't be equal
321  {
322  arc->SetFormulaF2(value.GetFormula(FormulaType::FromUser), value.getDoubleValue());
323  SaveOption(obj);
324  }
325  }
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
332  return *arc.data();
333 }
334 
335 //---------------------------------------------------------------------------------------------------------------------
336 void VToolArc::setArc(const VArc &arc)
337 {
339  QSharedPointer<VArc> arc2 = qSharedPointerDynamicCast<VArc>(obj);
340  *arc2.data() = arc;
341  SaveOption(obj);
342 }
343 
344 //---------------------------------------------------------------------------------------------------------------------
346 {
347  ShowToolVisualization<VisToolArc>(show);
348 }
349 
350 //---------------------------------------------------------------------------------------------------------------------
351 /**
352  * @brief showContextMenu handle context menu events.
353  * @param event context menu event.
354  * @param id tool id
355  */
356 void VToolArc::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
357 {
358  Q_UNUSED(id)
359 
360  try
361  {
362  ContextMenu<DialogArc>(event);
363  }
364  catch(const VExceptionToolWasDeleted &e)
365  {
366  Q_UNUSED(e)
367  return;//Leave this method immediately!!!
368  }
369 }
370 
371 //---------------------------------------------------------------------------------------------------------------------
372 /**
373  * @brief RemoveReferens decrement value of reference.
374  */
376 {
377  const auto arc = VAbstractTool::data.GeometricObject<VArc>(m_id);
378  doc->DecrementReferens(arc->GetCenter().getIdTool());
379 }
380 
381 //---------------------------------------------------------------------------------------------------------------------
382 /**
383  * @brief SaveDialog save options into file after change in dialog.
384  */
385 void VToolArc::SaveDialog(QDomElement &domElement)
386 {
387  SCASSERT(not m_dialog.isNull())
388  QSharedPointer<DialogArc> dialogTool = m_dialog.objectCast<DialogArc>();
389  SCASSERT(!dialogTool.isNull())
390  doc->SetAttribute(domElement, AttrType, ToolType);
391  doc->SetAttribute(domElement, AttrCenter, QString().setNum(dialogTool->getCenter()));
392  doc->SetAttribute(domElement, AttrRadius, dialogTool->getRadius());
393  doc->SetAttribute(domElement, AttrAngle1, dialogTool->getF1());
394  doc->SetAttribute(domElement, AttrAngle2, dialogTool->getF2());
395  doc->SetAttribute(domElement, AttrColor, dialogTool->getLineColor());
396  doc->SetAttribute(domElement, AttrPenStyle, dialogTool->getPenStyle());
397  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
398 }
399 
400 //---------------------------------------------------------------------------------------------------------------------
402 {
404 
405  QSharedPointer<VArc> arc = qSharedPointerDynamicCast<VArc>(obj);
406  SCASSERT(arc.isNull() == false)
407 
409  doc->SetAttribute(tag, AttrCenter, arc->GetCenter().id());
410  doc->SetAttribute(tag, AttrRadius, arc->GetFormulaRadius());
411  doc->SetAttribute(tag, AttrAngle1, arc->GetFormulaF1());
412  doc->SetAttribute(tag, AttrAngle2, arc->GetFormulaF2());
413 }
414 
415 //---------------------------------------------------------------------------------------------------------------------
417 {
418  if (not vis.isNull())
419  {
421  VisToolArc *visual = qobject_cast<VisToolArc *>(vis);
422  SCASSERT(visual != nullptr)
423 
424  const VTranslateVars *trVars = qApp->TrVars();
425  visual->setObject1Id(arc->GetCenter().id());
426  visual->setRadius(trVars->FormulaToUser(arc->GetFormulaRadius(), qApp->Settings()->GetOsSeparator()));
427  visual->setF1(trVars->FormulaToUser(arc->GetFormulaF1(), qApp->Settings()->GetOsSeparator()));
428  visual->setF2(trVars->FormulaToUser(arc->GetFormulaF2(), qApp->Settings()->GetOsSeparator()));
429  visual->setLineStyle(lineTypeToPenStyle(arc->GetPenStyle()));
430  visual->setLineWeight(arc->getLineWeight());
431  visual->RefreshGeometry();
432  }
433 }
434 
435 //---------------------------------------------------------------------------------------------------------------------
436 QString VToolArc::makeToolTip() const
437 {
439 
440  const QString toolTip = QString("<table>"
441  "<tr> <td><b>%10:</b> %11</td> </tr>"
442  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
443  "<tr> <td><b>%4:</b> %5 %3</td> </tr>"
444  "<tr> <td><b>%6:</b> %7°</td> </tr>"
445  "<tr> <td><b>%8:</b> %9°</td> </tr>"
446  "</table>")
447  .arg(tr(" Length"))
448  .arg(qApp->fromPixel(arc->GetLength()))
449  .arg(UnitsToStr(qApp->patternUnit(), true))
450  .arg(tr(" Radius"))
451  .arg(qApp->fromPixel(arc->GetRadius()))
452  .arg(tr("Start angle"))
453  .arg(arc->GetStartAngle())
454  .arg(tr(" End angle"))
455  .arg(arc->GetEndAngle())
456  .arg(tr(" Label"))
457  .arg(arc->name());
458  return toolTip;
459 }
The DialogArc class dialog for ToolArc. Help create arc and edit option.
Definition: dialogarc.h:69
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
The VToolArc class tool for creation arc.
Definition: vtoolarc.h:69
void setArc(const VArc &arc)
Definition: vtoolarc.cpp:336
void SetFormulaF2(const VFormula &value)
Definition: vtoolarc.cpp:314
void SetFormulaRadius(const VFormula &value)
Definition: vtoolarc.cpp:257
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
Definition: vtoolarc.cpp:345
QString CenterPointName() const
Definition: vtoolarc.cpp:215
static VToolArc * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool.
Definition: vtoolarc.cpp:126
VFormula GetFormulaF1() const
Definition: vtoolarc.cpp:272
void SetFormulaF1(const VFormula &value)
Definition: vtoolarc.cpp:285
VFormula GetFormulaRadius() const
Definition: vtoolarc.cpp:244
VFormula GetFormulaF2() const
Definition: vtoolarc.cpp:301
VArc getArc() const
Definition: vtoolarc.cpp:329
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
Definition: vtoolarc.cpp:375
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
Definition: vtoolarc.cpp:385
virtual QString makeToolTip() const Q_DECL_OVERRIDE
Definition: vtoolarc.cpp:436
VToolArc(VAbstractPattern *doc, VContainer *data, quint32 id, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolArc constuctor.
Definition: vtoolarc.cpp:87
static const QString ToolType
Definition: vtoolarc.h:81
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
Definition: vtoolarc.cpp:401
void setCenter(const quint32 &value)
Definition: vtoolarc.cpp:230
virtual void SetVisualization() Q_DECL_OVERRIDE
Definition: vtoolarc.cpp:416
virtual QString getTagName() const Q_DECL_OVERRIDE
Definition: vtoolarc.cpp:209
quint32 getCenter() const
Definition: vtoolarc.cpp:221
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
showContextMenu handle context menu events.
Definition: vtoolarc.cpp:356
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
Definition: vtoolarc.cpp:102
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
Source
Definition: def.h:106
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 AttrAngle2
Definition: ifcdef.cpp:105
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