]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_MenuMgr.cpp
Salome HOME
Issue #762: Show Detach menu item in Salome mode. Disable menu items for shapes in...
[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::Viewer);
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::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
209 {
210   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
211
212   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
213                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
214   if (isActiveSketch) {
215     theStdActions["WIREFRAME_CMD"]->setEnabled(false);
216     theStdActions["SHADING_CMD"]->setEnabled(false);
217     theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
218     theStdActions["SHOW_CMD"]->setEnabled(false);
219     theStdActions["HIDE_CMD"]->setEnabled(false);
220     theStdActions["HIDEALL_CMD"]->setEnabled(false);
221   }
222 }
223
224
225 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
226 {
227   if (myPrevId != -1) {
228     // Restore color for previous object
229     setLineColor(myPrevId, myColor, false);
230   }
231   myPrevId = theAction->data().toInt();
232   myColor = setLineColor(myPrevId, Qt::white, true);
233 }
234
235 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
236 {
237   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
238   XGUI_Workshop* aWorkshop = aConnector->workshop();
239   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
240
241   FeaturePtr aLine = myCoinsideLines.at(myPrevId);
242   std::list<ResultPtr>::const_iterator aIt;
243   const std::list<ResultPtr>& aResults = aLine->results();
244   QColor aColor;
245   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
246     aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
247   }
248   if (theUpdate)
249     aDisplayer->updateViewer();
250   return aColor;
251 }
252
253
254 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
255 {
256   int aId = theAction->data().toInt();
257   FeaturePtr aLine = myCoinsideLines.at(aId);
258   std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getPoint(mySelectedFeature,
259                                                         SketchPlugin_ConstraintCoincidence::ENTITY_A());
260   if (aOrig.get() == NULL)
261     aOrig = PartSet_Tools::getPoint(mySelectedFeature,
262                                     SketchPlugin_ConstraintCoincidence::ENTITY_B());
263   
264   gp_Pnt aOr = aOrig->impl<gp_Pnt>();
265   const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
266
267   QObjectPtrList aToDelFeatures;
268   std::set<AttributePtr>::const_iterator aIt;
269   // Find all coincedences corresponded to the selected line in the selected point
270   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
271     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
272     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
273     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
274       std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getPoint(aConstrFeature,
275                                             SketchPlugin_ConstraintCoincidence::ENTITY_A());
276       if (aPnt.get() == NULL)
277         aPnt = PartSet_Tools::getPoint(aConstrFeature,
278                                        SketchPlugin_ConstraintCoincidence::ENTITY_B());
279       if (aPnt.get() == NULL)
280         return;
281       gp_Pnt aP = aPnt->impl<gp_Pnt>();
282       if (aOrig->isEqual(aPnt)) {
283         aToDelFeatures.append(aConstrFeature);
284       } else {
285         aPnt = PartSet_Tools::getPoint(aConstrFeature,
286                                        SketchPlugin_ConstraintCoincidence::ENTITY_B());
287         aP = aPnt->impl<gp_Pnt>();
288         if (aOrig->isEqual(aPnt)) {
289           aToDelFeatures.append(aConstrFeature);
290           break;
291         }
292       }
293     }
294   }
295   if (aToDelFeatures.size() > 0) {
296     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
297     XGUI_Workshop* aWorkshop = aConnector->workshop();
298     ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
299     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
300       anOperation->abort();
301
302     SessionPtr aMgr = ModelAPI_Session::get();
303
304     QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
305     aMgr->startOperation(aName.toStdString());
306     aWorkshop->deleteFeatures(aToDelFeatures);
307     aMgr->finishOperation();
308   }
309   myCoinsideLines.clear();
310 }
311
312
313 void PartSet_MenuMgr::onDetachMenuHide()
314 {
315   if (myPrevId != -1) {
316     // Restore color for previous object
317     setLineColor(myPrevId, myColor, false);
318   }
319   // Clear previous definitions
320   myPrevId = -1;
321 }
322
323   
324 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
325 {
326   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
327
328   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
329                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
330   if (!isActiveSketch)
331     return;
332
333   QObjectPtrList anObjects;
334   bool isUseTransaction = false;
335   // 1. change auxiliary type of a created feature
336   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
337     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
338     anObjects.append(anOperation->feature());
339   }
340   else {
341     isUseTransaction = true;
342     // 2. change auxiliary type of selected sketch entities
343     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
344     anObjects = aSelection->selectedPresentations();
345   }
346
347   QAction* anAction = action("AUXILIARY_CMD");
348   SessionPtr aMgr = ModelAPI_Session::get();
349   if (isUseTransaction) {
350     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
351       anOperation->abort();
352     aMgr->startOperation(anAction->text().toStdString());
353   }
354   myModule->sketchMgr()->storeSelection();
355
356   if (anObjects.size() > 0) {
357     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
358     for (; anIt != aLast; anIt++) {
359       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
360       if (aFeature.get() != NULL) {
361         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
362                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
363         if (aSketchFeature.get() != NULL) {
364           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
365
366           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
367             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
368           if (anAuxiliaryAttr)
369             anAuxiliaryAttr->setValue(isChecked);
370         }
371       }
372     }
373   }
374   if (isUseTransaction) {
375     aMgr->finishOperation();
376     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
377     XGUI_Workshop* aWorkshop = aConnector->workshop();
378     aWorkshop->updateCommandStatus();
379   }
380
381   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
382   myModule->sketchMgr()->restoreSelection();
383 }
384
385 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
386 {
387   bool anEnabled = false;
388   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
389
390   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
391                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
392   if (!isActiveSketch)
393     return anEnabled;
394
395   QObjectPtrList anObjects;
396   // 1. change auxiliary type of a created feature
397   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
398     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
399     anObjects.append(anOperation->feature());
400   }
401   else {
402     /// The operation should not be aborted here, because the method does not changed
403     /// the auxilliary state, but checks the possibility to perform this
404     ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
405     ///  anOperation->abort();
406     // 2. change auxiliary type of selected sketch entities
407     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
408     anObjects = aSelection->selectedPresentations();
409   }
410
411   bool isNotAuxiliaryFound = false;
412   if (anObjects.size() > 0) {
413     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
414     for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
415       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
416       if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
417         anEnabled = true;
418         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
419                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
420         if (aSketchFeature.get() != NULL) {
421           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
422
423           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
424             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
425           if (anAuxiliaryAttr)
426             isNotAuxiliaryFound = !anAuxiliaryAttr->value();
427         }
428       }
429     }
430   }
431   theValue = anObjects.size() && !isNotAuxiliaryFound;
432   return anEnabled;
433 }
434
435 void PartSet_MenuMgr::onActivatePart(bool)
436 {
437   if (myModule->workshop()->currentOperation())
438     return;
439   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
440   if (aObjects.size() > 0) {
441     ObjectPtr aObj = aObjects.first();
442     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
443     if (!aPart.get()) {
444       FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
445       if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
446         aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
447       }
448     }
449     if (aPart.get())
450       aPart->activate();
451   }
452 }
453
454 void PartSet_MenuMgr::onActivatePartSet(bool)
455 {
456   if (myModule->workshop()->currentOperation())
457     return;
458   SessionPtr aMgr = ModelAPI_Session::get();
459   bool isNewTransaction = !aMgr->isOperation();
460   // activation may cause changes in current features in document, so it must be in transaction
461   if (isNewTransaction) {
462     aMgr->startOperation("Activation");
463   }
464   aMgr->setActiveDocument(aMgr->moduleDocument());
465   if (isNewTransaction) {
466     aMgr->finishOperation();
467   }
468 }
469
470 void PartSet_MenuMgr::onEdit(bool)
471 {
472   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
473   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
474   if (aFeature == NULL) {
475     ResultParameterPtr aParam = 
476       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
477     if (aParam.get() != NULL) {
478       aFeature = ModelAPI_Feature::feature(aParam);
479     }
480   }
481   if (aFeature.get() != NULL)
482     myModule->editFeature(aFeature);
483 }