1 /************************************************************************
2  **
3  **  @file   vtoollinepoint.cpp
4  **  @author Roman Telezhynskyi <dismine(at)gmail.com>
5  **  @date   November 15, 2013
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) 2013-2015 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 "vtoollinepoint.h"
30 
31 #include <QColor>
32 #include <QGraphicsLineItem>
33 #include <QLineF>
34 #include <QPen>
35 #include <QPoint>
36 #include <QPointF>
37 #include <QSharedPointer>
38 #include <Qt>
39 #include <new>
40 
41 #include "../ifc/xml/vabstractpattern.h"
42 #include "../ifc/xml/vdomdocument.h"
43 #include "../ifc/ifcdef.h"
44 #include "../vgeometry/vgobject.h"
45 #include "../vgeometry/vpointf.h"
46 #include "../vmisc/vabstractapplication.h"
47 #include "../vpatterndb/vcontainer.h"
48 #include "../../../../vabstracttool.h"
49 #include "../../../vdrawtool.h"
50 #include "../vtoolsinglepoint.h"
51 #include "../vwidgets/scalesceneitems.h"
52 
53 template <class T> class QSharedPointer;
54 
55 //---------------------------------------------------------------------------------------------------------------------
56 /**
57  * @brief VToolLinePoint constructor.
58  * @param doc dom document container.
59  * @param data container with variables.
60  * @param id object id in container.
61  * @param typeLine line type.
62  * @param lineColor line color.
63  * @param formula string with length formula.
64  * @param basePointId id base line point.
65  * @param angle line angle.
66  * @param parent parent object.
67  */
VToolLinePoint(VAbstractPattern * doc,VContainer * data,const quint32 & id,const QString & typeLine,const QString & lineColor,const QString & formula,const quint32 & basePointId,const qreal & angle,const QString & notes,QGraphicsItem * parent)68 VToolLinePoint::VToolLinePoint(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &typeLine,
69                                const QString &lineColor, const QString &formula, const quint32 &basePointId,
70                                const qreal &angle, const QString &notes, QGraphicsItem *parent)
71     :VToolSinglePoint(doc, data, id, notes, parent), formulaLength(formula), angle(angle), basePointId(basePointId),
72       mainLine(nullptr), lineColor(lineColor)
73 {
74     this->m_lineType = typeLine;
75     Q_ASSERT_X(basePointId != 0, Q_FUNC_INFO, "basePointId == 0"); //-V654 //-V712
76     QPointF point1 = static_cast<QPointF>(*data->GeometricObject<VPointF>(basePointId));
77     QPointF point2 = static_cast<QPointF>(*data->GeometricObject<VPointF>(id));
78     mainLine = new VScaledLine(QLineF(point1 - point2, QPointF()), this);
79     mainLine->SetBoldLine(false);
80     mainLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
81 }
82 
83 //---------------------------------------------------------------------------------------------------------------------
~VToolLinePoint()84 VToolLinePoint::~VToolLinePoint()
85 {
86     delete mainLine;
87 }
88 
89 //---------------------------------------------------------------------------------------------------------------------
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)90 void VToolLinePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
91 {
92     QPen mPen = mainLine->pen();
93     mPen.setColor(CorrectColor(this, lineColor));
94     mPen.setStyle(LineStyleToPenStyle(m_lineType));
95 
96     mainLine->setPen(mPen);
97 
98     VToolSinglePoint::paint(painter, option, widget);
99 }
100 
101 //---------------------------------------------------------------------------------------------------------------------
102 /**
103  * @brief RefreshGeometry  refresh item on scene.
104  */
RefreshGeometry()105 void VToolLinePoint::RefreshGeometry()
106 {
107     VToolSinglePoint::RefreshPointGeometry(*VDrawTool::data.GeometricObject<VPointF>(m_id));
108     QPointF point = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(m_id));
109     QPointF basePoint = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(basePointId));
110     mainLine->setLine(QLineF(basePoint - point, QPointF()));
111 }
112 
113 //---------------------------------------------------------------------------------------------------------------------
114 /**
115  * @brief RemoveReferens decrement value of reference.
116  */
RemoveReferens()117 void VToolLinePoint::RemoveReferens()
118 {
119     const auto basePoint = VAbstractTool::data.GetGObject(basePointId);
120     doc->DecrementReferens(basePoint->getIdTool());
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
SaveOptions(QDomElement & tag,QSharedPointer<VGObject> & obj)124 void VToolLinePoint::SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj)
125 {
126     VToolSinglePoint::SaveOptions(tag, obj);
127 
128     doc->SetAttribute(tag, AttrTypeLine, m_lineType);
129     doc->SetAttribute(tag, AttrLineColor, lineColor);
130 }
131 
132 //---------------------------------------------------------------------------------------------------------------------
hoverEnterEvent(QGraphicsSceneHoverEvent * event)133 void VToolLinePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
134 {
135     mainLine->SetBoldLine(true);
136     VToolSinglePoint::hoverEnterEvent(event);
137 }
138 
139 //---------------------------------------------------------------------------------------------------------------------
hoverLeaveEvent(QGraphicsSceneHoverEvent * event)140 void VToolLinePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
141 {
142     mainLine->SetBoldLine(false);
143     VToolSinglePoint::hoverLeaveEvent(event);
144 }
145 
146 //---------------------------------------------------------------------------------------------------------------------
MakeToolTip() const147 QString VToolLinePoint::MakeToolTip() const
148 {
149     const QSharedPointer<VPointF> first = VAbstractTool::data.GeometricObject<VPointF>(basePointId);
150     const QSharedPointer<VPointF> second = VAbstractTool::data.GeometricObject<VPointF>(m_id);
151 
152     const QLineF line(static_cast<QPointF>(*first), static_cast<QPointF>(*second));
153 
154     const QString toolTip = QString("<table>"
155                                     "<tr> <td><b>%6:</b> %7</td> </tr>"
156                                     "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
157                                     "<tr> <td><b>%4:</b> %5°</td> </tr>"
158                                     "</table>")
159             .arg(tr("Length"))
160             .arg(VAbstractValApplication::VApp()->fromPixel(line.length()))
161             .arg(UnitsToStr(VAbstractValApplication::VApp()->patternUnits(), true), tr("Angle"))
162             .arg(line.angle())
163             .arg(tr("Label"), second->name());
164     return toolTip;
165 }
166 
167 //---------------------------------------------------------------------------------------------------------------------
Disable(bool disable,const QString & namePP)168 void VToolLinePoint::Disable(bool disable, const QString &namePP)
169 {
170     VToolSinglePoint::Disable(disable, namePP);
171     mainLine->setEnabled(isEnabled());
172 }
173 
174 //---------------------------------------------------------------------------------------------------------------------
175 /**
176  * @brief FullUpdateFromFile update tool data form file.
177  */
FullUpdateFromFile()178 void VToolLinePoint::FullUpdateFromFile()
179 {
180     ReadAttributes();
181     RefreshGeometry();
182     SetVisualization();
183 }
184 
185 //---------------------------------------------------------------------------------------------------------------------
GetAngle() const186 qreal VToolLinePoint::GetAngle() const
187 {
188     return angle;
189 }
190 
191 //---------------------------------------------------------------------------------------------------------------------
SetAngle(const qreal & value)192 void VToolLinePoint::SetAngle(const qreal &value)
193 {
194     angle = value;
195     QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(m_id);
196     SaveOption(obj);
197 }
198 
199 //---------------------------------------------------------------------------------------------------------------------
GetLineColor() const200 QString VToolLinePoint::GetLineColor() const
201 {
202     return lineColor;
203 }
204 
205 //---------------------------------------------------------------------------------------------------------------------
SetLineColor(const QString & value)206 void VToolLinePoint::SetLineColor(const QString &value)
207 {
208     lineColor = value;
209 
210     QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(m_id);
211     SaveOption(obj);
212 }
213 
214 //---------------------------------------------------------------------------------------------------------------------
GetFormulaLength() const215 VFormula VToolLinePoint::GetFormulaLength() const
216 {
217     VFormula fLength(formulaLength, this->getData());
218     fLength.setCheckZero(false);
219     fLength.setToolId(m_id);
220     fLength.setPostfix(UnitsToStr(VAbstractValApplication::VApp()->patternUnits()));
221     fLength.Eval();
222 
223     return fLength;
224 }
225 
226 //---------------------------------------------------------------------------------------------------------------------
SetFormulaLength(const VFormula & value)227 void VToolLinePoint::SetFormulaLength(const VFormula &value)
228 {
229     if (value.error() == false)
230     {
231         formulaLength = value.GetFormula(FormulaType::FromUser);
232 
233         QSharedPointer<VGObject> obj = VAbstractTool::data.GetGObject(m_id);
234         SaveOption(obj);
235     }
236 }
237 
238 //---------------------------------------------------------------------------------------------------------------------
BasePointName() const239 QString VToolLinePoint::BasePointName() const
240 {
241     return VAbstractTool::data.GetGObject(basePointId)->name();
242 }
243