Seamly2D
Code documentation
vistoolspline.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 vistoolspline.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 18 8, 2014
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 "vistoolspline.h"
53 
54 #include <QLineF>
55 #include <QPainterPath>
56 #include <QSharedPointer>
57 #include <Qt>
58 #include <new>
59 
60 #include "../ifc/ifcdef.h"
61 #include "../vgeometry/vabstractcurve.h"
62 #include "../vgeometry/vgeometrydef.h"
63 #include "../vgeometry/vpointf.h"
64 #include "../vgeometry/vspline.h"
65 #include "../vpatterndb/vcontainer.h"
66 #include "../vwidgets/vcontrolpointspline.h"
67 #include "../visualization.h"
68 #include "vispath.h"
69 
70 const int EMPTY_ANGLE = -1;
71 
72 //---------------------------------------------------------------------------------------------------------------------
73 VisToolSpline::VisToolSpline(const VContainer *data, QGraphicsItem *parent)
74  : VisPath(data, parent),
75  object4Id(NULL_ID),
76  point1(nullptr),
77  point4(nullptr),
78  angle1(EMPTY_ANGLE),
79  angle2(EMPTY_ANGLE),
80  kAsm1(1),
81  kAsm2(1),
82  kCurve(1),
83  isLeftMousePressed(false),
84  p2Selected(false),
85  p3Selected(false),
86  p2(),
87  p3(),
88  controlPoints()
89 {
90  point1 = InitPoint(supportColor, this);
91  point4 = InitPoint(supportColor, this); //-V656
92 
93  auto *controlPoint1 = new VControlPointSpline(1, SplinePointPosition::FirstPoint, this);
94  controlPoint1->hide();
95  controlPoints.append(controlPoint1);
96 
97  auto *controlPoint2 = new VControlPointSpline(1, SplinePointPosition::LastPoint, this);
98  controlPoint2->hide();
99  controlPoints.append(controlPoint2);
100 }
101 
102 //---------------------------------------------------------------------------------------------------------------------
104 {
105  emit ToolTip("");
106 }
107 
108 //---------------------------------------------------------------------------------------------------------------------
110 {
111  //Radius of point circle, but little bigger. Need handle with hover sizes.
112  const static qreal radius = defPointRadiusPixel*1.5;
113 
114  if (object1Id > NULL_ID)
115  {
117  DrawPoint(point1, static_cast<QPointF>(*first), supportColor);
118 
119  if (mode == Mode::Creation)
120  {
121  if (isLeftMousePressed && not p2Selected)
122  {
124  controlPoints[0]->refreshCtrlPoint(1, SplinePointPosition::FirstPoint, p2,
125  static_cast<QPointF>(*first));
126 
127  if (not controlPoints[0]->isVisible())
128  {
129  if (QLineF(static_cast<QPointF>(*first), p2).length() > radius)
130  {
131  controlPoints[0]->show();
132  }
133  else
134  {
135  p2 = static_cast<QPointF>(*first);
136  }
137  }
138  }
139  else
140  {
141  p2Selected = true;
142  }
143  }
144 
145  if (object4Id <= NULL_ID)
146  {
148  DrawPath(this, spline.GetPath(), mainColor, lineStyle, lineWeight, Qt::RoundCap);
149  }
150  else
151  {
152  const auto second = Visualization::data->GeometricObject<VPointF>(object4Id);
153  DrawPoint(point4, static_cast<QPointF>(*second), supportColor);
154 
155  if (mode == Mode::Creation)
156  {
157  if (isLeftMousePressed && not p3Selected)
158  {
159  QLineF ctrlLine (static_cast<QPointF>(*second), Visualization::scenePos);
160  ctrlLine.setAngle(ctrlLine.angle()+180);
161  p3 = ctrlLine.p2();
162  controlPoints[1]->refreshCtrlPoint(1, SplinePointPosition::LastPoint, p3,
163  static_cast<QPointF>(*second));
164 
165  if (not controlPoints[1]->isVisible())
166  {
167  if (QLineF(static_cast<QPointF>(*second), p3).length() > radius)
168  {
169  controlPoints[1]->show();
170  }
171  else
172  {
173  p3 = static_cast<QPointF>(*second);
174  }
175  }
176  }
177  else
178  {
179  p3Selected = true;
180  }
181  }
182 
184  {
185  VSpline spline(*first, p2, p3, *second);
186  DrawPath(this, spline.GetPath(), mainColor, lineStyle, lineWeight, Qt::RoundCap);
187  }
188  else
189  {
190  VSpline spline(*first, *second, angle1, angle2, kAsm1, kAsm2, kCurve);
191  DrawPath(this, spline.GetPath(), spline.DirectionArrows(), mainColor, lineStyle,
192  lineWeight, Qt::RoundCap);
193  Visualization::toolTip = tr("Hold <b>SHIFT</b> to constrain angle");
195  }
196  }
197  }
198 }
199 
200 //---------------------------------------------------------------------------------------------------------------------
201 void VisToolSpline::setObject4Id(const quint32 &value)
202 {
203  object4Id = value;
204 }
205 
206 //---------------------------------------------------------------------------------------------------------------------
207 void VisToolSpline::SetAngle1(const qreal &value)
208 {
209  angle1 = value;
210 }
211 
212 //---------------------------------------------------------------------------------------------------------------------
213 void VisToolSpline::SetAngle2(const qreal &value)
214 {
215  angle2 = value;
216 }
217 
218 //---------------------------------------------------------------------------------------------------------------------
219 void VisToolSpline::SetKAsm1(const qreal &value)
220 {
221  kAsm1 = value;
222 }
223 
224 //---------------------------------------------------------------------------------------------------------------------
225 void VisToolSpline::SetKAsm2(const qreal &value)
226 {
227  kAsm2 = value;
228 }
229 
230 //---------------------------------------------------------------------------------------------------------------------
231 void VisToolSpline::SetKCurve(const qreal &value)
232 {
233  kCurve = value;
234 }
235 
236 //---------------------------------------------------------------------------------------------------------------------
237 QPointF VisToolSpline::GetP2() const
238 {
239  return p2;
240 }
241 
242 //---------------------------------------------------------------------------------------------------------------------
243 QPointF VisToolSpline::GetP3() const
244 {
245  return p3;
246 }
247 
248 //---------------------------------------------------------------------------------------------------------------------
250 {
251  if (mode == Mode::Creation)
252  {
253  isLeftMousePressed = true;
254  }
255 }
256 
257 //---------------------------------------------------------------------------------------------------------------------
259 {
260  if (mode == Mode::Creation)
261  {
262  isLeftMousePressed = false;
263  RefreshGeometry();
264  }
265 }
virtual QVector< DirectionArrow > DirectionArrows() const
virtual QPainterPath GetPath() const
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
The VControlPointSpline class control spline point.
The VPointF class keep data of point.
Definition: vpointf.h:75
VSpline class that implements the spline.
Definition: vspline.h:75
void SetKCurve(const qreal &value)
void SetAngle2(const qreal &value)
void SetKAsm1(const qreal &value)
void MouseLeftReleased()
VScaledEllipse * point4
Definition: vistoolspline.h:98
QPointF GetP3() const
bool isLeftMousePressed
void setObject4Id(const quint32 &value)
void SetAngle1(const qreal &value)
QVector< VControlPointSpline * > controlPoints
virtual void RefreshGeometry() Q_DECL_OVERRIDE
VisToolSpline(const VContainer *data, QGraphicsItem *parent=nullptr)
QPointF GetP2() const
void SetKAsm2(const qreal &value)
void MouseLeftPressed()
quint32 object4Id
Definition: vistoolspline.h:96
VScaledEllipse * point1
Definition: vistoolspline.h:97
virtual ~VisToolSpline()
QColor supportColor
void ToolTip(const QString &toolTip)
QColor mainColor
Definition: visualization.h:99
void DrawPoint(QGraphicsEllipseItem *point, const QPointF &pos, const QColor &color, Qt::PenStyle style=Qt::SolidLine)
const VContainer * data
Definition: visualization.h:97
void DrawPath(VCurvePathItem *pathItem, const QPainterPath &path, const QColor &color, Qt::PenStyle style=Qt::SolidLine, const qreal &weight=0.35, Qt::PenCapStyle cap=Qt::SquareCap)
QPointF scenePos
Definition: visualization.h:98
VScaledEllipse * InitPoint(const QColor &color, QGraphicsItem *parent, qreal z=0) const
quint32 object1Id
Qt::PenStyle lineStyle
static Q_REQUIRED_RESULT bool VFuzzyComparePossibleNulls(double p1, double p2)
Definition: def.h:490
const qreal defPointRadiusPixel
Definition: global.cpp:59
#define NULL_ID
Definition: ifcdef.h:76
const int EMPTY_ANGLE
@ Creation