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>
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 !PartSet_SketcherMgr::isNestedSketchOperation(anOperation,
116 myModule->sketchMgr()->activeSketch()))
119 myCoinsideLines.clear();
120 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
122 bool aIsDetach = false;
123 bool hasAttribute = false;
124 bool hasFeature = false;
126 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
127 if (aPrsList.size() > 1) {
129 } else if (aPrsList.size() == 1) {
132 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
133 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
134 if (aResult.get() != NULL) {
135 const GeomShapePtr& aShape = aPrs->shape();
136 if (aShape.get() && aShape->isEqual(aResult->shape()))
141 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
142 hasFeature = (aFeature.get() != NULL);
146 const GeomShapePtr& aShape = aPrsList.first()->shape();
147 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
148 // Find 2d coordinates
149 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
150 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
151 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
152 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
153 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
154 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
156 // Find coincident in these coordinates
157 ObjectPtr aObj = aPrsList.first()->object();
158 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
159 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
160 // If we have coincidence then add Detach menu
161 if (aCoincident.get() != NULL) {
162 QList<FeaturePtr> aCoins;
163 mySelectedFeature = aCoincident;
164 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
165 SketchPlugin_ConstraintCoincidence::ENTITY_A());
166 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
167 SketchPlugin_ConstraintCoincidence::ENTITY_B());
168 if (myCoinsideLines.size() > 0) {
170 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
171 theMenuActions[anIndex++] = aSubMenu->menuAction();
174 foreach (FeaturePtr aCoins, myCoinsideLines) {
175 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
176 aAction->setData(QVariant(i));
179 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
180 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
181 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
189 if (canSetAuxiliary(isAuxiliary)) {
190 QAction* anAction = action("AUXILIARY_CMD");
191 theMenuActions[anIndex++] = anAction;
192 anAction->setChecked(isAuxiliary);
196 if (!aIsDetach && hasFeature) {
197 // Delete item should be the last in the list of actions
198 theMenuActions[1000] = theStdActions["DELETE_CMD"];
204 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
206 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
208 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
209 PartSet_SketcherMgr::isNestedSketchOperation(anOperation,
210 myModule->sketchMgr()->activeSketch());
211 if (isActiveSketch) {
212 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
213 theStdActions["SHADING_CMD"]->setEnabled(false);
214 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
215 theStdActions["SHOW_CMD"]->setEnabled(false);
216 theStdActions["HIDE_CMD"]->setEnabled(false);
217 theStdActions["HIDEALL_CMD"]->setEnabled(false);
222 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
224 if (myPrevId != -1) {
225 // Restore color for previous object
226 setLineColor(myPrevId, myColor, false);
228 myPrevId = theAction->data().toInt();
229 myColor = setLineColor(myPrevId, Qt::white, true);
232 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
234 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
235 XGUI_Workshop* aWorkshop = aConnector->workshop();
236 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
238 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
239 std::list<ResultPtr>::const_iterator aIt;
240 const std::list<ResultPtr>& aResults = aLine->results();
242 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
243 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
246 aDisplayer->updateViewer();
251 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
252 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
253 QObjectPtrList& theOutList)
255 std::set<AttributePtr>::const_iterator aIt;
256 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
257 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
258 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
259 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
260 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
261 if (aPnt.get() == NULL)
263 gp_Pnt aP = aPnt->impl<gp_Pnt>();
264 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
265 theOutList.append(aConstrFeature);
271 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
273 int aId = theAction->data().toInt();
274 FeaturePtr aLine = myCoinsideLines.at(aId);
275 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
279 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
281 QObjectPtrList aToDelFeatures;
283 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
285 const std::list<ResultPtr>& aResults = aLine->results();
286 std::list<ResultPtr>::const_iterator aResIt;
287 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
288 ResultPtr aResult = (*aResIt);
289 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
290 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
292 if (aToDelFeatures.size() > 0) {
293 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
294 XGUI_Workshop* aWorkshop = aConnector->workshop();
295 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
297 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
298 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
299 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
300 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
301 // the active nested sketch operation should be aborted unconditionally
302 // the Delete action should be additionally granted for the Sketch operation
303 // in order to do not abort/commit it
304 if (!anOpMgr->canStartOperation(tr("Detach")))
305 return; // the objects are processed but can not be deleted
307 anOpMgr->startOperation(anOpAction);
308 aWorkshop->deleteFeatures(aToDelFeatures);
310 anOpMgr->commitOperation();
312 myCoinsideLines.clear();
316 void PartSet_MenuMgr::onDetachMenuHide()
318 if (myPrevId != -1) {
319 // Restore color for previous object
320 setLineColor(myPrevId, myColor, false);
322 // Clear previous definitions
327 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
329 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
331 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
332 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
333 PartSet_SketcherMgr::isNestedSketchOperation(anOperation, aSketch);
337 QObjectPtrList anObjects;
338 bool isUseTransaction = false;
339 // 1. change auxiliary type of a created feature
340 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation, aSketch) &&
341 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
342 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
345 anObjects.append(aFOperation->feature());
348 isUseTransaction = true;
349 // 2. change auxiliary type of selected sketch entities
350 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
351 anObjects = aSelection->selectedPresentations();
354 QAction* anAction = action("AUXILIARY_CMD");
355 //SessionPtr aMgr = ModelAPI_Session::get();
356 ModuleBase_OperationAction* anOpAction = 0;
357 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
358 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
359 if (isUseTransaction) {
360 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
361 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
363 if (!anOpMgr->canStartOperation(anOpAction->id()))
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>(aSketchFeature->data()->attribute(anAttribute));
381 anAuxiliaryAttr->setValue(isChecked);
386 if (isUseTransaction)
387 anOpMgr->commitOperation();
389 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
392 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
394 bool anEnabled = false;
395 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
397 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
398 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
399 PartSet_SketcherMgr::isNestedSketchOperation(anOperation, aSketch);
403 QObjectPtrList anObjects;
404 // 1. change auxiliary type of a created feature
405 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation, aSketch) &&
406 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
407 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
409 anObjects.append(aFOperation->feature());
412 /// The operation should not be aborted here, because the method does not changed
413 /// the auxilliary state, but checks the possibility to perform this
414 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation,
415 // myModule->sketchMgr()->activeSketch()))
416 /// anOperation->abort();
417 // 2. change auxiliary type of selected sketch entities
418 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
419 anObjects = aSelection->selectedPresentations();
422 bool isNotAuxiliaryFound = false;
423 if (anObjects.size() > 0) {
424 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
425 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
426 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
427 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
429 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
430 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
431 if (aSketchFeature.get() != NULL) {
432 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
434 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
435 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
437 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
442 theValue = anObjects.size() && !isNotAuxiliaryFound;
446 void PartSet_MenuMgr::onActivatePart(bool)
448 if (myModule->workshop()->currentOperation())
450 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
451 if (aObjects.size() > 0) {
452 ObjectPtr aObj = aObjects.first();
453 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
455 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
456 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
457 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
462 myModule->workshop()->updateCommandStatus();
466 void PartSet_MenuMgr::onActivatePartSet(bool)
468 if (myModule->workshop()->currentOperation())
473 void PartSet_MenuMgr::activatePartSet() const
475 SessionPtr aMgr = ModelAPI_Session::get();
476 bool isNewTransaction = !aMgr->isOperation();
477 // activation may cause changes in current features in document, so it must be in transaction
478 if (isNewTransaction) aMgr->startOperation("Activation");
479 aMgr->setActiveDocument(aMgr->moduleDocument());
480 if (isNewTransaction) aMgr->finishOperation();
482 myModule->workshop()->updateCommandStatus();
485 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
486 QStringList& theIds) const
488 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
489 theIds.append(tr("Detach"));
490 theIds.append(tr("Auxiliary"));
494 void PartSet_MenuMgr::onEdit(bool)
496 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
497 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
498 if (aFeature == NULL) {
499 ResultParameterPtr aParam =
500 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
501 if (aParam.get() != NULL) {
502 aFeature = ModelAPI_Feature::feature(aParam);
505 if (aFeature.get() != NULL)
506 myModule->editFeature(aFeature);
509 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
511 if (theEvent->type() == QEvent::MouseButtonDblClick) {
512 SessionPtr aMgr = ModelAPI_Session::get();
513 if (aMgr->activeDocument() != aMgr->moduleDocument())
516 return QObject::eventFilter(theObj, theEvent);