]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_MenuMgr.cpp
Salome HOME
0d8c0cc9a5f1cec77ec2e3f3772e9e8bedce9530
[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         if (aPnt.get() == NULL)
295           return;
296         aP = aPnt->impl<gp_Pnt>();
297         if (aOrig->isEqual(aPnt)) {
298           aToDelFeatures.append(aConstrFeature);
299           break;
300         }
301       }
302     }
303   }
304   if (aToDelFeatures.size() > 0) {
305     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
306     XGUI_Workshop* aWorkshop = aConnector->workshop();
307     ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
308     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
309       anOperation->abort();
310
311     SessionPtr aMgr = ModelAPI_Session::get();
312
313     QString aName = tr("Detach %1").arg(aLine->data()->name().c_str());
314     aMgr->startOperation(aName.toStdString());
315     aWorkshop->deleteFeatures(aToDelFeatures);
316     aMgr->finishOperation();
317   }
318   myCoinsideLines.clear();
319 }
320
321
322 void PartSet_MenuMgr::onDetachMenuHide()
323 {
324   if (myPrevId != -1) {
325     // Restore color for previous object
326     setLineColor(myPrevId, myColor, false);
327   }
328   // Clear previous definitions
329   myPrevId = -1;
330 }
331
332   
333 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
334 {
335   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
336
337   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
338                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
339   if (!isActiveSketch)
340     return;
341
342   QObjectPtrList anObjects;
343   bool isUseTransaction = false;
344   // 1. change auxiliary type of a created feature
345   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
346     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
347     anObjects.append(anOperation->feature());
348   }
349   else {
350     isUseTransaction = true;
351     // 2. change auxiliary type of selected sketch entities
352     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
353     anObjects = aSelection->selectedPresentations();
354   }
355
356   QAction* anAction = action("AUXILIARY_CMD");
357   SessionPtr aMgr = ModelAPI_Session::get();
358   if (isUseTransaction) {
359     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
360       anOperation->abort();
361     aMgr->startOperation(anAction->text().toStdString());
362   }
363   myModule->sketchMgr()->storeSelection();
364
365   if (anObjects.size() > 0) {
366     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
367     for (; anIt != aLast; anIt++) {
368       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
369       if (aFeature.get() != NULL) {
370         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
371                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
372         if (aSketchFeature.get() != NULL) {
373           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
374
375           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
376             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
377           if (anAuxiliaryAttr)
378             anAuxiliaryAttr->setValue(isChecked);
379         }
380       }
381     }
382   }
383   if (isUseTransaction) {
384     aMgr->finishOperation();
385     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
386     XGUI_Workshop* aWorkshop = aConnector->workshop();
387     aWorkshop->updateCommandStatus();
388   }
389
390   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
391   myModule->sketchMgr()->restoreSelection();
392 }
393
394 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
395 {
396   bool anEnabled = false;
397   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
398
399   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
400                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
401   if (!isActiveSketch)
402     return anEnabled;
403
404   QObjectPtrList anObjects;
405   // 1. change auxiliary type of a created feature
406   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
407     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
408     anObjects.append(anOperation->feature());
409   }
410   else {
411     /// The operation should not be aborted here, because the method does not changed
412     /// the auxilliary state, but checks the possibility to perform this
413     ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
414     ///  anOperation->abort();
415     // 2. change auxiliary type of selected sketch entities
416     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
417     anObjects = aSelection->selectedPresentations();
418   }
419
420   bool isNotAuxiliaryFound = false;
421   if (anObjects.size() > 0) {
422     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
423     for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
424       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
425       if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
426         anEnabled = true;
427         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
428                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
429         if (aSketchFeature.get() != NULL) {
430           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
431
432           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
433             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
434           if (anAuxiliaryAttr)
435             isNotAuxiliaryFound = !anAuxiliaryAttr->value();
436         }
437       }
438     }
439   }
440   theValue = anObjects.size() && !isNotAuxiliaryFound;
441   return anEnabled;
442 }
443
444 void PartSet_MenuMgr::onActivatePart(bool)
445 {
446   if (myModule->workshop()->currentOperation())
447     return;
448   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
449   if (aObjects.size() > 0) {
450     ObjectPtr aObj = aObjects.first();
451     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
452     if (!aPart.get()) {
453       FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
454       if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
455         aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
456       }
457     }
458     if (aPart.get())
459       aPart->activate();
460   }
461 }
462
463 void PartSet_MenuMgr::onActivatePartSet(bool)
464 {
465   if (myModule->workshop()->currentOperation())
466     return;
467   SessionPtr aMgr = ModelAPI_Session::get();
468   bool isNewTransaction = !aMgr->isOperation();
469   // activation may cause changes in current features in document, so it must be in transaction
470   if (isNewTransaction) {
471     aMgr->startOperation("Activation");
472   }
473   aMgr->setActiveDocument(aMgr->moduleDocument());
474   if (isNewTransaction) {
475     aMgr->finishOperation();
476   }
477 }
478
479 void PartSet_MenuMgr::onEdit(bool)
480 {
481   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
482   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
483   if (aFeature == NULL) {
484     ResultParameterPtr aParam = 
485       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
486     if (aParam.get() != NULL) {
487       aFeature = ModelAPI_Feature::feature(aParam);
488     }
489   }
490   if (aFeature.get() != NULL)
491     myModule->editFeature(aFeature);
492 }
493
494 void PartSet_MenuMgr::onSelectParentFeature()
495 {
496   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
497   if (aObjects.size() != 1)
498     return;
499
500   SessionPtr aMgr = ModelAPI_Session::get();
501   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
502   if( !aResult.get() )
503     return;
504
505   FeaturePtr aParentFeature = aResult->document()->feature( aResult );
506   QObjectPtrList aSelection;
507   aSelection.append( aParentFeature );
508   myModule->workshop()->selection()->setSelectedObjects( aSelection );
509 }