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