Seamly2D
Code documentation
vscenepoint.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 14 6, 2017
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) 2017 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 "vscenepoint.h"
53 #include "../vmisc/def.h"
54 #include "../vmisc/vabstractapplication.h"
55 #include "../vgeometry/vpointf.h"
56 #include "global.h"
58 #include "scalesceneitems.h"
59 
60 #include <QBrush>
61 #include <QFont>
62 #include <QPen>
63 #include <QColor>
64 #include <QtDebug>
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 VScenePoint::VScenePoint(const QColor &lineColor, QGraphicsItem *parent)
68  : QGraphicsEllipseItem(parent)
69  , m_pointName(new VGraphicsSimpleTextItem(m_pointColor, this))
70  , m_pointLeader(new VScaledLine(this))
71  , m_pointColor(QColor(correctColor(this,lineColor)))
72  , m_onlyPoint(false)
73  , m_isHovered(false)
74  , m_showPointName(true)
75 {
77  m_pointLeader->setLine(QLineF(0, 0, 1, 0));
78  m_pointLeader->setVisible(false);
79 
80  setZValue(100);
81  setAcceptHoverEvents(true);
82  setFlag(QGraphicsItem::ItemIsFocusable, true); // For keyboard input focus
83 }
84 
85 //---------------------------------------------------------------------------------------------------------------------
86 void VScenePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
87 {
88  const qreal scale = sceneScale(scene());
89 
90  setPointPen(scale);
91  scaleCircleSize(this, scale * .75);
92 
93  if (qApp->Settings()->getPointNameSize()*scale < 6 || !qApp->Settings()->getHidePointNames())
94  {
95  m_pointName->setVisible(false);
96  m_pointLeader->setVisible(false);
97  }
98  else
99  {
100  if (!m_onlyPoint)
101  {
102  m_pointName->setVisible(m_showPointName);
103 
104  QPen leaderPen = m_pointLeader->pen();
105  if (qApp->Settings()->getUseToolColor())
106  {
107  QColor leaderColor = correctColor(m_pointLeader, m_pointColor);
108  leaderColor.setAlpha(128);
109  leaderPen.setColor(leaderColor);
110  }
111  else
112  {
113  QColor leaderColor = correctColor(m_pointLeader, QColor(qApp->Settings()->getPointNameColor()));
114  leaderColor.setAlpha(128);
115  leaderPen.setColor(leaderColor);
116  }
117  m_pointLeader->setPen(leaderPen);
118 
119  refreshLeader();
120  }
121  }
122 
123  QGraphicsEllipseItem::paint(painter, option, widget);
124 }
125 
126 //---------------------------------------------------------------------------------------------------------------------
128 {
129  setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
130  setPos(static_cast<QPointF>(point));
131  setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
132 
134 
135  m_pointName->blockSignals(true);
136  m_pointName->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
137  m_pointName->setPosition(QPointF(point.mx(), point.my()));
138  m_pointName->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
139  m_pointName->blockSignals(false);
140  m_pointName->setText(point.name());
142  m_pointName->setVisible(m_showPointName);
143 
144  refreshLeader();
145 }
146 
147 //---------------------------------------------------------------------------------------------------------------------
149 {
150  m_onlyPoint = value;
151  m_pointName->setVisible(!m_onlyPoint);
152  m_pointLeader->setVisible(!m_onlyPoint);
153 }
154 
155 //---------------------------------------------------------------------------------------------------------------------
157 {
158  return m_onlyPoint;
159 }
160 
161 void VScenePoint::setPointColor(const QString &value)
162 {
163  m_pointColor = QColor(value);
164 }
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 void VScenePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
168 {
169  m_isHovered = true;
170  QGraphicsEllipseItem::hoverEnterEvent(event);
171 }
172 
173 //---------------------------------------------------------------------------------------------------------------------
174 void VScenePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
175 {
176  m_isHovered = false;
177  QGraphicsEllipseItem::hoverLeaveEvent(event);
178 }
179 
180 //---------------------------------------------------------------------------------------------------------------------
182 {
183  QRectF nRect = m_pointName->sceneBoundingRect();
184  nRect.translate(- scenePos());
185  if (not rect().intersects(nRect))
186  {
187  const QRectF nameRect = m_pointName->sceneBoundingRect();
188  QPointF p1;
189  QPointF p2;
191  QLineF(QPointF(), nameRect.center() - scenePos()), p1, p2);
192  const QPointF pointRect = VGObject::LineIntersectRect(nameRect, QLineF(scenePos(), nameRect.center()));
193 
194  if (QLineF(p1, pointRect - scenePos()).length() <= ToPixel(4/qMax(1.0, sceneScale(scene())), Unit::Mm))
195  {
196  m_pointLeader->setVisible(false);
197  }
198  else
199  {
200  m_pointLeader->setLine(QLineF(p1, pointRect - scenePos()));
201  m_pointLeader->setVisible(m_showPointName);
202  }
203  }
204  else
205  {
206  m_pointLeader->setVisible(false);
207  }
208 }
209 
210 //---------------------------------------------------------------------------------------------------------------------
211 void VScenePoint::setPointPen(qreal scale)
212 {
213  const qreal width = scaleWidth(m_isHovered ? widthMainLine : widthHairLine, scale);
214 
215  if (qApp->Settings()->getUseToolColor() || isOnlyPoint())
216  {
217  setPen(QPen(correctColor(this, m_pointColor), width));
218  if (!m_onlyPoint)
219  {
220  if (!qApp->Settings()->isWireframe())
221  {
222  setBrush(QBrush(correctColor(this, m_pointColor), Qt::SolidPattern));
223  }
224  else
225  {
226  setBrush(QBrush(correctColor(this, m_pointColor), Qt::NoBrush));
227  }
228  }
229  else
230  {
231  setBrush(QBrush(correctColor(this, m_pointColor),Qt::NoBrush));
232  }
233  }
234  else
235  {
236  setPen(QPen(correctColor(this, QColor(qApp->Settings()->getPointNameColor())), width));
237 
238  if (!qApp->Settings()->isWireframe())
239  {
240  setBrush(QBrush(correctColor(this, QColor(qApp->Settings()->getPointNameColor())),Qt::SolidPattern));
241  }
242  else
243  {
244  setBrush(QBrush(correctColor(this, QColor(qApp->Settings()->getPointNameColor())),Qt::NoBrush));
245  }
246 
247  }
248 }
static qint32 LineIntersectCircle(const QPointF &center, qreal radius, const QLineF &line, QPointF &p1, QPointF &p2)
LineIntersectCircle find point intersection line and circle.
Definition: vgobject.cpp:391
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
static QPointF LineIntersectRect(const QRectF &rec, const QLineF &line)
LineIntersectRect find point intersection line and rect.
Definition: vgobject.cpp:313
The VGraphicsSimpleTextItem class pointer label.
void setTextColor(const QColor &color)
The VPointF class keep data of point.
Definition: vpointf.h:75
qreal mx() const
mx return offset name respect to x
Definition: vpointf.cpp:177
bool isShowPointName() const
Definition: vpointf.cpp:259
qreal my() const
my return offset name respect to y
Definition: vpointf.cpp:187
void setBasicWidth(const qreal &value)
VGraphicsSimpleTextItem * m_pointName
Definition: vscenepoint.h:80
bool m_onlyPoint
m_pointColor color of point.
Definition: vscenepoint.h:83
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
VScenePoint(const QColor &lineColor, QGraphicsItem *parent=nullptr)
Definition: vscenepoint.cpp:67
QColor m_pointColor
pointL line that we see if Text is moved too away from point.
Definition: vscenepoint.h:82
void setPointColor(const QString &value)
VScaledLine * m_pointLeader
namePoint point text.
Definition: vscenepoint.h:81
bool isOnlyPoint() const
void setOnlyPoint(bool value)
void refreshLeader()
bool m_showPointName
Definition: vscenepoint.h:85
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
void setPointPen(qreal scale)
bool m_isHovered
Definition: vscenepoint.h:84
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
Definition: vscenepoint.cpp:86
virtual void refreshPointGeometry(const VPointF &point)
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
const qreal widthMainLine
Definition: global.cpp:60
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
void scaleCircleSize(QGraphicsEllipseItem *item, qreal scale)
Definition: global.cpp:114
qreal scaledRadius(qreal scale)
Definition: global.cpp:103
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130
QColor correctColor(const QGraphicsItem *item, const QColor &color)
Definition: global.cpp:80
const qreal widthHairLine
Definition: global.cpp:61
#define qApp
Definition: vapplication.h:67