Seamly2D
Code documentation
vnodepoint.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 vnodepoint.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 "vnodepoint.h"
53 
54 #include <QBrush>
55 #include <QDomElement>
56 #include <QFlags>
57 #include <QGraphicsLineItem>
58 #include <QGraphicsScene>
59 #include <QGraphicsSceneMouseEvent>
60 #include <QPen>
61 #include <QPoint>
62 #include <QRectF>
63 #include <QSharedPointer>
64 #include <QStaticStringData>
65 #include <QStringData>
66 #include <QStringDataPtr>
67 #include <Qt>
68 #include <QIcon>
69 #include <QMenu>
70 #include <QToolTip>
71 #include <QRect>
72 #include <new>
73 
74 #include "../../../vgeometry/vpointf.h"
75 #include "../../../vwidgets/vgraphicssimpletextitem.h"
76 #include "../../undocommands/label/showpointname.h"
77 #include "../../undocommands/label/movelabel.h"
78 #include "../ifc/xml/vdomdocument.h"
79 #include "../ifc/ifcdef.h"
80 #include "../vmisc/vabstractapplication.h"
81 #include "../vpatterndb/vcontainer.h"
82 #include "../vpatterndb/vpiecenode.h"
83 #include "../vwidgets/vmaingraphicsscene.h"
84 #include "../vwidgets/vmaingraphicsview.h"
85 #include "../vabstracttool.h"
86 #include "../vdatatool.h"
87 #include "../pattern_piece_tool.h"
88 #include "vabstractnode.h"
89 
90 const QString VNodePoint::ToolType = QStringLiteral("modeling");
91 
92 //---------------------------------------------------------------------------------------------------------------------
93 /**
94  * @brief VNodePoint constructor.
95  * @param doc dom document container.
96  * @param data container with variables.
97  * @param id object id in container.
98  * @param idPoint object id in containerPoint.
99  * @param typeCreation way we create this tool.
100  * @param idTool tool id.
101  * @param qoParent QObject parent
102  * @param parent parent object.
103  */
104 VNodePoint::VNodePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 idPoint, const Source &typeCreation,
105  const QString &blockName, const quint32 &idTool, QObject *qoParent, QGraphicsItem *parent)
106  : VAbstractNode(doc, data, id, idPoint, blockName, idTool, qoParent)
107  , VScenePoint(QColor(qApp->Settings()->getPointNameColor()), parent)
108 {
113  this, [this](QGraphicsSceneContextMenuEvent *event)
114  {
115  contextMenuEvent(event);
116  });
117 
118  refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(id));
119  ToolCreation(typeCreation);
120  setCursor(Qt::ArrowCursor);
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
124 /**
125  * @brief Create help create tool.
126  * @param doc dom document container.
127  * @param data container with variables.
128  * @param id object id in container.
129  * @param idPoint object id in containerPoint.
130  * @param parse parser file mode.
131  * @param typeCreation way we create this tool.
132  * @param idTool tool id.
133  */
135  quint32 id, quint32 idPoint, const Document &parse,
136  const Source &typeCreation, const QString &blockName, const quint32 &idTool)
137 {
138  if (parse == Document::FullParse)
139  {
141  //TODO Need create garbage collector and remove all nodes that we don't use.
142  //Better check garbage before each saving file. Check only modeling tags.
143  VNodePoint *point = new VNodePoint(doc, data, id, idPoint, typeCreation, blockName, idTool, doc);
144 
150  VAbstractPattern::AddTool(id, point);
151  if (idTool != NULL_ID)
152  {
153  //Some nodes we don't show on scene. Tool that create this node must free memory.
155  SCASSERT(tool != nullptr)
156  point->setParent(tool); // Adopted by a tool
157  }
158  else
159  {
160  // Try to prevent memory leak
161  scene->addItem(point); // First adopted by scene
162  point->hide(); // If no one will use node, it will stay hidden
164  }
165  }
166  else
167  {
168  doc->UpdateToolData(id, data);
169  }
170 }
171 
172 //---------------------------------------------------------------------------------------------------------------------
173 QString VNodePoint::getTagName() const
174 {
176 }
177 
178 //---------------------------------------------------------------------------------------------------------------------
180 {
182 }
183 
184 //---------------------------------------------------------------------------------------------------------------------
185 /**
186  * @brief FullUpdateFromFile update tool data form file.
187  */
189 {
190  try
191  {
192  refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(m_id));
193  }
194  catch (const VExceptionBadId &)
195  {
196  // do nothing
197  }
198 }
199 
200 //---------------------------------------------------------------------------------------------------------------------
201 /**
202  * @brief AddToFile add tag with Information about tool into file.
203  */
205 {
207  QDomElement domElement = doc->createElement(getTagName());
208 
209  doc->SetAttribute(domElement, VDomDocument::AttrId, m_id);
210  doc->SetAttribute(domElement, AttrType, ToolType);
211  doc->SetAttribute(domElement, AttrIdObject, idNode);
212  doc->SetAttribute(domElement, AttrMx, qApp->fromPixel(point->mx()));
213  doc->SetAttribute(domElement, AttrMy, qApp->fromPixel(point->my()));
214  doc->SetAttribute<bool>(domElement, AttrShowPointName, point->isShowPointName());
215 
216  if (idTool != NULL_ID)
217  {
218  doc->SetAttribute(domElement, AttrIdTool, idTool);
219  }
220 
221  AddToModeling(domElement);
222 }
223 
224 //---------------------------------------------------------------------------------------------------------------------
225 void VNodePoint::mousePressEvent(QGraphicsSceneMouseEvent *event)
226 {
227  // Special for not selectable item first need to call standard mousePressEvent then accept event
228  VScenePoint::mousePressEvent(event);
229 
230  // Somehow clicking on notselectable object do not clean previous selections.
231  if (not (flags() & ItemIsSelectable) && scene())
232  {
233  scene()->clearSelection();
234  }
235 
236  event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event
237 }
238 
239 //---------------------------------------------------------------------------------------------------------------------
240 void VNodePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
241 {
242  //Use QToolTip::showText() to offset tooltip so it's not hidden under cursor
243  QPoint point = event->screenPos();
244  QToolTip::showText(QPoint(point.x()+10, point.y()+10), VAbstractTool::data.GetGObject(m_id)->name());
246 }
247 
248 //---------------------------------------------------------------------------------------------------------------------
249 /**
250  * @brief mouseReleaseEvent handle mouse release events.
251  * @param event mouse release event.
252  */
253 void VNodePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
254 {
255  if (event->button() == Qt::LeftButton)
256  {
258  }
259  VScenePoint::mouseReleaseEvent(event);
260 }
261 
262 //---------------------------------------------------------------------------------------------------------------------
263 /**
264  * @brief setPointNamePosition change name text position.
265  * @param pos new position.
266  */
267 void VNodePoint::setPointNamePosition(quint32 id, const QPointF &pos)
268 {
269  if (id == m_id)
270  {
272  point->setMx(pos.x());
273  point->setMy(pos.y());
274  m_pointName->blockSignals(true);
275  m_pointName->setPosition(pos);
276  m_pointName->blockSignals(false);
277  refreshLeader();
278 
279  if (QGraphicsScene *sc = scene())
280  {
281  VMainGraphicsView::NewSceneRect(sc, qApp->getSceneView(), this);
282  }
283  }
284 }
285 
286 //---------------------------------------------------------------------------------------------------------------------
287 void VNodePoint::setPointNameVisiblity(quint32 id, bool visible)
288 {
289  if (id == m_id)
290  {
292  point->setShowPointName(visible);
293  refreshPointGeometry(*point);
294  }
295 }
296 
297 //---------------------------------------------------------------------------------------------------------------------
298 /**
299  * @brief nameChangedPosition point name change position.
300  * @param pos new position.
301  */
302 void VNodePoint::nameChangedPosition(const QPointF &pos)
303 {
304  qApp->getUndoStack()->push(new MoveLabel(doc, pos - this->pos(), m_id));
305 }
306 
307 //---------------------------------------------------------------------------------------------------------------------
309 {
310  if (parentType != ParentType::Scene && not m_exluded)
311  {
312  show();
313  }
314 }
315 
316 //---------------------------------------------------------------------------------------------------------------------
318 {
319  hide();
320 }
321 
322 //---------------------------------------------------------------------------------------------------------------------
323 void VNodePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
324 {
326  {
327  return;
328  }
329  if (qgraphicsitem_cast<PatternPieceTool *>(parentItem()))
330  {
331  QMenu menu;
332 
333  QAction *actionShowPointName = menu.addAction(QIcon("://icon/16x16/open_eye.png"), tr("Show Point Name"));
334  actionShowPointName->setCheckable(true);
335  actionShowPointName->setChecked(VAbstractTool::data.GeometricObject<VPointF>(m_id)->isShowPointName());
336 
337  QAction *selectedAction = menu.exec(event->screenPos());
338  if (selectedAction == actionShowPointName)
339  {
340  qApp->getUndoStack()->push(new ShowPointName(doc, m_id, selectedAction->isChecked()));
341  }
342  }
343 }
344 
345 //---------------------------------------------------------------------------------------------------------------------
347 {
348  m_pointName->setFlag(QGraphicsItem::ItemIsMovable, move);
349 }
350 
351 //---------------------------------------------------------------------------------------------------------------------
352 void VNodePoint::AllowHover(bool enabled)
353 {
354  setAcceptHoverEvents(enabled);
355 }
356 
357 //---------------------------------------------------------------------------------------------------------------------
358 void VNodePoint::AllowSelecting(bool enabled)
359 {
360  setFlag(QGraphicsItem::ItemIsSelectable, enabled);
361 }
362 
363 //---------------------------------------------------------------------------------------------------------------------
364 void VNodePoint::allowTextHover(bool enabled)
365 {
366  m_pointName->setAcceptHoverEvents(enabled);
367 }
368 
369 //---------------------------------------------------------------------------------------------------------------------
371 {
372  m_pointName->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
373 }
The VAbstractNode class parent class for all detail node.
Definition: vabstractnode.h:71
ParentType parentType
Definition: vabstractnode.h:91
static const QString AttrIdTool
Definition: vabstractnode.h:77
quint32 idTool
idTool id tool.
Definition: vabstractnode.h:97
quint32 idNode
idNodenode id.
Definition: vabstractnode.h:94
virtual void ToolCreation(const Source &typeCreation) Q_DECL_OVERRIDE
void AddToModeling(const QDomElement &domElement)
AddToModeling add tag to modeling tag current pattern peace.
void SetParentType(const ParentType &value)
virtual void UpdateToolData(const quint32 &id, VContainer *data)=0
static void AddTool(quint32 id, VDataTool *tool)
AddTool add tool to list tools.
static VDataTool * getTool(quint32 id)
getTool return tool from tool list.
static const QString TagPoint
VAbstractPattern * doc
doc dom document container
static bool m_suppressContextMenu
Definition: vabstracttool.h:87
void chosenTool(quint32 id, SceneObject type)
chosenTool emit if object was clicked.
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
The VDataTool class need for getting access to data container of tool.
Definition: vdatatool.h:71
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
static const QString AttrId
Definition: vdomdocument.h:108
The VExceptionBadId class for exception bad id.
void showContextMenu(QGraphicsSceneContextMenuEvent *event)
showContextMenu emit when need show tool context menu.
void nameChangedPosition(const QPointF &pos)
nameChangedPosition emit when label change position.
The VMainGraphicsScene class main scene.
void EnablePointItemSelection(bool enable)
void enableTextItemSelection(bool enable)
void EnablePointItemHover(bool enable)
void enableTextItemHover(bool enable)
void EnableToolMove(bool move)
static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item=nullptr)
NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
The VNodePoint class point detail node.
Definition: vnodepoint.h:73
virtual void AddToFile() Q_DECL_OVERRIDE
AddToFile add tag with Information about tool into file.
Definition: vnodepoint.cpp:204
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:323
void pointChosen()
Definition: vnodepoint.cpp:179
virtual void setPointNameVisiblity(quint32 id, bool visible) Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:287
virtual void ShowNode() Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:308
VNodePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 idPoint, const Source &typeCreation, const QString &blockName=QString(), const quint32 &idTool=0, QObject *qoParent=nullptr, QGraphicsItem *parent=nullptr)
VNodePoint constructor.
Definition: vnodepoint.cpp:104
virtual void setPointNamePosition(quint32 id, const QPointF &pos) Q_DECL_OVERRIDE
setPointNamePosition change name text position.
Definition: vnodepoint.cpp:267
static const QString ToolType
Definition: vnodepoint.h:80
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override
Definition: vnodepoint.cpp:240
void allowTextHover(bool enabled)
Definition: vnodepoint.cpp:364
static void Create(VAbstractPattern *doc, VContainer *data, VMainGraphicsScene *scene, quint32 id, quint32 idPoint, const Document &parse, const Source &typeCreation, const QString &blockName=QString(), const quint32 &idTool=0)
Create help create tool.
Definition: vnodepoint.cpp:134
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:225
void allowTextSelectable(bool enabled)
Definition: vnodepoint.cpp:370
virtual QString getTagName() const Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:173
virtual void HideNode() Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:317
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
mouseReleaseEvent handle mouse release events.
Definition: vnodepoint.cpp:253
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:352
void nameChangedPosition(const QPointF &pos)
nameChangedPosition point name change position.
Definition: vnodepoint.cpp:302
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
showContextMenu emit when need show tool context menu.
Definition: vnodepoint.cpp:188
void EnableToolMove(bool move)
Definition: vnodepoint.cpp:346
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
Definition: vnodepoint.cpp:358
The VPointF class keep data of point.
Definition: vpointf.h:75
VGraphicsSimpleTextItem * m_pointName
Definition: vscenepoint.h:80
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
void refreshLeader()
virtual void refreshPointGeometry(const VPointF &point)
#define SCASSERT(cond)
Definition: def.h:317
@ NodePoint
Source
Definition: def.h:106
const QString AttrMx
Definition: ifcdef.cpp:74
const QString AttrShowPointName
Definition: ifcdef.cpp:153
const QString AttrType
Definition: ifcdef.cpp:73
const QString AttrMy
Definition: ifcdef.cpp:75
const QString AttrIdObject
Definition: ifcdef.cpp:148
#define NULL_ID
Definition: ifcdef.h:76
Document
#define qApp
Definition: vapplication.h:67