]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_MenuMgr.cpp
Salome HOME
#1017 Can build lines through several created points
[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         FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
153         // If we have coincidence then add Detach menu
154         if (aCoincident.get() != NULL) {
155           mySelectedFeature = aCoincident;
156           PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
157                                           SketchPlugin_ConstraintCoincidence::ENTITY_A());
158           PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines,
159                                           SketchPlugin_ConstraintCoincidence::ENTITY_B());
160           if (myCoinsideLines.size() > 0) {
161             aIsDetach = true;
162             QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
163             QAction* aAction;
164             int i = 0;
165             foreach (FeaturePtr aCoins, myCoinsideLines) {
166               aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
167               aAction->setData(QVariant(i));
168               i++;
169             }
170             connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
171             connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
172             connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
173           } 
174         }
175       }
176     }
177   }
178   if ((!aIsDetach) && hasFeature) {
179     theMenu->addAction(theStdActions["DELETE_CMD"]);
180   }
181   if (hasAttribute)
182     return true;
183   bool isAuxiliary;
184   if (canSetAuxiliary(isAuxiliary)) {
185     QAction* anAction = action("AUXILIARY_CMD");
186     theMenu->addAction(anAction);
187     anAction->setChecked(isAuxiliary);
188   }
189   return true;
190 }
191
192 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
193 {
194   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
195
196   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
197                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
198   if (isActiveSketch) {
199     theStdActions["WIREFRAME_CMD"]->setEnabled(false);
200     theStdActions["SHADING_CMD"]->setEnabled(false);
201     theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
202     theStdActions["SHOW_CMD"]->setEnabled(false);
203     theStdActions["HIDE_CMD"]->setEnabled(false);
204     theStdActions["HIDEALL_CMD"]->setEnabled(false);
205   }
206 }
207
208
209 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
210 {
211   if (myPrevId != -1) {
212     // Restore color for previous object
213     setLineColor(myPrevId, myColor, false);
214   }
215   myPrevId = theAction->data().toInt();
216   myColor = setLineColor(myPrevId, Qt::white, true);
217 }
218
219 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
220 {
221   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
222   XGUI_Workshop* aWorkshop = aConnector->workshop();
223   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
224
225   FeaturePtr aLine = myCoinsideLines.at(myPrevId);
226   std::list<ResultPtr>::const_iterator aIt;
227   const std::list<ResultPtr>& aResults = aLine->results();
228   QColor aColor;
229   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
230     aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
231   }
232   if (theUpdate)
233     aDisplayer->updateViewer();
234   return aColor;
235 }
236
237
238 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList, 
239   std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
240   QObjectPtrList& theOutList)
241 {
242   std::set<AttributePtr>::const_iterator aIt;
243   for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
244     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
245     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
246     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) { 
247       std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
248       if (aPnt.get() == NULL)
249         return;
250       gp_Pnt aP = aPnt->impl<gp_Pnt>();
251       if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
252         theOutList.append(aConstrFeature);
253       } 
254     }
255   }
256 }
257
258 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
259 {
260   int aId = theAction->data().toInt();
261   FeaturePtr aLine = myCoinsideLines.at(aId);
262   std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
263   if (!aOrig.get())
264     return;
265   
266   const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
267
268   QObjectPtrList aToDelFeatures;
269
270   addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
271
272   const std::list<ResultPtr>& aResults = aLine->results();
273   std::list<ResultPtr>::const_iterator aResIt;
274   for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
275     ResultPtr aResult = (*aResIt);
276     const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
277     addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
278   }
279   if (aToDelFeatures.size() > 0) {
280     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
281     XGUI_Workshop* aWorkshop = aConnector->workshop();
282     ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
283
284     ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
285                                    tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
286     bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
287     XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
288     // the active nested sketch operation should be aborted unconditionally
289     // the Delete action should be additionally granted for the Sketch operation
290     // in order to do not abort/commit it
291     if (!anOpMgr->canStartOperation(tr("Detach")))
292       return; // the objects are processed but can not be deleted
293
294     anOpMgr->startOperation(anOpAction);
295     aWorkshop->deleteFeatures(aToDelFeatures);
296     
297     anOpMgr->commitOperation();
298   }
299   myCoinsideLines.clear();
300 }
301
302
303 void PartSet_MenuMgr::onDetachMenuHide()
304 {
305   if (myPrevId != -1) {
306     // Restore color for previous object
307     setLineColor(myPrevId, myColor, false);
308   }
309   // Clear previous definitions
310   myPrevId = -1;
311 }
312
313   
314 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
315 {
316   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
317
318   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
319                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
320   if (!isActiveSketch)
321     return;
322
323   QObjectPtrList anObjects;
324   bool isUseTransaction = false;
325   // 1. change auxiliary type of a created feature
326   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
327       PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
328       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
329                                                                (anOperation);
330       if (aFOperation)
331         anObjects.append(aFOperation->feature());
332   }
333   else {
334     isUseTransaction = true;
335     // 2. change auxiliary type of selected sketch entities
336     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
337     anObjects = aSelection->selectedPresentations();
338   }
339
340   QAction* anAction = action("AUXILIARY_CMD");
341   //SessionPtr aMgr = ModelAPI_Session::get();
342   ModuleBase_OperationAction* anOpAction = 0;
343   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
344   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
345   if (isUseTransaction) {
346     anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
347     bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
348
349     if (!anOpMgr->canStartOperation(anOpAction->id()))
350       return; // the objects are processed but can not be deleted
351
352     anOpMgr->startOperation(anOpAction);
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     anOpMgr->commitOperation();
376
377   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
378   myModule->sketchMgr()->restoreSelection();
379 }
380
381 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
382 {
383   bool anEnabled = false;
384   ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
385
386   bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
387                         PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
388   if (!isActiveSketch)
389     return anEnabled;
390
391   QObjectPtrList anObjects;
392   // 1. change auxiliary type of a created feature
393   if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
394     PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
395     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
396     if (aFOperation)
397       anObjects.append(aFOperation->feature());
398   }
399   else {
400     /// The operation should not be aborted here, because the method does not changed
401     /// the auxilliary state, but checks the possibility to perform this
402     ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
403     ///  anOperation->abort();
404     // 2. change auxiliary type of selected sketch entities
405     ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
406     anObjects = aSelection->selectedPresentations();
407   }
408
409   bool isNotAuxiliaryFound = false;
410   if (anObjects.size() > 0) {
411     QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
412     for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
413       FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
414       if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
415         anEnabled = true;
416         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
417                             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
418         if (aSketchFeature.get() != NULL) {
419           std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
420
421           std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
422             std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
423           if (anAuxiliaryAttr)
424             isNotAuxiliaryFound = !anAuxiliaryAttr->value();
425         }
426       }
427     }
428   }
429   theValue = anObjects.size() && !isNotAuxiliaryFound;
430   return anEnabled;
431 }
432
433 void PartSet_MenuMgr::onActivatePart(bool)
434 {
435   if (myModule->workshop()->currentOperation())
436     return;
437   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
438   if (aObjects.size() > 0) {
439     ObjectPtr aObj = aObjects.first();
440     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
441     if (!aPart.get()) {
442       FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
443       if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
444         aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
445       }
446     }
447     if (aPart.get())
448       aPart->activate();
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
469 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
470                                           QStringList& theIds) const
471 {
472   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
473     theIds.append(tr("Detach"));
474     theIds.append(tr("Auxiliary"));
475   }
476 }
477
478 void PartSet_MenuMgr::onEdit(bool)
479 {
480   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
481   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
482   if (aFeature == NULL) {
483     ResultParameterPtr aParam = 
484       std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
485     if (aParam.get() != NULL) {
486       aFeature = ModelAPI_Feature::feature(aParam);
487     }
488   }
489   if (aFeature.get() != NULL)
490     myModule->editFeature(aFeature);
491 }
492
493 void PartSet_MenuMgr::onSelectParentFeature()
494 {
495   QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
496   if (aObjects.size() != 1)
497     return;
498
499   SessionPtr aMgr = ModelAPI_Session::get();
500   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
501   if( !aResult.get() )
502     return;
503
504   FeaturePtr aParentFeature = aResult->document()->feature( aResult );
505   QObjectPtrList aSelection;
506   aSelection.append( aParentFeature );
507   myModule->workshop()->selection()->setSelectedObjects( aSelection );
508 }