Seamly2D
Code documentation
vdrawtool.h
Go to the documentation of this file.
1 /**************************************************************************
2  **
3  ** @file vdrawtool.h
4  ** @author Roman Telezhynskyi <dismine(at)gmail.com>
5  ** @date November 15, 2013
6  **
7  ** @author Douglas S Caskey
8  ** @date 7.23.2022
9  **
10  ** @brief
11  ** @copyright
12  ** This source code is part of the Valentine project, a pattern making
13  ** program, whose allow create and modeling patterns of clothing.
14  ** Copyright (C) 2013-2022 Seamly2D project
15  ** <https://github.com/fashionfreedom/seamly2d> All Rights Reserved.
16  **
17  ** Seamly2D is free software: you can redistribute it and/or modify
18  ** it under the terms of the GNU General Public License as published by
19  ** the Free Software Foundation, either version 3 of the License, or
20  ** (at your option) any later version.
21  **
22  ** Seamly2D is distributed in the hope that it will be useful,
23  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  ** GNU General Public License for more details.
26  **
27  ** You should have received a copy of the GNU General Public License
28  ** along with Seamly2D. If not, see <http://www.gnu.org/licenses/>.
29  **
30  *************************************************************************/
31 #ifndef VDRAWTOOL_H
32 #define VDRAWTOOL_H
33 
34 
35 #include "../vinteractivetool.h"
36 #include "../ifc/exception/vexceptionbadid.h"
37 #include "../vdatatool.h"
38 #include "../vgeometry/vpointf.h"
39 #include "../vmisc/vabstractapplication.h"
40 #include "../vmisc/def.h"
41 #include "../vmisc/vabstractapplication.h"
42 #include "../vwidgets/vabstractmainwindow.h"
43 #include "../vwidgets/vmaingraphicsscene.h"
44 #include "../vwidgets/vmaingraphicsview.h"
45 #include "../vtools/undocommands/addgroup.h"
46 #include "../vtools/undocommands/add_groupitem.h"
47 #include "../vtools/undocommands/remove_groupitem.h"
48 
49 #include <qcompilerdetection.h>
50 #include <QAction>
51 #include <QByteArray>
52 #include <QColor>
53 #include <QDomElement>
54 #include <QGraphicsSceneContextMenuEvent>
55 #include <QIcon>
56 #include <QMenu>
57 #include <QMetaObject>
58 #include <QObject>
59 #include <QString>
60 #include <QtGlobal>
61 
62 template <class T> class QSharedPointer;
63 
64 /**
65  * @brief The VDrawTool abstract class for all draw tool.
66  */
68 {
69  Q_OBJECT
70 
71 public:
72  VDrawTool(VAbstractPattern *doc, VContainer *data, quint32 id, QObject *parent = nullptr);
73  virtual ~VDrawTool() Q_DECL_EQ_DEFAULT;
74 
75  QString getLineType() const;
76  virtual void setLineType(const QString &value);
77 
78  QString getLineWeight() const;
79  virtual void setLineWeight(const QString &value);
80 
81  virtual bool isPointNameVisible(quint32 id) const;
82 
83 signals:
84  void ChangedToolSelection(bool selected, quint32 object, quint32 tool);
85 
86 public slots:
87  virtual void ShowTool(quint32 id, bool enable);
88  virtual void activeBlockChanged(const QString &newName);
89  void blockNameChanged(const QString &oldName, const QString &newName);
90  virtual void EnableToolMove(bool move);
91  virtual void Disable(bool disable, const QString &draftBlockName)=0;
92  virtual void piecesMode(bool mode);
93 
94 protected slots:
95  virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID)=0;
96 
97 protected:
98  enum class RemoveOption : bool {Disable = false, Enable = true};
99  enum class Referens : bool {Follow = true, Ignore = false};
100 
101 
102  QString activeBlockName; /** @brief activeBlockName name of tool's pattern peace. */
103  QString m_lineType; /** @brief typeLine line type. */
104  QString m_lineWeight; /** @brief typeLine line weight. */
105 
106  void AddToCalculation(const QDomElement &domElement);
107  void addDependence(QList<quint32> &list, quint32 objectId) const;
108 
109  /** @brief SaveDialog save options into file after change in dialog. */
110  virtual void SaveDialog(QDomElement &domElement)=0;
111  virtual void SaveDialogChange() Q_DECL_FINAL;
112  virtual void AddToFile() Q_DECL_OVERRIDE;
113  void SaveOption(QSharedPointer<VGObject> &obj);
114  virtual void SaveOptions(QDomElement &tag, QSharedPointer<VGObject> &obj);
115  virtual QString makeToolTip() const;
116 
117  bool CorrectDisable(bool disable, const QString &draftBlockName) const;
118 
119  void ReadAttributes();
120  virtual void ReadToolAttributes(const QDomElement &domElement)=0;
121  virtual void updatePointNameVisibility(quint32 id, bool visible);
122 
123  template <typename Dialog>
124  void ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemId = NULL_ID,
125  const RemoveOption &showRemove = RemoveOption::Enable,
126  const Referens &ref = Referens::Follow);
127 
128  template <typename Item>
129  void ShowItem(Item *item, quint32 id, bool enable);
130 
131  template <typename T>
132  QString ObjectName(quint32 id) const;
133 
134  template <typename T>
135  static void InitDrawToolConnections(VMainGraphicsScene *scene, T *tool);
136 
137 private:
138  Q_DISABLE_COPY(VDrawTool)
139 };
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 template <typename Dialog>
143 /**
144  * @brief ContextMenu show context menu for tool.
145  * @param itemId id of point.
146  * @param event context menu event.
147  * @param showRemove true - tool enable option delete.
148  * @param ref true - do not ignore referens value.
149  */
150 void VDrawTool::ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemId,
151  const RemoveOption &showRemove, const Referens &ref)
152 {
153  SCASSERT(event != nullptr)
154 
156  {
157  return;
158  }
159 
160  GOType itemType = GOType::Unknown;
161  if(itemId != NULL_ID)
162  {
163  try
164  {
165  itemType = data.GetGObject(itemId)->getType();
166  }
167  catch (const VExceptionBadId &e)
168  { // Possible case. Parent was deleted, but the node object is still here.
169  qWarning() << qUtf8Printable(e.ErrorMessage());
170  }
171  }
172 
173  qCDebug(vTool, "Creating tool context menu.");
174  QMenu menu;
175  QAction *actionOption = menu.addAction(QIcon::fromTheme("preferences-other"), tr("Properties"));
176 
177  // Show object name menu item
178  QAction *actionShowPointName = menu.addAction(QIcon("://icon/16x16/open_eye.png"), tr("Show Point Name"));
179  actionShowPointName->setCheckable(true);
180 
181  if (itemType == GOType::Point)
182  {
183  actionShowPointName->setChecked(isPointNameVisible(itemId));
184  }
185  else
186  {
187  actionShowPointName->setVisible(false);
188  }
189 
190  QAction *actionDelete = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
191  if (showRemove == RemoveOption::Enable)
192  {
193  if (ref == Referens::Follow)
194  {
195  if (_referens > 1)
196  {
197  qCDebug(vTool, "Delete disabled. Tool has children.");
198  actionDelete->setEnabled(false);
199  }
200  else
201  {
202  qCDebug(vTool, "Delete enabled. Tool has no children.");
203  actionDelete->setEnabled(true);
204  }
205  }
206  else
207  {
208  qCDebug(vTool, "Delete enabled. Ignore referens value.");
209  actionDelete->setEnabled(true);
210  }
211  }
212  else
213  {
214  qCDebug(vTool, "Delete disabled.");
215  actionDelete->setEnabled(false);
216  }
217 
218  // Add Group Item menu item
219  QMap<quint32,QString> groupsNotContainingItem = doc->getGroupsContainingItem(this->getId(), itemId, false);
220  QActionGroup* actionAddGroupMenu= new QActionGroup(this);
221 
222  if(not groupsNotContainingItem.empty())
223  {
224  QMenu *menuAddGroupItem = menu.addMenu(QIcon("://icon/32x32/add.png"), tr("Add Group Object"));
225  QStringList list = QStringList(groupsNotContainingItem.values());
226  list.sort(Qt::CaseInsensitive);
227 
228  for(int i=0; i<list.count(); ++i)
229  {
230  QAction *actionAddGroupItem = menuAddGroupItem->addAction(list[i]);
231  actionAddGroupMenu->addAction(actionAddGroupItem);
232  const quint32 groupId = groupsNotContainingItem.key(list[i]);
233  actionAddGroupItem->setData(groupId);
234  groupsNotContainingItem.remove(groupId); // delete any duplicate groups
235  }
236  }
237 
238  // Remove Group Item menu item
239  QMap<quint32,QString> groupsContainingItem = doc->getGroupsContainingItem(this->getId(), itemId, true);
240  QActionGroup* actionDeleteGroupMenu = new QActionGroup(this);
241 
242  if(not groupsContainingItem.empty())
243  {
244  QMenu *menuRemoveGroupItem = menu.addMenu(QIcon("://icon/32x32/remove.png"), tr("Remove Group Object"));
245 
246  QStringList list = QStringList(groupsContainingItem.values());
247  list.sort(Qt::CaseInsensitive);
248 
249  for(int i=0; i<list.count(); ++i)
250  {
251  QAction *actionRemoveGroupItem = menuRemoveGroupItem->addAction(list[i]);
252  actionDeleteGroupMenu->addAction(actionRemoveGroupItem);
253  const quint32 groupId = groupsContainingItem.key(list[i]);
254  actionRemoveGroupItem->setData(groupId);
255  groupsContainingItem.remove(groupId);
256  }
257  }
258 
259  QAction *selectedAction = menu.exec(event->screenPos());
260  if(selectedAction == nullptr)
261  {
262  return;
263  }
264  if (selectedAction == actionOption)
265  {
266  qCDebug(vTool, "Show options.");
267  qApp->getSceneView()->itemClicked(nullptr);
268  m_dialog = QSharedPointer<Dialog>(new Dialog(getData(), m_id, qApp->getMainWindow()));
269  m_dialog->setModal(true);
270 
273 
274  this->setDialog();
275 
276  m_dialog->show();
277  }
278  else if (selectedAction == actionDelete)
279  {
280  qCDebug(vTool, "Deleting Object.");
281  deleteTool(); // do not catch exception here
282  return; //Leave this method immediately after call!!!
283  }
284  else if (selectedAction == actionShowPointName)
285  {
286  updatePointNameVisibility(itemId, selectedAction->isChecked());
287  }
288  else if (selectedAction->actionGroup() == actionAddGroupMenu)
289  {
290  quint32 groupId = selectedAction->data().toUInt();
291  QDomElement item = doc->addGroupItem(this->getId(), itemId, groupId);
292 
293  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
294  SCASSERT(scene != nullptr)
295  scene->clearSelection();
296 
297  VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
298  SCASSERT(window != nullptr)
299  {
300  AddGroupItem *command = new AddGroupItem(item, doc, groupId);
302  qApp->getUndoStack()->push(command);
303  }
304  }
305  else if (selectedAction->actionGroup() == actionDeleteGroupMenu)
306  {
307  quint32 groupId = selectedAction->data().toUInt();
308  QDomElement item = doc->removeGroupItem(this->getId(), itemId, groupId);
309 
310  VMainGraphicsScene *scene = qobject_cast<VMainGraphicsScene *>(qApp->getCurrentScene());
311  SCASSERT(scene != nullptr)
312  scene->clearSelection();
313 
314  VAbstractMainWindow *window = qobject_cast<VAbstractMainWindow *>(qApp->getMainWindow());
315  SCASSERT(window != nullptr)
316  {
317  RemoveGroupItem *command = new RemoveGroupItem(item, doc, groupId);
319  qApp->getUndoStack()->push(command);
320  }
321  }
322 }
323 
324 //---------------------------------------------------------------------------------------------------------------------
325 template <typename Item>
326 /**
327  * @brief ShowItem highlight tool.
328  * @param item tool.
329  * @param id object id in container.
330  * @param enable enable or disable highlight.
331  */
332 void VDrawTool::ShowItem(Item *item, quint32 id, bool enable)
333 {
334  SCASSERT(item != nullptr)
335  if (id == item->m_id)
336  {
337  ShowVisualization(enable);
338  }
339 }
340 
341 //---------------------------------------------------------------------------------------------------------------------
342 template <typename T>
343 /**
344  * @brief ObjectName get object (point, curve, arc) name.
345  * @param id object id in container.
346  */
347 QString VDrawTool::ObjectName(quint32 id) const
348 {
349  try
350  {
351  return data.GeometricObject<T>(id)->name();
352  }
353  catch (const VExceptionBadId &e)
354  {
355  qCDebug(vTool, "Error! Couldn't get object name by id = %s. %s %s", qUtf8Printable(QString().setNum(id)),
356  qUtf8Printable(e.ErrorMessage()),
357  qUtf8Printable(e.DetailedInformation()));
358  return QString("");// Return empty string for property browser
359  }
360 }
361 
362 //---------------------------------------------------------------------------------------------------------------------
363 template <typename T>
365 {
366  SCASSERT(scene != nullptr)
367  SCASSERT(tool != nullptr)
368 
369  QObject::connect(tool, &T::chosenTool, scene, &VMainGraphicsScene::chosenItem);
370  QObject::connect(tool, &T::ChangedToolSelection, scene, &VMainGraphicsScene::SelectedItem);
371  QObject::connect(scene, &VMainGraphicsScene::DisableItem, tool, &T::Disable);
372  QObject::connect(scene, &VMainGraphicsScene::EnableToolMove, tool, &T::EnableToolMove);
373  QObject::connect(scene, &VMainGraphicsScene::curvePiecesMode, tool, &T::piecesMode);
374  QObject::connect(scene, &VMainGraphicsScene::ItemSelection, tool, &T::ToolSelectionType);
375 }
376 
377 #endif // VDRAWTOOL_H
void updateGroups()
void DialogClosed(int result)
DialogClosed signal dialog closed.
void DialogApplied()
DialogApplied emit signal dialog apply changes.
virtual void updateGroups()=0
QDomElement removeGroupItem(quint32 toolId, quint32 objectId, quint32 groupId)
Removes the item of given toolId and objectId from the group of given groupId.
QDomElement addGroupItem(quint32 toolId, quint32 objectId, quint32 groupId)
Adds an item to the given group with the given toolId and objectId.
QMap< quint32, QString > getGroupsContainingItem(quint32 toolId, quint32 objectId, bool containsItem)
Returns the groups that contain or do not contain the item identified by the toolid and the objectid.
VAbstractPattern * doc
doc dom document container
virtual void deleteTool(bool ask=true)
deleteTool full delete object form scene and file.
static bool m_suppressContextMenu
Definition: vabstracttool.h:87
const VContainer * getData() const
getData return pointer to data container.
virtual void ShowVisualization(bool show)=0
quint32 getId() const
getId return object id.
const quint32 m_id
id object id.
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
quint32 _referens
_referens keep count tools what use this tool. If value more than 1 you can't delete tool.
Definition: vdatatool.h:87
VContainer data
data container with data
Definition: vdatatool.h:84
The VDrawTool abstract class for all draw tool.
Definition: vdrawtool.h:68
virtual void activeBlockChanged(const QString &newName)
activeBlockChanged disable or enable context menu after change active draft block.
Definition: vdrawtool.cpp:90
virtual bool isPointNameVisible(quint32 id) const
Definition: vdrawtool.cpp:277
virtual void showContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 id=NULL_ID)=0
void ShowItem(Item *item, quint32 id, bool enable)
ShowItem highlight tool.
Definition: vdrawtool.h:332
void ContextMenu(QGraphicsSceneContextMenuEvent *event, quint32 itemId=NULL_ID, const RemoveOption &showRemove=RemoveOption::Enable, const Referens &ref=Referens::Follow)
ContextMenu show context menu for tool.
Definition: vdrawtool.h:150
QString m_lineWeight
typeLine line type.
Definition: vdrawtool.h:104
virtual QString makeToolTip() const
Definition: vdrawtool.cpp:171
VDrawTool(VAbstractPattern *doc, VContainer *data, quint32 id, QObject *parent=nullptr)
VDrawTool constructor.
Definition: vdrawtool.cpp:62
virtual void setLineType(const QString &value)
Definition: vdrawtool.cpp:253
QString activeBlockName
Definition: vdrawtool.h:102
virtual void SaveDialog(QDomElement &domElement)=0
SaveDialog save options into file after change in dialog.
static void InitDrawToolConnections(VMainGraphicsScene *scene, T *tool)
Definition: vdrawtool.h:364
virtual void SaveDialogChange() Q_DECL_FINAL
Definition: vdrawtool.cpp:110
virtual void SaveOptions(QDomElement &tag, QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:163
virtual void piecesMode(bool mode)
Definition: vdrawtool.cpp:218
void addDependence(QList< quint32 > &list, quint32 objectId) const
Definition: vdrawtool.cpp:237
QString ObjectName(quint32 id) const
ObjectName get object (point, curve, arc) name.
Definition: vdrawtool.h:347
QString m_lineType
activeBlockName name of tool's pattern peace.
Definition: vdrawtool.h:103
virtual void ReadToolAttributes(const QDomElement &domElement)=0
virtual void setLineWeight(const QString &value)
Definition: vdrawtool.cpp:268
virtual void Disable(bool disable, const QString &draftBlockName)=0
void SaveOption(QSharedPointer< VGObject > &obj)
Definition: vdrawtool.cpp:142
virtual void updatePointNameVisibility(quint32 id, bool visible)
Definition: vdrawtool.cpp:204
void AddToCalculation(const QDomElement &domElement)
typeLine line weight.
Definition: vdrawtool.cpp:229
virtual ~VDrawTool() Q_DECL_EQ_DEFAULT
virtual void ShowTool(quint32 id, bool enable)
ShowTool highlight tool.
Definition: vdrawtool.cpp:79
QString getLineWeight() const
Definition: vdrawtool.cpp:262
virtual void EnableToolMove(bool move)
Definition: vdrawtool.cpp:211
virtual void AddToFile() Q_DECL_OVERRIDE
AddToFile add tag with Information about tool into file.
Definition: vdrawtool.cpp:133
void blockNameChanged(const QString &oldName, const QString &newName)
blockNameChanged save new name active draft block.
Definition: vdrawtool.cpp:101
bool CorrectDisable(bool disable, const QString &draftBlockName) const
Definition: vdrawtool.cpp:177
void ChangedToolSelection(bool selected, quint32 object, quint32 tool)
QString getLineType() const
Definition: vdrawtool.cpp:247
void ReadAttributes()
Definition: vdrawtool.cpp:190
The VExceptionBadId class for exception bad id.
virtual QString ErrorMessage() const Q_DECL_OVERRIDE
ErrorMessage return main error message.
virtual QString DetailedInformation() const
DetailedInformation return detailed information about error.
Definition: vexception.cpp:134
The VGObject class keep information graphical objects.
Definition: vgobject.h:74
void FullUpdateFromGuiApply()
FullUpdateFromGuiApply refresh tool data after change in options but do not delete dialog.
virtual void setDialog()
setDialog set dialog when user want change tool option.
QSharedPointer< DialogTool > m_dialog
m_dialog tool's dialog options.
void FullUpdateFromGuiOk(int result)
FullUpdateFromGuiOk refresh tool data after change in options.
The VMainGraphicsScene class main scene.
void chosenItem(quint32 id, const SceneObject &type)
chosenItem emit ChosenObject signal.
void curvePiecesMode(bool mode)
void ItemSelection(const SelectionType &type)
void EnableToolMove(bool move)
void DisableItem(bool disable, const QString &draftBlockName)
void SelectedItem(bool selected, quint32 object, quint32 tool)
#define SCASSERT(cond)
Definition: def.h:317
#define NULL_ID
Definition: ifcdef.h:76
#define qApp
Definition: vapplication.h:67
GOType
Definition: vgeometrydef.h:56