Seamly2D
Code documentation
vtoolheight.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 vtoolheight.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 "vtoolheight.h"
53 
54 #include <QSharedPointer>
55 #include <QStaticStringData>
56 #include <QStringData>
57 #include <QStringDataPtr>
58 #include <new>
59 
60 #include "../../../../../dialogs/tools/dialogheight.h"
61 #include "../../../../../dialogs/tools/dialogtool.h"
62 #include "../../../../../visualization/visualization.h"
63 #include "../../../../../visualization/line/vistoolheight.h"
64 #include "../ifc/exception/vexception.h"
65 #include "../ifc/ifcdef.h"
66 #include "../vgeometry/vgobject.h"
67 #include "../vgeometry/vpointf.h"
68 #include "../vpatterndb/vcontainer.h"
69 #include "../vwidgets/vmaingraphicsscene.h"
70 #include "../../../../vabstracttool.h"
71 #include "../../../vdrawtool.h"
72 #include "vtoollinepoint.h"
73 
74 template <class T> class QSharedPointer;
75 
76 const QString VToolHeight::ToolType = QStringLiteral("height");
77 
78 //---------------------------------------------------------------------------------------------------------------------
79 /**
80  * @brief VToolHeight constructor.
81  * @param doc dom document container.
82  * @param data container with variables.
83  * @param id object id in container.
84  * @param lineType line type.
85  * @param lineWeight line weight.
86  * @param lineColor line color.
87  * @param basePointId id base point of projection.
88  * @param p1LineId id first point of line.
89  * @param p2LineId id second point of line.
90  * @param typeCreation way we create this tool.
91  * @param parent parent object.
92  */
93 VToolHeight::VToolHeight(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType,
94  const QString &lineWeight, const QString &lineColor, const quint32 &basePointId,
95  const quint32 &p1LineId, const quint32 &p2LineId,
96  const Source &typeCreation, QGraphicsItem * parent)
97  : VToolLinePoint(doc, data, id, lineType, lineWeight, lineColor, QString()
98  , basePointId, 0, parent)
99  , p1LineId(p1LineId)
100  , p2LineId(p2LineId)
101 {
102  ToolCreation(typeCreation);
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
106 /**
107  * @brief setDialog set dialog when user want change tool option.
108  */
110 {
111  SCASSERT(not m_dialog.isNull())
112  QSharedPointer<DialogHeight> dialogTool = m_dialog.objectCast<DialogHeight>();
113  SCASSERT(not dialogTool.isNull())
115  dialogTool->setLineType(m_lineType);
116  dialogTool->setLineWeight(m_lineWeight);
117  dialogTool->setLineColor(lineColor);
118  dialogTool->SetBasePointId(basePointId);
119  dialogTool->SetP1LineId(p1LineId);
120  dialogTool->SetP2LineId(p2LineId);
121  dialogTool->SetPointName(p->name());
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
125 /**
126  * @brief Create help create tool from GUI.
127  * @param dialog dialog.
128  * @param scene pointer to scene.
129  * @param doc dom document container.
130  * @param data container with variables.
131  * @return the created tool
132  */
134  VContainer *data)
135 {
136  SCASSERT(not dialog.isNull())
137  QSharedPointer<DialogHeight> dialogTool = dialog.objectCast<DialogHeight>();
138  SCASSERT(not dialogTool.isNull())
139  const QString pointName = dialogTool->getPointName();
140  const QString lineType = dialogTool->getLineType();
141  const QString lineWeight = dialogTool->getLineWeight();
142  const QString lineColor = dialogTool->getLineColor();
143  const quint32 basePointId = dialogTool->GetBasePointId();
144  const quint32 p1LineId = dialogTool->GetP1LineId();
145  const quint32 p2LineId = dialogTool->GetP2LineId();
146 
147  VToolHeight *point = Create(0, pointName, lineType, lineWeight, lineColor, basePointId, p1LineId, p2LineId, 5, 10, true,
149  if (point != nullptr)
150  {
151  point->m_dialog = dialogTool;
152  }
153  return point;
154 }
155 
156 //---------------------------------------------------------------------------------------------------------------------
157 /**
158  * @brief Create help create tool
159  * @param _id tool id, 0 if tool doesn't exist yet.
160  * @param pointName point name.
161  * @param lineType line type.
162  * @param lineWeight line weight.
163  * @param lineColor line color.
164  * @param basePointId id base point of projection.
165  * @param p1LineId id first point of line.
166  * @param p2LineId id second point of line.
167  * @param mx label bias x axis.
168  * @param my label bias y axis.
169  * @param showPointName show/hide point name text
170  * @param scene pointer to scene.
171  * @param doc dom document container.
172  * @param data container with variables.
173  * @param parse parser file mode.
174  * @param typeCreation way we create this tool.
175  * @return the created tool
176  */
177 VToolHeight* VToolHeight::Create(const quint32 _id, const QString &pointName, const QString &lineType,
178  const QString &lineWeight, const QString &lineColor, quint32 basePointId,
179  quint32 p1LineId, quint32 p2LineId,
180  qreal mx, qreal my, bool showPointName, VMainGraphicsScene *scene,
181  VAbstractPattern *doc, VContainer *data, const Document &parse,
182  const Source &typeCreation)
183 {
187 
188  QPointF pHeight = FindPoint(QLineF(static_cast<QPointF>(*p1Line), static_cast<QPointF>(*p2Line)),
189  static_cast<QPointF>(*basePoint));
190  quint32 id = _id;
191  VPointF *p = new VPointF(pHeight, pointName, mx, my);
192  p->setShowPointName(showPointName);
193 
194  if (typeCreation == Source::FromGui)
195  {
196  id = data->AddGObject(p);
197  data->AddLine(basePointId, id);
198  data->AddLine(p1LineId, id);
199  data->AddLine(p2LineId, id);
200  }
201  else
202  {
203  data->UpdateGObject(id, p);
204  data->AddLine(basePointId, id);
205  data->AddLine(p1LineId, id);
206  data->AddLine(p2LineId, id);
207  if (parse != Document::FullParse)
208  {
209  doc->UpdateToolData(id, data);
210  }
211  }
212 
213  if (parse == Document::FullParse)
214  {
216  VToolHeight *point = new VToolHeight(doc, data, id, lineType, lineWeight, lineColor, basePointId, p1LineId, p2LineId,
217  typeCreation);
218  scene->addItem(point);
219  InitToolConnections(scene, point);
220  VAbstractPattern::AddTool(id, point);
221  doc->IncrementReferens(basePoint->getIdTool());
222  doc->IncrementReferens(p1Line->getIdTool());
223  doc->IncrementReferens(p2Line->getIdTool());
224  return point;
225  }
226  return nullptr;
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
230 /**
231  * @brief FindPoint find projection base point onto line.
232  * @param line line
233  * @param point base point.
234  * @return point onto line.
235  */
236 QPointF VToolHeight::FindPoint(const QLineF &line, const QPointF &point)
237 {
238  return VGObject::ClosestPoint(line, point);
239 }
240 
241 //---------------------------------------------------------------------------------------------------------------------
243 {
244  return VAbstractTool::data.GetGObject(p1LineId)->name();
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
249 {
250  return VAbstractTool::data.GetGObject(p2LineId)->name();
251 }
252 
253 //---------------------------------------------------------------------------------------------------------------------
254 /**
255  * @brief contextMenuEvent handle context menu events.
256  * @param event context menu event.
257  */
258 void VToolHeight::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
259 {
260  try
261  {
262  ContextMenu<DialogHeight>(event, id);
263  }
264  catch(const VExceptionToolWasDeleted &e)
265  {
266  Q_UNUSED(e)
267  return;//Leave this method immediately!!!
268  }
269 }
270 
271 //---------------------------------------------------------------------------------------------------------------------
272 /**
273  * @brief SaveDialog save options into file after change in dialog.
274  */
275 void VToolHeight::SaveDialog(QDomElement &domElement)
276 {
277  SCASSERT(not m_dialog.isNull())
278  QSharedPointer<DialogHeight> dialogTool = m_dialog.objectCast<DialogHeight>();
279  SCASSERT(not dialogTool.isNull())
280  doc->SetAttribute(domElement, AttrName, dialogTool->getPointName());
281  doc->SetAttribute(domElement, AttrLineType, dialogTool->getLineType());
282  doc->SetAttribute(domElement, AttrLineWeight, dialogTool->getLineWeight());
283  doc->SetAttribute(domElement, AttrLineColor, dialogTool->getLineColor());
284  doc->SetAttribute(domElement, AttrBasePoint, QString().setNum(dialogTool->GetBasePointId()));
285  doc->SetAttribute(domElement, AttrP1Line, QString().setNum(dialogTool->GetP1LineId()));
286  doc->SetAttribute(domElement, AttrP2Line, QString().setNum(dialogTool->GetP2LineId()));
287 }
288 
289 //---------------------------------------------------------------------------------------------------------------------
291 {
292  VToolLinePoint::SaveOptions(tag, obj);
293 
298 }
299 
300 //---------------------------------------------------------------------------------------------------------------------
301 void VToolHeight::ReadToolAttributes(const QDomElement &domElement)
302 {
304  m_lineWeight = doc->GetParametrString(domElement, AttrLineWeight, "0.35");
309 }
310 
311 //---------------------------------------------------------------------------------------------------------------------
313 {
314  if (not vis.isNull())
315  {
316  VisToolHeight *visual = qobject_cast<VisToolHeight *>(vis);
317  SCASSERT(visual != nullptr)
318 
319  visual->setObject1Id(basePointId);
320  visual->setLineP1Id(p1LineId);
321  visual->setLineP2Id(p2LineId);
323  visual->setLineWeight(m_lineWeight);
324  visual->RefreshGeometry();
325  }
326 }
327 
328 //---------------------------------------------------------------------------------------------------------------------
330 {
335 
336  const QLineF curLine(static_cast<QPointF>(*basePoint), static_cast<QPointF>(*current));
337  const QLineF p1ToCur(static_cast<QPointF>(*p1Line), static_cast<QPointF>(*current));
338  const QLineF p2ToCur(static_cast<QPointF>(*p2Line), static_cast<QPointF>(*current));
339 
340  const QString toolTip = QString("<table>"
341  "<tr> <td><b> %10:</b> %11</td> </tr>"
342  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
343  "<tr> <td><b> %4:</b> %5°</td> </tr>"
344  "<tr> <td><b>%6:</b> %7 %3</td> </tr>"
345  "<tr> <td><b>%8:</b> %9 %3</td> </tr>"
346  "</table>")
347  .arg(tr("Length"))
348  .arg(qApp->fromPixel(curLine.length()))
349  .arg(UnitsToStr(qApp->patternUnit(), true))
350  .arg(tr("Angle"))
351  .arg(curLine.angle())
352  .arg(QString("%1->%2").arg(p1Line->name(), current->name()))
353  .arg(qApp->fromPixel(p1ToCur.length()))
354  .arg(QString("%1->%2").arg(p2Line->name(), current->name()))
355  .arg(qApp->fromPixel(p2ToCur.length()))
356  .arg(tr("Name"))
357  .arg(current->name());
358 
359  return toolTip;
360 }
361 
362 //---------------------------------------------------------------------------------------------------------------------
364 {
365  return p2LineId;
366 }
367 
368 //---------------------------------------------------------------------------------------------------------------------
369 void VToolHeight::SetP2LineId(const quint32 &value)
370 {
371  if (value != NULL_ID)
372  {
373  p2LineId = value;
374 
376  SaveOption(obj);
377  }
378 }
379 
380 //---------------------------------------------------------------------------------------------------------------------
382 {
383  ShowToolVisualization<VisToolHeight>(show);
384 }
385 
386 //---------------------------------------------------------------------------------------------------------------------
388 {
389  return p1LineId;
390 }
391 
392 //---------------------------------------------------------------------------------------------------------------------
393 void VToolHeight::SetP1LineId(const quint32 &value)
394 {
395  if (value != NULL_ID)
396  {
397  p1LineId = value;
398 
400  SaveOption(obj);
401  }
402 }
The DialogHeight class dialog for ToolHeight. Help create point and edit option.
Definition: dialogheight.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.
const quint32 m_id
id object id.
The VContainer class container of all variables.
Definition: vcontainer.h:141
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
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
static QPointF ClosestPoint(const QLineF &line, const QPointF &point)
ClosestPoint find point projection of point onto line.
Definition: vgobject.cpp:436
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 VToolHeight class tool for creation point of height. Help find point of projection onto line.
Definition: vtoolheight.h:75
void SetP1LineId(const quint32 &value)
virtual QString makeToolTip() const Q_DECL_OVERRIDE
QString SecondLinePointName() const
quint32 p1LineId
Definition: vtoolheight.h:118
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
static VToolHeight * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
Create help create tool from GUI.
static QPointF FindPoint(const QLineF &line, const QPointF &point)
FindPoint find projection base point onto line.
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
contextMenuEvent handle context menu events.
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
void SetP2LineId(const quint32 &value)
quint32 p2LineId
p1LineId id first point of line.
Definition: vtoolheight.h:119
virtual void SetVisualization() Q_DECL_OVERRIDE
VToolHeight(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType, const QString &lineWeight, const QString &lineColor, const quint32 &basePointId, const quint32 &p1LineId, const quint32 &p2LineId, const Source &typeCreation, QGraphicsItem *parent=nullptr)
p2LineId id second point of line.
Definition: vtoolheight.cpp:93
static const QString ToolType
Definition: vtoolheight.h:90
quint32 GetP1LineId() const
QString FirstLinePointName() const
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
quint32 GetP2LineId() const
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
The VToolLinePoint class parent for all tools what create point with line.
quint32 basePointId
angle line angle.
QString lineColor
mainLine line item.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setLineP2Id(const quint32 &value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setLineP1Id(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
@ Height
Source
Definition: def.h:106
const QString AttrBasePoint
Definition: ifcdef.cpp:95
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString LineTypeSolidLine
Definition: ifcdef.cpp:159
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrP1Line
Definition: ifcdef.cpp:108
const QString AttrP2Line
Definition: ifcdef.cpp:109
const QString ColorBlack
Definition: ifcdef.cpp:373
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