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