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