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 QList<bool> anIsAttributes;
164 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
165 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
167 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
168 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
170 if (myCoinsideLines.size() > 0) {
172 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
173 theMenuActions[anIndex++] = aSubMenu->menuAction();
176 foreach (FeaturePtr aCoins, myCoinsideLines) {
177 QString anItemText = aCoins->data()->name().c_str();
179 if (anIsAttributes[i])
180 anItemText += " [attribute]";
182 aAction = aSubMenu->addAction(anItemText);
183 aAction->setData(QVariant(i));
186 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
187 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
188 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
196 if (canSetAuxiliary(isAuxiliary)) {
197 QAction* anAction = action("AUXILIARY_CMD");
198 theMenuActions[anIndex++] = anAction;
199 anAction->setChecked(isAuxiliary);
203 if (!aIsDetach && hasFeature) {
204 // Delete item should be the last in the list of actions
205 theMenuActions[1000] = theStdActions["DELETE_CMD"];
211 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
213 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
215 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
216 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
217 if (isActiveSketch) {
218 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
219 theStdActions["SHADING_CMD"]->setEnabled(false);
220 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
221 theStdActions["SHOW_CMD"]->setEnabled(false);
222 theStdActions["HIDE_CMD"]->setEnabled(false);
223 theStdActions["HIDEALL_CMD"]->setEnabled(false);
228 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
230 if (myPrevId != -1) {
231 // Restore color for previous object
232 setLineColor(myPrevId, myColor, false);
234 myPrevId = theAction->data().toInt();
235 myColor = setLineColor(myPrevId, Qt::white, true);
238 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
240 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
241 XGUI_Workshop* aWorkshop = aConnector->workshop();
242 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
244 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
245 std::list<ResultPtr>::const_iterator aIt;
246 const std::list<ResultPtr>& aResults = aLine->results();
248 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
249 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
252 aDisplayer->updateViewer();
257 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
258 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
259 QObjectPtrList& theOutList)
261 std::set<AttributePtr>::const_iterator aIt;
262 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
263 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
264 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
265 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
266 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
267 if (aPnt.get() == NULL)
269 gp_Pnt aP = aPnt->impl<gp_Pnt>();
270 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
271 theOutList.append(aConstrFeature);
277 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
279 int aId = theAction->data().toInt();
280 FeaturePtr aLine = myCoinsideLines.at(aId);
281 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
285 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
287 QObjectPtrList aToDelFeatures;
289 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
291 const std::list<ResultPtr>& aResults = aLine->results();
292 std::list<ResultPtr>::const_iterator aResIt;
293 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
294 ResultPtr aResult = (*aResIt);
295 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
296 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
298 if (aToDelFeatures.size() > 0) {
299 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
300 XGUI_Workshop* aWorkshop = aConnector->workshop();
301 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
303 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
304 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
305 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
306 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
307 // the active nested sketch operation should be aborted unconditionally
308 // the Delete action should be additionally granted for the Sketch operation
309 // in order to do not abort/commit it
311 if (!anOpMgr->canStartOperation(tr("Detach"), isCommitted))
312 return; // the objects are processed but can not be deleted
314 anOpMgr->startOperation(anOpAction);
315 aWorkshop->deleteFeatures(aToDelFeatures);
317 anOpMgr->commitOperation();
319 myCoinsideLines.clear();
323 void PartSet_MenuMgr::onDetachMenuHide()
325 if (myPrevId != -1) {
326 // Restore color for previous object
327 setLineColor(myPrevId, myColor, false);
329 // Clear previous definitions
334 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
336 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
338 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
339 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
340 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
344 QObjectPtrList anObjects;
345 bool isUseTransaction = false;
346 // 1. change auxiliary type of a created feature
347 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
348 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
349 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
352 anObjects.append(aFOperation->feature());
355 isUseTransaction = true;
356 // 2. change auxiliary type of selected sketch entities
357 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
358 anObjects = aSelection->selectedPresentations();
361 QAction* anAction = action("AUXILIARY_CMD");
362 //SessionPtr aMgr = ModelAPI_Session::get();
363 ModuleBase_OperationAction* anOpAction = 0;
364 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
365 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
366 if (isUseTransaction) {
367 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
368 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
371 if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
372 return; // the objects are processed but can not be deleted
374 anOpMgr->startOperation(anOpAction);
376 if (anObjects.size() > 0) {
377 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
378 for (; anIt != aLast; anIt++) {
379 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
380 if (aFeature.get() != NULL) {
381 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
382 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
383 if (aSketchFeature.get() != NULL) {
384 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
386 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
387 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
388 aSketchFeature->data()->attribute(anAttribute));
390 anAuxiliaryAttr->setValue(isChecked);
395 if (isUseTransaction)
396 anOpMgr->commitOperation();
398 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
401 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
403 bool anEnabled = false;
404 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
406 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
407 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
408 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
412 QObjectPtrList anObjects;
413 // 1. change auxiliary type of a created feature
414 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
415 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
416 ModuleBase_OperationFeature* aFOperation =
417 dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
419 anObjects.append(aFOperation->feature());
422 /// The operation should not be aborted here, because the method does not changed
423 /// the auxilliary state, but checks the possibility to perform this
424 ///if (myModule->sketchMgr()->isNestedSketchOperation(anOperation))
425 /// anOperation->abort();
426 // 2. change auxiliary type of selected sketch entities
427 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
428 anObjects = aSelection->selectedPresentations();
431 bool isNotAuxiliaryFound = false;
432 if (anObjects.size() > 0) {
433 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
434 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
435 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
436 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
438 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
439 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
440 if (aSketchFeature.get() != NULL) {
441 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
443 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
444 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
445 aSketchFeature->data()->attribute(anAttribute));
447 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
452 theValue = anObjects.size() && !isNotAuxiliaryFound;
456 void PartSet_MenuMgr::onActivatePart(bool)
458 if (myModule->workshop()->currentOperation())
460 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
461 if (aObjects.size() > 0) {
462 ObjectPtr aObj = aObjects.first();
463 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
465 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
466 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
467 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
472 myModule->workshop()->updateCommandStatus();
477 void PartSet_MenuMgr::activatePart(ResultPartPtr thePart) const
479 bool isFirstLoad = !thePart->partDoc().get();
482 XGUI_Workshop* aWorkshop = myModule->getWorkshop();
483 XGUI_ObjectsBrowser* aObjBrowser = aWorkshop->objectBrowser();
484 DocumentPtr aDoc = thePart->partDoc();
485 std::list<bool> aStates;
486 aDoc->restoreNodesState(aStates);
487 aObjBrowser->setStateForDoc(aDoc, aStates);
491 void PartSet_MenuMgr::onActivatePartSet(bool)
493 if (myModule->workshop()->currentOperation())
498 void PartSet_MenuMgr::activatePartSet() const
500 SessionPtr aMgr = ModelAPI_Session::get();
501 bool isNewTransaction = !aMgr->isOperation();
502 // activation may cause changes in current features in document, so it must be in transaction
503 if (isNewTransaction)
504 aMgr->startOperation("Activation");
505 aMgr->setActiveDocument(aMgr->moduleDocument());
506 if (isNewTransaction) aMgr->finishOperation();
508 myModule->workshop()->updateCommandStatus();
511 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
512 QStringList& theIds) const
514 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
515 theIds.append(tr("Detach"));
516 theIds.append(tr("Auxiliary"));
520 void PartSet_MenuMgr::onEdit(bool)
522 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
524 foreach(ObjectPtr aObj, aObjects) {
525 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
529 if (aFeature.get() == NULL) {
530 ResultParameterPtr aParam;
531 foreach(ObjectPtr aObj, aObjects) {
532 aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
536 if (aParam.get() != NULL)
537 aFeature = ModelAPI_Feature::feature(aParam);
539 if (aFeature.get() != NULL)
540 myModule->editFeature(aFeature);
543 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
545 if (theEvent->type() == QEvent::MouseButtonDblClick) {
546 SessionPtr aMgr = ModelAPI_Session::get();
547 if (aMgr->activeDocument() != aMgr->moduleDocument())
550 return QObject::eventFilter(theObj, theEvent);