Seamly2D
Code documentation
vsimplecurve.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 vsimplecurve.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 7 5, 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 "vsimplecurve.h"
53 
54 #include <QBrush>
55 #include <QFlags>
56 #include <QGraphicsScene>
57 #include <QGraphicsSceneMouseEvent>
58 #include <QKeyEvent>
59 #include <QMessageLogger>
60 #include <QPainterPath>
61 #include <QPen>
62 #include <Qt>
63 #include <QtDebug>
64 
65 #include "global.h"
66 #include "../vgeometry/vabstractcurve.h"
67 #include "../vmisc/vabstractapplication.h"
68 #include "../ifc/xml/vabstractpattern.h"
69 
70 template <class T> class QSharedPointer;
71 
72 //---------------------------------------------------------------------------------------------------------------------
73 VSimpleCurve::VSimpleCurve(quint32 id, const QSharedPointer<VAbstractCurve> &curve, QObject *parent)
74  : VAbstractSimple(id, parent)
75  , VCurvePathItem()
76  , m_curve(curve)
77  , m_isHovered(false)
78 {
79  this->setBrush(QBrush(Qt::NoBrush));
80  this->setAcceptHoverEvents(true);
81  this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
82 }
83 
84 //---------------------------------------------------------------------------------------------------------------------
86 {
87  m_curve = curve;
88 
89  if (not m_curve.isNull())
90  {
92  setPath(m_curve->GetPath());
93  SetPoints(m_curve->getPoints());
94  }
95  else
96  {
97  qWarning() << tr("VSimpleCurve::RefreshGeometry: pointer to curve is null.");
98  }
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
103 {
104  emit Choosed(id);
105 }
106 
107 //---------------------------------------------------------------------------------------------------------------------
108 void VSimpleCurve::CurveSelected(bool selected)
109 {
110  emit Selected(selected, id);
111 }
112 
113 //---------------------------------------------------------------------------------------------------------------------
114 void VSimpleCurve::mousePressEvent(QGraphicsSceneMouseEvent *event)
115 {
116  // Special for not selectable item first need to call standard mousePressEvent then accept event
117  QGraphicsPathItem::mousePressEvent(event);
118 
119  // Somehow clicking on notselectable object do not clean previous selections.
120  if (not (flags() & ItemIsSelectable) && scene())
121  {
122  scene()->clearSelection();
123  }
124 
126  {
127  event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event
128  }
129  else
130  {
131  if (event->button() == Qt::LeftButton)
132  {
133  emit Choosed(id);
134  }
135  }
136 }
137 
138 //---------------------------------------------------------------------------------------------------------------------
139 void VSimpleCurve::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
140 {
142  {
143  if (event->button() == Qt::LeftButton)
144  {
145  emit Choosed(id);
146  }
147  }
148  QGraphicsPathItem::mouseReleaseEvent(event);
149 }
150 
151 //---------------------------------------------------------------------------------------------------------------------
152 void VSimpleCurve::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
153 {
154  m_isHovered = true;
155  if (not m_curve.isNull())
156  {
157  SetDirectionArrows(m_curve->DirectionArrows());
158  }
159  QGraphicsPathItem::hoverEnterEvent(event);
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
163 void VSimpleCurve::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
164 {
165  m_isHovered = false;
167  QGraphicsPathItem::hoverLeaveEvent(event);
168 }
169 
170 //---------------------------------------------------------------------------------------------------------------------
171 QVariant VSimpleCurve::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
172 {
173  if (change == QGraphicsItem::ItemSelectedChange)
174  {
175  emit Selected(value.toBool(), id);
176  }
177 
178  return QGraphicsPathItem::itemChange(change, value);
179 }
180 
181 //---------------------------------------------------------------------------------------------------------------------
182 void VSimpleCurve::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
183 {
184  emit showContextMenu(event, id);
185 }
186 
187 //---------------------------------------------------------------------------------------------------------------------
188 void VSimpleCurve::keyReleaseEvent(QKeyEvent *event)
189 {
190  switch (event->key())
191  {
192  case Qt::Key_Delete:
193  emit Delete();
194  return; //Leave this method immediately after call!!!
195  default:
196  break;
197  }
198  QGraphicsPathItem::keyReleaseEvent ( event );
199 }
200 
201 //---------------------------------------------------------------------------------------------------------------------
203 {
204  qreal width = 1;
205  if (m_isHovered)
206  {
207  width = widthMainLine;
208  }
209  else
210  {
211  width = ToPixel(qApp->getCurrentDocument()->useGroupLineWeight(id, m_curve->getLineWeight()).toDouble(), Unit::Mm);
212  }
213 
214  width = scaleWidth(width, sceneScale(scene()));
215  setPen(QPen(correctColor(this, qApp->getCurrentDocument()->useGroupColor(id, m_curve->getLineColor())),
216  width,
217  lineTypeToPenStyle(qApp->getCurrentDocument()->useGroupLineType(id, m_curve->GetPenStyle()))));
218 }
void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=null_id)
SelectionType selectionType
void SetPoints(const QVector< QPointF > &points)
void SetDirectionArrows(const QVector< QPair< QLineF, QLineF >> &arrows)
void CurveSelected(bool selected)
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE
void RefreshGeometry(const QSharedPointer< VAbstractCurve > &curve)
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
void Selected(bool selected, quint32 id)
VSimpleCurve(quint32 id, const QSharedPointer< VAbstractCurve > &curve, QObject *parent=nullptr)
virtual void ScalePenWidth() Q_DECL_OVERRIDE
void CurveChoosed()
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
void Choosed(quint32 id)
Choosed send id when clicked.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
QSharedPointer< VAbstractCurve > m_curve
Definition: vsimplecurve.h:108
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
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130
QColor correctColor(const QGraphicsItem *item, const QColor &color)
Definition: global.cpp:80
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
#define qApp
Definition: vapplication.h:67