1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_MenuMgr.cpp
4 // Created: 03 April 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_MenuMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_SketcherMgr.h"
10 #include "PartSet_Tools.h"
12 #include <PartSetPlugin_Part.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
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>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_Operation.h>
25 #include <ModuleBase_OperationAction.h>
26 #include <ModuleBase_OperationFeature.h>
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>
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>
45 #include <BRep_Tool.hxx>
47 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
48 : QObject(theModule), myModule(theModule), myPrevId(-1)
54 QAction* PartSet_MenuMgr::action(const QString& theId) const
56 if (myActions.contains(theId))
57 return myActions[theId];
61 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
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;
70 void PartSet_MenuMgr::createActions()
74 aAction = new QAction(tr("Auxiliary"), this);
75 aAction->setCheckable(true);
76 addAction("AUXILIARY_CMD", aAction);
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;
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;
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;
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;
98 void PartSet_MenuMgr::onAction(bool isChecked)
100 QAction* aAction = static_cast<QAction*>(sender());
101 QString anId = aAction->data().toString();
103 if (anId == "AUXILIARY_CMD") {
104 setAuxiliary(isChecked);
108 bool PartSet_MenuMgr::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
110 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
111 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
112 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
115 myCoinsideLines.clear();
116 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
118 bool aIsDetach = false;
119 bool hasAttribute = false;
120 bool hasFeature = false;
122 QList<ModuleBase_ViewerPrs> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
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()))
134 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs.object());
135 hasFeature = (aFeature.get() != NULL);
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);
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) {
164 QMenu* aSubMenu = theMenu->addMenu(tr("Detach"));
167 foreach (FeaturePtr aCoins, myCoinsideLines) {
168 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
169 aAction->setData(QVariant(i));
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*)));
180 if ((!aIsDetach) && hasFeature) {
181 theMenu->addAction(theStdActions["DELETE_CMD"]);
186 if (canSetAuxiliary(isAuxiliary)) {
187 QAction* anAction = action("AUXILIARY_CMD");
188 theMenu->addAction(anAction);
189 anAction->setChecked(isAuxiliary);
194 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
196 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
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);
211 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
213 if (myPrevId != -1) {
214 // Restore color for previous object
215 setLineColor(myPrevId, myColor, false);
217 myPrevId = theAction->data().toInt();
218 myColor = setLineColor(myPrevId, Qt::white, true);
221 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
223 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
224 XGUI_Workshop* aWorkshop = aConnector->workshop();
225 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
227 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
228 std::list<ResultPtr>::const_iterator aIt;
229 const std::list<ResultPtr>& aResults = aLine->results();
231 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
232 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
235 aDisplayer->updateViewer();
240 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
241 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
242 QObjectPtrList& theOutList)
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)
252 gp_Pnt aP = aPnt->impl<gp_Pnt>();
253 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
254 theOutList.append(aConstrFeature);
260 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
262 int aId = theAction->data().toInt();
263 FeaturePtr aLine = myCoinsideLines.at(aId);
264 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
268 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
270 QObjectPtrList aToDelFeatures;
272 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
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);
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();
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
296 anOpMgr->startOperation(anOpAction);
297 aWorkshop->deleteFeatures(aToDelFeatures);
299 anOpMgr->commitOperation();
301 myCoinsideLines.clear();
305 void PartSet_MenuMgr::onDetachMenuHide()
307 if (myPrevId != -1) {
308 // Restore color for previous object
309 setLineColor(myPrevId, myColor, false);
311 // Clear previous definitions
316 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
318 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
320 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
321 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
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*>
333 anObjects.append(aFOperation->feature());
336 isUseTransaction = true;
337 // 2. change auxiliary type of selected sketch entities
338 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
339 anObjects = aSelection->selectedPresentations();
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);
351 if (!anOpMgr->canStartOperation(anOpAction->id()))
352 return; // the objects are processed but can not be deleted
354 anOpMgr->startOperation(anOpAction);
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();
366 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
367 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
369 anAuxiliaryAttr->setValue(isChecked);
374 if (isUseTransaction)
375 anOpMgr->commitOperation();
377 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
380 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
382 bool anEnabled = false;
383 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
385 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
386 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
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);
396 anObjects.append(aFOperation->feature());
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();
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())) {
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();
420 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
421 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
423 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
428 theValue = anObjects.size() && !isNotAuxiliaryFound;
432 void PartSet_MenuMgr::onActivatePart(bool)
434 if (myModule->workshop()->currentOperation())
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);
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());
448 myModule->workshop()->updateCommandStatus();
452 void PartSet_MenuMgr::onActivatePartSet(bool)
454 if (myModule->workshop()->currentOperation())
459 void PartSet_MenuMgr::activatePartSet() const
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();
468 myModule->workshop()->updateCommandStatus();
471 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
472 QStringList& theIds) const
474 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
475 theIds.append(tr("Detach"));
476 theIds.append(tr("Auxiliary"));
480 void PartSet_MenuMgr::onEdit(bool)
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);
491 if (aFeature.get() != NULL)
492 myModule->editFeature(aFeature);
495 void PartSet_MenuMgr::onSelectParentFeature()
497 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
498 if (aObjects.size() != 1)
501 SessionPtr aMgr = ModelAPI_Session::get();
502 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>( aObjects.first() );
506 FeaturePtr aParentFeature = aResult->document()->feature( aResult );
507 QObjectPtrList aSelection;
508 aSelection.append( aParentFeature );
509 myModule->workshop()->selection()->setSelectedObjects( aSelection );
512 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
514 if (theEvent->type() == QEvent::MouseButtonDblClick) {
515 SessionPtr aMgr = ModelAPI_Session::get();
516 if (aMgr->activeDocument() != aMgr->moduleDocument())
519 return QObject::eventFilter(theObj, theEvent);