Seamly2D
Code documentation
vtoollinepoint.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 vtoollinepoint.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date November 15, 2013
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) 2013-2015 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 "vtoollinepoint.h"
53 
54 #include <QColor>
55 #include <QGraphicsLineItem>
56 #include <QLineF>
57 #include <QPen>
58 #include <QPoint>
59 #include <QPointF>
60 #include <QSharedPointer>
61 #include <Qt>
62 #include <new>
63 
64 #include "../ifc/xml/vabstractpattern.h"
65 #include "../ifc/xml/vdomdocument.h"
66 #include "../ifc/ifcdef.h"
67 #include "../vgeometry/vgobject.h"
68 #include "../vgeometry/vpointf.h"
69 #include "../vmisc/vabstractapplication.h"
70 #include "../vpatterndb/vcontainer.h"
71 #include "../../../../vabstracttool.h"
72 #include "../../../vdrawtool.h"
73 #include "../vtoolsinglepoint.h"
74 #include "../vwidgets/scalesceneitems.h"
75 
76 template <class T> class QSharedPointer;
77 
78 //---------------------------------------------------------------------------------------------------------------------
79 /**
80  * @brief VToolLinePoint constructor.
81  * @param doc dom document container.
82  * @param data container with variables.
83  * @param id object id in container.
84  * @param lineType line type.
85  * @param lineColor line color.
86  * @param formula string with length formula.
87  * @param basePointId id base line point.
88  * @param angle line angle.
89  * @param parent parent object.
90  */
92  const QString &lineType, const QString &lineWeight,
93  const QString &lineColor, const QString &formula, const quint32 &basePointId,
94  const qreal &angle, QGraphicsItem *parent)
95  : VToolSinglePoint(doc, data, id, QColor(lineColor), parent)
96  , formulaLength(formula)
97  , angle(angle)
98  , basePointId(basePointId)
99  , mainLine(nullptr)
100  , lineColor(lineColor)
101 {
102  this->m_lineType = lineType;
103  this->m_lineWeight = lineWeight;
104 
106 
107  Q_ASSERT_X(basePointId != 0, Q_FUNC_INFO, "basePointId == 0"); //-V654 //-V712
108  QPointF point1 = static_cast<QPointF>(*data->GeometricObject<VPointF>(basePointId));
109  QPointF point2 = static_cast<QPointF>(*data->GeometricObject<VPointF>(id));
110  mainLine = new VScaledLine(QLineF(point1 - point2, QPointF()), this);
112  mainLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
113 }
114 
115 //---------------------------------------------------------------------------------------------------------------------
117 {
118  delete mainLine;
119 }
120 
121 //---------------------------------------------------------------------------------------------------------------------
122 void VToolLinePoint::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
123 {
124  QPen pen = mainLine->pen();
125  pen.setColor(correctColor(this, doc->useGroupColor(m_id, lineColor)));
127  pen.setWidthF(ToPixel(doc->useGroupLineWeight(m_id, m_lineWeight).toDouble(), Unit::Mm));
128 
129  mainLine->setPen(pen);
130 
131  VToolSinglePoint::paint(painter, option, widget);
132 }
133 
134 //---------------------------------------------------------------------------------------------------------------------
135 /**
136  * @brief RefreshGeometry refresh item on scene.
137  */
139 {
141  VToolSinglePoint::refreshPointGeometry(*VDrawTool::data.GeometricObject<VPointF>(m_id));
142  QPointF point = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(m_id));
143  QPointF basePoint = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(basePointId));
144 
146  mainLine->setLine(QLineF(basePoint - point, QPointF()));
147 }
148 
149 //---------------------------------------------------------------------------------------------------------------------
150 /**
151  * @brief RemoveReferens decrement value of reference.
152  */
154 {
155  const auto basePoint = VAbstractTool::data.GetGObject(basePointId);
156  doc->DecrementReferens(basePoint->getIdTool());
157 }
158 
159 //---------------------------------------------------------------------------------------------------------------------
161 {
163 
167 }
168 
169 //---------------------------------------------------------------------------------------------------------------------
170 void VToolLinePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
171 {
174 }
175 
176 //---------------------------------------------------------------------------------------------------------------------
177 void VToolLinePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
178 {
181 }
182 
183 //---------------------------------------------------------------------------------------------------------------------
185 {
188 
189  const QLineF line(static_cast<QPointF>(*first), static_cast<QPointF>(*second));
190 
191  const QString toolTip = QString("<table>"
192  "<tr> <td><b> %6:</b> %7</td> </tr>"
193  "<tr> <td><b>%1:</b> %2 %3</td> </tr>"
194  "<tr> <td><b> %4:</b> %5°</td> </tr>"
195  "</table>")
196  .arg(tr("Length"))
197  .arg(qApp->fromPixel(line.length()))
198  .arg(UnitsToStr(qApp->patternUnit(), true))
199  .arg(tr("Angle"))
200  .arg(line.angle())
201  .arg(tr("Name"))
202  .arg(second->name());
203 
204  return toolTip;
205 }
206 
207 //---------------------------------------------------------------------------------------------------------------------
208 void VToolLinePoint::Disable(bool disable, const QString &draftBlockName)
209 {
210  VToolSinglePoint::Disable(disable, draftBlockName);
211  mainLine->setEnabled(isEnabled());
212 }
213 
214 //---------------------------------------------------------------------------------------------------------------------
215 /**
216  * @brief FullUpdateFromFile update tool data form file.
217  */
219 {
220  ReadAttributes();
221  RefreshGeometry();
223 }
224 
225 //---------------------------------------------------------------------------------------------------------------------
227 {
228  return angle;
229 }
230 
231 //---------------------------------------------------------------------------------------------------------------------
232 void VToolLinePoint::SetAngle(const qreal &value)
233 {
234  angle = value;
236  SaveOption(obj);
237 }
238 
239 //---------------------------------------------------------------------------------------------------------------------
241 {
242  return lineColor;
243 }
244 
245 //---------------------------------------------------------------------------------------------------------------------
246 void VToolLinePoint::setLineColor(const QString &value)
247 {
248  lineColor = value;
249 
251  SaveOption(obj);
252 }
253 
254 //---------------------------------------------------------------------------------------------------------------------
256 {
257  return basePointId;
258 }
259 
260 //---------------------------------------------------------------------------------------------------------------------
261 void VToolLinePoint::SetBasePointId(const quint32 &value)
262 {
263  if (value != NULL_ID)
264  {
265  basePointId = value;
266 
268  SaveOption(obj);
269  }
270 }
271 
272 //---------------------------------------------------------------------------------------------------------------------
274 {
275  VFormula fLength(formulaLength, this->getData());
276  fLength.setCheckZero(true);
277  fLength.setToolId(m_id);
278  fLength.setPostfix(UnitsToStr(qApp->patternUnit()));
279 
280  return fLength;
281 }
282 
283 //---------------------------------------------------------------------------------------------------------------------
285 {
286  if (value.error() == false)
287  {
289 
291  SaveOption(obj);
292  }
293 }
294 
295 //---------------------------------------------------------------------------------------------------------------------
297 {
299 }
QString useGroupColor(quint32 toolId, QString color)
QString useGroupLineWeight(quint32 toolId, QString weight)
QString useGroupLineType(quint32 toolId, QString type)
virtual void DecrementReferens(quint32 id) const =0
VAbstractPattern * doc
doc dom document container
const VContainer * getData() const
getData return pointer to data container.
const quint32 m_id
id object id.
virtual void SetVisualization()=0
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< VGObject > GetGObject(quint32 id) const
GetGObject returns a point by id.
Definition: vcontainer.cpp:150
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
VContainer data
data container with data
Definition: vdatatool.h:84
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const
SetAttribute set attribute in pattern file. Replace "," by ".".
Definition: vdomdocument.h:185
QString m_lineWeight
typeLine line type.
Definition: vdrawtool.h:104
QString m_lineType
activeBlockName name of tool's pattern peace.
Definition: vdrawtool.h:103
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
void ReadAttributes()
Definition: vdrawtool.cpp:190
bool error() const
Definition: vformula.cpp:238
QString GetFormula(FormulaType type=FormulaType::ToUser) const
Definition: vformula.cpp:135
void setToolId(const quint32 &value)
Definition: vformula.cpp:216
void setCheckZero(bool value)
Definition: vformula.cpp:184
void setPostfix(const QString &value)
Definition: vformula.cpp:228
The VPointF class keep data of point.
Definition: vpointf.h:75
void setBasicWidth(const qreal &value)
void setPointColor(const QString &value)
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
Definition: vscenepoint.cpp:86
virtual void refreshPointGeometry(const VPointF &point)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
VFormula GetFormulaLength() const
VToolLinePoint(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &lineType, const QString &lineWeight, const QString &lineColor, const QString &formula, const quint32 &basePointId, const qreal &angle, QGraphicsItem *parent=nullptr)
VToolLinePoint constructor.
virtual void RefreshGeometry()
lineColor color of a line.
VScaledLine * mainLine
basePointId id base line point.
qreal angle
formula string with length formula.
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
FullUpdateFromFile update tool data form file.
void SetAngle(const qreal &value)
QString getLineColor() const
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
QString BasePointName() const
virtual QString makeToolTip() const Q_DECL_OVERRIDE
qreal GetAngle() const
virtual ~VToolLinePoint() Q_DECL_OVERRIDE
quint32 basePointId
angle line angle.
void SetFormulaLength(const VFormula &value)
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
quint32 GetBasePointId() const
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
QString lineColor
mainLine line item.
void SetBasePointId(const quint32 &value)
void setLineColor(const QString &value)
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
QString formulaLength
The VToolSinglePoint class parent for all tools what create points.
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
double ToPixel(double val, const Unit &unit)
Definition: def.cpp:231
QString UnitsToStr(const Unit &unit, const bool translate)
UnitsToStr translate unit to string.
Definition: def.cpp:702
QColor correctColor(const QGraphicsItem *item, const QColor &color)
Definition: global.cpp:80
const QString AttrLineType
Definition: ifcdef.cpp:90
const QString AttrLineWeight
Definition: ifcdef.cpp:91
const QString AttrLineColor
Definition: ifcdef.cpp:130
Qt::PenStyle lineTypeToPenStyle(const QString &lineType)
LineStyle return pen style for current line style.
Definition: ifcdef.cpp:183
#define NULL_ID
Definition: ifcdef.h:76
#define qApp
Definition: vapplication.h:67