Seamly2D
Code documentation
vtooldoublepoint.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 vtooldoublepoint.cpp
27  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
28  ** @date 20 6, 2015
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) 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 "vtooldoublepoint.h"
53 
54 #include <QColor>
55 #include <QDomElement>
56 #include <QKeyEvent>
57 #include <QPoint>
58 #include <QSharedPointer>
59 #include <QUndoStack>
60 #include <Qt>
61 #include <new>
62 
63 #include "../../../../undocommands/label/movedoublelabel.h"
64 #include "../../../../undocommands/label/showdoublepointname.h"
65 #include "../ifc/exception/vexception.h"
66 #include "../ifc/exception/vexceptionbadid.h"
67 #include "../ifc/xml/vabstractpattern.h"
68 #include "../vgeometry/vgobject.h"
69 #include "../vgeometry/vpointf.h"
70 #include "../vmisc/vabstractapplication.h"
71 #include "../vmisc/logging.h"
72 #include "../vpatterndb/vcontainer.h"
73 #include "../vwidgets/../ifc/ifcdef.h"
74 #include "../vwidgets/vsimplepoint.h"
75 #include "../../../vabstracttool.h"
76 #include "../../../vdatatool.h"
77 #include "../../vdrawtool.h"
78 #include "../vabstractpoint.h"
79 
80 //---------------------------------------------------------------------------------------------------------------------
81 VToolDoublePoint::VToolDoublePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 p1id, quint32 p2id,
82  QGraphicsItem *parent)
83  : VAbstractPoint(doc, data, id)
84  , QGraphicsPathItem(parent)
85  , firstPoint(nullptr)
86  , secondPoint(nullptr)
87  , p1id(p1id)
88  , p2id(p2id)
89 {
90  firstPoint = new VSimplePoint(p1id, QColor(qApp->Settings()->getPointNameColor()));
91  firstPoint->setParentItem(this);
92  firstPoint->setToolTip(complexToolTip(p1id));
98  firstPoint->refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(p1id));
99 
100  secondPoint = new VSimplePoint(p2id, QColor(qApp->Settings()->getPointNameColor()));
101  secondPoint->setParentItem(this);
102  secondPoint->setToolTip(complexToolTip(p2id));
108  secondPoint->refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(p2id));
109 }
110 
111 //---------------------------------------------------------------------------------------------------------------------
113 {
114  return ObjectName<VPointF>(p1id);
115 }
116 
117 //---------------------------------------------------------------------------------------------------------------------
118 void VToolDoublePoint::setNameP1(const QString &name)
119 {
120  SetPointName(p1id, name);
121 }
122 
123 //---------------------------------------------------------------------------------------------------------------------
125 {
126  return ObjectName<VPointF>(p2id);
127 }
128 
129 //---------------------------------------------------------------------------------------------------------------------
130 void VToolDoublePoint::setNameP2(const QString &name)
131 {
132  SetPointName(p2id, name);
133 }
134 
135 //---------------------------------------------------------------------------------------------------------------------
136 void VToolDoublePoint::GroupVisibility(quint32 object, bool visible)
137 {
138  if (object == p1id)
139  {
140  firstPoint->setVisible(visible);
141  }
142  else if (object == p2id)
143  {
144  secondPoint->setVisible(visible);
145  }
146 }
147 
148 //---------------------------------------------------------------------------------------------------------------------
150 {
151  if (p1id == id)
152  {
153  return VAbstractTool::data.GeometricObject<VPointF>(p1id)->isShowPointName();
154  }
155  else if (p2id == id)
156  {
157  return VAbstractTool::data.GeometricObject<VPointF>(p2id)->isShowPointName();
158  }
159  else
160  {
161  return false;
162  }
163 }
164 
165 //---------------------------------------------------------------------------------------------------------------------
166 void VToolDoublePoint::setPointNameVisiblity(quint32 id, bool visible)
167 {
168  if (p1id == id)
169  {
171  point->setShowPointName(visible);
173  }
174  else if (p2id == id)
175  {
177  point->setShowPointName(visible);
179  }
180 }
181 
182 //---------------------------------------------------------------------------------------------------------------------
184 {
185  updatePointNamePosition(p1id, pos - firstPoint->pos());
186 }
187 
188 //---------------------------------------------------------------------------------------------------------------------
190 {
192 }
193 
194 //---------------------------------------------------------------------------------------------------------------------
195 void VToolDoublePoint::Disable(bool disable, const QString &draftBlockName)
196 {
197  const bool enabled = !CorrectDisable(disable, draftBlockName);
198  this->setEnabled(enabled);
199  firstPoint->SetEnabled(enabled);
200  secondPoint->SetEnabled(enabled);
201 }
202 
203 //---------------------------------------------------------------------------------------------------------------------
205 {
206  firstPoint->EnableToolMove(move);
208 }
209 
210 //---------------------------------------------------------------------------------------------------------------------
212 {
214 }
215 
216 //---------------------------------------------------------------------------------------------------------------------
218 {
220 }
221 
222 //---------------------------------------------------------------------------------------------------------------------
224 {
225  emit ChangedToolSelection(selected, p1id, m_id);
226 }
227 
228 //---------------------------------------------------------------------------------------------------------------------
230 {
231  emit ChangedToolSelection(selected, p2id, m_id);
232 }
233 
234 //---------------------------------------------------------------------------------------------------------------------
236 {
237  ReadAttributes();
238  firstPoint->setToolTip(complexToolTip(p1id));
239  firstPoint->refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(p1id));
240 
241  secondPoint->setToolTip(complexToolTip(p2id));
242  secondPoint->refreshPointGeometry(*VAbstractTool::data.GeometricObject<VPointF>(p2id));
244 }
245 
246 //---------------------------------------------------------------------------------------------------------------------
247 void VToolDoublePoint::setPointNamePosition(quint32 id, const QPointF &pos)
248 {
249  if (id == p1id)
250  {
251  VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject<VPointF>(p1id));
252  point->setMx(pos.x());
253  point->setMy(pos.y());
255 
256  if (QGraphicsScene *sc = firstPoint->scene())
257  {
258  VMainGraphicsView::NewSceneRect(sc, qApp->getSceneView(), firstPoint);
259  }
260  }
261  else if (id == p2id)
262  {
263  VPointF *point = new VPointF(*VAbstractTool::data.GeometricObject<VPointF>(p2id));
264  point->setMx(pos.x());
265  point->setMy(pos.y());
267 
268  if (QGraphicsScene *sc = secondPoint->scene())
269  {
270  VMainGraphicsView::NewSceneRect(sc, qApp->getSceneView(), secondPoint);
271  }
272  }
273 }
274 
275 //---------------------------------------------------------------------------------------------------------------------
277 {
278  firstPoint->setAcceptHoverEvents(enabled);
279  secondPoint->setAcceptHoverEvents(enabled);
280 }
281 
282 //---------------------------------------------------------------------------------------------------------------------
284 {
285  firstPoint->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
286  secondPoint->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
287 }
288 
289 //---------------------------------------------------------------------------------------------------------------------
291 {
292  firstPoint->allowTextHover(enabled);
293  secondPoint->allowTextHover(enabled);
294 }
295 
296 //---------------------------------------------------------------------------------------------------------------------
298 {
301 }
302 
303 //---------------------------------------------------------------------------------------------------------------------
305 {
309 }
310 
311 //---------------------------------------------------------------------------------------------------------------------
312 void VToolDoublePoint::updatePointNamePosition(quint32 id, const QPointF &pos)
313 {
314  if (id == p1id)
315  {
316  qApp->getUndoStack()->push(new MoveDoubleLabel(doc, pos, MoveDoublePoint::FirstPoint, m_id, p1id));
317  }
318  else if (id == p2id)
319  {
320  qApp->getUndoStack()->push(new MoveDoubleLabel(doc, pos, MoveDoublePoint::SecondPoint, m_id, p2id));
321  }
322 }
323 
324 //---------------------------------------------------------------------------------------------------------------------
325 /**
326  * @brief itemChange hadle item change.
327  * @param change change.
328  * @param value value.
329  * @return value.
330  */
331 QVariant VToolDoublePoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
332 {
333  if (change == QGraphicsItem::ItemSelectedChange)
334  {
335  if (value == true)
336  {
337  // do stuff if selected
338  this->setFocus();
339  }
340  else
341  {
342  // do stuff if not selected
343  }
344  }
345 
346  return QGraphicsPathItem::itemChange(change, value);
347 }
348 
349 //---------------------------------------------------------------------------------------------------------------------
350 /**
351  * @brief keyReleaseEvent handle key release events.
352  * @param event key release event.
353  */
354 void VToolDoublePoint::keyReleaseEvent(QKeyEvent *event)
355 {
356  switch (event->key())
357  {
358  case Qt::Key_Delete:
359  try
360  {
361  deleteTool();
362  }
363  catch(const VExceptionToolWasDeleted &e)
364  {
365  Q_UNUSED(e)
366  return;//Leave this method immediately!!!
367  }
368  break;
369  default:
370  break;
371  }
372  QGraphicsPathItem::keyReleaseEvent ( event );
373 }
374 
375 //---------------------------------------------------------------------------------------------------------------------
376 void VToolDoublePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
377 {
378  showContextMenu(event);
379 }
380 
381 //---------------------------------------------------------------------------------------------------------------------
383 {
384  VDrawTool::SaveOptions(tag, obj);
385 
386  if (obj->id() == p1id)
387  {
388  QSharedPointer<VPointF> point = qSharedPointerDynamicCast<VPointF>(obj);
389  SCASSERT(point.isNull() == false)
390 
391  doc->SetAttribute(tag, AttrName1, point->name());
392  doc->SetAttribute(tag, AttrMx1, qApp->fromPixel(point->mx()));
393  doc->SetAttribute(tag, AttrMy1, qApp->fromPixel(point->my()));
394  doc->SetAttribute<bool>(tag, AttrShowPointName1, point->isShowPointName());
395  }
396  else if (obj->id() == p2id)
397  {
398  QSharedPointer<VPointF> point = qSharedPointerDynamicCast<VPointF>(obj);
399  SCASSERT(point.isNull() == false)
400 
401  doc->SetAttribute(tag, AttrName2, point->name());
402  doc->SetAttribute(tag, AttrMx2, qApp->fromPixel(point->mx()));
403  doc->SetAttribute(tag, AttrMy2, qApp->fromPixel(point->my()));
404  doc->SetAttribute<bool>(tag, AttrShowPointName2, point->isShowPointName());
405  }
406  else
407  {
410 
412  doc->SetAttribute(tag, AttrName1, p1->name());
413  doc->SetAttribute(tag, AttrMx1, qApp->fromPixel(p1->mx()));
414  doc->SetAttribute(tag, AttrMy1, qApp->fromPixel(p1->my()));
416 
418  doc->SetAttribute(tag, AttrName2, p2->name());
419  doc->SetAttribute(tag, AttrMx2, qApp->fromPixel(p2->mx()));
420  doc->SetAttribute(tag, AttrMy2, qApp->fromPixel(p2->my()));
422  }
423 }
424 
425 //---------------------------------------------------------------------------------------------------------------------
427 {
428  QDomElement domElement = doc->createElement(getTagName());
430  SaveOptions(domElement, obj);
431  AddToCalculation(domElement);
432 }
433 
434 //---------------------------------------------------------------------------------------------------------------------
435 void VToolDoublePoint::updatePointNameVisibility(quint32 id, bool visible)
436 {
437  if (id == p1id)
438  {
439  qApp->getUndoStack()->push(new ShowDoublePointName(doc, m_id, p1id, visible, ShowDoublePoint::FirstPoint));
440  }
441  else if (id == p2id)
442  {
443  qApp->getUndoStack()->push(new ShowDoublePointName(doc, m_id, p2id, visible, ShowDoublePoint::SecondPoint));
444  }
445 }
446 
447 //---------------------------------------------------------------------------------------------------------------------
448 QString VToolDoublePoint::complexToolTip(quint32 itemId) const
449 {
451 
452  const QString toolTipStr = QString("<table>"
453  "<tr> <td><b>%1:</b> %2</td> </tr>"
454  "%3"
455  "</table>")
456  .arg(tr("Name"), point->name(), makeToolTip());
457  return toolTipStr;
458 }
virtual QString getTagName() const Q_DECL_OVERRIDE
void SetPointName(quint32 id, const QString &name)
void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=null_id)
VAbstractPattern * doc
doc dom document container
virtual void deleteTool(bool ask=true)
deleteTool full delete object form scene and file.
void chosenTool(quint32 id, SceneObject type)
chosenTool emit if object was clicked.
const quint32 m_id
id object id.
virtual void SetVisualization()=0
virtual void ToolSelectionType(const SelectionType &type)
The VContainer class container of all variables.
Definition: vcontainer.h:141
const QSharedPointer< T > GeometricObject(const quint32 &id) const
Definition: vcontainer.h:266
static const QSharedPointer< VGObject > GetFakeGObject(quint32 id)
Definition: vcontainer.cpp:156
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
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID)=0
virtual QString makeToolTip() const
Definition: vdrawtool.cpp:171
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:163
void AddToCalculation(const QDomElement &domElement)
typeLine line weight.
Definition: vdrawtool.cpp:229
bool CorrectDisable(bool disable, const QString &draftBlockName) const
Definition: vdrawtool.cpp:177
void ChangedToolSelection(bool selected, quint32 object, quint32 tool)
void ReadAttributes()
Definition: vdrawtool.cpp:190
virtual QString name() const
name return name graphical object.
Definition: vgobject.cpp:148
static void NewSceneRect(QGraphicsScene *sc, QGraphicsView *view, QGraphicsItem *item=nullptr)
NewSceneRect calculate scene rect what contains all items and doesn't less that size of scene view.
The VPointF class keep data of point.
Definition: vpointf.h:75
void setMx(qreal mx)
setMx set offset name respect to x
Definition: vpointf.cpp:197
void setMy(qreal my)
setMy set offset name respect to y
Definition: vpointf.cpp:207
qreal mx() const
mx return offset name respect to x
Definition: vpointf.cpp:177
bool isShowPointName() const
Definition: vpointf.cpp:259
qreal my() const
my return offset name respect to y
Definition: vpointf.cpp:187
virtual void refreshPointGeometry(const VPointF &point)
void nameChangedPosition(const QPointF &pos, quint32 id)
void SetEnabled(bool enabled)
void EnableToolMove(bool move)
void Choosed(quint32 id)
Choosed send id when clicked.
void allowTextHover(bool enabled)
void allowTextSelectable(bool enabled)
void Selected(bool selected, quint32 id)
virtual void ToolSelectionType(const SelectionType &type) Q_DECL_OVERRIDE
virtual void updatePointNamePosition(quint32 id, const QPointF &pos) Q_DECL_OVERRIDE
VSimplePoint * secondPoint
VToolDoublePoint(VAbstractPattern *doc, VContainer *data, quint32 id, quint32 p1id, quint32 p2id, QGraphicsItem *parent=nullptr)
QString nameP1() const
void setNameP2(const QString &name)
virtual void Disable(bool disable, const QString &draftBlockName) Q_DECL_OVERRIDE
void setNameP1(const QString &name)
virtual bool isPointNameVisible(quint32 id) const Q_DECL_OVERRIDE
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj) Q_DECL_OVERRIDE
virtual void ToolSelectionType(const SelectionType &type) Q_DECL_OVERRIDE
virtual void AddToFile() Q_DECL_OVERRIDE
AddToFile add tag with Information about tool into file.
virtual void FullUpdateFromFile() Q_DECL_OVERRIDE
VSimplePoint * firstPoint
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) Q_DECL_OVERRIDE
virtual int type() const Q_DECL_OVERRIDE
QString nameP2() const
void changePointName1Position(const QPointF &pos)
void allowTextSelectable(bool enabled)
void point1Selected(bool selected)
virtual void GroupVisibility(quint32 object, bool visible) Q_DECL_OVERRIDE
virtual void setPointNameVisiblity(quint32 id, bool visible) Q_DECL_OVERRIDE
virtual void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE
keyReleaseEvent handle key release events.
virtual void EnableToolMove(bool move) Q_DECL_OVERRIDE
void point2Selected(bool selected)
virtual void updatePointNameVisibility(quint32 id, bool visible) Q_DECL_OVERRIDE
void changePointName2Position(const QPointF &pos)
void allowTextHover(bool enabled)
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) Q_DECL_OVERRIDE
itemChange hadle item change.
QString complexToolTip(quint32 itemId) const
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE
virtual void setPointNamePosition(quint32 id, const QPointF &pos) Q_DECL_OVERRIDE
#define SCASSERT(cond)
Definition: def.h:317
SelectionType
Definition: def.h:108
const QString AttrPoint2
Definition: ifcdef.cpp:116
const QString AttrName1
Definition: ifcdef.cpp:79
const QString AttrShowPointName2
Definition: ifcdef.cpp:155
const QString AttrShowPointName1
Definition: ifcdef.cpp:154
const QString AttrMx2
Definition: ifcdef.cpp:80
const QString AttrPoint1
Definition: ifcdef.cpp:115
const QString AttrName2
Definition: ifcdef.cpp:82
const QString AttrMx1
Definition: ifcdef.cpp:77
const QString AttrMy1
Definition: ifcdef.cpp:78
const QString AttrMy2
Definition: ifcdef.cpp:81
#define qApp
Definition: vapplication.h:67