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>
35 #include <XGUI_ObjectsBrowser.h>
37 #include <Events_Loop.h>
38 #include <ModelAPI_Events.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_ResultParameter.h>
42 #include <QMainWindow>
48 #include <BRep_Tool.hxx>
50 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
51 : QObject(theModule), myModule(theModule), myPrevId(-1)
57 QAction* PartSet_MenuMgr::action(const QString& theId) const
59 if (myActions.contains(theId))
60 return myActions[theId];
64 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
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;
73 void PartSet_MenuMgr::createActions()
77 QWidget* aParent = myModule->workshop()->desktop();
78 aAction = ModuleBase_Tools::createAction(QIcon(), tr("Auxiliary"), aParent);
79 aAction->setCheckable(true);
80 addAction("AUXILIARY_CMD", aAction);
82 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
83 this, SLOT(onActivatePart(bool)));
84 myActions["ACTIVATE_PART_CMD"] = aAction;
87 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
88 this, SLOT(onActivatePartSet(bool)));
89 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
91 aAction = ModuleBase_Tools::createAction(QIcon(":icons/edit.png"), tr("Edit..."), aParent,
92 this, SLOT(onEdit(bool)));
93 myActions["EDIT_CMD"] = aAction;
97 void PartSet_MenuMgr::onAction(bool isChecked)
99 QAction* aAction = static_cast<QAction*>(sender());
100 QString anId = aAction->data().toString();
102 if (anId == "AUXILIARY_CMD") {
103 setAuxiliary(isChecked);
107 bool PartSet_MenuMgr::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
109 QMap<int, QAction*>& theMenuActions) const
113 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
114 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
115 !myModule->sketchMgr()->isNestedSketchOperation(anOperation))
118 myCoinsideLines.clear();
119 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
121 bool aIsDetach = false;
122 bool hasAttribute = false;
123 bool hasFeature = false;
125 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
126 if (aPrsList.size() > 1) {
128 } else if (aPrsList.size() == 1) {
131 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
132 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
133 if (aResult.get() != NULL) {
134 const GeomShapePtr& aShape = aPrs->shape();
135 if (aShape.get() && aShape->isEqual(aResult->shape()))
140 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
141 hasFeature = (aFeature.get() != NULL);
145 const GeomShapePtr& aShape = aPrsList.first()->shape();
146 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
147 // Find 2d coordinates
148 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
149 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
150 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
151 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
152 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
153 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
155 // Find coincident in these coordinates
156 ObjectPtr aObj = aPrsList.first()->object();
157 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
158 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
159 // If we have coincidence then add Detach menu
160 if (aCoincident.get() != NULL) {
161 QList<FeaturePtr> aCoins;
162 mySelectedFeature = aCoincident;
163 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
164 SketchPlugin_ConstraintCoincidence::ENTITY_A());
165 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
166 SketchPlugin_ConstraintCoincidence::ENTITY_B());
167 if (myCoinsideLines.size() > 0) {
169 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
170 theMenuActions[anIndex++] = aSubMenu->menuAction();
173 foreach (FeaturePtr aCoins, myCoinsideLines) {
174 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
175 aAction->setData(QVariant(i));
178 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
179 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
180 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
188 if (canSetAuxiliary(isAuxiliary)) {
189 QAction* anAction = action("AUXILIARY_CMD");
190 theMenuActions[anIndex++] = anAction;
191 anAction->setChecked(isAuxiliary);
195 if (!aIsDetach && hasFeature) {
196 // Delete item should be the last in the list of actions
197 theMenuActions[1000] = theStdActions["DELETE_CMD"];
203 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
205 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
207 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
208 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
209 if (isActiveSketch) {
210 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
211 theStdActions["SHADING_CMD"]->setEnabled(false);
212 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
213 theStdActions["SHOW_CMD"]->setEnabled(false);
214 theStdActions["HIDE_CMD"]->setEnabled(false);
215 theStdActions["HIDEALL_CMD"]->setEnabled(false);
220 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
222 if (myPrevId != -1) {
223 // Restore color for previous object
224 setLineColor(myPrevId, myColor, false);
226 myPrevId = theAction->data().toInt();
227 myColor = setLineColor(myPrevId, Qt::white, true);
230 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
232 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
233 XGUI_Workshop* aWorkshop = aConnector->workshop();
234 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
236 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
237 std::list<ResultPtr>::const_iterator aIt;
238 const std::list<ResultPtr>& aResults = aLine->results();
240 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
241 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
244 aDisplayer->updateViewer();
249 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
250 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
251 QObjectPtrList& theOutList)
253 std::set<AttributePtr>::const_iterator aIt;
254 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
255 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
256 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
257 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
258 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
259 if (aPnt.get() == NULL)
261 gp_Pnt aP = aPnt->impl<gp_Pnt>();
262 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
263 theOutList.append(aConstrFeature);
269 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
271 int aId = theAction->data().toInt();
272 FeaturePtr aLine = myCoinsideLines.at(aId);
273 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
277 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
279 QObjectPtrList aToDelFeatures;
281 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
283 const std::list<ResultPtr>& aResults = aLine->results();
284 std::list<ResultPtr>::const_iterator aResIt;
285 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
286 ResultPtr aResult = (*aResIt);
287 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
288 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
290 if (aToDelFeatures.size() > 0) {
291 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
292 XGUI_Workshop* aWorkshop = aConnector->workshop();
293 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
295 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
296 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
297 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
298 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
299 // the active nested sketch operation should be aborted unconditionally
300 // the Delete action should be additionally granted for the Sketch operation
301 // in order to do not abort/commit it
303 if (!anOpMgr->canStartOperation(tr("Detach"), isCommitted))
304 return; // the objects are processed but can not be deleted
306 anOpMgr->startOperation(anOpAction);
307 aWorkshop->deleteFeatures(aToDelFeatures);
309 anOpMgr->commitOperation();
311 myCoinsideLines.clear();
315 void PartSet_MenuMgr::onDetachMenuHide()
317 if (myPrevId != -1) {
318 // Restore color for previous object
319 setLineColor(myPrevId, myColor, false);
321 // Clear previous definitions
326 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
328 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
330 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
331 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
332 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
336 QObjectPtrList anObjects;
337 bool isUseTransaction = false;
338 // 1. change auxiliary type of a created feature
339 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
340 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
341 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
344 anObjects.append(aFOperation->feature());
347 isUseTransaction = true;
348 // 2. change auxiliary type of selected sketch entities
349 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
350 anObjects = aSelection->selectedPresentations();
353 QAction* anAction = action("AUXILIARY_CMD");
354 //SessionPtr aMgr = ModelAPI_Session::get();
355 ModuleBase_OperationAction* anOpAction = 0;
356 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
357 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
358 if (isUseTransaction) {
359 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
360 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
363 if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
364 return; // the objects are processed but can not be deleted
366 anOpMgr->startOperation(anOpAction);
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();
378 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
379 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
380 aSketchFeature->data()->attribute(anAttribute));
382 anAuxiliaryAttr->setValue(isChecked);
387 if (isUseTransaction)
388 anOpMgr->commitOperation();
390 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
393 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
395 bool anEnabled = false;
396 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
398 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
399 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
400 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
404 QObjectPtrList anObjects;
405 // 1. change auxiliary type of a created feature
406 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
407 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
408 ModuleBase_OperationFeature* aFOperation =
409 dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
411 anObjects.append(aFOperation->feature());
414 /// The operation should not be aborted here, because the method does not changed
415 /// the auxilliary state, but checks the possibility to perform this
416 ///if (myModule->sketchMgr()->isNestedSketchOperation(anOperation))
417 /// anOperation->abort();
418 // 2. change auxiliary type of selected sketch entities
419 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
420 anObjects = aSelection->selectedPresentations();
423 bool isNotAuxiliaryFound = false;
424 if (anObjects.size() > 0) {
425 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
426 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
427 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
428 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
430 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
431 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
432 if (aSketchFeature.get() != NULL) {
433 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
435 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
436 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
437 aSketchFeature->data()->attribute(anAttribute));
439 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
444 theValue = anObjects.size() && !isNotAuxiliaryFound;
448 void PartSet_MenuMgr::onActivatePart(bool)
450 if (myModule->workshop()->currentOperation())
452 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
453 if (aObjects.size() > 0) {
454 ObjectPtr aObj = aObjects.first();
455 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
457 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
458 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
459 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
464 myModule->workshop()->updateCommandStatus();
469 void PartSet_MenuMgr::activatePart(ResultPartPtr thePart) const
471 bool isFirstLoad = !thePart->partDoc().get();
474 XGUI_Workshop* aWorkshop = myModule->getWorkshop();
475 XGUI_ObjectsBrowser* aObjBrowser = aWorkshop->objectBrowser();
476 DocumentPtr aDoc = thePart->partDoc();
477 std::list<bool> aStates;
478 aDoc->restoreNodesState(aStates);
479 aObjBrowser->setStateForDoc(aDoc, aStates);
483 void PartSet_MenuMgr::onActivatePartSet(bool)
485 if (myModule->workshop()->currentOperation())
490 void PartSet_MenuMgr::activatePartSet() const
492 SessionPtr aMgr = ModelAPI_Session::get();
493 bool isNewTransaction = !aMgr->isOperation();
494 // activation may cause changes in current features in document, so it must be in transaction
495 if (isNewTransaction)
496 aMgr->startOperation("Activation");
497 aMgr->setActiveDocument(aMgr->moduleDocument());
498 if (isNewTransaction) aMgr->finishOperation();
500 myModule->workshop()->updateCommandStatus();
503 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
504 QStringList& theIds) const
506 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
507 theIds.append(tr("Detach"));
508 theIds.append(tr("Auxiliary"));
512 void PartSet_MenuMgr::onEdit(bool)
514 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
516 foreach(ObjectPtr aObj, aObjects) {
517 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
521 if (aFeature.get() == NULL) {
522 ResultParameterPtr aParam;
523 foreach(ObjectPtr aObj, aObjects) {
524 aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
528 if (aParam.get() != NULL)
529 aFeature = ModelAPI_Feature::feature(aParam);
531 if (aFeature.get() != NULL)
532 myModule->editFeature(aFeature);
535 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
537 if (theEvent->type() == QEvent::MouseButtonDblClick) {
538 SessionPtr aMgr = ModelAPI_Session::get();
539 if (aMgr->activeDocument() != aMgr->moduleDocument())
542 return QObject::eventFilter(theObj, theEvent);