1 /************************************************************************
2  **
3  **  @file
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   16 5, 2017
6  **
7  **  @brief
8  **  @copyright
9  **  This source code is part of the Valentina project, a pattern making
10  **  program, whose allow create and modeling patterns of clothing.
11  **  Copyright (C) 2017 Valentina project
12  **  <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
13  **
14  **  Valentina is free software: you can redistribute it and/or modify
15  **  it under the terms of the GNU General Public License as published by
16  **  the Free Software Foundation, either version 3 of the License, or
17  **  (at your option) any later version.
18  **
19  **  Valentina is distributed in the hope that it will be useful,
20  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  **  GNU General Public License for more details.
23  **
24  **  You should have received a copy of the GNU General Public License
25  **  along with Valentina.  If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "vcurvepathitem.h"
30 #include "../vwidgets/global.h"
31 #include "../vgeometry/vabstractcurve.h"
32 #include "../vmisc/vabstractapplication.h"
33 
34 #include <QPainter>
35 
36 //---------------------------------------------------------------------------------------------------------------------
VCurvePathItem(QGraphicsItem * parent)37 VCurvePathItem::VCurvePathItem(QGraphicsItem *parent)
38     : QGraphicsPathItem(parent),
39       m_directionArrows(),
40       m_points(),
41       m_defaultWidth(VAbstractApplication::VApp()->Settings()->WidthMainLine())
42 {}
43 
44 //---------------------------------------------------------------------------------------------------------------------
shape() const45 QPainterPath VCurvePathItem::shape() const
46 {
47     QPainterPath itemPath;
48 
49     if (not m_points.isEmpty())
50     {
51         for (qint32 i = 0; i < m_points.count()-1; ++i)
52         {
53             itemPath.moveTo(m_points.at(i));
54             itemPath.lineTo(m_points.at(i+1));
55         }
56     }
57     else
58     {
59         itemPath = path();
60     }
61 
62     const QPainterPath arrowsPath = VAbstractCurve::ShowDirection(m_directionArrows,
63                                                                  ScaleWidth(VAbstractCurve::LengthCurveDirectionArrow(),
64                                                                              SceneScale(scene())));
65     if (arrowsPath != QPainterPath())
66     {
67         itemPath.addPath(arrowsPath);
68     }
69     itemPath.setFillRule(Qt::WindingFill);
70     return ItemShapeFromPath(itemPath, pen());
71 }
72 
73 //---------------------------------------------------------------------------------------------------------------------
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)74 void VCurvePathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 {
76     ScalePenWidth();
77 
78     const QPainterPath arrowsPath = VAbstractCurve::ShowDirection(m_directionArrows,
79                                                                  ScaleWidth(VAbstractCurve::LengthCurveDirectionArrow(),
80                                                                              SceneScale(scene())));
81 
82     if (arrowsPath != QPainterPath())
83     {
84         painter->save();
85 
86         QPen arrowPen(pen());
87         arrowPen.setStyle(Qt::SolidLine);
88 
89         painter->setPen(arrowPen);
90         painter->setBrush(brush());
91         painter->drawPath(arrowsPath);
92 
93         painter->restore();
94     }
95 
96     PaintWithFixItemHighlightSelected<QGraphicsPathItem>(this, painter, option, widget);
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
SetDirectionArrows(const QVector<QPair<QLineF,QLineF>> & arrows)100 void VCurvePathItem::SetDirectionArrows(const QVector<QPair<QLineF, QLineF> > &arrows)
101 {
102     m_directionArrows = arrows;
103 }
104 
105 //---------------------------------------------------------------------------------------------------------------------
SetPoints(const QVector<QPointF> & points)106 void VCurvePathItem::SetPoints(const QVector<QPointF> &points)
107 {
108     m_points = points;
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
SetWidth(qreal width)112 void VCurvePathItem::SetWidth(qreal width)
113 {
114     m_defaultWidth = width;
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
ScalePenWidth()118 void VCurvePathItem::ScalePenWidth()
119 {
120     const qreal width = ScaleWidth(m_defaultWidth, SceneScale(scene()));
121 
122     QPen toolPen = pen();
123     toolPen.setWidthF(width);
124 
125     setPen(toolPen);
126 }
127