Seamly2D
Code documentation
vcurvepathitem.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 16 5, 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 "vcurvepathitem.h"
53 #include "../vwidgets/global.h"
54 #include "../vgeometry/vabstractcurve.h"
55 
56 #include <QPainter>
57 
58 //---------------------------------------------------------------------------------------------------------------------
59 VCurvePathItem::VCurvePathItem(QGraphicsItem *parent)
60  : QGraphicsPathItem(parent),
61  m_directionArrows(),
62  m_points()
63 {
64 }
65 
66 //---------------------------------------------------------------------------------------------------------------------
67 QPainterPath VCurvePathItem::shape() const
68 {
69  QPainterPath itemPath;
70 
71  if (not m_points.isEmpty())
72  {
73  for (qint32 i = 0; i < m_points.count()-1; ++i)
74  {
75  itemPath.moveTo(m_points.at(i));
76  itemPath.lineTo(m_points.at(i+1));
77  }
78  }
79  else
80  {
81  itemPath = path();
82  }
83 
84  const QPainterPath arrowsPath = VAbstractCurve::ShowDirection(m_directionArrows,
86  sceneScale(scene())));
87  if (arrowsPath != QPainterPath())
88  {
89  itemPath.addPath(arrowsPath);
90  }
91  itemPath.setFillRule(Qt::WindingFill);
92  return ItemShapeFromPath(itemPath, pen());
93 }
94 
95 //---------------------------------------------------------------------------------------------------------------------
96 void VCurvePathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
97 {
98  ScalePenWidth();
99 
100  const QPainterPath arrowsPath = VAbstractCurve::ShowDirection(m_directionArrows,
102  sceneScale(scene())));
103 
104  if (arrowsPath != QPainterPath())
105  {
106  painter->save();
107 
108  QPen arrowPen(pen());
109  arrowPen.setStyle(Qt::SolidLine);
110 
111  painter->setPen(arrowPen);
112  painter->setBrush(brush());
113  painter->drawPath(arrowsPath);
114 
115  painter->restore();
116  }
117 
118  QGraphicsPathItem::paint(painter, option, widget);
119 }
120 
121 //---------------------------------------------------------------------------------------------------------------------
122 void VCurvePathItem::SetDirectionArrows(const QVector<QPair<QLineF, QLineF> > &arrows)
123 {
124  m_directionArrows = arrows;
125 }
126 
127 //---------------------------------------------------------------------------------------------------------------------
129 {
130  m_points = points;
131 }
132 
133 //---------------------------------------------------------------------------------------------------------------------
135 {
136  const qreal width = scaleWidth(widthMainLine, sceneScale(scene()));
137 
138  QPen toolPen = pen();
139  toolPen.setWidthF(width);
140 
141  setPen(toolPen);
142 }
static const qreal lengthCurveDirectionArrow
static QPainterPath ShowDirection(const QVector< DirectionArrow > &arrows, qreal width)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
QVector< QPointF > m_points
void SetPoints(const QVector< QPointF > &points)
virtual void ScalePenWidth()
void SetDirectionArrows(const QVector< QPair< QLineF, QLineF >> &arrows)
virtual QPainterPath shape() const Q_DECL_OVERRIDE
QVector< QPair< QLineF, QLineF > > m_directionArrows
VCurvePathItem(QGraphicsItem *parent=nullptr)
const qreal widthMainLine
Definition: global.cpp:60
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen)
Definition: global.cpp:140
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130