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>
29 #include <XGUI_ModuleConnector.h>
30 #include <XGUI_Workshop.h>
31 #include <XGUI_Displayer.h>
32 #include <XGUI_DataModel.h>
33 #include <XGUI_OperationMgr.h>
35 #include <Events_Loop.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_ResultPart.h>
39 #include <ModelAPI_ResultParameter.h>
46 #include <BRep_Tool.hxx>
48 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
49 : QObject(theModule), myModule(theModule), myPrevId(-1)
55 QAction* PartSet_MenuMgr::action(const QString& theId) const
57 if (myActions.contains(theId))
58 return myActions[theId];
62 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
64 if (myActions.contains(theId))
65 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
66 theAction->setData(theId);
67 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
68 myActions[theId] = theAction;
71 void PartSet_MenuMgr::createActions()
75 aAction = new QAction(tr("Auxiliary"), this);
76 aAction->setCheckable(true);
77 addAction("AUXILIARY_CMD", aAction);
79 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
80 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePart(bool)));
81 myActions["ACTIVATE_PART_CMD"] = aAction;
84 aAction = new QAction(QIcon(":icons/activate.png"), tr("Activate"), this);
85 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onActivatePartSet(bool)));
86 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
88 aAction = new QAction(QIcon(":icons/edit.png"), tr("Edit..."), this);
89 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(onEdit(bool)));
90 myActions["EDIT_CMD"] = aAction;
94 void PartSet_MenuMgr::onAction(bool isChecked)
96 QAction* aAction = static_cast<QAction*>(sender());
97 QString anId = aAction->data().toString();
99 if (anId == "AUXILIARY_CMD") {
100 setAuxiliary(isChecked);
104 bool PartSet_MenuMgr::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
106 QMap<int, QAction*>& theMenuActions) 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_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
123 if (aPrsList.size() > 1) {
125 } else if (aPrsList.size() == 1) {
128 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
129 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
130 if (aResult.get() != NULL) {
131 const GeomShapePtr& aShape = aPrs->shape();
132 if (aShape.get() && aShape->isEqual(aResult->shape()))
137 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
138 hasFeature = (aFeature.get() != NULL);
142 const GeomShapePtr& aShape = aPrsList.first()->shape();
143 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
144 // Find 2d coordinates
145 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
146 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
147 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
148 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
149 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
150 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
152 // Find coincident in these coordinates
153 ObjectPtr aObj = aPrsList.first()->object();
154 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
155 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
156 // If we have coincidence then add Detach menu
157 if (aCoincident.get() != NULL) {
158 QList<FeaturePtr> aCoins;
159 mySelectedFeature = aCoincident;
160 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
161 SketchPlugin_ConstraintCoincidence::ENTITY_A());
162 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
163 SketchPlugin_ConstraintCoincidence::ENTITY_B());
164 if (myCoinsideLines.size() > 0) {
166 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
167 theMenuActions[anIndex++] = aSubMenu->menuAction();
170 foreach (FeaturePtr aCoins, myCoinsideLines) {
171 aAction = aSubMenu->addAction(aCoins->data()->name().c_str());
172 aAction->setData(QVariant(i));
175 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
176 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
177 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
185 if (canSetAuxiliary(isAuxiliary)) {
186 QAction* anAction = action("AUXILIARY_CMD");
187 theMenuActions[anIndex++] = anAction;
188 anAction->setChecked(isAuxiliary);
192 if (!aIsDetach && hasFeature) {
193 // Delete item should be the last in the list of actions
194 theMenuActions[1000] = theStdActions["DELETE_CMD"];
200 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
202 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
204 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
205 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
206 if (isActiveSketch) {
207 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
208 theStdActions["SHADING_CMD"]->setEnabled(false);
209 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
210 theStdActions["SHOW_CMD"]->setEnabled(false);
211 theStdActions["HIDE_CMD"]->setEnabled(false);
212 theStdActions["HIDEALL_CMD"]->setEnabled(false);
217 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
219 if (myPrevId != -1) {
220 // Restore color for previous object
221 setLineColor(myPrevId, myColor, false);
223 myPrevId = theAction->data().toInt();
224 myColor = setLineColor(myPrevId, Qt::white, true);
227 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
229 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
230 XGUI_Workshop* aWorkshop = aConnector->workshop();
231 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
233 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
234 std::list<ResultPtr>::const_iterator aIt;
235 const std::list<ResultPtr>& aResults = aLine->results();
237 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
238 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
241 aDisplayer->updateViewer();
246 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
247 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
248 QObjectPtrList& theOutList)
250 std::set<AttributePtr>::const_iterator aIt;
251 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
252 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
253 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
254 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
255 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
256 if (aPnt.get() == NULL)
258 gp_Pnt aP = aPnt->impl<gp_Pnt>();
259 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
260 theOutList.append(aConstrFeature);
266 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
268 int aId = theAction->data().toInt();
269 FeaturePtr aLine = myCoinsideLines.at(aId);
270 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
274 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
276 QObjectPtrList aToDelFeatures;
278 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
280 const std::list<ResultPtr>& aResults = aLine->results();
281 std::list<ResultPtr>::const_iterator aResIt;
282 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
283 ResultPtr aResult = (*aResIt);
284 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
285 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
287 if (aToDelFeatures.size() > 0) {
288 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
289 XGUI_Workshop* aWorkshop = aConnector->workshop();
290 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
292 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
293 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
294 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
295 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
296 // the active nested sketch operation should be aborted unconditionally
297 // the Delete action should be additionally granted for the Sketch operation
298 // in order to do not abort/commit it
299 if (!anOpMgr->canStartOperation(tr("Detach")))
300 return; // the objects are processed but can not be deleted
302 anOpMgr->startOperation(anOpAction);
303 aWorkshop->deleteFeatures(aToDelFeatures);
305 anOpMgr->commitOperation();
307 myCoinsideLines.clear();
311 void PartSet_MenuMgr::onDetachMenuHide()
313 if (myPrevId != -1) {
314 // Restore color for previous object
315 setLineColor(myPrevId, myColor, false);
317 // Clear previous definitions
322 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
324 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
326 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
327 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
331 QObjectPtrList anObjects;
332 bool isUseTransaction = false;
333 // 1. change auxiliary type of a created feature
334 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
335 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
336 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
339 anObjects.append(aFOperation->feature());
342 isUseTransaction = true;
343 // 2. change auxiliary type of selected sketch entities
344 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
345 anObjects = aSelection->selectedPresentations();
348 QAction* anAction = action("AUXILIARY_CMD");
349 //SessionPtr aMgr = ModelAPI_Session::get();
350 ModuleBase_OperationAction* anOpAction = 0;
351 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
352 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
353 if (isUseTransaction) {
354 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
355 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
357 if (!anOpMgr->canStartOperation(anOpAction->id()))
358 return; // the objects are processed but can not be deleted
360 anOpMgr->startOperation(anOpAction);
362 if (anObjects.size() > 0) {
363 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
364 for (; anIt != aLast; anIt++) {
365 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
366 if (aFeature.get() != NULL) {
367 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
368 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
369 if (aSketchFeature.get() != NULL) {
370 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
372 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
373 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
375 anAuxiliaryAttr->setValue(isChecked);
380 if (isUseTransaction)
381 anOpMgr->commitOperation();
383 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
386 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
388 bool anEnabled = false;
389 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
391 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
392 PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
396 QObjectPtrList anObjects;
397 // 1. change auxiliary type of a created feature
398 if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
399 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
400 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
402 anObjects.append(aFOperation->feature());
405 /// The operation should not be aborted here, because the method does not changed
406 /// the auxilliary state, but checks the possibility to perform this
407 ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
408 /// anOperation->abort();
409 // 2. change auxiliary type of selected sketch entities
410 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
411 anObjects = aSelection->selectedPresentations();
414 bool isNotAuxiliaryFound = false;
415 if (anObjects.size() > 0) {
416 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
417 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
418 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
419 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
421 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
422 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
423 if (aSketchFeature.get() != NULL) {
424 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
426 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
427 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
429 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
434 theValue = anObjects.size() && !isNotAuxiliaryFound;
438 void PartSet_MenuMgr::onActivatePart(bool)
440 if (myModule->workshop()->currentOperation())
442 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
443 if (aObjects.size() > 0) {
444 ObjectPtr aObj = aObjects.first();
445 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
447 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
448 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
449 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
454 myModule->workshop()->updateCommandStatus();
458 void PartSet_MenuMgr::onActivatePartSet(bool)
460 if (myModule->workshop()->currentOperation())
465 void PartSet_MenuMgr::activatePartSet() const
467 SessionPtr aMgr = ModelAPI_Session::get();
468 bool isNewTransaction = !aMgr->isOperation();
469 // activation may cause changes in current features in document, so it must be in transaction
470 if (isNewTransaction) aMgr->startOperation("Activation");
471 aMgr->setActiveDocument(aMgr->moduleDocument());
472 if (isNewTransaction) aMgr->finishOperation();
474 myModule->workshop()->updateCommandStatus();
477 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
478 QStringList& theIds) const
480 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
481 theIds.append(tr("Detach"));
482 theIds.append(tr("Auxiliary"));
486 void PartSet_MenuMgr::onEdit(bool)
488 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
489 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjects.first());
490 if (aFeature == NULL) {
491 ResultParameterPtr aParam =
492 std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObjects.first());
493 if (aParam.get() != NULL) {
494 aFeature = ModelAPI_Feature::feature(aParam);
497 if (aFeature.get() != NULL)
498 myModule->editFeature(aFeature);
501 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
503 if (theEvent->type() == QEvent::MouseButtonDblClick) {
504 SessionPtr aMgr = ModelAPI_Session::get();
505 if (aMgr->activeDocument() != aMgr->moduleDocument())
508 return QObject::eventFilter(theObj, theEvent);