Seamly2D
Code documentation
vtoolmirrorbyaxis.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 16 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 "vtoolmirrorbyaxis.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/dialogmirrorbyaxis.h"
70 #include "../../../../visualization/line/operation/vistoolmirrorbyaxis.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 VToolMirrorByAxis::ToolType = QStringLiteral("flippingByAxis");
91 
92 //---------------------------------------------------------------------------------------------------------------------
94 {
95  SCASSERT(not m_dialog.isNull())
97  SCASSERT(not dialogTool.isNull())
98  dialogTool->setOriginPointId(m_originPointId);
99  dialogTool->setAxisType(m_axisType);
100  dialogTool->setSuffix(suffix);
101 }
102 
103 //---------------------------------------------------------------------------------------------------------------------
105  VAbstractPattern *doc, VContainer *data)
106 {
107  SCASSERT(not dialog.isNull())
108  QSharedPointer<DialogMirrorByAxis> dialogTool = dialog.objectCast<DialogMirrorByAxis>();
109  SCASSERT(not dialogTool.isNull())
110  const quint32 originPointId = dialogTool->getOriginPointId();
111  const AxisType axisType = dialogTool->getAxisType();
112  const QString suffix = dialogTool->getSuffix();
113  const QVector<SourceItem> source = dialogTool->getSourceObjects();
114  VToolMirrorByAxis* operation = Create(0, originPointId, axisType, suffix, source, QVector<DestinationItem>(),
116  if (operation != nullptr)
117  {
118  operation->m_dialog = dialogTool;
119  }
120  return operation;
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
124 VToolMirrorByAxis *VToolMirrorByAxis::Create(const quint32 _id, quint32 originPointId, AxisType axisType,
125  const QString &suffix, const QVector<SourceItem> &source,
126  const QVector<DestinationItem> &destination, VMainGraphicsScene *scene,
127  VAbstractPattern *doc, VContainer *data, const Document &parse,
128  const Source &typeCreation)
129 {
130  const auto originPoint = *data->GeometricObject<VPointF>(originPointId);
131  const QPointF fPoint = static_cast<QPointF>(originPoint);
132 
133  QPointF sPoint;
134  if (axisType == AxisType::VerticalAxis)
135  {
136  sPoint = QPointF(fPoint.x(), fPoint.y() + 100);
137  }
138  else
139  {
140  sPoint = QPointF(fPoint.x() + 100, fPoint.y());
141  }
142 
144 
145  quint32 id = _id;
146  createDestination(typeCreation, id, dest, source, fPoint, sPoint, suffix, doc, data, parse);
147 
148  if (parse == Document::FullParse)
149  {
151  VToolMirrorByAxis *tool = new VToolMirrorByAxis(doc, data, id, originPointId, axisType, suffix, source,
152  dest, typeCreation);
153  scene->addItem(tool);
154  initOperationToolConnections(scene, tool);
155  VAbstractPattern::AddTool(id, tool);
156  doc->IncrementReferens(originPoint.getIdTool());
157  for (int i = 0; i < source.size(); ++i)
158  {
159  const SourceItem item = source.at(i);
160  doc->IncrementReferens(data->GetGObject(item.id)->getIdTool());
161  }
162  return tool;
163  }
164  return nullptr;
165 }
166 
167 //---------------------------------------------------------------------------------------------------------------------
169 {
170  return m_axisType;
171 }
172 
173 //---------------------------------------------------------------------------------------------------------------------
175 {
176  m_axisType = value;
177 
179  SaveOption(obj);
180 }
181 
182 //---------------------------------------------------------------------------------------------------------------------
184 {
186 }
187 
188 //---------------------------------------------------------------------------------------------------------------------
190 {
191  return m_originPointId;
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
195 void VToolMirrorByAxis::setOriginPointId(const quint32 &value)
196 {
197  if (value != NULL_ID)
198  {
199  m_originPointId = value;
200 
202  SaveOption(obj);
203  }
204 }
205 
206 //---------------------------------------------------------------------------------------------------------------------
208 {
209  ShowToolVisualization<VisToolMirrorByAxis>(show);
210 }
211 
212 //---------------------------------------------------------------------------------------------------------------------
214 {
215  if (not vis.isNull())
216  {
217  VisToolMirrorByAxis *visual = qobject_cast<VisToolMirrorByAxis *>(vis);
218  SCASSERT(visual != nullptr)
219 
222  visual->setAxisType(m_axisType);
223  visual->RefreshGeometry();
224  }
225 }
226 
227 //---------------------------------------------------------------------------------------------------------------------
228 void VToolMirrorByAxis::SaveDialog(QDomElement &domElement)
229 {
230  SCASSERT(not m_dialog.isNull())
232  SCASSERT(not dialogTool.isNull())
233 
234  doc->SetAttribute(domElement, AttrCenter, QString().setNum(dialogTool->getOriginPointId()));
235  doc->SetAttribute(domElement, AttrAxisType, QString().setNum(static_cast<int>(dialogTool->getAxisType())));
236  doc->SetAttribute(domElement, AttrSuffix, dialogTool->getSuffix());
237 }
238 
239 //---------------------------------------------------------------------------------------------------------------------
240 void VToolMirrorByAxis::ReadToolAttributes(const QDomElement &domElement)
241 {
243  m_axisType = static_cast<AxisType>(doc->GetParametrUInt(domElement, AttrAxisType, "1"));
244  suffix = doc->GetParametrString(domElement, AttrSuffix);
245 }
246 
247 //---------------------------------------------------------------------------------------------------------------------
249 {
250  VDrawTool::SaveOptions(tag, obj);
251 
253  doc->SetAttribute(tag, AttrCenter, QString().setNum(m_originPointId));
254  doc->SetAttribute(tag, AttrAxisType, QString().setNum(static_cast<int>(m_axisType)));
256 
258 }
259 
260 //---------------------------------------------------------------------------------------------------------------------
261 void VToolMirrorByAxis::showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id)
262 {
263  try
264  {
265  ContextMenu<DialogMirrorByAxis>(event, id);
266  }
267  catch(const VExceptionToolWasDeleted &e)
268  {
269  Q_UNUSED(e)
270  return;//Leave this method immediately!!!
271  }
272 }
273 
274 //---------------------------------------------------------------------------------------------------------------------
276 {
277  const QString toolTipStr = QString("<tr> <td><b>%1:</b> %2</td> </tr>")
278  .arg(tr("Origin point"))
279  .arg(getOriginPointName());
280  return toolTipStr;
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
284 VToolMirrorByAxis::VToolMirrorByAxis(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 originPointId,
285  AxisType axisType, const QString &suffix,
286  const QVector<SourceItem> &source, const QVector<DestinationItem> &destination,
287  const Source &typeCreation, QGraphicsItem *parent)
288  : VAbstractMirror(doc, data, id, suffix, source, destination, parent)
289  , m_originPointId(originPointId)
290  , m_axisType(axisType)
291 {
293  ToolCreation(typeCreation);
294 }
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
static VToolMirrorByAxis * 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 ReadToolAttributes(const QDomElement &domElement) Q_DECL_OVERRIDE
static const QString ToolType
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID) Q_DECL_OVERRIDE
virtual QString makeToolTip() const Q_DECL_OVERRIDE
quint32 getOriginPointId() const
VToolMirrorByAxis(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 originPointId, AxisType axisType, const QString &suffix, const QVector< SourceItem > &source, const QVector< DestinationItem > &destination, const Source &typeCreation, QGraphicsItem *parent=nullptr)
void setAxisType(AxisType value)
void setOriginPointId(const quint32 &value)
AxisType getAxisType() const
virtual void ShowVisualization(bool show) Q_DECL_OVERRIDE
virtual void SetVisualization() Q_DECL_OVERRIDE
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void setDialog() Q_DECL_OVERRIDE
setDialog set dialog when user want change tool option.
QString getOriginPointName() const
void setObjects(QVector< quint32 > objects)
void setAxisType(AxisType value)
virtual void RefreshGeometry() Q_DECL_OVERRIDE
void setOriginPointId(quint32 value)
#define SCASSERT(cond)
Definition: def.h:317
@ MirrorByAxis
Source
Definition: def.h:106
const QString AttrAxisType
Definition: ifcdef.cpp:138
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrSuffix
Definition: ifcdef.cpp:147
const QString AttrCenter
Definition: ifcdef.cpp:99
#define NULL_ID
Definition: ifcdef.h:76
#define NULL_ID_STR
Definition: ifcdef.h:77
QVector< quint32 > sourceToObjects(const QVector< SourceItem > &source)
Document
AxisType