Seamly2D
Code documentation
doubleline_point_tool.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  **
3  ** @file doubleline_point_tool.h
4  ** @author Douglas S Caskey
5  ** @date 7.19.2022
6  **
7  ** @brief
8  ** @copyright
9  ** This source code is part of the Valentine project, a pattern making
10  ** program, whose allow create and modeling patterns of clothing.
11  ** Copyright (C) 2013-2022 Seamly2D project
12  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
13  **
14  ** Seamly2D 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  ** Seamly2D 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 Seamly2D. If not, see <http://www.gnu.org/licenses/>.
26  **
27  *************************************************************************/
28 
29 #include "doubleline_point_tool.h"
30 
31 #include "../vtoolsinglepoint.h"
32 #include "../ifc/xml/vabstractpattern.h"
33 #include "../ifc/xml/vdomdocument.h"
34 #include "../ifc/ifcdef.h"
35 #include "../vgeometry/vgobject.h"
36 #include "../vgeometry/vpointf.h"
37 #include "../vmisc/vabstractapplication.h"
38 #include "../vpatterndb/vcontainer.h"
39 #include "../vwidgets/scalesceneitems.h"
40 #include "../../../vdrawtool.h"
41 #include "../../../../vabstracttool.h"
42 
43 #include <QColor>
44 #include <QGraphicsLineItem>
45 #include <QLineF>
46 #include <QPen>
47 #include <QPoint>
48 #include <QPointF>
49 #include <QSharedPointer>
50 #include <Qt>
51 #include <new>
52 
53 template <class T> class QSharedPointer;
54 
55 //---------------------------------------------------------------------------------------------------------------------
56 /**
57  * @brief DoubleLinePointTool 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 firstPointId id first point.
65  * @param secondPointId id second point.
66  * @param parent parent object.
67  */
69  const QString &typeLine, const QString &lineWeight, const QString &lineColor,
70  const quint32 &firstPointId, const quint32 &secondPointId,
71  QGraphicsItem *parent)
72  : VToolSinglePoint(doc, data, id, QColor(lineColor), parent)
73  , firstPointId(firstPointId)
74  , secondPointId(secondPointId)
75  , firstLine(nullptr)
76  , secondLine(nullptr)
77  , lineColor(lineColor)
78 {
79  this->m_lineType = typeLine;
80  this->m_lineWeight = lineWeight;
81 
83 
84  Q_ASSERT_X(firstPointId != 0, Q_FUNC_INFO, "firstPointId == 0"); //-V654 //-V712
85  Q_ASSERT_X(secondPointId != 0, Q_FUNC_INFO, "secondPointId == 0"); //-V654 //-V712
86  QPointF point1 = static_cast<QPointF>(*data->GeometricObject<VPointF>(firstPointId));
87  QPointF point2 = static_cast<QPointF>(*data->GeometricObject<VPointF>(secondPointId));
88  QPointF point3 = static_cast<QPointF>(*data->GeometricObject<VPointF>(id));
89 
90  firstLine = new VScaledLine(QLineF(point1 - point3, QPointF()), this);
92  firstLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
93 
94  secondLine = new VScaledLine(QLineF(point2 - point3, QPointF()), this);
96  secondLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
97 }
98 
99 //---------------------------------------------------------------------------------------------------------------------
101 {
102  delete firstLine;
103  delete secondLine;
104 }
105 
106 //---------------------------------------------------------------------------------------------------------------------
107 void DoubleLinePointTool::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
108 {
109  QPen pen = firstLine->pen();
110  pen.setColor(correctColor(this, lineColor));
111  pen.setStyle(lineTypeToPenStyle(m_lineType));
112  pen.setWidthF(ToPixel(m_lineWeight.toDouble(), Unit::Mm));
113 
114  firstLine->setPen(pen);
115  secondLine->setPen(pen);
116 
117  VToolSinglePoint::paint(painter, option, widget);
118 }
119 
120 //---------------------------------------------------------------------------------------------------------------------
121 /**
122  * @brief RefreshGeometry refresh item on scene.
123  */
125 {
127  VToolSinglePoint::refreshPointGeometry(*VDrawTool::data.GeometricObject<VPointF>(m_id));
128 
129  QPointF point1 = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(firstPointId));
130  QPointF point2 = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(secondPointId));
131  QPointF point3 = static_cast<QPointF>(*VDrawTool::data.GeometricObject<VPointF>(m_id));
132 
134  firstLine->setLine(QLineF(point1 - point3, QPointF()));
135 
137  secondLine->setLine(QLineF(point2 - point3, QPointF()));
138 }
139 
140 //---------------------------------------------------------------------------------------------------------------------
141 /**
142  * @brief RemoveReferens decrement value of reference.
143  */
145 {
146  const auto point1 = VAbstractTool::data.GetGObject(firstPointId);
147  const auto point2 = VAbstractTool::data.GetGObject(secondPointId);
148  doc->DecrementReferens(point1->getIdTool());
149  doc->DecrementReferens(point2->getIdTool());
150 }
151 
152 //---------------------------------------------------------------------------------------------------------------------
154 {
156 
160 }
161 
162 //---------------------------------------------------------------------------------------------------------------------
163 void DoubleLinePointTool::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
164 {
168 }
169 
170 //---------------------------------------------------------------------------------------------------------------------
171 void DoubleLinePointTool::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
172 {
176 }
177 
178 //---------------------------------------------------------------------------------------------------------------------
180 {
184 
185  const QLineF line1(static_cast<QPointF>(*point1), static_cast<QPointF>(*point3));
186  const QLineF line2(static_cast<QPointF>(*point2), static_cast<QPointF>(*point3));
187 
188  const QString toolTip = QString("<table>"
189  "<tr>"
190  "<td align ='right'><b>%9: </b></td>" // Tool name
191  "<td align ='left'>Intersect XY</td>"
192  "</tr>"
193  "<tr>"
194  "<td align ='right'><b>%1: </b></td>" // Point Name
195  "<td align ='left'>%5</td>"
196  "</tr>"
197  "<tr>"
198  "<td colspan ='2'><hr></td>" // Divider
199  "</tr>"
200  "<tr>"
201  "<td align ='right'><b>%1: </b></td>" // Line 1 name
202  "<td align ='left'>Line_%3_%5</td>"
203  "</tr>"
204  "<tr>"
205  "<td align ='right'><b>%2: </b></td>" // Line 1 length
206  "<td align ='left'>%6 %8</td>"
207  "</tr>"
208  "<tr>"
209  "<td colspan ='2'><hr></td>" // Divider
210  "</tr>"
211  "<tr>"
212  "<td align ='right'><b>%1: </b></td>" // Line 2 name
213  "<td align ='left'>Line_%4_%5</td>"
214  "</tr>"
215  "<tr>"
216  "<td align ='right'><b>%2: </b></td>" // Line 2 length
217  "<td align ='left'>%7 %8</td>"
218  "</tr>"
219  "</table>")
220  .arg(tr("Name")) //1
221  .arg(tr("Length")) //2
222  .arg(point1->name()) //3
223  .arg(point2->name()) //4
224  .arg(point3->name()) //5
225  .arg(qApp->fromPixel(line1.length())) //6
226  .arg(qApp->fromPixel(line2.length())) //7
227  .arg(UnitsToStr(qApp->patternUnit(), true)) //8
228  .arg(tr("Tool")); //9
229 
230  return toolTip;
231 }
232 
233 //---------------------------------------------------------------------------------------------------------------------
234 void DoubleLinePointTool::Disable(bool disable, const QString &draftBlockName)
235 {
236  VToolSinglePoint::Disable(disable, draftBlockName);
237  firstLine->setEnabled(isEnabled());
238  secondLine->setEnabled(isEnabled());
239 }
240 
241 //---------------------------------------------------------------------------------------------------------------------
242 /**
243  * @brief FullUpdateFromFile update tool data from file.
244  */
246 {
247  ReadAttributes();
248  RefreshGeometry();
250 }
251 
252 //---------------------------------------------------------------------------------------------------------------------
254 {
255  return lineColor;
256 }
257 
258 //---------------------------------------------------------------------------------------------------------------------
259 void DoubleLinePointTool::setLineColor(const QString &value)
260 {
261  lineColor = value;
262 
264  SaveOption(obj);
265 }
266 
267 //---------------------------------------------------------------------------------------------------------------------
269 {
270  return firstPointId;
271 }
272 
273 //---------------------------------------------------------------------------------------------------------------------
274 void DoubleLinePointTool::setFirstPointId(const quint32 &value)
275 {
276  if (value != NULL_ID)
277  {
278  firstPointId = value;
279 
281  SaveOption(obj);
282  }
283 }
284 
285 //---------------------------------------------------------------------------------------------------------------------
287 {
288  return secondPointId;
289 }
290 
291 //---------------------------------------------------------------------------------------------------------------------
292 void DoubleLinePointTool::setSecondPointId(const quint32 &value)
293 {
294  if (value != NULL_ID)
295  {
296  secondPointId = value;
297 
299  SaveOption(obj);
300  }
301 }
302 
303 //---------------------------------------------------------------------------------------------------------------------
305 {
307 }
308 
309 //---------------------------------------------------------------------------------------------------------------------
311 {
313 }
quint32 getSecondPointId() const
void setLineColor(const QString &value)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) Q_DECL_OVERRIDE
virtual void RefreshGeometry()
lineColor color of a line.
virtual QString makeToolTip() const Q_DECL_OVERRIDE
virtual void RemoveReferens() Q_DECL_OVERRIDE
RemoveReferens decrement value of reference.
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
DoubleLinePointTool(VAbstractPattern *doc, VContainer *data, const quint32 &id, const QString &typeLine, const QString &lineWeight, const QString &lineColor, const quint32 &firstPointId, const quint32 &secondPointId, QGraphicsItem *parent=nullptr)
DoubleLinePointTool constructor.
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
VScaledLine * secondLine
firstLine first line item.
void setSecondPointId(const quint32 &value)
quint32 secondPointId
firstPointId id first line point.
void setFirstPointId(const quint32 &value)
QString lineColor
secondLine second line item.
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
VScaledLine * firstLine
secondPointId id second line point.
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
FullUpdateFromFile update tool data from file.
virtual ~DoubleLinePointTool() Q_DECL_OVERRIDE
virtual void DecrementReferens(quint32 id) const =0
VAbstractPattern * doc
doc dom document 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
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)
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