Seamly2D
Code documentation
vsimplepoint.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 vsimplepoint.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 20 6, 2015
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) 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 "vsimplepoint.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 <QLineF>
62 #include <QPen>
63 #include <QPoint>
64 #include <QRectF>
65 #include <Qt>
66 
67 #include "global.h"
68 #include "../vgeometry/vgobject.h"
69 #include "../vgeometry/vpointf.h"
71 #include "../vmisc/vabstractapplication.h"
72 
73 //---------------------------------------------------------------------------------------------------------------------
74 VSimplePoint::VSimplePoint(quint32 id, const QColor &currentColor, QObject *parent)
75  : VAbstractSimple(id, parent)
76  , VScenePoint(currentColor)
77  , m_visualizationMode(false)
78  , m_alwaysHovered(false)
79 {
80  m_pointColor = currentColor;
86 }
87 
88 //---------------------------------------------------------------------------------------------------------------------
90 {
91  m_visualizationMode = value;
92  setFlag(QGraphicsItem::ItemIsFocusable, not m_visualizationMode);
93 }
94 
95 //---------------------------------------------------------------------------------------------------------------------
97 {
98  return m_visualizationMode;
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
103 {
104  m_alwaysHovered = value;
105  m_isHovered = value;
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
109 void VSimplePoint::SetEnabled(bool enabled)
110 {
111  setEnabled(enabled);
112  m_pointName->setEnabled(enabled);
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
117 {
118  m_pointName->setFlag(QGraphicsItem::ItemIsMovable, move);
119 }
120 
121 //---------------------------------------------------------------------------------------------------------------------
123 {
124  m_pointName->setAcceptHoverEvents(enabled);
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
129 {
130  m_pointName->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135 {
138 }
139 
140 //---------------------------------------------------------------------------------------------------------------------
142 {
143  emit Delete();
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
148 {
149  emit Choosed(id);
150 }
151 
152 //---------------------------------------------------------------------------------------------------------------------
153 void VSimplePoint::pointSelected(bool selected)
154 {
155  setSelected(selected);
156 }
157 
158 //---------------------------------------------------------------------------------------------------------------------
160 {
161  emit nameChangedPosition(pos, id);
162 }
163 
164 //---------------------------------------------------------------------------------------------------------------------
165 void VSimplePoint::mousePressEvent(QGraphicsSceneMouseEvent *event)
166 {
168  {
169  event->ignore();
170  }
171  else
172  {
173  // Special for not selectable item first need to call standard mousePressEvent then accept event
174  QGraphicsEllipseItem::mousePressEvent(event);
175 
176  // Somehow clicking on notselectable object do not clean previous selections.
177  if (not (flags() & ItemIsSelectable) && scene())
178  {
179  scene()->clearSelection();
180  }
181 
183  {// Special for not selectable item first need to call standard mousePressEvent then accept event
184  event->accept();
185  }
186  else
187  {
188  if (event->button() == Qt::LeftButton)
189  {
190  emit Choosed(id);
191  }
192  }
193  }
194 }
195 
196 //---------------------------------------------------------------------------------------------------------------------
197 void VSimplePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
198 {
199  if (not m_visualizationMode)
200  {
202  {
203  if (event->button() == Qt::LeftButton)
204  {
205  emit Choosed(id);
206  }
207  }
208  VScenePoint::mouseReleaseEvent(event);
209  }
210 }
211 
212 //---------------------------------------------------------------------------------------------------------------------
213 void VSimplePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
214 {
215  m_isHovered = true;
216  QGraphicsEllipseItem::hoverEnterEvent(event);
217 }
218 
219 //---------------------------------------------------------------------------------------------------------------------
220 void VSimplePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
221 {
222  if (not m_alwaysHovered)
223  {
224  m_isHovered = false;
225  }
226  QGraphicsEllipseItem::hoverLeaveEvent(event);
227 }
228 
229 //---------------------------------------------------------------------------------------------------------------------
230 void VSimplePoint::keyReleaseEvent(QKeyEvent *event)
231 {
232  switch (event->key())
233  {
234  case Qt::Key_Delete:
235  emit Delete();
236  return; //Leave this method immediately after call!!!
237  default:
238  break;
239  }
240  VScenePoint::keyReleaseEvent ( event );
241 }
242 
243 //---------------------------------------------------------------------------------------------------------------------
244 QVariant VSimplePoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
245 {
246  if (change == QGraphicsItem::ItemSelectedChange)
247  {
248  m_pointName->blockSignals(true);
249  m_pointName->setSelected(value.toBool());
250  m_pointName->blockSignals(false);
251  emit Selected(value.toBool(), id);
252  }
253 
254  return VScenePoint::itemChange(change, value);
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
258 void VSimplePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
259 {
260  emit showContextMenu(event, id);
261 }
void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=null_id)
SelectionType selectionType
virtual void ToolSelectionType(const SelectionType &type)
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)
VGraphicsSimpleTextItem * m_pointName
Definition: vscenepoint.h:80
QColor m_pointColor
pointL line that we see if Text is moved too away from point.
Definition: vscenepoint.h:82
bool m_isHovered
Definition: vscenepoint.h:84
bool m_visualizationMode
Definition: vsimplepoint.h:123
void deletePoint()
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE
VSimplePoint(quint32 id, const QColor &currentColor, QObject *parent=nullptr)
void nameChangedPosition(const QPointF &pos, quint32 id)
void SetEnabled(bool enabled)
void SetPointHighlight(bool value)
void pointChosen()
void EnableToolMove(bool move)
bool m_alwaysHovered
Definition: vsimplepoint.h:124
void Choosed(quint32 id)
Choosed send id when clicked.
void SetVisualizationMode(bool value)
void allowTextHover(bool enabled)
void allowTextSelectable(bool enabled)
bool IsVisualizationMode() const
virtual int type() const Q_DECL_OVERRIDE
Definition: vsimplepoint.h:79
void Selected(bool selected, quint32 id)
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
void pointnameChangedPosition(const QPointF &pos)
void pointSelected(bool selected)
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
virtual void ToolSelectionType(const SelectionType &type) Q_DECL_OVERRIDE
SelectionType
Definition: def.h:108