Seamly2D
Code documentation
vtoolmirrorbyline.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
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 12 9, 2016
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) 2016 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 "vtoolmirrorbyline.h"
53 
54 #include <limits.h>
55 #include <qiterator.h>
56 #include <QColor>
57 #include <QDomNode>
58 #include <QDomNodeList>
59 #include <QMapIterator>
60 #include <QPoint>
61 #include <QSharedPointer>
62 #include <QStaticStringData>
63 #include <QStringData>
64 #include <QStringDataPtr>
65 #include <QUndoStack>
66 #include <new>
67 
68 #include "../../../../dialogs/tools/dialogtool.h"
69 #include "../../../../dialogs/tools/dialogmirrorbyline.h"
70 #include "../../../../visualization/line/operation/vistoolmirrorbyline.h"
71 #include "../../../../visualization/visualization.h"
72 #include "../vgeometry/vpointf.h"
73 #include "../vpatterndb/vtranslatevars.h"
74 #include "../vmisc/vabstractapplication.h"
75 #include "../vmisc/vcommonsettings.h"
76 #include "../vmisc/diagnostic.h"
77 #include "../vmisc/logging.h"
78 #include "../vpatterndb/vcontainer.h"
79 #include "../vpatterndb/vformula.h"
80 #include "../ifc/ifcdef.h"
81 #include "../ifc/exception/vexception.h"
82 #include "../vwidgets/vabstractsimple.h"
83 #include "../vwidgets/vmaingraphicsscene.h"
84 #include "../../../vabstracttool.h"
85 #include "../../../vdatatool.h"
86 #include "../../vdrawtool.h"
87 
88 template <class T> class QSharedPointer;
89 
90 const QString VToolMirrorByLine::ToolType = QStringLiteral("flippingByLine");
91 
92 //---------------------------------------------------------------------------------------------------------------------
94 {
95  SCASSERT(not m_dialog.isNull())
97  SCASSERT(not dialogTool.isNull())
98  dialogTool->setFirstLinePointId(m_firstLinePointId);
99  dialogTool->setSecondLinePointId(m_secondLinePointId);
100  dialogTool->setSuffix(suffix);
101 }
102 
103 //---------------------------------------------------------------------------------------------------------------------
105  VAbstractPattern *doc, VContainer *data)
106 {
107  SCASSERT(not dialog.isNull())
108  QSharedPointer<DialogMirrorByLine> dialogTool = dialog.objectCast<DialogMirrorByLine>();
109  SCASSERT(not dialogTool.isNull())
110  const quint32 firstLinePointId = dialogTool->getFirstLinePointId();
111  const quint32 secondLinePointId = dialogTool->getSecondLinePointId();
112  const QString suffix = dialogTool->getSuffix();
113  const QVector<SourceItem> source = dialogTool->getSourceObjects();
114  VToolMirrorByLine* operation = Create(0, firstLinePointId, secondLinePointId, suffix, source,
117  if (operation != nullptr)
118  {
119  operation->m_dialog = dialogTool;
120  }
121  return operation;
122 }
123 
124 //---------------------------------------------------------------------------------------------------------------------
125 VToolMirrorByLine *VToolMirrorByLine::Create(const quint32 _id, quint32 firstLinePointId, quint32 secondLinePointId,
126  const QString &suffix, const QVector<SourceItem> &source,
127  const QVector<DestinationItem> &destination, VMainGraphicsScene *scene,
128  VAbstractPattern *doc, VContainer *data, const Document &parse,
129  const Source &typeCreation)
130 {
131  const auto firstPoint = *data->GeometricObject<VPointF>(firstLinePointId);
132  const QPointF fPoint = static_cast<QPointF>(firstPoint);
133 
134  const auto secondPoint = *data->GeometricObject<VPointF>(secondLinePointId);
135  const QPointF sPoint = static_cast<QPointF>(secondPoint);
136 
138 
139  quint32 id = _id;
140  createDestination(typeCreation, id, dest, source, fPoint, sPoint, suffix, doc, data, parse);
141 
142  if (parse == Document::FullParse)
143  {
145  VToolMirrorByLine *tool = new VToolMirrorByLine(doc, data, id, firstLinePointId, secondLinePointId, suffix,
146  source, dest, typeCreation);
147  scene->addItem(tool);
148  initOperationToolConnections(scene, tool);
149  VAbstractPattern::AddTool(id, tool);
150  doc->IncrementReferens(firstPoint.getIdTool());
151  doc->IncrementReferens(secondPoint.getIdTool());
152  for (int i = 0; i < source.size(); ++i)
153  {
154  const SourceItem item = source.at(i);
155  doc->IncrementReferens(data->GetGObject(item.id)->getIdTool());
156  }
157  return tool;
158  }
159  return nullptr;
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
164 {
166 }
167 
168 //---------------------------------------------------------------------------------------------------------------------
170 {
172 }
173 
174 //---------------------------------------------------------------------------------------------------------------------
176 {
177  return m_firstLinePointId;
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
181 void VToolMirrorByLine::setFirstLinePointId(const quint32 &value)
182 {
183  if (value != NULL_ID)
184  {
185  m_firstLinePointId = value;
186 
188  SaveOption(obj);
189  }
190 }
191 
192 //---------------------------------------------------------------------------------------------------------------------
194 {
195  return m_secondLinePointId;
196 }
197 
198 //---------------------------------------------------------------------------------------------------------------------
199 void VToolMirrorByLine::setSecondLinePointId(const quint32 &value)
200 {
201  if (value != NULL_ID)
202  {
203  m_secondLinePointId = value;
204 
206  SaveOption(obj);
207  }
208 }
209 
210 //---------------------------------------------------------------------------------------------------------------------
212 {
213  ShowToolVisualization<VisToolMirrorByLine>(show);
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------------
218 {
219  if (not vis.isNull())
220  {
221  VisToolMirrorByLine *visual = qobject_cast<VisToolMirrorByLine *>(vis);
222  SCASSERT(visual != nullptr)
223 
227  visual->RefreshGeometry();
228  }
229 }
230 
231 //---------------------------------------------------------------------------------------------------------------------
232 void VToolMirrorByLine::SaveDialog(QDomElement &domElement)
233 {
234  SCASSERT(not m_dialog.isNull())
236  SCASSERT(not dialogTool.isNull())
237 
238  doc->SetAttribute(domElement, AttrP1Line, QString().setNum(dialogTool->getFirstLinePointId()));
239  doc->SetAttribute(domElement, AttrP2Line, QString().setNum(dialogTool->getSecondLinePointId()));
240  doc->SetAttribute(domElement, AttrSuffix, dialogTool->getSuffix());
241 }
242 
243 //---------------------------------------------------------------------------------------------------------------------
244 void VToolMirrorByLine::ReadToolAttributes(const QDomElement &domElement)
245 {
248  suffix = doc->GetParametrString(domElement, AttrSuffix);
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  VDrawTool::SaveOptions(tag, obj);
255 
257  doc->SetAttribute(tag, AttrP1Line, QString().setNum(m_firstLinePointId));
258  doc->SetAttribute(tag, AttrP2Line, QString().setNum(m_secondLinePointId));
260 
262 }
263 
264 //---------------------------------------------------------------------------------------------------------------------
265 void VToolMirrorByLine::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
266 {
267  try
268  {
269  ContextMenu<DialogMirrorByLine>(event, id);
270  }
271  catch(const VExceptionToolWasDeleted &e)
272  {
273  Q_UNUSED(e)
274  return;//Leave this method immediately!!!
275  }
276 }
277 
278 //---------------------------------------------------------------------------------------------------------------------
280 {
281  const QString toolTipStr = QString("<tr> <td><b>%1:</b> %2</td> </tr>"
282  "<tr> <td><b>%3:</b> %4</td> </tr>")
283  .arg(tr("First line point"))
284  .arg(firstLinePointName())
285  .arg(tr("Second line point"))
286  .arg(secondLinePointName());
287  return toolTipStr;
288 }
289 
290 //---------------------------------------------------------------------------------------------------------------------
291 VToolMirrorByLine::VToolMirrorByLine(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 firstLinePointId,
292  quint32 secondLinePointId, const QString &suffix,
293  const QVector<SourceItem> &source, const QVector<DestinationItem> &destination,
294  const Source &typeCreation, QGraphicsItem *parent)
295  : VAbstractMirror(doc, data, id, suffix, source, destination, parent)
296  , m_firstLinePointId(firstLinePointId)
297  , m_secondLinePointId(secondLinePointId)
298 {
300  ToolCreation(typeCreation);
301 }
static void createDestination(Source typeCreation, quint32 &id, QVector< DestinationItem > &dest, const QVector< SourceItem > &source, const QPointF &fPoint, const QPointF &sPoint, const QString &suffix, VAbstractPattern *doc, VContainer *data, const Document &parse)
QVector< SourceItem > source
static void initOperationToolConnections(VMainGraphicsScene *scene, T *tool)
void SaveSourceDestination(QDomElement &tag)
QVector< DestinationItem > destination
virtual void IncrementReferens(quint32 id) const =0
static void AddTool(quint32 id, VDataTool *tool)
AddTool add tool to list tools.
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
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
static const QSharedPointer< VGObject > GetFakeGObject(quint32 id)
Definition: vcontainer.cpp:156
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
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:163
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
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 showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
void setSecondLinePointId(const quint32 &value)
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
quint32 getSecondLinePointId() const
static const QString ToolType
quint32 getFirstLinePointId() const
static VToolMirrorByLine * Create(QSharedPointer< DialogTool > dialog, VMainGraphicsScene *scene, VAbstractPattern *doc, VContainer *data)
virtual void SaveDialog(QDomElement &domElement) Q_DECL_OVERRIDE
SaveDialog save options into file after change in dialog.
virtual void SetVisualization() Q_DECL_OVERRIDE
QString secondLinePointName() const
VToolMirrorByLine(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 firstLinePointId, quint32 secondLinePointId, const QString &suffix, const QVector< SourceItem > &source, const QVector< DestinationItem > &destination, const Source &typeCreation, QGraphicsItem *parent=nullptr)
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setFirstLinePointId(const quint32 &value)
virtual void ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
QString firstLinePointName() const
virtual QString makeToolTip() const Q_DECL_OVERRIDE
void setObjects(QVector< quint32 > objects)
void setFirstLinePointId(quint32 value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setSecondLinePointId(quint32 value)
#define SCASSERT(cond)
Definition: def.h:317
@ MirrorByLine
Source
Definition: def.h:106
const QString AttrP1Line
Definition: ifcdef.cpp:108
const QString AttrP2Line
Definition: ifcdef.cpp:109
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrSuffix
Definition: ifcdef.cpp:147
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
QVector< quint32 > sourceToObjects(const QVector< SourceItem > &source)
Document