Salome HOME
dfc184a42646b09abc8555f9331ea3d1b767ec12
[modules/shaper.git] / src / PartSet / PartSet_MenuMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_MenuMgr.cpp
4 // Created:     03 April 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_MenuMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_SketcherMgr.h"
10 #include "PartSet_Tools.h"
11
12 #include <PartSetPlugin_Part.h>
13
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <SketchPlugin_ConstraintCoincidence.h>
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Circle.h>
20 #include <SketchPlugin_Point.h>
21 #include <SketchPlugin_Sketch.h>
22
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_Operation.h>
25
26 #include <XGUI_ModuleConnector.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_Displayer.h>
29
30 #include <Events_Loop.h>
31 #include <ModelAPI_Events.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_ResultPart.h>
34 #include <ModelAPI_ResultParameter.h>
35
36 #include <QAction>
37 #include <QMenu>
38
39 #include <TopoDS.hxx>
40 #include <BRep_Tool.hxx>
41
42 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
43   : QObject(theModule), myModule(theModule), myPrevId(-1)
44 {
45   createActions();
46 }
47
48
49 QAction* PartSet_MenuMgr::action(const QString& theId) const
50 {
51   if (myActions.contains(theId))
52     return myActions[theId];
53   return 0;
54 }
55
56 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
57 {
58   if (myActions.contains(theId))
59     qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
60   theAction->setData(theId);
61   connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
62   myActions[theId] = theAction;
63 }
64
65 void PartSet_MenuMgr::createActions()
66 {
67   QAction* aAction;
68
69   aAction = new QAction(tr("Auxiliary"), this);
70   aAction->setCheckable(true);
71   addAction("AUXILIARY_CMD", aAction);
72
73   aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
74   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
75   myActions["ACTIVATE_PART_CMD"] = aAction;
76
77   aAction = new QAction(QIcon(":icons/deactivate.png"), tr("Deactivate"), this);
78   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
79   myActions["DEACTIVATE_PART_CMD"] = aAction;
80
81   // Activate PartSet
82   aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
83   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
84   myActions["ACTIVATE_PARTSET_CMD"] = aAction;
85
86   aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
87   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
88   myActions["EDIT_CMD"] = aAction;
89 }
90
91
92 void PartSet_MenuMgr::onAction(bool isChecked)
93 {
94   QAction* aAction = static_cast<QAction*>(sender());
95   QString anId = aAction->data().toString();
96
97   if (anId == "AUXILIARY_CMD") {
98     setAuxiliary(isChecked);
99   }
100 }
101
102 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
103 {
104   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
105   if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
106       !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
107     return false;
108
109   myCoinsideLines.clear();
110   ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
111
112   bool aIsDetach = false;
113   bool hasAttribute = false;
114   bool hasFeature = false;
115
116   QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::AllControls);
117   TopoDS_Shape aShape;
118   ResultPtr aResult;
119   FeaturePtr aFeature;
120   foreach(ModuleBase_ViewerPrs aPrs, aPrsList) {
121     aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
122     if (aResult.get() != NULL) {
123       aShape = aPrs.shape();
124       if (aShape.IsEqual(aResult->shape()->impl<TopoDS_Shape>()))
125         hasFeature = true;
126       else
127         hasAttribute = true;
128     } else {
129       aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
130       hasFeature = (aFeature.get() != NULL);
131     }
132   }
133
134   if (aPrsList.size() == 1) {
135     TopoDS_Shape aShape = aPrsList.first().shape();
136     if ((!aShape.IsNull()) && aShape.ShapeType() == TopAbs_VERTEX) {
137       // Find 2d coordinates
138       FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
139       if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
140         gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
141         std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
142         std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
143
144         // Find coincident in these coordinates
145         ObjectPtr aObj = aPrsList.first().object();
146         FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
147         const std::set<AttributePtr>& aRefsList = aFeature->data()->refsToMe();
148         std::set<AttributePtr>::const_iterator aIt;
149         FeaturePtr aCoincident;
150         for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
151           std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
152           FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
153           if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
154             std::shared_ptr<GeomAPI_Pnt2d> a2dPnt = 
155               PartSet_Tools::getPoint(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A());
156             if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) { 
157               aCoincident = aConstrFeature;
158               break;
159             } else {
160               a2dPnt = PartSet_Tools::getPoint(aConstrFeature,
161                                                SketchPlugin_ConstraintCoincidence::ENTITY_B());
162               if (a2dPnt.get() && aSelPnt->isEqual(a2dPnt)) { 
163                 aCoincident = aConstrFeature;
164                 break;
165               }
166             }
167           }
168         }
169         // If we have coincidence then add Detach menu
170         if (aCoincident.get() != NULL) {
171           mySelectedFeature = aCoincident;
172           PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
173                                           SketchPlugin_ConstraintCoincidence::ENTITY_A());
174           PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
175                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
176           if (myCoinsideLines.size() > 0) {
177             aIsDetach = true;
178             QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
179             QAction* aAction;
180             int i = 0;
181             foreach (FeaturePtr aCoins, myCoinsideLines) {
182               aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
183               aAction->setData(QVariant(i));
184               i++;
185             }
186             connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
187             connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
188             connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
189           } 
190         }
191       }
192     }
193   }
194   if ((!aIsDetach) && hasFeature) {
195     theMenu->addAction(theStdActions["DELETE_CMD"]);
196   }
197   if (hasAttribute)
198     return true;
199   bool isAuxiliary;
200   if (canSetAuxiliary(isAuxiliary)) {
201     QAction* anAction = action("AUXILIARY_CMD");
202     theMenu->addAction(anAction);
203     anAction->setChecked(isAuxiliary);
204   }
205   return true;
206 }
207
208 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
209 {
210   if (myPrevId != -1) {
211     // Restore color for previous object
212     setLineColor(myPrevId, myColor, false);
213   }
214   myPrevId = theAction->data().toInt();
215   myColor = setLineColor(myPrevId, Qt::white, true);
216 }
217
218 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
219 {
220   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
221   XGUI_Workshop* aWorkshop = aConnector->workshop();
222   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
223
224   FeaturePtr aLine = myCoinsideLines.at(myPrevId);
225   std::list<ResultPtr>::const_iterator aIt;
226   const std::list<ResultPtr>& aResults = aLine->results();
227   QColor aColor;
228   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
229     aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
230   }
231   if (theUpdate)
232     aDisplayer->updateViewer();
233   return aColor;
234 }
235
236
237 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
238 {
239   int aId = theAction->data().toInt();
240   FeaturePtr aLine = myCoinsideLines.at(aId);
241   std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
242                                                         SketchPlugin_ConstraintCoincidence::ENTITY_A());
243   if (aOrig.get() == NULL)
244     aOrig = PartSet_Tools::getPoint(mySelectedFeature,
245                                     SketchPlugin_ConstraintCoincidence::ENTITY_B());
246   
247   gp_Pnt aOr = aOrig->impl<gp_Pnt>();
248   const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
249
250   QObjectPtrList aToDelFeatures;
251   std::set<AttributePtr>::const_iterator aIt;
252   // Find all coincedences corresponded to the selected line in the selected point
253   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
254     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
255     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
256     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
257       std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
258                                             SketchPlugin_ConstraintCoincidence::ENTITY_A());
259       if (aPnt.get() == NULL)
260         aPnt = PartSet_Tools::getPoint(aConstrFeature,
261                                        SketchPlugin_ConstraintCoincidence::ENTITY_B());
262       if (aPnt.get() == NULL)
263         return;
264       gp_Pnt aP = aPnt->impl<gp_Pnt>();
265       if (aOrig->isEqual(aPnt)) {
266         aToDelFeatures.append(aConstrFeature);
267       } else {
268         aPnt = PartSet_Tools::getPoint(aConstrFeature,
269                                        SketchPlugin_ConstraintCoincidence::ENTITY_B());
270         aP = aPnt->impl<gp_Pnt>();
271         if (aOrig->isEqual(aPnt)) {
272           aToDelFeatures.append(aConstrFeature);
273           break;
274         }
275       }
276     }
277   }
278   if (aToDelFeatures.size() > 0) {
279     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
280     XGUI_Workshop* aWorkshop = aConnector->workshop();
281     ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
282     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
283       anOperation->abort();
284
285     SessionPtr aMgr = ModelAPI_Session::get();
286
287     QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
288     aMgr->startOperation(aName.toStdString());
289     aWorkshop->deleteFeatures(aToDelFeatures);
290     aMgr->finishOperation();
291   }
292   myCoinsideLines.clear();
293 }
294
295
296 void PartSet_MenuMgr::onDetachMenuHide()
297 {
298   if (myPrevId != -1) {
299     // Restore color for previous object
300     setLineColor(myPrevId, myColor, false);
301   }
302   // Clear previous definitions
303   myPrevId = -1;
304 }
305
306   
307 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
308 {
309   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
310
311   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
312                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
313   if (!isActiveSketch)
314     return;
315
316   QObjectPtrList anObjects;
317   bool isUseTransaction = false;
318   // 1. change auxiliary type of a created feature
319   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
320     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
321     anObjects.append(anOperation->feature());
322   }
323   else {
324     isUseTransaction = true;
325     // 2. change auxiliary type of selected sketch entities
326     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
327     anObjects = aSelection->selectedPresentations();
328   }
329
330   QAction* anAction = action("AUXILIARY_CMD");
331   SessionPtr aMgr = ModelAPI_Session::get();
332   if (isUseTransaction) {
333     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
334       anOperation->abort();
335     aMgr->startOperation(anAction->text().toStdString());
336   }
337   myModule->sketchMgr()->storeSelection();
338
339   if (anObjects.size() > 0) {
340     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
341     for (; anIt != aLast; anIt++) {
342       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
343       if (aFeature.get() != NULL) {
344         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
345                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
346         if (aSketchFeature.get() != NULL) {
347           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
348
349           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
350             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
351           if (anAuxiliaryAttr)
352             anAuxiliaryAttr->setValue(isChecked);
353         }
354       }
355     }
356   }
357   if (isUseTransaction) {
358     aMgr->finishOperation();
359     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
360     XGUI_Workshop* aWorkshop = aConnector->workshop();
361     aWorkshop->updateCommandStatus();
362   }
363
364   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
365   myModule->sketchMgr()->restoreSelection();
366 }
367
368 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
369 {
370   bool anEnabled = false;
371   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
372
373   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
374                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
375   if (!isActiveSketch)
376     return anEnabled;
377
378   QObjectPtrList anObjects;
379   // 1. change auxiliary type of a created feature
380   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
381     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
382     anObjects.append(anOperation->feature());
383   }
384   else {
385     /// The operation should not be aborted here, because the method does not changed
386     /// the auxilliary state, but checks the possibility to perform this
387     ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
388     ///  anOperation->abort();
389     // 2. change auxiliary type of selected sketch entities
390     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
391     anObjects = aSelection->selectedPresentations();
392   }
393
394   bool isNotAuxiliaryFound = false;
395   if (anObjects.size() > 0) {
396     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
397     for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
398       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
399       if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
400         anEnabled = true;
401         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
402                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
403         if (aSketchFeature.get() != NULL) {
404           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
405
406           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
407             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
408           if (anAuxiliaryAttr)
409             isNotAuxiliaryFound = !anAuxiliaryAttr->value();
410         }
411       }
412     }
413   }
414   theValue = anObjects.size() && !isNotAuxiliaryFound;
415   return anEnabled;
416 }
417
418 void PartSet_MenuMgr::onActivatePart(bool)
419 {
420   if (myModule->workshop()->currentOperation())
421     return;
422   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
423   if (aObjects.size() > 0) {
424     ObjectPtr aObj = aObjects.first();
425     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
426     if (!aPart.get()) {
427       FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
428       if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
429         aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
430       }
431     }
432     if (aPart.get())
433       aPart->activate();
434   }
435 }
436
437 void PartSet_MenuMgr::onActivatePartSet(bool)
438 {
439   if (myModule->workshop()->currentOperation())
440     return;
441   SessionPtr aMgr = ModelAPI_Session::get();
442   bool isNewTransaction = !aMgr->isOperation();
443   // activation may cause changes in current features in document, so it must be in transaction
444   if (isNewTransaction) {
445     aMgr->startOperation("Activation");
446   }
447   aMgr->setActiveDocument(aMgr->moduleDocument());
448   if (isNewTransaction) {
449     aMgr->finishOperation();
450   }
451 }
452
453 void PartSet_MenuMgr::onEdit(bool)
454 {
455   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
456   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
457   if (aFeature == NULL) {
458     ResultParameterPtr aParam = 
459       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
460     if (aParam.get() != NULL) {
461       aFeature = ModelAPI_Feature::feature(aParam);
462     }
463   }
464   if (aFeature.get() != NULL)
465     myModule->editFeature(aFeature);
466 }