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