Seamly2D
Code documentation
scalesceneitems.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 21 6, 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 "scalesceneitems.h"
53 #include "vcurvepathitem.h"
54 #include "global.h"
55 
56 #include <QtCore/qmath.h>
57 #include <QPen>
58 
59 //---------------------------------------------------------------------------------------------------------------------
60 VScaledLine::VScaledLine(QGraphicsItem *parent)
61  : QGraphicsLineItem(parent),
62  basicWidth(widthMainLine)
63 {}
64 
65 //---------------------------------------------------------------------------------------------------------------------
66 VScaledLine::VScaledLine(const QLineF &line, QGraphicsItem *parent)
67  : QGraphicsLineItem(line, parent),
68  basicWidth(widthMainLine)
69 {}
70 
71 //---------------------------------------------------------------------------------------------------------------------
72 void VScaledLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
73 {
74  QPen lPen = pen();
75  lPen.setWidthF(scaleWidth(basicWidth, sceneScale(scene())));
76  setPen(lPen);
77 
78  QGraphicsLineItem::paint(painter, option, widget);
79 }
80 
81 //---------------------------------------------------------------------------------------------------------------------
83 {
84  return basicWidth;
85 }
86 
87 //---------------------------------------------------------------------------------------------------------------------
88 void VScaledLine::setBasicWidth(const qreal &value)
89 {
90  basicWidth = value;
91 }
92 
93 //---------------------------------------------------------------------------------------------------------------------
94 ArrowedLineItem::ArrowedLineItem(QGraphicsItem *parent)
95  : QGraphicsLineItem(parent)
96  , m_arrows(new VCurvePathItem(this))
97 {}
98 
99 //---------------------------------------------------------------------------------------------------------------------
100 ArrowedLineItem::ArrowedLineItem(const QLineF &line, QGraphicsItem *parent)
101  : QGraphicsLineItem(line, parent)
102  , m_arrows(new VCurvePathItem(this))
103 {}
104 
105 //---------------------------------------------------------------------------------------------------------------------
106 void ArrowedLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
107 {
108  QPen lPen = pen();
109  lPen.setWidthF(scaleWidth(widthMainLine, sceneScale(scene())));
110  setPen(lPen);
111  m_arrows->setPen(lPen);
112 
113  QPainterPath path;
114  path.moveTo(line().p1());
115  path.lineTo(line().p2());
116 
117  qreal arrow_step = 60;
118  qreal arrow_size = 10;
119 
120  if (line().length() < arrow_step)
121  {
122  drawArrow(line(), path, arrow_size);
123  }
124 
125  QLineF axis;
126  axis.setP1(line().p1());
127  axis.setAngle(line().angle());
128  axis.setLength(arrow_step);
129 
130  int steps = qFloor(line().length()/arrow_step);
131  for (int i=0; i<steps; ++i)
132  {
133  drawArrow(axis, path, arrow_size);
134  axis.setLength(axis.length()+arrow_step);
135  }
136  m_arrows->setPath(path);
137 
138  QGraphicsLineItem::paint(painter, option, widget);
139 }
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 void ArrowedLineItem::drawArrow(const QLineF &axis, QPainterPath &path, const qreal &arrow_size)
143 {
144  QLineF arrowPart1;
145  arrowPart1.setP1(axis.p2());
146  arrowPart1.setLength(arrow_size);
147  arrowPart1.setAngle(axis.angle()+180+35);
148 
149  path.moveTo(arrowPart1.p1());
150  path.lineTo(arrowPart1.p2());
151 
152  QLineF arrowPart2;
153  arrowPart2.setP1(axis.p2());
154  arrowPart2.setLength(arrow_size);
155  arrowPart2.setAngle(axis.angle()+180-35);
156 
157  path.moveTo(arrowPart2.p1());
158  path.lineTo(arrowPart2.p2());
159 }
160 
161 //---------------------------------------------------------------------------------------------------------------------
162 VScaledEllipse::VScaledEllipse(QGraphicsItem *parent)
163  : QGraphicsEllipseItem(parent)
164 {}
165 
166 //---------------------------------------------------------------------------------------------------------------------
167 void VScaledEllipse::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
168 {
169  const qreal scale = sceneScale(scene());
170  const qreal width = scaleWidth(widthMainLine, scale);
171 
172  QPen visPen = pen();
173  visPen.setWidthF(width);
174 
175  setPen(visPen);
176  scaleCircleSize(this, scale);
177 
178  QGraphicsEllipseItem::paint(painter, option, widget);
179 }
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
ArrowedLineItem(QGraphicsItem *parent=nullptr)
void drawArrow(const QLineF &axis, QPainterPath &path, const qreal &arrow_size)
VCurvePathItem * m_arrows
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
VScaledEllipse(QGraphicsItem *parent=nullptr)
qreal GetBasicWidth() const
void setBasicWidth(const qreal &value)
VScaledLine(QGraphicsItem *parent=nullptr)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
const qreal widthMainLine
Definition: global.cpp:60
qreal sceneScale(QGraphicsScene *scene)
Definition: global.cpp:63
void scaleCircleSize(QGraphicsEllipseItem *item, qreal scale)
Definition: global.cpp:114
qreal scaleWidth(qreal width, qreal scale)
Definition: global.cpp:130