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>
27 #include <ModuleBase_ViewerPrs.h>
28 #include <ModuleBase_Tools.h>
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>
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>
47 #include <BRep_Tool.hxx>
49 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
50 : QObject(theModule), myModule(theModule), myPrevId(-1)
56 QAction* PartSet_MenuMgr::action(const QString& theId) const
58 if (myActions.contains(theId))
59 return myActions[theId];
63 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
65 if (myActions.contains(theId))
66 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
67 theAction->setData(theId);
68 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
69 myActions[theId] = theAction;
72 void PartSet_MenuMgr::createActions()
76 aAction = ModuleBase_Tools::createAction(QIcon(), tr("Auxiliary"), this);
77 aAction->setCheckable(true);
78 addAction("AUXILIARY_CMD", aAction);
80 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), this,
81 this, SLOT(onActivatePart(bool)));
82 myActions["ACTIVATE_PART_CMD"] = aAction;
85 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), this,
86 this, SLOT(onActivatePartSet(bool)));
87 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
89 aAction = ModuleBase_Tools::createAction(QIcon(":icons/edit.png"), tr("Edit..."), this,
90 this, SLOT(onEdit(bool)));
91 myActions["EDIT_CMD"] = aAction;
95 void PartSet_MenuMgr::onAction(bool isChecked)
97 QAction* aAction = static_cast<QAction*>(sender());
98 QString anId = aAction->data().toString();
100 if (anId == "AUXILIARY_CMD") {
101 setAuxiliary(isChecked);
105 bool PartSet_MenuMgr::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
107 QMap<int, QAction*>& theMenuActions) const
111 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
112 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
113 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
116 myCoinsideLines.clear();
117 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
119 bool aIsDetach = false;
120 bool hasAttribute = false;
121 bool hasFeature = false;
123 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
124 if (aPrsList.size() > 1) {
126 } else if (aPrsList.size() == 1) {
129 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
130 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
131 if (aResult.get() != NULL) {
132 const GeomShapePtr& aShape = aPrs->shape();
133 if (aShape.get() && aShape->isEqual(aResult->shape()))
138 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
139 hasFeature = (aFeature.get() != NULL);
143 const GeomShapePtr& aShape = aPrsList.first()->shape();
144 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
145 // Find 2d coordinates
146 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
147 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
148 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
149 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
150 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
151 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
153 // Find coincident in these coordinates
154 ObjectPtr aObj = aPrsList.first()->object();
155 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
156 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
157 // If we have coincidence then add Detach menu
158 if (aCoincident.get() != NULL) {
159 QList<FeaturePtr> aCoins;
160 mySelectedFeature = aCoincident;
161 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
162 SketchPlugin_ConstraintCoincidence::ENTITY_A());
163 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
164 SketchPlugin_ConstraintCoincidence::ENTITY_B());
165 if (myCoinsideLines.size() > 0) {
167 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
168 theMenuActions[anIndex++] = aSubMenu->menuAction();
171 foreach (FeaturePtr aCoins, myCoinsideLines) {
172 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
173 aAction->setData(QVariant(i));
176 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
177 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
178 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
186 if (canSetAuxiliary(isAuxiliary)) {
187 QAction* anAction = action("AUXILIARY_CMD");
188 theMenuActions[anIndex++] = anAction;
189 anAction->setChecked(isAuxiliary);
193 if (!aIsDetach && hasFeature) {
194 // Delete item should be the last in the list of actions
195 theMenuActions[1000] = theStdActions["DELETE_CMD"];
201 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
203 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
205 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
206 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
207 if (isActiveSketch) {
208 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
209 theStdActions["SHADING_CMD"]->setEnabled(false);
210 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
211 theStdActions["SHOW_CMD"]->setEnabled(false);
212 theStdActions["HIDE_CMD"]->setEnabled(false);
213 theStdActions["HIDEALL_CMD"]->setEnabled(false);
218 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
220 if (myPrevId != -1) {
221 // Restore color for previous object
222 setLineColor(myPrevId, myColor, false);
224 myPrevId = theAction->data().toInt();
225 myColor = setLineColor(myPrevId, Qt::white, true);
228 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
230 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
231 XGUI_Workshop* aWorkshop = aConnector->workshop();
232 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
234 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
235 std::list<ResultPtr>::const_iterator aIt;
236 const std::list<ResultPtr>& aResults = aLine->results();
238 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
239 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
242 aDisplayer->updateViewer();
247 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
248 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
249 QObjectPtrList& theOutList)
251 std::set<AttributePtr>::const_iterator aIt;
252 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
253 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
254 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
255 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
256 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
257 if (aPnt.get() == NULL)
259 gp_Pnt aP = aPnt->impl<gp_Pnt>();
260 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
261 theOutList.append(aConstrFeature);
267 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
269 int aId = theAction->data().toInt();
270 FeaturePtr aLine = myCoinsideLines.at(aId);
271 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
275 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
277 QObjectPtrList aToDelFeatures;
279 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
281 const std::list<ResultPtr>& aResults = aLine->results();
282 std::list<ResultPtr>::const_iterator aResIt;
283 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
284 ResultPtr aResult = (*aResIt);
285 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
286 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
288 if (aToDelFeatures.size() > 0) {
289 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
290 XGUI_Workshop* aWorkshop = aConnector->workshop();
291 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
293 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
294 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
295 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
296 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
297 // the active nested sketch operation should be aborted unconditionally
298 // the Delete action should be additionally granted for the Sketch operation
299 // in order to do not abort/commit it
300 if (!anOpMgr->canStartOperation(tr("Detach")))
301 return; // the objects are processed but can not be deleted
303 anOpMgr->startOperation(anOpAction);
304 aWorkshop->deleteFeatures(aToDelFeatures);
306 anOpMgr->commitOperation();
308 myCoinsideLines.clear();
312 void PartSet_MenuMgr::onDetachMenuHide()
314 if (myPrevId != -1) {
315 // Restore color for previous object
316 setLineColor(myPrevId, myColor, false);
318 // Clear previous definitions
323 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
325 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
327 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
328 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
332 QObjectPtrList anObjects;
333 bool isUseTransaction = false;
334 // 1. change auxiliary type of a created feature
335 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
336 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
337 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
340 anObjects.append(aFOperation->feature());
343 isUseTransaction = true;
344 // 2. change auxiliary type of selected sketch entities
345 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
346 anObjects = aSelection->selectedPresentations();
349 QAction* anAction = action("AUXILIARY_CMD");
350 //SessionPtr aMgr = ModelAPI_Session::get();
351 ModuleBase_OperationAction* anOpAction = 0;
352 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
353 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
354 if (isUseTransaction) {
355 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
356 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
358 if (!anOpMgr->canStartOperation(anOpAction->id()))
359 return; // the objects are processed but can not be deleted
361 anOpMgr->startOperation(anOpAction);
363 if (anObjects.size() > 0) {
364 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
365 for (; anIt != aLast; anIt++) {
366 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
367 if (aFeature.get() != NULL) {
368 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
369 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
370 if (aSketchFeature.get() != NULL) {
371 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
373 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
374 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
376 anAuxiliaryAttr->setValue(isChecked);
381 if (isUseTransaction)
382 anOpMgr->commitOperation();
384 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
387 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
389 bool anEnabled = false;
390 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
392 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
393 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
397 QObjectPtrList anObjects;
398 // 1. change auxiliary type of a created feature
399 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
400 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
401 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
403 anObjects.append(aFOperation->feature());
406 /// The operation should not be aborted here, because the method does not changed
407 /// the auxilliary state, but checks the possibility to perform this
408 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
409 /// anOperation->abort();
410 // 2. change auxiliary type of selected sketch entities
411 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
412 anObjects = aSelection->selectedPresentations();
415 bool isNotAuxiliaryFound = false;
416 if (anObjects.size() > 0) {
417 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
418 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
419 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
420 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
422 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
423 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
424 if (aSketchFeature.get() != NULL) {
425 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
427 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
428 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
430 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
435 theValue = anObjects.size() && !isNotAuxiliaryFound;
439 void PartSet_MenuMgr::onActivatePart(bool)
441 if (myModule->workshop()->currentOperation())
443 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
444 if (aObjects.size() > 0) {
445 ObjectPtr aObj = aObjects.first();
446 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
448 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
449 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
450 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
455 myModule->workshop()->updateCommandStatus();
459 void PartSet_MenuMgr::onActivatePartSet(bool)
461 if (myModule->workshop()->currentOperation())
466 void PartSet_MenuMgr::activatePartSet() const
468 SessionPtr aMgr = ModelAPI_Session::get();
469 bool isNewTransaction = !aMgr->isOperation();
470 // activation may cause changes in current features in document, so it must be in transaction
471 if (isNewTransaction) aMgr->startOperation("Activation");
472 aMgr->setActiveDocument(aMgr->moduleDocument());
473 if (isNewTransaction) aMgr->finishOperation();
475 myModule->workshop()->updateCommandStatus();
478 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
479 QStringList& theIds) const
481 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
482 theIds.append(tr("Detach"));
483 theIds.append(tr("Auxiliary"));
487 void PartSet_MenuMgr::onEdit(bool)
489 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
490 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
491 if (aFeature == NULL) {
492 ResultParameterPtr aParam =
493 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
494 if (aParam.get() != NULL) {
495 aFeature = ModelAPI_Feature::feature(aParam);
498 if (aFeature.get() != NULL)
499 myModule->editFeature(aFeature);
502 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
504 if (theEvent->type() == QEvent::MouseButtonDblClick) {
505 SessionPtr aMgr = ModelAPI_Session::get();
506 if (aMgr->activeDocument() != aMgr->moduleDocument())
509 return QObject::eventFilter(theObj, theEvent);