Seamly2D
Code documentation
vtoolalongline.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 vtoolalongline.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 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 "vtoolalongline.h"
53 
54 #include <QLineF>
55 #include <QPointF>
56 #include <QSharedPointer>
57 #include <QStaticStringData>
58 #include <QStringData>
59 #include <QStringDataPtr>
60 #include <new>
61 
62 #include "../../../../../dialogs/tools/dialogalongline.h"
63 #include "../../../../../dialogs/tools/dialogtool.h"
64 #include "../../../../../visualization/visualization.h"
65 #include "../../../../../visualization/line/vistoolalongline.h"
66 #include "../ifc/exception/vexception.h"
67 #include "../ifc/ifcdef.h"
68 #include "../vgeometry/vgobject.h"
69 #include "../vgeometry/vpointf.h"
70 #include "../vmisc/vabstractapplication.h"
71 #include "../vmisc/vcommonsettings.h"
72 #include "../vpatterndb/vcontainer.h"
73 #include "../vpatterndb/vtranslatevars.h"
74 #include "../vpatterndb/variables/vlinelength.h"
75 #include "../vwidgets/vmaingraphicsscene.h"
76 #include "../../../../vabstracttool.h"
77 #include "../../../vdrawtool.h"
78 #include "vtoollinepoint.h"
79 
80 template <class T> class QSharedPointer;
81 
82 const QString VToolAlongLine::ToolType = QStringLiteral("alongLine");
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 /**
86  * @brief VToolAlongLine constuctor.
87  * @param doc dom document container.
88  * @param data container with variables.
89  * @param id object id in container.
90  * @param formula string with length formula.
91  * @param firstPointId id first point of line.
92  * @param secondPointId id second point of line.
93  * @param lineType line type. line type.
94  * @param typeCreation way we create this tool.
95  * @param parent parent object.
96  */
97 VToolAlongLine::VToolAlongLine(VAbstractPattern *doc, VContainer *data, quint32 id, const QString &formula,
98  const quint32 &firstPointId, const quint32 &secondPointId,
99  const QString &lineType, const QString &lineWeight, const QString &lineColor,
100  const Source &typeCreation, QGraphicsItem *parent)
101  :VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, formula, firstPointId, 0, parent), secondPointId(secondPointId)
102 {
103  m_pointColor = QColor(lineColor);
104  ToolCreation(typeCreation);
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 /**
109  * @brief contextMenuEvent handle context menu events. handle context menu event.
110  * @param event context menu event.
111  */
112 //cppcheck-suppress unusedFunction
113 void VToolAlongLine::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
114 {
115  try
116  {
117  ContextMenu<DialogAlongLine>(event, id);
118  }
119  catch(const VExceptionToolWasDeleted &e)
120  {
121  Q_UNUSED(e)
122  return;//Leave this method immediately!!!
123  }
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
127 /**
128  * @brief RemoveReferens decrement value of reference.
129  */
131 {
132  const auto secondPoint = VAbstractTool::data.GetGObject(secondPointId);
133  doc->DecrementReferens(secondPoint->getIdTool());
135 }
136 
137 //---------------------------------------------------------------------------------------------------------------------
138 /**
139  * @brief SaveDialog save options into file after change in dialog.
140  */
141 void VToolAlongLine::SaveDialog(QDomElement &domElement)
142 {
143  SCASSERT(not m_dialog.isNull())
144  QSharedPointer<DialogAlongLine> dialogTool = m_dialog.objectCast<DialogAlongLine>();
145  SCASSERT(not dialogTool.isNull())
146  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
147  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
148  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
149  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
150  doc->SetAttribute(domElement, AttrLength, dialogTool->GetFormula());
151  doc->SetAttribute(domElement, AttrFirstPoint, dialogTool->GetFirstPointId());
152  doc->SetAttribute(domElement, AttrSecondPoint, dialogTool->GetSecondPointId());
153 }
154 
155 //---------------------------------------------------------------------------------------------------------------------
157 {
158  VToolLinePoint::SaveOptions(tag, obj);
159 
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 void VToolAlongLine::ReadToolAttributes(const QDomElement &domElement)
168 {
170  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
172  formulaLength = doc->GetParametrString(domElement, AttrLength, "");
175 }
176 
177 //---------------------------------------------------------------------------------------------------------------------
179 {
180  if (not vis.isNull())
181  {
182  VisToolAlongLine *visual = qobject_cast<VisToolAlongLine *>(vis);
183  SCASSERT(visual != nullptr)
184  visual->setObject1Id(basePointId);
185  visual->setObject2Id(secondPointId);
186  visual->setLength(qApp->TrVars()->FormulaToUser(formulaLength, qApp->Settings()->GetOsSeparator()));
188  visual->setLineWeight(m_lineWeight);
189  visual->RefreshGeometry();
190  }
191 }
192 
193 //---------------------------------------------------------------------------------------------------------------------
195 {
199 
200  const QLineF curLine(static_cast<QPointF>(*basePoint), static_cast<QPointF>(*current));
201  const QLineF curToSecond(static_cast<QPointF>(*current), static_cast<QPointF>(*secondPoint));
202 
203  const QString toolTip = QString("<table>"
204  "<tr> <td><b> %9:</b> %10</td> </tr>"
205  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
206  "<tr> <td><b> %4:</b> %5°</td> </tr>"
207  "<tr> <td><b>%6:</b> %2 %3</td> </tr>"
208  "<tr> <td><b>%7:</b> %8 %3</td> </tr>"
209  "</table>")
210  .arg(tr("Length"))
211  .arg(qApp->fromPixel(curLine.length()))
212  .arg(UnitsToStr(qApp->patternUnit(), true))
213  .arg(tr("Angle"))
214  .arg(curLine.angle())
215  .arg(QString("%1->%2").arg(basePoint->name(), current->name()))
216  .arg(QString("%1->%2").arg(current->name(), secondPoint->name()))
217  .arg(qApp->fromPixel(curToSecond.length()))
218  .arg(tr("Name"))
219  .arg(current->name());
220 
221  return toolTip;
222 }
223 
224 //---------------------------------------------------------------------------------------------------------------------
226 {
227  return secondPointId;
228 }
229 
230 //---------------------------------------------------------------------------------------------------------------------
231 void VToolAlongLine::SetSecondPointId(const quint32 &value)
232 {
233  if (value != NULL_ID)
234  {
235  secondPointId = value;
236 
238  SaveOption(obj);
239  }
240 }
241 
242 //---------------------------------------------------------------------------------------------------------------------
244 {
245  ShowToolVisualization<VisToolAlongLine>(show);
246 }
247 
248 //---------------------------------------------------------------------------------------------------------------------
249 /**
250  * @brief setDialog set dialog when user want change tool option.
251  */
253 {
254  SCASSERT(not m_dialog.isNull())
255  QSharedPointer<DialogAlongLine> dialogTool = m_dialog.objectCast<DialogAlongLine>();
256  SCASSERT(not dialogTool.isNull())
258  dialogTool->setLineType(m_lineType);
259  dialogTool->setLineWeight(m_lineWeight);
260  dialogTool->setLineColor(lineColor);
261  dialogTool->SetFormula(formulaLength);
262  dialogTool->SetFirstPointId(basePointId);
263  dialogTool->SetSecondPointId(secondPointId);
264  dialogTool->SetPointName(p->name());
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
268 /**
269  * @brief Create help create tool form GUI.
270  * @param dialog dialog options.
271  * @param scene pointer to scene.
272  * @param doc dom document container.
273  * @param data container with variables.
274  */
276  VAbstractPattern *doc, VContainer *data)
277 {
278  SCASSERT(not dialog.isNull())
279  QSharedPointer<DialogAlongLine> dialogTool = dialog.objectCast<DialogAlongLine>();
280  SCASSERT(not dialogTool.isNull())
281  QString formula = dialogTool->GetFormula();
282  const quint32 firstPointId = dialogTool->GetFirstPointId();
283  const quint32 secondPointId = dialogTool->GetSecondPointId();
284  const QString lineType = dialogTool->getLineType();
285  const QString lineWeight = dialogTool->getLineWeight();
286  const QString lineColor = dialogTool->getLineColor();
287  const QString pointName = dialogTool->getPointName();
288  VToolAlongLine *point = Create(0, pointName, lineType, lineWeight, lineColor, formula, firstPointId, secondPointId,
289  5, 10, true, scene, doc, data, Document::FullParse, Source::FromGui);
290  if (point != nullptr)
291  {
292  point->m_dialog = dialogTool;
293  }
294  return point;
295 }
296 
297 //---------------------------------------------------------------------------------------------------------------------
298 /**
299  * @brief Create help create tool.
300  * @param _id tool id, 0 if tool doesn't exist yet.
301  * @param pointName point name. point name.
302  * @param lineType line type.
303  * @param lineWeight line weight.
304  * @param lineColor line color.
305  * @param formula string with length formula.
306  * @param firstPointId id first point of line.
307  * @param secondPointId id second point of line.
308  * @param mx label bias x axis.
309  * @param my label bias y axis.
310  * @param scene pointer to scene.
311  * @param doc dom document container.
312  * @param data container with variables.
313  * @param parse parser file mode.
314  * @param typeCreation way we create this tool.
315  */
316 VToolAlongLine* VToolAlongLine::Create(const quint32 _id, const QString &pointName, const QString &lineType,
317  const QString &lineWeight,
318  const QString &lineColor, QString &formula, const quint32 &firstPointId,
319  quint32 secondPointId, qreal mx, qreal my, bool showPointName,
320  VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data,
321  const Document &parse, const Source &typeCreation)
322 {
323  const QSharedPointer<VPointF> firstPoint = data->GeometricObject<VPointF>(firstPointId);
325  QLineF line = QLineF(static_cast<QPointF>(*firstPoint), static_cast<QPointF>(*secondPoint));
326 
327  //Declare special variable "CurrentLength"
328  VLengthLine *length = new VLengthLine(firstPoint.data(), firstPointId, secondPoint.data(),
330  length->SetName(currentLength);
331  data->AddVariable(currentLength, length);
332 
333  line.setLength(qApp->toPixel(CheckFormula(_id, formula, data)));
334 
335  quint32 id = _id;
336  VPointF *p = new VPointF(line.p2(), pointName, mx, my);
337  p->setShowPointName(showPointName);
338 
339  if (typeCreation == Source::FromGui)
340  {
341  id = data->AddGObject(p);
342  data->AddLine(firstPointId, id);
343  data->AddLine(id, secondPointId);
344  }
345  else
346  {
347  data->UpdateGObject(id, p);
348  data->AddLine(firstPointId, id);
349  data->AddLine(id, secondPointId);
350  if (parse != Document::FullParse)
351  {
352  doc->UpdateToolData(id, data);
353  }
354  }
355 
356  VToolAlongLine *point = nullptr;
357  if (parse == Document::FullParse)
358  {
360  point = new VToolAlongLine(doc, data, id, formula, firstPointId, secondPointId, lineType, lineWeight, lineColor,
361  typeCreation);
362  scene->addItem(point);
363  InitToolConnections(scene, point);
364  VAbstractPattern::AddTool(id, point);
365  doc->IncrementReferens(firstPoint->getIdTool());
366  doc->IncrementReferens(secondPoint->getIdTool());
367  }
368  //Very important to delete it. Only this tool need this special variable.
370  return point;
371 }
372 
373 //---------------------------------------------------------------------------------------------------------------------
375 {
377 }
The DialogAlongLine class dialog for ToolAlongLine. Help create point and edit option.
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.
virtual void DecrementReferens(quint32 id) const =0
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
void AddVariable(const QString &name, T *var)
Definition: vcontainer.h:327
void RemoveVariable(const QString &name)
Definition: vcontainer.cpp:481
void AddLine(const quint32 &firstPointId, const quint32 &secondPointId)
AddLine add line to container.
Definition: vcontainer.cpp:382
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
const Unit * GetPatternUnit() const
Definition: vcontainer.cpp:599
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
QString m_lineWeight
typeLine line type.
Definition: vdrawtool.h:104
QString m_lineType
activeBlockName name of tool's pattern peace.
Definition: vdrawtool.h:103
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
QSharedPointer< DialogTool > m_dialog
m_dialog tool's dialog options.
void SetName(const QString &name)
The VMainGraphicsScene class main scene.
The VPointF class keep data of point.
Definition: vpointf.h:75
void setShowPointName(bool show)
Definition: vpointf.cpp:265
QColor m_pointColor
pointL line that we see if Text is moved too away from point.
Definition: vscenepoint.h:82
The VToolAlongLine class tool for creation point along line.
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
static VToolAlongLine * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool form GUI.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
quint32 secondPointId
secondPointId id second point of line.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
quint32 GetSecondPointId() const
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
static const QString ToolType
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events. handle context menu event.
QString SecondPointName() const
VToolAlongLine(VAbstractPattern *doc, VContainer *data, quint32 id, const QString &formula, const quint32 &firstPointId, const quint32 &secondPointId, const QString &typeLine, const QString &lineWeight, const QString &lineColor, const Source &typeCreation, QGraphicsItem *parent=nullptr)
VToolAlongLine constuctor.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual QString makeToolTip() const Q_DECL_OVERRIDE
void SetSecondPointId(const quint32 &value)
virtual void SetVisualization() Q_DECL_OVERRIDE
The VToolLinePoint class parent for all tools what create point with line.
quint32 basePointId
angle line angle.
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
QString lineColor
mainLine line item.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
QString formulaLength
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLength(const QString &expression)
void setObject2Id(const quint32 &value)
void setLineWeight(const QString &value)
void setObject1Id(const quint32 &value)
void setLineStyle(const Qt::PenStyle &value)
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
#define SCASSERT(cond)
Definition: def.h:317
@ AlongLine
Source
Definition: def.h:106
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLength
Definition: ifcdef.cpp:94
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString AttrSecondPoint
Definition: ifcdef.cpp:97
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString AttrFirstPoint
Definition: ifcdef.cpp:96
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString ColorBlack
Definition: ifcdef.cpp:373
const QString currentLength
Definition: ifcdef.cpp:441
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrLineColor
Definition: ifcdef.cpp:130
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