1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PartSet_MenuMgr.h"
21 #include "PartSet_Module.h"
22 #include "PartSet_SketcherMgr.h"
23 #include "PartSet_Tools.h"
25 #include <PartSetPlugin_Part.h>
27 #include <GeomAPI_Pnt2d.h>
28 #include <GeomDataAPI_Point2D.h>
30 #include <SketchPlugin_ConstraintCoincidence.h>
31 #include <SketchPlugin_Line.h>
32 #include <SketchPlugin_Circle.h>
33 #include <SketchPlugin_Point.h>
34 #include <SketchPlugin_Sketch.h>
36 #include <ModuleBase_ISelection.h>
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_OperationAction.h>
39 #include <ModuleBase_OperationFeature.h>
40 #include <ModuleBase_ViewerPrs.h>
41 #include <ModuleBase_Tools.h>
43 #include <XGUI_ModuleConnector.h>
44 #include <XGUI_Workshop.h>
45 #include <XGUI_Displayer.h>
46 #include <XGUI_DataModel.h>
47 #include <XGUI_OperationMgr.h>
48 #include <XGUI_ObjectsBrowser.h>
50 #include <Events_Loop.h>
51 #include <ModelAPI_Events.h>
52 #include <ModelAPI_Session.h>
53 #include <ModelAPI_ResultParameter.h>
55 #include <QMainWindow>
61 #include <BRep_Tool.hxx>
63 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
64 : QObject(theModule), myModule(theModule), myPrevId(-1)
70 QAction* PartSet_MenuMgr::action(const QString& theId) const
72 if (myActions.contains(theId))
73 return myActions[theId];
77 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
79 if (myActions.contains(theId))
80 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
81 theAction->setData(theId);
82 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
83 myActions[theId] = theAction;
86 void PartSet_MenuMgr::createActions()
90 QWidget* aParent = myModule->workshop()->desktop();
91 aAction = ModuleBase_Tools::createAction(QIcon(), tr("Auxiliary"), aParent);
92 aAction->setCheckable(true);
93 addAction("AUXILIARY_CMD", aAction);
95 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
96 this, SLOT(onActivatePart(bool)));
97 myActions["ACTIVATE_PART_CMD"] = aAction;
100 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
101 this, SLOT(onActivatePartSet(bool)));
102 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
104 aAction = ModuleBase_Tools::createAction(QIcon(":icons/edit.png"), tr("Edit..."), aParent,
105 this, SLOT(onEdit(bool)));
106 myActions["EDIT_CMD"] = aAction;
110 void PartSet_MenuMgr::onAction(bool isChecked)
112 QAction* aAction = static_cast<QAction*>(sender());
113 QString anId = aAction->data().toString();
115 if (anId == "AUXILIARY_CMD") {
116 setAuxiliary(isChecked);
120 bool PartSet_MenuMgr::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
122 QMap<int, QAction*>& theMenuActions) const
126 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
127 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
128 !myModule->sketchMgr()->isNestedSketchOperation(anOperation))
131 myCoinsideLines.clear();
132 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
134 bool aIsDetach = false;
135 bool hasAttribute = false;
136 bool hasFeature = false;
138 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
139 if (aPrsList.size() > 1) {
141 } else if (aPrsList.size() == 1) {
144 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
145 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
146 if (aResult.get() != NULL) {
147 const GeomShapePtr& aShape = aPrs->shape();
148 if (aShape.get() && aShape->isEqual(aResult->shape()))
153 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
154 hasFeature = (aFeature.get() != NULL);
158 const GeomShapePtr& aShape = aPrsList.first()->shape();
159 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
160 // Find 2d coordinates
161 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
162 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
163 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
164 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
165 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
166 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
168 // Find coincident in these coordinates
169 ObjectPtr aObj = aPrsList.first()->object();
170 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
171 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
172 // If we have coincidence then add Detach menu
173 if (aCoincident.get() != NULL) {
174 QList<FeaturePtr> aCoins;
175 mySelectedFeature = aCoincident;
176 QList<bool> anIsAttributes;
177 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
178 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
180 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
181 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
183 if (myCoinsideLines.size() > 0) {
185 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
186 theMenuActions[anIndex++] = aSubMenu->menuAction();
189 foreach (FeaturePtr aCoins, myCoinsideLines) {
190 QString anItemText = aCoins->data()->name().c_str();
192 if (anIsAttributes[i])
193 anItemText += " [attribute]";
195 aAction = aSubMenu->addAction(anItemText);
196 aAction->setData(QVariant(i));
199 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
200 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
201 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
209 if (canSetAuxiliary(isAuxiliary)) {
210 QAction* anAction = action("AUXILIARY_CMD");
211 theMenuActions[anIndex++] = anAction;
212 anAction->setChecked(isAuxiliary);
216 if (!aIsDetach && hasFeature) {
217 // Delete item should be the last in the list of actions
218 theMenuActions[1000] = theStdActions["DELETE_CMD"];
224 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
226 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
228 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
229 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
230 if (isActiveSketch) {
231 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
232 theStdActions["SHADING_CMD"]->setEnabled(false);
233 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
234 theStdActions["SHOW_CMD"]->setEnabled(false);
235 theStdActions["HIDE_CMD"]->setEnabled(false);
236 theStdActions["HIDEALL_CMD"]->setEnabled(false);
241 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
243 if (myPrevId != -1) {
244 // Restore color for previous object
245 setLineColor(myPrevId, myColor, false);
247 myPrevId = theAction->data().toInt();
248 myColor = setLineColor(myPrevId, Qt::white, true);
251 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
253 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
254 XGUI_Workshop* aWorkshop = aConnector->workshop();
255 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
257 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
258 std::list<ResultPtr>::const_iterator aIt;
259 const std::list<ResultPtr>& aResults = aLine->results();
261 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
262 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
265 aDisplayer->updateViewer();
270 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
271 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
272 QObjectPtrList& theOutList)
274 std::set<AttributePtr>::const_iterator aIt;
275 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
276 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
277 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
278 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
279 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
280 if (aPnt.get() == NULL)
282 gp_Pnt aP = aPnt->impl<gp_Pnt>();
283 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
284 theOutList.append(aConstrFeature);
290 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
292 int aId = theAction->data().toInt();
293 FeaturePtr aLine = myCoinsideLines.at(aId);
294 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
298 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
300 QObjectPtrList aToDelFeatures;
302 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
304 const std::list<ResultPtr>& aResults = aLine->results();
305 std::list<ResultPtr>::const_iterator aResIt;
306 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
307 ResultPtr aResult = (*aResIt);
308 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
309 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
311 if (aToDelFeatures.size() > 0) {
312 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
313 XGUI_Workshop* aWorkshop = aConnector->workshop();
314 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
316 ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(
317 tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
318 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
319 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
320 // the active nested sketch operation should be aborted unconditionally
321 // the Delete action should be additionally granted for the Sketch operation
322 // in order to do not abort/commit it
324 if (!anOpMgr->canStartOperation(tr("Detach"), isCommitted))
325 return; // the objects are processed but can not be deleted
327 anOpMgr->startOperation(anOpAction);
328 aWorkshop->deleteFeatures(aToDelFeatures);
330 anOpMgr->commitOperation();
332 myCoinsideLines.clear();
336 void PartSet_MenuMgr::onDetachMenuHide()
338 if (myPrevId != -1) {
339 // Restore color for previous object
340 setLineColor(myPrevId, myColor, false);
342 // Clear previous definitions
347 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
349 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
351 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
352 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
353 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
357 QObjectPtrList anObjects;
358 bool isUseTransaction = false;
359 // 1. change auxiliary type of a created feature
360 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
361 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
362 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
365 anObjects.append(aFOperation->feature());
368 isUseTransaction = true;
369 // 2. change auxiliary type of selected sketch entities
370 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
371 anObjects = aSelection->selectedPresentations();
374 QAction* anAction = action("AUXILIARY_CMD");
375 //SessionPtr aMgr = ModelAPI_Session::get();
376 ModuleBase_OperationAction* anOpAction = 0;
377 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
378 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
379 if (isUseTransaction) {
380 anOpAction = new ModuleBase_OperationAction(anAction->text(), myModule);
381 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
384 if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
385 return; // the objects are processed but can not be deleted
387 anOpMgr->startOperation(anOpAction);
389 if (anObjects.size() > 0) {
390 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
391 for (; anIt != aLast; anIt++) {
392 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
393 if (aFeature.get() != NULL) {
394 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
395 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
396 if (aSketchFeature.get() != NULL) {
397 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
399 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
400 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
401 aSketchFeature->data()->attribute(anAttribute));
403 anAuxiliaryAttr->setValue(isChecked);
408 if (isUseTransaction)
409 anOpMgr->commitOperation();
411 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
414 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
416 bool anEnabled = false;
417 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
419 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
420 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
421 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
425 QObjectPtrList anObjects;
426 // 1. change auxiliary type of a created feature
427 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
428 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
429 ModuleBase_OperationFeature* aFOperation =
430 dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
432 anObjects.append(aFOperation->feature());
435 /// The operation should not be aborted here, because the method does not changed
436 /// the auxilliary state, but checks the possibility to perform this
437 ///if (myModule->sketchMgr()->isNestedSketchOperation(anOperation))
438 /// anOperation->abort();
439 // 2. change auxiliary type of selected sketch entities
440 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
441 anObjects = aSelection->selectedPresentations();
444 bool isNotAuxiliaryFound = false;
445 if (anObjects.size() > 0) {
446 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
447 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
448 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
449 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
451 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
452 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
453 if (aSketchFeature.get() != NULL) {
454 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
456 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
457 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
458 aSketchFeature->data()->attribute(anAttribute));
460 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
465 theValue = anObjects.size() && !isNotAuxiliaryFound;
469 void PartSet_MenuMgr::onActivatePart(bool)
471 if (myModule->workshop()->currentOperation())
473 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
474 if (aObjects.size() > 0) {
475 ObjectPtr aObj = aObjects.first();
476 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
478 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
479 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
480 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
485 myModule->workshop()->updateCommandStatus();
490 void PartSet_MenuMgr::activatePart(ResultPartPtr thePart) const
492 bool isFirstLoad = !thePart->partDoc().get();
495 XGUI_Workshop* aWorkshop = myModule->getWorkshop();
496 XGUI_ObjectsBrowser* aObjBrowser = aWorkshop->objectBrowser();
497 DocumentPtr aDoc = thePart->partDoc();
498 std::list<bool> aStates;
499 aDoc->restoreNodesState(aStates);
500 aObjBrowser->setStateForDoc(aDoc, aStates);
504 void PartSet_MenuMgr::onActivatePartSet(bool)
506 if (myModule->workshop()->currentOperation())
511 void PartSet_MenuMgr::activatePartSet() const
513 SessionPtr aMgr = ModelAPI_Session::get();
514 bool isNewTransaction = !aMgr->isOperation();
515 // activation may cause changes in current features in document, so it must be in transaction
516 if (isNewTransaction)
517 aMgr->startOperation("Activation");
518 aMgr->setActiveDocument(aMgr->moduleDocument());
519 if (isNewTransaction)
520 aMgr->finishOperation();
522 myModule->workshop()->updateCommandStatus();
525 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
526 QStringList& theIds) const
528 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
529 theIds.append(tr("Detach"));
530 theIds.append(tr("Auxiliary"));
534 void PartSet_MenuMgr::onEdit(bool)
536 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
538 foreach(ObjectPtr aObj, aObjects) {
539 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
543 if (aFeature.get() == NULL) {
544 ResultParameterPtr aParam;
545 foreach(ObjectPtr aObj, aObjects) {
546 aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
550 if (aParam.get() != NULL)
551 aFeature = ModelAPI_Feature::feature(aParam);
553 if (aFeature.get() != NULL)
554 myModule->editFeature(aFeature);
557 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
559 if (theEvent->type() == QEvent::MouseButtonDblClick) {
560 SessionPtr aMgr = ModelAPI_Session::get();
561 if (aMgr->activeDocument() != aMgr->moduleDocument())
564 return QObject::eventFilter(theObj, theEvent);