Seamly2D
Code documentation
vistoolrotation.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 vistoolrotation.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 11 4, 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 "vistoolrotation.h"
53 
54 #include <limits.h>
55 #include <QGraphicsLineItem>
56 #include <QGraphicsPathItem>
57 #include <QGuiApplication>
58 #include <QLineF>
59 #include <QPainterPath>
60 #include <QSharedPointer>
61 #include <Qt>
62 #include <QtAlgorithms>
63 #include <new>
64 
65 #include "../vmisc/diagnostic.h"
66 #include "../vgeometry/vabstractcurve.h"
67 #include "../vgeometry/varc.h"
68 #include "../vgeometry/vcubicbezier.h"
69 #include "../vgeometry/vcubicbezierpath.h"
70 #include "../vgeometry/vellipticalarc.h"
71 #include "../vgeometry/vgeometrydef.h"
72 #include "../vgeometry/vgobject.h"
73 #include "../vgeometry/vpointf.h"
74 #include "../vgeometry/vspline.h"
75 #include "../vgeometry/vsplinepath.h"
76 #include "../vmisc/vabstractapplication.h"
77 #include "../vpatterndb/vcontainer.h"
78 #include "../vwidgets/vmaingraphicsscene.h"
79 #include "visoperation.h"
80 
81 //---------------------------------------------------------------------------------------------------------------------
82 VisToolRotation::VisToolRotation(const VContainer *data, QGraphicsItem *parent)
83  : VisOperation(data, parent),
84  angle(INT_MIN),
85  point(nullptr),
86  angleArc(nullptr),
87  xAxis(nullptr)
88 {
89  point = InitPoint(supportColor2, this);
90  angleArc = InitItem<VCurvePathItem>(supportColor2, this);
91  xAxis = InitItem<VScaledLine>(supportColor2, this);
92 }
93 
94 //---------------------------------------------------------------------------------------------------------------------
96 {
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
100 QT_WARNING_PUSH
101 QT_WARNING_DISABLE_GCC("-Wswitch-default")
102 
103 void VisToolRotation::RefreshGeometry()
104 {
105  if (objects.isEmpty())
106  {
107  return;
108  }
109 
111 
112  qreal tempAngle = 0;
113 
114  if (object1Id != NULL_ID)
115  {
116  origin = Visualization::data->GeometricObject<VPointF>(object1Id);
117  DrawPoint(point, static_cast<QPointF>(*origin), supportColor2);
118 
119  QLineF rLine;
120  if (VFuzzyComparePossibleNulls(angle, INT_MIN))
121  {
122  rLine = QLineF(static_cast<QPointF>(*origin), Visualization::scenePos);
123 
124  if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
125  {
126  rLine.setAngle(CorrectAngle(rLine.angle()));
127  }
128 
129  rLine.setP2(Ray(static_cast<QPointF>(*origin), rLine.angle()));
130  tempAngle = rLine.angle();
131  }
132  else
133  {
134  rLine = QLineF(static_cast<QPointF>(*origin), Ray(static_cast<QPointF>(*origin), angle));
135  tempAngle = angle;
136  }
137 
138  DrawLine(this, rLine, supportColor2, lineWeight, Qt::DashLine);
139  DrawLine(xAxis, QLineF(static_cast<QPointF>(*origin), Ray(static_cast<QPointF>(*origin), 0)), supportColor2,
140  lineWeight, Qt::DashLine);
141 
142  VArc arc(*origin, defPointRadiusPixel*2, 0, tempAngle);
143  DrawPath(angleArc, arc.GetPath(), supportColor2, Qt::SolidLine, lineWeight, Qt::RoundCap);
144 
145  Visualization::toolTip = tr("Rotating angle = %1°, Hold <b>SHIFT</b> to constrain angle, "
146  "<b>Mouse click</b> - finish creation").arg(tempAngle);
147  }
148 
149  int iPoint = -1;
150  int iCurve = -1;
151  for (int i = 0; i < objects.size(); ++i)
152  {
153  const quint32 id = objects.at(i);
155 
156  // This check helps to find missed objects in the switch
157  Q_STATIC_ASSERT_X(static_cast<int>(GOType::Unknown) == 7, "Not all objects was handled.");
158 
159  switch(static_cast<GOType>(obj->getType()))
160  {
161  case GOType::Point:
162  {
164 
165  ++iPoint;
166  VScaledEllipse *point = GetPoint(static_cast<quint32>(iPoint), supportColor2);
167  DrawPoint(point, static_cast<QPointF>(*p), supportColor2);
168 
169  ++iPoint;
170  point = GetPoint(static_cast<quint32>(iPoint), supportColor);
171 
172  if (object1Id != NULL_ID)
173  {
174  DrawPoint(point, static_cast<QPointF>(p->Rotate(static_cast<QPointF>(*origin), tempAngle)),
175  supportColor);
176  }
177  break;
178  }
179  case GOType::Arc:
180  {
181  iCurve = AddCurve<VArc>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
182  break;
183  }
185  {
186  iCurve = AddCurve<VEllipticalArc>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
187  break;
188  }
189  case GOType::Spline:
190  {
191  iCurve = AddCurve<VSpline>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
192  break;
193  }
194  case GOType::SplinePath:
195  {
196  iCurve = AddCurve<VSplinePath>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
197  break;
198  }
199  case GOType::CubicBezier:
200  {
201  iCurve = AddCurve<VCubicBezier>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
202  break;
203  }
205  {
206  iCurve = AddCurve<VCubicBezierPath>(tempAngle, static_cast<QPointF>(*origin), id, iCurve);
207  break;
208  }
209  case GOType::Unknown:
210  case GOType::Curve:
211  case GOType::Path:
212  case GOType::AllCurves:
213  default:
214  break;
215  }
216  }
217 }
219 
220 //---------------------------------------------------------------------------------------------------------------------
222 {
223  object1Id = value;
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
227 QString VisToolRotation::Angle() const
228 {
229  return QString::number(line().angle());
230 }
231 
232 //---------------------------------------------------------------------------------------------------------------------
233 void VisToolRotation::SetAngle(const QString &expression)
234 {
235  angle = FindVal(expression, Visualization::data->DataVariables());
236 }
237 
238 //---------------------------------------------------------------------------------------------------------------------
239 template <class Item>
240 int VisToolRotation::AddCurve(qreal angle, const QPointF &origin, quint32 id, int i)
241 {
242  const QSharedPointer<Item> curve = Visualization::data->template GeometricObject<Item>(id);
243 
244  ++i;
245  VCurvePathItem *path = GetCurve(static_cast<quint32>(i), supportColor2);
246  DrawPath(path, curve->GetPath(), curve->DirectionArrows(), supportColor2, Qt::SolidLine,
247  lineWeight, Qt::RoundCap);
248 
249  ++i;
250  path = GetCurve(static_cast<quint32>(i), supportColor);
251  if (object1Id != NULL_ID)
252  {
253  const Item rotated = curve->Rotate(origin, angle);
254  DrawPath(path, rotated.GetPath(), rotated.DirectionArrows(), supportColor, Qt::SolidLine,
255  lineWeight, Qt::RoundCap);
256  }
257 
258  return i;
259 }
virtual QPainterPath GetPath() const
VArc class for anticlockwise arc.
Definition: varc.h:74
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< VGObject > GetGObject(quint32 id) const
GetGObject returns a point by id.
Definition: vcontainer.cpp:150
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
The VPointF class keep data of point.
Definition: vpointf.h:75
VCurvePathItem * GetCurve(quint32 i, const QColor &color)
QColor supportColor2
Definition: visoperation.h:75
void SetAngle(const QString &expression)
VScaledLine * xAxis
virtual ~VisToolRotation()
VCurvePathItem * angleArc
VScaledEllipse * point
int AddCurve(qreal angle, const QPointF &origin, quint32 id, int i)
QString Angle() const
void SetOriginPointId(quint32 value)
VisToolRotation(const VContainer *data, QGraphicsItem *parent=nullptr)
QColor supportColor
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
static qreal FindVal(const QString &expression, const QHash< QString, QSharedPointer< VInternalVariable > > *vars)
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
GOType
Definition: vgeometrydef.h:56
@ CubicBezierPath
@ SplinePath
@ EllipticalArc
@ CubicBezier