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_OperationFeature.h>
39 #include <ModuleBase_ViewerPrs.h>
40 #include <ModuleBase_Tools.h>
42 #include <XGUI_ModuleConnector.h>
43 #include <XGUI_Workshop.h>
44 #include <XGUI_Displayer.h>
45 #include <XGUI_DataModel.h>
46 #include <XGUI_OperationMgr.h>
47 #include <XGUI_ObjectsBrowser.h>
49 #include <Events_Loop.h>
50 #include <ModelAPI_Events.h>
51 #include <ModelAPI_Session.h>
52 #include <ModelAPI_ResultParameter.h>
54 #include <QMainWindow>
60 #include <BRep_Tool.hxx>
62 PartSet_MenuMgr::PartSet_MenuMgr(PartSet_Module* theModule)
63 : QObject(theModule), myModule(theModule), myPrevId(-1)
69 QAction* PartSet_MenuMgr::action(const QString& theId) const
71 if (myActions.contains(theId))
72 return myActions[theId];
76 void PartSet_MenuMgr::addAction(const QString& theId, QAction* theAction)
78 if (myActions.contains(theId))
79 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
80 theAction->setData(theId);
81 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
82 myActions[theId] = theAction;
85 void PartSet_MenuMgr::createActions()
89 QWidget* aParent = myModule->workshop()->desktop();
90 aAction = ModuleBase_Tools::createAction(QIcon(), tr("Auxiliary"), aParent);
91 aAction->setCheckable(true);
92 addAction("AUXILIARY_CMD", aAction);
94 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
95 this, SLOT(onActivatePart(bool)));
96 myActions["ACTIVATE_PART_CMD"] = aAction;
99 aAction = ModuleBase_Tools::createAction(QIcon(":icons/activate.png"), tr("Activate"), aParent,
100 this, SLOT(onActivatePartSet(bool)));
101 myActions["ACTIVATE_PARTSET_CMD"] = aAction;
103 aAction = ModuleBase_Tools::createAction(QIcon(":icons/edit.png"), tr("Edit..."), aParent,
104 this, SLOT(onEdit(bool)));
105 myActions["EDIT_CMD"] = aAction;
109 void PartSet_MenuMgr::onAction(bool isChecked)
111 QAction* aAction = static_cast<QAction*>(sender());
112 QString anId = aAction->data().toString();
114 if (anId == "AUXILIARY_CMD") {
115 setAuxiliary(isChecked);
119 bool PartSet_MenuMgr::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
121 QMap<int, QAction*>& theMenuActions) const
125 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
126 if (!PartSet_SketcherMgr::isSketchOperation(anOperation) &&
127 !myModule->sketchMgr()->isNestedSketchOperation(anOperation))
130 myCoinsideLines.clear();
131 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
133 bool aIsDetach = false;
134 bool hasAttribute = false;
135 bool hasFeature = false;
137 QList<ModuleBase_ViewerPrsPtr> aPrsList = aSelection->getSelected(ModuleBase_ISelection::Viewer);
138 if (aPrsList.size() > 1) {
140 } else if (aPrsList.size() == 1) {
143 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
144 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs->object());
145 if (aResult.get() != NULL) {
146 const GeomShapePtr& aShape = aPrs->shape();
147 if (aShape.get() && aShape->isEqual(aResult->shape()))
152 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aPrs->object());
153 hasFeature = (aFeature.get() != NULL);
157 const GeomShapePtr& aShape = aPrsList.first()->shape();
158 if (aShape.get() && !aShape->isNull() && aShape->shapeType() == GeomAPI_Shape::VERTEX) {
159 // Find 2d coordinates
160 FeaturePtr aSketchFea = myModule->sketchMgr()->activeSketch();
161 if (aSketchFea->getKind() == SketchPlugin_Sketch::ID()) {
162 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
163 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aTDShape));
164 std::shared_ptr<GeomAPI_Pnt> aPnt3d(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
165 std::shared_ptr<GeomAPI_Pnt2d> aSelPnt = PartSet_Tools::convertTo2D(aSketchFea, aPnt3d);
167 // Find coincident in these coordinates
168 ObjectPtr aObj = aPrsList.first()->object();
169 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
170 FeaturePtr aCoincident = PartSet_Tools::findFirstCoincidence(aFeature, aSelPnt);
171 // If we have coincidence then add Detach menu
172 if (aCoincident.get() != NULL) {
173 QList<FeaturePtr> aCoins;
174 mySelectedFeature = aCoincident;
175 QList<bool> anIsAttributes;
176 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
177 SketchPlugin_ConstraintCoincidence::ENTITY_A(),
179 PartSet_Tools::findCoincidences(mySelectedFeature, myCoinsideLines, aCoins,
180 SketchPlugin_ConstraintCoincidence::ENTITY_B(),
182 if (myCoinsideLines.size() > 0) {
184 QMenu* aSubMenu = new QMenu(tr("Detach"), theParent);
185 theMenuActions[anIndex++] = aSubMenu->menuAction();
188 foreach (FeaturePtr aCoins, myCoinsideLines) {
189 QString anItemText = aCoins->data()->name().c_str();
191 if (anIsAttributes[i])
192 anItemText += " [attribute]";
194 aAction = aSubMenu->addAction(anItemText);
195 aAction->setData(QVariant(i));
198 connect(aSubMenu, SIGNAL(hovered(QAction*)), SLOT(onLineHighlighted(QAction*)));
199 connect(aSubMenu, SIGNAL(aboutToHide()), SLOT(onDetachMenuHide()));
200 connect(aSubMenu, SIGNAL(triggered(QAction*)), SLOT(onLineDetach(QAction*)));
208 if (canSetAuxiliary(isAuxiliary)) {
209 QAction* anAction = action("AUXILIARY_CMD");
210 theMenuActions[anIndex++] = anAction;
211 anAction->setChecked(isAuxiliary);
215 if (!aIsDetach && hasFeature) {
216 // Delete item should be the last in the list of actions
217 theMenuActions[1000] = theStdActions["DELETE_CMD"];
223 void PartSet_MenuMgr::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
225 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
227 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
228 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
229 if (isActiveSketch) {
230 theStdActions["WIREFRAME_CMD"]->setEnabled(false);
231 theStdActions["SHADING_CMD"]->setEnabled(false);
232 theStdActions["SHOW_ONLY_CMD"]->setEnabled(false);
233 theStdActions["SHOW_CMD"]->setEnabled(false);
234 theStdActions["HIDE_CMD"]->setEnabled(false);
235 theStdActions["HIDEALL_CMD"]->setEnabled(false);
240 void PartSet_MenuMgr::onLineHighlighted(QAction* theAction)
242 if (myPrevId != -1) {
243 // Restore color for previous object
244 setLineColor(myPrevId, myColor, false);
246 myPrevId = theAction->data().toInt();
247 myColor = setLineColor(myPrevId, Qt::white, true);
250 QColor PartSet_MenuMgr::setLineColor(int theId, const QColor theColor, bool theUpdate)
252 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
253 XGUI_Workshop* aWorkshop = aConnector->workshop();
254 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
256 FeaturePtr aLine = myCoinsideLines.at(myPrevId);
257 std::list<ResultPtr>::const_iterator aIt;
258 const std::list<ResultPtr>& aResults = aLine->results();
260 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
261 aColor = aDisplayer->setObjectColor((*aIt), theColor, false);
264 aDisplayer->updateViewer();
269 void addRefCoincidentFeatures(const std::set<AttributePtr>& theRefList,
270 std::shared_ptr<GeomAPI_Pnt2d>& theRefPnt,
271 QObjectPtrList& theOutList)
273 std::set<AttributePtr>::const_iterator aIt;
274 for (aIt = theRefList.cbegin(); aIt != theRefList.cend(); ++aIt) {
275 std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
276 FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
277 if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
278 std::shared_ptr<GeomAPI_Pnt2d> aPnt = PartSet_Tools::getCoincedencePoint(aConstrFeature);
279 if (aPnt.get() == NULL)
281 gp_Pnt aP = aPnt->impl<gp_Pnt>();
282 if (theRefPnt->isEqual(aPnt) && (!theOutList.contains(aConstrFeature))) {
283 theOutList.append(aConstrFeature);
289 void PartSet_MenuMgr::onLineDetach(QAction* theAction)
291 int aId = theAction->data().toInt();
292 FeaturePtr aLine = myCoinsideLines.at(aId);
293 std::shared_ptr<GeomAPI_Pnt2d> aOrig = PartSet_Tools::getCoincedencePoint(mySelectedFeature);
297 const std::set<AttributePtr>& aRefsList = aLine->data()->refsToMe();
299 QObjectPtrList aToDelFeatures;
301 addRefCoincidentFeatures(aRefsList, aOrig, aToDelFeatures);
303 const std::list<ResultPtr>& aResults = aLine->results();
304 std::list<ResultPtr>::const_iterator aResIt;
305 for (aResIt = aResults.cbegin(); aResIt != aResults.cend(); aResIt++) {
306 ResultPtr aResult = (*aResIt);
307 const std::set<AttributePtr>& aRefList = aResult->data()->refsToMe();
308 addRefCoincidentFeatures(aRefList, aOrig, aToDelFeatures);
310 if (aToDelFeatures.size() > 0) {
311 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
312 XGUI_Workshop* aWorkshop = aConnector->workshop();
313 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
315 ModuleBase_Operation* anOpAction =
316 new ModuleBase_Operation(tr("Detach %1").arg(aLine->data()->name().c_str()), myModule);
317 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
318 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
319 // the active nested sketch operation should be aborted unconditionally
320 // the Delete action should be additionally granted for the Sketch operation
321 // in order to do not abort/commit it
323 if (!anOpMgr->canStartOperation(tr("Detach"), isCommitted))
324 return; // the objects are processed but can not be deleted
326 anOpMgr->startOperation(anOpAction);
327 aWorkshop->deleteFeatures(aToDelFeatures);
329 anOpMgr->commitOperation();
331 myCoinsideLines.clear();
335 void PartSet_MenuMgr::onDetachMenuHide()
337 if (myPrevId != -1) {
338 // Restore color for previous object
339 setLineColor(myPrevId, myColor, false);
341 // Clear previous definitions
346 void PartSet_MenuMgr::setAuxiliary(const bool isChecked)
348 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
350 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
351 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
352 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
356 QObjectPtrList anObjects;
357 bool isUseTransaction = false;
358 // 1. change auxiliary type of a created feature
359 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
360 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
361 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
364 anObjects.append(aFOperation->feature());
367 isUseTransaction = true;
368 // 2. change auxiliary type of selected sketch entities
369 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
370 anObjects = aSelection->selectedPresentations();
373 QAction* anAction = action("AUXILIARY_CMD");
374 //SessionPtr aMgr = ModelAPI_Session::get();
375 ModuleBase_Operation* anOpAction = 0;
376 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
377 XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
378 if (isUseTransaction) {
379 anOpAction = new ModuleBase_Operation(anAction->text(), myModule);
380 bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation);
383 if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
384 return; // the objects are processed but can not be deleted
386 anOpMgr->startOperation(anOpAction);
388 if (anObjects.size() > 0) {
389 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
390 for (; anIt != aLast; anIt++) {
391 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
392 if (aFeature.get() != NULL) {
393 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
394 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
395 if (aSketchFeature.get() != NULL) {
396 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
398 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
399 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
400 aSketchFeature->data()->attribute(anAttribute));
402 anAuxiliaryAttr->setValue(isChecked);
407 if (isUseTransaction)
408 anOpMgr->commitOperation();
410 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
413 bool PartSet_MenuMgr::canSetAuxiliary(bool& theValue) const
415 bool anEnabled = false;
416 ModuleBase_Operation* anOperation = myModule->workshop()->currentOperation();
418 CompositeFeaturePtr aSketch = myModule->sketchMgr()->activeSketch();
419 bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
420 myModule->sketchMgr()->isNestedSketchOperation(anOperation);
424 QObjectPtrList anObjects;
425 // 1. change auxiliary type of a created feature
426 if (myModule->sketchMgr()->isNestedCreateOperation(anOperation, aSketch) &&
427 PartSet_SketcherMgr::isEntity(anOperation->id().toStdString()) ) {
428 ModuleBase_OperationFeature* aFOperation =
429 dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
431 anObjects.append(aFOperation->feature());
434 /// The operation should not be aborted here, because the method does not changed
435 /// the auxilliary state, but checks the possibility to perform this
436 ///if (myModule->sketchMgr()->isNestedSketchOperation(anOperation))
437 /// anOperation->abort();
438 // 2. change auxiliary type of selected sketch entities
439 ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
440 anObjects = aSelection->selectedPresentations();
443 bool isNotAuxiliaryFound = false;
444 if (anObjects.size() > 0) {
445 QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
446 for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
447 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
448 if ((aFeature.get() != NULL) && PartSet_SketcherMgr::isEntity(aFeature->getKind())) {
450 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
451 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
452 if (aSketchFeature.get() != NULL) {
453 std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
455 std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
456 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
457 aSketchFeature->data()->attribute(anAttribute));
459 isNotAuxiliaryFound = !anAuxiliaryAttr->value();
464 theValue = anObjects.size() && !isNotAuxiliaryFound;
468 void PartSet_MenuMgr::onActivatePart(bool)
470 if (myModule->workshop()->currentOperation())
472 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
473 if (aObjects.size() > 0) {
474 ObjectPtr aObj = aObjects.first();
475 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
477 FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
478 if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
479 aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
484 myModule->workshop()->updateCommandStatus();
489 void PartSet_MenuMgr::activatePart(ResultPartPtr thePart) const
491 bool isFirstLoad = !thePart->partDoc().get();
494 XGUI_Workshop* aWorkshop = myModule->getWorkshop();
495 XGUI_ObjectsBrowser* aObjBrowser = aWorkshop->objectBrowser();
496 DocumentPtr aDoc = thePart->partDoc();
497 std::list<bool> aStates;
498 aDoc->restoreNodesState(aStates);
499 aObjBrowser->setStateForDoc(aDoc, aStates);
503 void PartSet_MenuMgr::onActivatePartSet(bool)
505 if (myModule->workshop()->currentOperation())
510 void PartSet_MenuMgr::activatePartSet() const
512 SessionPtr aMgr = ModelAPI_Session::get();
513 bool isNewTransaction = !aMgr->isOperation();
514 // activation may cause changes in current features in document, so it must be in transaction
515 if (isNewTransaction)
516 aMgr->startOperation("Activation");
517 aMgr->setActiveDocument(aMgr->moduleDocument());
518 if (isNewTransaction)
519 aMgr->finishOperation();
521 myModule->workshop()->updateCommandStatus();
524 void PartSet_MenuMgr::grantedOperationIds(ModuleBase_Operation* theOperation,
525 QStringList& theIds) const
527 if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
528 theIds.append(tr("Detach"));
529 theIds.append(tr("Auxiliary"));
533 void PartSet_MenuMgr::onEdit(bool)
535 QObjectPtrList aObjects = myModule->workshop()->selection()->selectedObjects();
537 foreach(ObjectPtr aObj, aObjects) {
538 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
542 if (aFeature.get() == NULL) {
543 ResultParameterPtr aParam;
544 foreach(ObjectPtr aObj, aObjects) {
545 aParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aObj);
549 if (aParam.get() != NULL)
550 aFeature = ModelAPI_Feature::feature(aParam);
552 if (aFeature.get() != NULL)
553 myModule->editFeature(aFeature);
556 bool PartSet_MenuMgr::eventFilter(QObject* theObj, QEvent* theEvent)
558 if (theEvent->type() == QEvent::MouseButtonDblClick) {
559 SessionPtr aMgr = ModelAPI_Session::get();
560 if (aMgr->activeDocument() != aMgr->moduleDocument())
563 return QObject::eventFilter(theObj, theEvent);