Salome HOME
Provide preselection
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureEditMulti.cpp
index ac2cd698268690709d071e4cea9565324222ddf2..4b10858a0bc40ff31371022d816479a616c610e3 100644 (file)
@@ -8,6 +8,8 @@
 
 #include <ModuleBase_OperationDescription.h>
 #include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_IViewer.h>
+#include <ModuleBase_ISelection.h>
 
 #include <ModelAPI_Events.h>
 
 
 #include <V3d_View.hxx>
 
+#include <QMouseEvent>
 #ifdef _DEBUG
 #include <QDebug>
 #endif
 
-#include <QMouseEvent>
-
-using namespace std;
+//using namespace std;
 
 PartSet_OperationFeatureEditMulti::PartSet_OperationFeatureEditMulti(const QString& theId,
-                                                 QObject* theParent,
-                                              FeaturePtr theFeature)
-: PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myIsBlockedSelection(false)
+                                                                     QObject* theParent,
+                                                                     CompositeFeaturePtr theFeature)
+    : PartSet_OperationSketchBase(theId, theParent),
+      mySketch(theFeature),
+      myIsBlockedSelection(false)
 {
+  myIsEditing = true;
 }
 
 PartSet_OperationFeatureEditMulti::~PartSet_OperationFeatureEditMulti()
 {
 }
 
-bool PartSet_OperationFeatureEditMulti::isGranted(ModuleBase_IOperation* theOperation) const
-{
-  return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
-}
 
-void PartSet_OperationFeatureEditMulti::initSelection(const std::list<ModuleBase_ViewerPrs>& theSelected,
-                                                      const std::list<ModuleBase_ViewerPrs>& theHighlighted)
+bool isContains(const QList<ModuleBase_ViewerPrs>& theSelected, const ModuleBase_ViewerPrs& thePrs)
 {
-  if (!theHighlighted.empty()) {
-    // if there is highlighted object, we check whether it is in the list of selected objects
-    // in that case this object is a handle of the moved lines. If there no such object in the selection,
-    // the hightlighted object should moved and the selection is skipped. The skipped selection will be
-    // deselected in the viewer by blockSelection signal in the startOperation method.
-    bool isSelected = false;
-    std::list<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(), aLast = theSelected.end();
-    for (; anIt != aLast && !isSelected; anIt++) {
-      isSelected = (*anIt).feature() == feature();
-    }
-    if (!isSelected)
-      myFeatures = theHighlighted;
-    else
-      myFeatures = theSelected;
+  foreach (ModuleBase_ViewerPrs aPrs, theSelected) {
+    if (aPrs.object() == thePrs.object())
+      return true;
   }
-  else
-    myFeatures = theSelected;
+  return false;
 }
 
-void PartSet_OperationFeatureEditMulti::initFeature(FeaturePtr theFeature)
+
+void PartSet_OperationFeatureEditMulti::initSelection(ModuleBase_ISelection* theSelection)
 {
-  setEditingFeature(theFeature);
+  //if (!theHighlighted.empty()) {
+  //  // if there is highlighted object, we check whether it is in the list of selected objects
+  //  // in that case this object is a handle of the moved lines. If there no such object in the selection,
+  //  // the hightlighted object should moved and the selection is skipped. The skipped selection will be
+  //  // deselected in the viewer by blockSelection signal in the startOperation method.
+  //  bool isSelected = false;
+  //  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = theSelected.begin(), 
+  //    aLast = theSelected.end();
+  //  for (; anIt != aLast && !isSelected; anIt++) {
+  //    isSelected = ModelAPI_Feature::feature((*anIt).object()) == feature();
+  //  }
+  //  if (!isSelected)
+  //    myFeatures = theHighlighted;
+  //  else
+  //    myFeatures = theSelected;
+  //} else
+  myFeatures = theSelection->getSelected();
+  QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
+  // add highlighted elements if they are not selected
+  foreach (ModuleBase_ViewerPrs aPrs, aHighlighted) {
+    if (!isContains(myFeatures, aPrs))
+      myFeatures.append(aPrs);
+  }
+  // Remove current feature if it is in the list (it will be moved as main feature)
+  foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
+    FeaturePtr aF = ModelAPI_Feature::feature(aPrs.object());
+    if (ModelAPI_Feature::feature(aPrs.object()) == feature()) {
+      myFeatures.removeOne(aPrs);
+      break;
+    }
+  }
 }
 
-FeaturePtr PartSet_OperationFeatureEditMulti::sketch() const
+CompositeFeaturePtr PartSet_OperationFeatureEditMulti::sketch() const
 {
   return mySketch;
 }
 
-void PartSet_OperationFeatureEditMulti::mousePressed(QMouseEvent* theEvent, Handle(V3d_View) theView,
-                                             const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
-                                             const std::list<ModuleBase_ViewerPrs>& theHighlighted)
-{
-}
+//void PartSet_OperationFeatureEditMulti::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
+//{
+//}
 
-void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
+void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
 {
-  if (!(theEvent->buttons() &  Qt::LeftButton))
+  if (!(theEvent->buttons() & Qt::LeftButton))
     return;
 
-  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
+  if (theViewer->isSelectionEnabled())
+    theViewer->enableSelection(false);
+
+  Handle(V3d_View) aView = theViewer->activeView();
+  gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
 
-  blockSelection(true);
+  //blockSelection(true);
   if (myCurPoint.myIsInitialized) {
     double aCurX, aCurY;
-    PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), theView, aCurX, aCurY);
+    PartSet_Tools::convertTo2D(myCurPoint.myPoint, sketch(), aView, aCurX, aCurY);
 
     double aX, anY;
-    PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
+    PartSet_Tools::convertTo2D(aPoint, sketch(), aView, aX, anY);
 
     double aDeltaX = aX - aCurX;
     double aDeltaY = anY - aCurY;
 
-    boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
-                           boost::dynamic_pointer_cast<SketchPlugin_Feature>(feature());
+    boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
+        SketchPlugin_Feature>(feature());
     aSketchFeature->move(aDeltaX, aDeltaY);
 
-    std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
-    for (; anIt != aLast; anIt++) {
-      FeaturePtr aFeature = (*anIt).feature();
-      if (!aFeature || aFeature == feature())
+    foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
+      ObjectPtr aObject = aPrs.object();
+      if (!aObject || aObject == feature())
         continue;
-      aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
-      aSketchFeature->move(aDeltaX, aDeltaY);
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
+      if (aFeature) {
+        aSketchFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+        if (aSketchFeature)
+          aSketchFeature->move(aDeltaX, aDeltaY);
+      }
     }
   }
   sendFeatures();
@@ -122,71 +145,69 @@ void PartSet_OperationFeatureEditMulti::mouseMoved(QMouseEvent* theEvent, Handle
   myCurPoint.setPoint(aPoint);
 }
 
-void PartSet_OperationFeatureEditMulti::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
-                                              const std::list<ModuleBase_ViewerPrs>& /*theSelected*/,
-                                              const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
+void PartSet_OperationFeatureEditMulti::mouseReleased(
+    QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
+    ModuleBase_ISelection* theSelection)
 {
-  std::list<ModuleBase_ViewerPrs> aFeatures = myFeatures;
-  commit();
-  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
-  for (; anIt != aLast; anIt++) {
-    FeaturePtr aFeature = (*anIt).feature();
-    if (aFeature) {
-      emit featureConstructed(aFeature, FM_Deactivation);
-       }
+  theViewer->enableSelection(true);
+  if (commit()) {
+    foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
+      ObjectPtr aFeature = aPrs.object();
+      if (aFeature) {
+        emit featureConstructed(aFeature, FM_Deactivation);
+      }
+    }
   }
 }
 
 void PartSet_OperationFeatureEditMulti::startOperation()
 {
   PartSet_OperationSketchBase::startOperation();
-  emit multiSelectionEnabled(false);
+  //emit multiSelectionEnabled(false);
 
-  blockSelection(true);
+  //blockSelection(true);
 
   myCurPoint.clear();
 }
 
 void PartSet_OperationFeatureEditMulti::stopOperation()
 {
-  emit multiSelectionEnabled(true);
+  //emit multiSelectionEnabled(true);
 
-  blockSelection(false, true);
+  //blockSelection(false, true);
 
   myFeatures.clear();
 }
 
-void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked, const bool isRestoreSelection)
-{
-  if (myIsBlockedSelection == isBlocked)
-    return;
-
-  myIsBlockedSelection = isBlocked;
-  QFeatureList aFeatureList;
-  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(),
-                                            aLast = myFeatures.end();
-  for(; anIt != aLast; anIt++)
-    aFeatureList.append((*anIt).feature());
-  if (isBlocked) {
-    emit setSelection(QFeatureList());
-    emit stopSelection(aFeatureList, true);
-  }
-  else {
-    emit stopSelection(aFeatureList, false);
-    if (isRestoreSelection) {
-      emit setSelection(aFeatureList);
-    }
-  }
-}
+//void PartSet_OperationFeatureEditMulti::blockSelection(bool isBlocked,
+//                                                       const bool isRestoreSelection)
+//{
+//  if (myIsBlockedSelection == isBlocked)
+//    return;
+//
+//  myIsBlockedSelection = isBlocked;
+//  QList<ObjectPtr> aFeatureList;
+////  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast =
+////      myFeatures.end();
+//  /*for(; anIt != aLast; anIt++)
+//   aFeatureList.append((*anIt).feature());*/
+//  //if (isBlocked) {
+//  //  emit setSelection(QList<ObjectPtr>());
+//  //  emit stopSelection(aFeatureList, true);
+//  //} else {
+//  //  emit stopSelection(aFeatureList, false);
+//  //  if (isRestoreSelection) {
+//  //    emit setSelection(aFeatureList);
+//  //  }
+//  //}
+//}
 
 void PartSet_OperationFeatureEditMulti::sendFeatures()
 {
-  static Events_ID anEvent = Events_Loop::eventByName(EVENT_FEATURE_MOVED);
+  static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
 
-  std::list<FeaturePtr > aFeatures;
-  std::list<ModuleBase_ViewerPrs>::const_iterator anIt = myFeatures.begin(), aLast = myFeatures.end();
-  for (; anIt != aLast; anIt++) {
-    FeaturePtr aFeature = (*anIt).feature();
+  foreach (ModuleBase_ViewerPrs aPrs, myFeatures) {
+    ObjectPtr aFeature = aPrs.object();
     if (!aFeature)
       continue;