Seamly2D
Code documentation
vtoolsinglepoint.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 vtoolsinglepoint.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 "vtoolsinglepoint.h"
53 
54 #include <QBrush>
55 #include <QFlags>
56 #include <QFont>
57 #include <QGraphicsLineItem>
58 #include <QGraphicsScene>
59 #include <QGraphicsSceneMouseEvent>
60 #include <QKeyEvent>
61 #include <QPen>
62 #include <QPoint>
63 #include <QRectF>
64 #include <QSharedPointer>
65 #include <QUndoStack>
66 #include <Qt>
67 #include <new>
68 
69 #include "../../../../undocommands/label/movelabel.h"
70 #include "../../../../undocommands/label/showpointname.h"
71 #include "../ifc/exception/vexception.h"
72 #include "../ifc/exception/vexceptionbadid.h"
73 #include "../ifc/ifcdef.h"
74 #include "../ifc/xml/vabstractpattern.h"
75 #include "../vmisc/diagnostic.h"
76 #include "../vmisc/logging.h"
77 #include "../vgeometry/vgobject.h"
78 #include "../vgeometry/vpointf.h"
79 #include "../vmisc/vabstractapplication.h"
80 #include "../vpatterndb/vcontainer.h"
81 #include "../vwidgets/vgraphicssimpletextitem.h"
82 #include "../vwidgets/scalesceneitems.h"
83 #include "../../../vabstracttool.h"
84 #include "../../vdrawtool.h"
85 #include "../vabstractpoint.h"
86 
87 QT_WARNING_PUSH
88 QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes")
89 QT_WARNING_DISABLE_INTEL(1418)
90 
91 Q_LOGGING_CATEGORY(vToolSinglePoint, "v.toolSinglePoint")
92 
94 
95 //---------------------------------------------------------------------------------------------------------------------
96 /**
97  * @brief VToolSinglePoint constructor.
98  * @param doc dom document container.
99  * @param data container with variables.
100  * @param id object id in container.
101  * @param parent parent object.
102  */
104  const QColor &lineColor, QGraphicsItem *parent)
105  : VAbstractPoint(doc, data, id)
106  , VScenePoint(lineColor, parent)
107 {
113  refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(id));
114 }
115 
116 //---------------------------------------------------------------------------------------------------------------------
117 QString VToolSinglePoint::name() const
118 {
119  return ObjectName<VPointF>(m_id);
120 }
121 
122 //---------------------------------------------------------------------------------------------------------------------
123 void VToolSinglePoint::setName(const QString &name)
124 {
126 }
127 
128 //---------------------------------------------------------------------------------------------------------------------
130 {
131  setEnabled(enabled);
132  m_pointLeader->setEnabled(enabled);
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 void VToolSinglePoint::GroupVisibility(quint32 object, bool visible)
137 {
138  Q_UNUSED(object)
139  setVisible(visible);
140 }
141 
142 //---------------------------------------------------------------------------------------------------------------------
144 {
145  if (m_id == id)
146  {
148  return point->isShowPointName();
149  }
150  else
151  {
152  return false;
153  }
154 }
155 
156 //---------------------------------------------------------------------------------------------------------------------
157 void VToolSinglePoint::setPointNameVisiblity(quint32 id, bool visible)
158 {
159  if (m_id == id)
160  {
162  point->setShowPointName(visible);
163  refreshPointGeometry(*point);
164  }
165 }
166 
167 //---------------------------------------------------------------------------------------------------------------------
168 void VToolSinglePoint::updatePointNameVisibility(quint32 id, bool visible)
169 {
170  if (id == m_id)
171  {
172  qApp->getUndoStack()->push(new ShowPointName(doc, id, visible));
173  }
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 void VToolSinglePoint::setPointNamePosition(quint32 id, const QPointF &pos)
178 {
179  if (id == m_id)
180  {
182  point->setMx(pos.x());
183  point->setMy(pos.y());
184  m_pointName->blockSignals(true);
185  m_pointName->setPosition(pos);
186  m_pointName->blockSignals(false);
187  refreshLeader();
188 
189  if (QGraphicsScene *sc = scene())
190  {
191  VMainGraphicsView::NewSceneRect(sc, qApp->getSceneView(), this);
192  }
193  }
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
197 /**
198  * @brief pointnameChangedPosition handle change position point text.
199  * @param pos new position.
200  */
202 {
203  updatePointNamePosition(m_id, pos - this->pos());
204 }
205 
206 //---------------------------------------------------------------------------------------------------------------------
207 /**
208  * @brief updatePointNamePosition save new position text to the pattern file.
209  */
210 void VToolSinglePoint::updatePointNamePosition(quint32 id, const QPointF &pos)
211 {
212  if (id == m_id)
213  {
214  qApp->getUndoStack()->push(new MoveLabel(doc, pos, id));
215  }
216 }
217 
218 //---------------------------------------------------------------------------------------------------------------------
219 void VToolSinglePoint::mousePressEvent(QGraphicsSceneMouseEvent *event)
220 {
221  // Special for not selectable item first need to call standard mousePressEvent then accept event
222  VScenePoint::mousePressEvent(event);
223 
224  // Somehow clicking on notselectable object do not clean previous selections.
225  if (not (flags() & ItemIsSelectable) && scene())
226  {
227  scene()->clearSelection();
228  }
229 
231  {
232  event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event
233  }
234  else
235  {
236  if (event->button() == Qt::LeftButton)
237  {
238  pointChosen();
239  }
240  }
241 }
242 
243 //---------------------------------------------------------------------------------------------------------------------
244 void VToolSinglePoint::Disable(bool disable, const QString &draftBlockName)
245 {
246  const bool enabled = !CorrectDisable(disable, draftBlockName);
247  SetEnabled(enabled);
248  m_pointName->setEnabled(enabled);
249 }
250 
251 //---------------------------------------------------------------------------------------------------------------------
253 {
254  m_pointName->setFlag(QGraphicsItem::ItemIsMovable, move);
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
259 {
261 }
262 
263 //---------------------------------------------------------------------------------------------------------------------
265 {
266  setSelected(selected);
267 }
268 
269 //---------------------------------------------------------------------------------------------------------------------
270 /**
271  * @brief FullUpdateFromFile update tool data form file.
272  */
274 {
275  ReadAttributes();
276  refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(m_id));
278 }
279 
280 //---------------------------------------------------------------------------------------------------------------------
281 /**
282  * @brief mouseReleaseEvent handle mouse release events.
283  * @param event mouse release event.
284  */
285 void VToolSinglePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
286 {
288  {
289  if (event->button() == Qt::LeftButton)
290  {
291  pointChosen();
292  }
293  }
294  VScenePoint::mouseReleaseEvent(event);
295 }
296 
297 //---------------------------------------------------------------------------------------------------------------------
298 void VToolSinglePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
299 {
300  setToolTip(makeToolTip());
302 }
303 
304 //---------------------------------------------------------------------------------------------------------------------
305 /**
306  * @brief itemChange hadle item change.
307  * @param change change.
308  * @param value value.
309  * @return value.
310  */
311 QVariant VToolSinglePoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
312 {
313  if (change == QGraphicsItem::ItemSelectedChange)
314  {
315  m_pointName->blockSignals(true);
316  m_pointName->setSelected(value.toBool());
317  m_pointName->blockSignals(false);
318  emit ChangedToolSelection(value.toBool(), m_id, m_id);
319  }
320 
321  return VScenePoint::itemChange(change, value);
322 }
323 
324 //---------------------------------------------------------------------------------------------------------------------
325 /**
326  * @brief keyReleaseEvent handle key release events.
327  * @param event key release event.
328  */
329 void VToolSinglePoint::keyReleaseEvent(QKeyEvent *event)
330 {
331  switch (event->key())
332  {
333  case Qt::Key_Delete:
334  try
335  {
336  deleteTool();
337  }
338  catch(const VExceptionToolWasDeleted &e)
339  {
340  Q_UNUSED(e)
341  return;//Leave this method immediately!!!
342  }
343  break;
344  default:
345  break;
346  }
347  VScenePoint::keyReleaseEvent ( event );
348 }
349 
350 //---------------------------------------------------------------------------------------------------------------------
351 void VToolSinglePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
352 {
353  showContextMenu(event, m_id);
354 }
355 
356 //---------------------------------------------------------------------------------------------------------------------
358 {
359  VDrawTool::SaveOptions(tag, obj);
360 
361  QSharedPointer<VPointF> point = qSharedPointerDynamicCast<VPointF>(obj);
362  SCASSERT(point.isNull() == false)
363 
364  doc->SetAttribute(tag, AttrName, point->name());
365  doc->SetAttribute(tag, AttrMx, qApp->fromPixel(point->mx()));
366  doc->SetAttribute(tag, AttrMy, qApp->fromPixel(point->my()));
367  doc->SetAttribute<bool>(tag, AttrShowPointName, point->isShowPointName());
368 
369 }
370 
371 //---------------------------------------------------------------------------------------------------------------------
373 {
374  setAcceptHoverEvents(enabled);
375 }
376 
377 //---------------------------------------------------------------------------------------------------------------------
379 {
380  setFlag(QGraphicsItem::ItemIsSelectable, enabled);
381 }
382 
383 //---------------------------------------------------------------------------------------------------------------------
385 {
386  m_pointName->setAcceptHoverEvents(enabled);
387 }
388 
389 //---------------------------------------------------------------------------------------------------------------------
391 {
392  m_pointName->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
393 }
394 
395 //---------------------------------------------------------------------------------------------------------------------
397 {
400 }
void SetPointName(quint32 id, const QString &name)
VAbstractPattern * doc
doc dom document container
virtual void deleteTool(bool ask=true)
deleteTool full delete object form scene and file.
void chosenTool(quint32 id, SceneObject type)
chosenTool emit if object was clicked.
const quint32 m_id
id object id.
virtual void SetVisualization()=0
virtual void ToolSelectionType(const SelectionType &type)
SelectionType selectionType
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
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
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID)=0
virtual QString makeToolTip() const
Definition: vdrawtool.cpp:171
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:163
bool CorrectDisable(bool disable, const QString &draftBlockName) const
Definition: vdrawtool.cpp:177
void ChangedToolSelection(bool selected, quint32 object, quint32 tool)
void ReadAttributes()
Definition: vdrawtool.cpp:190
void textSelectionType(const SelectionType &type)
void showContextMenu(QGraphicsSceneContextMenuEvent *event)
showContextMenu emit when need show tool context menu.
void nameChangedPosition(const QPointF &pos)
nameChangedPosition emit when label change position.
void pointSelected(bool selected)
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 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
VScaledLine * m_pointLeader
namePoint point text.
Definition: vscenepoint.h:81
void refreshLeader()
virtual void refreshPointGeometry(const VPointF &point)
The VToolSinglePoint class parent for all tools what create points.
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
void pointnameChangedPosition(const QPointF &pos)
pointnameChangedPosition handle change position point text.
virtual void EnableToolMove(bool move) Q_DECL_OVERRIDE
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
mouseReleaseEvent handle mouse release events.
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
FullUpdateFromFile update tool data form file.
void pointSelected(bool selected)
virtual void updatePointNamePosition(quint32 id, const QPointF &pos) Q_DECL_OVERRIDE
updatePointNamePosition save new position text to the pattern file.
QString name() const
virtual void setPointNamePosition(quint32 id, const QPointF &pos) Q_DECL_OVERRIDE
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
keyReleaseEvent handle key release events.
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
virtual int type() const Q_DECL_OVERRIDE
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
void setName(const QString &name)
virtual void ToolSelectionType(const SelectionType &type) Q_DECL_OVERRIDE
virtual bool isPointNameVisible(quint32 id) const Q_DECL_OVERRIDE
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
void allowTextHover(bool enabled)
void SetEnabled(bool enabled)
void allowTextSelectable(bool enabled)
virtual void updatePointNameVisibility(quint32 id, bool visible) Q_DECL_OVERRIDE
virtual void setPointNameVisiblity(quint32 id, bool visible) Q_DECL_OVERRIDE
virtual void GroupVisibility(quint32 object, bool visible) Q_DECL_OVERRIDE
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE
itemChange hadle item change.
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
#define SCASSERT(cond)
Definition: def.h:317
SelectionType
Definition: def.h:108
const QString AttrName
Definition: ifcdef.cpp:76
const QString AttrMx
Definition: ifcdef.cpp:74
const QString AttrShowPointName
Definition: ifcdef.cpp:153
const QString AttrMy
Definition: ifcdef.cpp:75
#define qApp
Definition: vapplication.h:67