Seamly2D
Code documentation
visline.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  **
3  ** @file visline.cpp
4  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
5  ** @date 21 7, 2014
6  **
7  ** @author Douglas S Caskey
8  ** @date 7.23.2022
9  **
10  ** @brief
11  ** @copyright
12  ** This source code is part of the Valentine project, a pattern making
13  ** program, whose allow create and modeling patterns of clothing.
14  ** Copyright (C) 2013-2022 Seamly2D project
15  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
16  **
17  ** Seamly2D is free software: you can redistribute it and/or modify
18  ** it under the terms of the GNU General Public License as published by
19  ** the Free Software Foundation, either version 3 of the License, or
20  ** (at your option) any later version.
21  **
22  ** Seamly2D is distributed in the hope that it will be useful,
23  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  ** GNU General Public License for more details.
26  **
27  ** You should have received a copy of the GNU General Public License
28  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
29  **
30  *************************************************************************/
31 
32 #include "visline.h"
33 
34 #include <QGraphicsScene>
35 #include <QGuiApplication>
36 #include <QPen>
37 #include <QRectF>
38 
39 #include "../ifc/ifcdef.h"
40 #include "../vgeometry/vgobject.h"
41 #include "../vmisc/vabstractapplication.h"
42 #include "../vmisc/vmath.h"
43 #include "../vpatterndb/vcontainer.h"
44 
45 //---------------------------------------------------------------------------------------------------------------------
46 VisLine::VisLine(const VContainer *data, QGraphicsItem *parent)
47  :Visualization(data), VScaledLine(parent)
48 {
49  this->setZValue(1);// Show on top real tool
50  initPen();
51 }
52 
53 //---------------------------------------------------------------------------------------------------------------------
54 qreal VisLine::CorrectAngle(const qreal &angle)
55 {
56  qreal ang = angle;
57  if (angle > 360)
58  {
59  ang = angle - 360.0 * qFloor(angle/360);
60  }
61 
62  switch (qFloor((qAbs(ang)+22.5)/45))
63  {
64  case 1: // <67.5
65  return 45;
66  case 2: // <112.5
67  return 90;
68  case 3: // <157.5
69  return 135;
70  case 4: // <202.5
71  return 180;
72  case 5: // <247.5
73  return 225;
74  case 6: // < 292.5
75  return 270;
76  case 7: // <337.5
77  return 315;
78  case 0: // <22.5
79  default: // <360
80  return 0;
81  }
82 }
83 
84 //---------------------------------------------------------------------------------------------------------------------
85 QPointF VisLine::Ray(const QPointF &firstPoint, const qreal &angle) const
86 {
87  if (this->scene() == nullptr)
88  {
89  QLineF line = QLineF(firstPoint, Visualization::scenePos);
90  line.setAngle(angle);
91  return line.p2();// We can't find ray because item doesn't have scene. We will return cursor position on scene.
92  }
93 
94  QRectF scRect = this->scene()->sceneRect();
95 
96  //Limit size of the scene rect. Axis that has same size as scene rect cause scene size growth.
97  QLineF line1 = QLineF(scRect.topLeft(), scRect.bottomRight());
98  line1.setLength(2);
99 
100  QLineF line2 = QLineF(scRect.bottomRight(), scRect.topLeft());
101  line2.setLength(2);
102 
103  scRect = QRectF(line1.p2(), line2.p2());
104 
105  if (QGuiApplication::keyboardModifiers() == Qt::ShiftModifier)
106  {
107  return VGObject::BuildRay(firstPoint, CorrectAngle(angle), scRect);
108  }
109  else
110  {
111  return VGObject::BuildRay(firstPoint, angle, scRect);
112  }
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
116 QPointF VisLine::Ray(const QPointF &firstPoint) const
117 {
118  QLineF line = QLineF(firstPoint, Visualization::scenePos);
119  return Ray(firstPoint, line.angle());
120 }
121 
122 //---------------------------------------------------------------------------------------------------------------------
123 QLineF VisLine::Axis(const QPointF &p, const qreal &angle) const
124 {
125  QPointF endP1 = Ray(p, angle+180);
126  QPointF endP2 = Ray(p, angle);
127  return QLineF(endP1, endP2);
128 }
129 
130 //---------------------------------------------------------------------------------------------------------------------
131 QLineF VisLine::Axis(const QPointF &p1, const QPointF &p2) const
132 {
133  QLineF line(p1, p2);
134  return Axis(p1, line.angle());
135 }
136 
137 //---------------------------------------------------------------------------------------------------------------------
139 {
140  QPen visPen = pen();
141  visPen.setColor(mainColor);
142  visPen.setStyle(lineStyle);
143  visPen.setWidthF(lineWeight);
144 
145  setPen(visPen);
146 }
147 
148 //---------------------------------------------------------------------------------------------------------------------
150 {
151  addItem(this);
152 }
153 
154 //---------------------------------------------------------------------------------------------------------------------
155 void VisLine::DrawRay(VScaledLine *lineItem, const QPointF &p, const QPointF &pTangent, const QColor &color,
156  Qt::PenStyle style)
157 {
158  SCASSERT (lineItem != nullptr)
159 
160  const qreal angle = QLineF(p, pTangent).angle();
161  const QPointF endRay = Ray(p, angle);
162  const QLineF tangent = VGObject::BuildLine(p, QLineF(p, endRay).length(), angle);
163  DrawLine(lineItem, tangent, color, 0.35, style);
164 }
The VContainer class container of all variables.
Definition: vcontainer.h:141
static QPointF BuildRay(const QPointF &firstPoint, const qreal &angle, const QRectF &scRect)
Definition: vgobject.cpp:251
static QLineF BuildLine(const QPointF &p1, const qreal &length, const qreal &angle)
Definition: vgobject.cpp:241
VisLine(const VContainer *data, QGraphicsItem *parent=nullptr)
Definition: visline.cpp:46
virtual void initPen() Q_DECL_OVERRIDE
Definition: visline.cpp:138
static qreal CorrectAngle(const qreal &angle)
Definition: visline.cpp:54
void DrawRay(VScaledLine *lineItem, const QPointF &p, const QPointF &pTangent, const QColor &color, Qt::PenStyle style)
Definition: visline.cpp:155
virtual void AddOnScene() Q_DECL_OVERRIDE
Definition: visline.cpp:149
QLineF Axis(const QPointF &p, const qreal &angle) const
Definition: visline.cpp:123
QPointF Ray(const QPointF &firstPoint, const qreal &angle) const
Definition: visline.cpp:85
QColor mainColor
Definition: visualization.h:99
QPointF scenePos
Definition: visualization.h:98
virtual void DrawLine(VScaledLine *lineItem, const QLineF &line, const QColor &color, const qreal &lineWeight, Qt::PenStyle style=Qt::SolidLine)
Qt::PenStyle lineStyle
void addItem(Item *item)
#define SCASSERT(cond)
Definition: def.h:317