Salome HOME
Correction of icon
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
index db967f651434316543cb3d3d0c9b82471acb18b6..a6ae7a8a5bcb4f656210a239a4d08275dbb4878f 100644 (file)
@@ -15,6 +15,7 @@
 #include <XGUI_ModuleConnector.h>
 #include <XGUI_Displayer.h>
 #include <XGUI_Workshop.h>
+#include <XGUI_ContextMenuMgr.h>
 #include <XGUI_Selection.h>
 #include <XGUI_SelectionMgr.h>
 #include <ModuleBase_ModelWidget.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
 #include <SketchPlugin_ConstraintRadius.h>
 #include <SketchPlugin_ConstraintRigid.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
+#include <SketchPlugin_ConstraintVertical.h>
+#include <SketchPlugin_ConstraintEqual.h>
+#include <SketchPlugin_ConstraintTangent.h>
 
 #include <SelectMgr_IndexedMapOfOwner.hxx>
 #include <StdSelect_BRepOwner.hxx>
@@ -55,6 +60,7 @@
 //#include <AIS_Shape.hxx>
 
 #include <ModelAPI_Events.h>
+#include <ModelAPI_Session.h>
 
 #include <QMouseEvent>
 #include <QApplication>
@@ -114,7 +120,8 @@ void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
     myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
-    myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true)
+    myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
+    myIsPopupMenuActive(false)
 {
   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
@@ -160,6 +167,10 @@ void PartSet_SketcherMgr::onLeaveViewPort()
 {
   if (!isNestedCreateOperation(getCurrentOperation()))
     return;
+  // the method should be performed if the popup menu is called,
+  // the reset of the current widget should not happen
+  if (myIsPopupMenuActive)
+    return;
 
   myIsMouseOverViewProcessed = false;
   myIsMouseOverWindow = false;
@@ -486,6 +497,10 @@ void PartSet_SketcherMgr::onApplicationStarted()
   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
+
+  XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
+  connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
+  connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
 }
 
 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
@@ -506,6 +521,16 @@ void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWid
   }
 }
 
+void PartSet_SketcherMgr::onBeforeContextMenu()
+{
+  myIsPopupMenuActive = true;
+}
+
+void PartSet_SketcherMgr::onAfterContextMenu()
+{
+  myIsPopupMenuActive = false;
+}
+
 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
                                      Point& thePoint)
 {
@@ -557,6 +582,10 @@ QStringList PartSet_SketcherMgr::sketchOperationIdList()
     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
+    aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
+    aIds << SketchPlugin_ConstraintVertical::ID().c_str();
+    aIds << SketchPlugin_ConstraintEqual::ID().c_str();
+    aIds << SketchPlugin_ConstraintTangent::ID().c_str();
   }
   return aIds;
 }
@@ -572,6 +601,21 @@ bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOpera
          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
 }
 
+bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
+{
+  return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
+}
+
+bool PartSet_SketcherMgr::isEntityOperation(ModuleBase_Operation* theOperation)
+{
+  std::string aId = theOperation ? theOperation->id().toStdString() : "";
+
+  return (aId == SketchPlugin_Line::ID()) ||
+         (aId == SketchPlugin_Point::ID()) ||
+         (aId == SketchPlugin_Arc::ID()) ||
+         (aId == SketchPlugin_Circle::ID());
+}
+
 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
 {
   std::string aId = theOperation ? theOperation->id().toStdString() : "";
@@ -710,6 +754,8 @@ bool PartSet_SketcherMgr::canDisplayObject() const
       return aCanDisplay;
     }
   }
+  if (myIsPopupMenuActive)
+    return aCanDisplay;
 
   // during a nested create operation, the feature is redisplayed only if the mouse over view
   // of there was a value modified in the property panel after the mouse left the view
@@ -717,6 +763,113 @@ bool PartSet_SketcherMgr::canDisplayObject() const
   return aCanDisplay;
 }
 
+bool PartSet_SketcherMgr::canSetAuxiliary(bool& theValue) const
+{
+  bool anEnabled = false;
+  ModuleBase_Operation* anOperation = getCurrentOperation();
+
+  bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
+                        PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
+  if (!isActiveSketch)
+    return anEnabled;
+
+  QObjectPtrList anObjects;
+  // 1. change auxiliary type of a created feature
+  if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
+      PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
+    anObjects.append(anOperation->feature());
+  }
+  else {
+    /// The operation should not be aborted here, because the method does not changed
+    /// the auxilliary state, but checks the possibility to perform this
+    ///if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
+    ///  anOperation->abort();
+    // 2. change auxiliary type of selected sketch entities
+    ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
+    anObjects = aSelection->selectedPresentations();
+  }
+  anEnabled = anObjects.size() > 0;
+
+  bool isNotAuxiliaryFound = false;
+  if (anObjects.size() > 0) {
+    QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
+    for (; anIt != aLast && !isNotAuxiliaryFound; anIt++) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
+      if (aFeature.get() != NULL) {
+        std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+                            std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+        if (aSketchFeature.get() != NULL) {
+          std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
+
+          std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
+            std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
+          if (anAuxiliaryAttr)
+            isNotAuxiliaryFound = !anAuxiliaryAttr->value();
+        }
+      }
+    }
+  }
+  theValue = anObjects.size() && !isNotAuxiliaryFound;
+  return anEnabled;
+}
+  
+void PartSet_SketcherMgr::setAuxiliary(const bool isChecked)
+{
+  ModuleBase_Operation* anOperation = getCurrentOperation();
+
+  bool isActiveSketch = PartSet_SketcherMgr::isSketchOperation(anOperation) ||
+                        PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
+  if (!isActiveSketch)
+    return;
+
+  QObjectPtrList anObjects;
+  bool isUseTransaction = false;
+  // 1. change auxiliary type of a created feature
+  if (PartSet_SketcherMgr::isNestedCreateOperation(anOperation) &&
+      PartSet_SketcherMgr::isEntityOperation(anOperation) ) {
+    anObjects.append(anOperation->feature());
+  }
+  else {
+    isUseTransaction = true;
+    // 2. change auxiliary type of selected sketch entities
+    ModuleBase_ISelection* aSelection = myModule->workshop()->selection();
+    anObjects = aSelection->selectedPresentations();
+  }
+
+  QAction* anAction = myModule->action("AUXILIARY_CMD");
+  SessionPtr aMgr = ModelAPI_Session::get();
+  if (isUseTransaction) {
+    if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation))
+      anOperation->abort();
+    aMgr->startOperation(anAction->text().toStdString());
+  }
+  storeSelection();
+
+  if (anObjects.size() > 0) {
+    QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
+    for (; anIt != aLast; anIt++) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
+      if (aFeature.get() != NULL) {
+        std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+                            std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+        if (aSketchFeature.get() != NULL) {
+          std::string anAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
+
+          std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr = 
+            std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(aSketchFeature->data()->attribute(anAttribute));
+          if (anAuxiliaryAttr)
+            anAuxiliaryAttr->setValue(isChecked);
+        }
+      }
+    }
+  }
+  if (isUseTransaction) {
+    aMgr->finishOperation();
+  }
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  restoreSelection();
+}
+
 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
 {
   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
@@ -795,6 +948,21 @@ void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
 
+  // 1. found the feature's owners. Check the AIS objects of the constructions
+  AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
+  if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
+    Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
+
+    SelectMgr_IndexedMapOfOwner aSelectedOwners;  
+    aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
+    for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
+      Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
+      if (!anOwner.IsNull())
+        anOwnersToSelect.Add(anOwner);
+    }
+  }
+
+  // 2. found the feature results's owners
   std::list<ResultPtr> aResults = theFeature->results();
   std::list<ResultPtr>::const_iterator aIt;
   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
@@ -862,11 +1030,6 @@ ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
   return myModule->workshop()->currentOperation();
 }
 
-bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation) const
-{
-  return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
-}
-
 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
                                            const bool isToDisplay)
 {
@@ -902,7 +1065,6 @@ void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
 {
   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
-
   ModuleBase_ISelection* aSelect = aWorkshop->selection();
   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
     
@@ -925,12 +1087,12 @@ void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
     FeaturePtr aFeature = anIt.key();
     getCurrentSelection(aFeature, myCurrentSketch, aWorkshop, myCurrentSelection);
   }
-  qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
+  //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
 }
 
 void PartSet_SketcherMgr::restoreSelection()
 {
-  qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
+  //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),