Salome HOME
Preselection realization for multi-selector widget to accept some selection values.
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
index b3bf0b07572308f26b62a2fc2c177a4ae2eb24dc..b934df13061c2bd6e6d74c9d1465c85e3ca809a2 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * ModuleBase_Operation.cpp
  *
 
 #include "ModuleBase_OperationDescription.h"
 #include "ModuleBase_ModelWidget.h"
-#include "ModuleBase_WidgetValueFeature.h"
 #include "ModuleBase_ViewerPrs.h"
 #include "ModuleBase_IPropertyPanel.h"
 #include "ModuleBase_ISelection.h"
+#include "ModuleBase_IViewer.h"
 
 #include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Document.h>
@@ -29,8 +31,7 @@
 
 #include <Events_Loop.h>
 
-#include <TopoDS.hxx>
-#include <TopoDS_Vertex.hxx>
+#include <QTimer>
 
 #ifdef _DEBUG
 #include <QDebug>
@@ -48,6 +49,7 @@ ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* thePar
 ModuleBase_Operation::~ModuleBase_Operation()
 {
   delete myDescription;
+  clearPreselection();
 }
 
 QString ModuleBase_Operation::id() const
@@ -64,76 +66,26 @@ bool ModuleBase_Operation::isValid() const
 {
   if (!myFeature)
     return true; // rename operation
+  if (myFeature->isAction())
+    return true;
   //Get validators for the Id
   SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
   return aFactory->validate(myFeature);
 }
 
-bool ModuleBase_Operation::isNestedOperationsEnabled() const
-{
-  return true;
-}
-
-void ModuleBase_Operation::storeCustomValue()
-{
-  if (!myFeature) {
-#ifdef _DEBUG
-    qDebug() << "ModuleBase_Operation::storeCustom: " <<
-    "trying to store value without opening a transaction.";
-#endif
-    return;
-  }
-
-  ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
-  if (aCustom)
-    aCustom->storeValue();
-}
-
-void ModuleBase_Operation::startOperation()
-{
-  if (!myIsEditing)
-    createFeature();
-}
-
-void ModuleBase_Operation::stopOperation()
-{
-}
-
-void ModuleBase_Operation::abortOperation()
-{
-}
-
-void ModuleBase_Operation::commitOperation()
-{
-}
-
-void ModuleBase_Operation::afterCommitOperation()
-{
-}
 
 bool ModuleBase_Operation::canBeCommitted() const
 {
-  return true;
-}
-
-void ModuleBase_Operation::flushUpdated()
-{
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
-}
-
-void ModuleBase_Operation::flushCreated()
-{
-  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
+  return isValid();
 }
 
-FeaturePtr ModuleBase_Operation::createFeature(
-  const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
+FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
 {
-  if (theCompositeFeature) {
-    myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
+  if (myParentFeature.get()) {
+    myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
   } else {
-    boost::shared_ptr<ModelAPI_Document> aDoc = document();
+    std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
   }
   if (myFeature) {  // TODO: generate an error if feature was not created
@@ -149,7 +101,7 @@ FeaturePtr ModuleBase_Operation::createFeature(
   }
 
   if (theFlushMessage)
-    flushCreated();
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
   return myFeature;
 }
 
@@ -168,33 +120,47 @@ bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
     std::list<ResultPtr> aResults = aFeature->results();
     std::list<ResultPtr>::const_iterator aIt;
     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
-      if ((*aIt) == theObj)
+      if (theObj == (*aIt))
         return true;
     }
   }
   return false;
 }
 
-
-boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
-{
-  return ModelAPI_Session::get()->moduleDocument();
-}
-
-
 void ModuleBase_Operation::start()
 {
-  ModelAPI_Session::get()->startOperation();
+  QString anId = getDescription()->operationId();
+  if (myIsEditing) {
+      anId = anId.append(EditSuffix());
+  }
+  ModelAPI_Session::get()->startOperation(anId.toStdString());
+
+  if (!myIsEditing) {
+    FeaturePtr aFeature = createFeature();
+    // if the feature is not created, there is no sense to start the operation
+    if (aFeature.get() == NULL) {
+      // it is necessary to abor the operation in the session and emit the aborted signal
+      // in order to update commands status in the workshop, to be exact the feature action
+      // to be unchecked
+      abort();
+      return;
+    }
+  }
 
   startOperation();
   emit started();
+
+}
+
+void ModuleBase_Operation::postpone()
+{
+  postponeOperation();
+  emit postponed();
 }
 
 void ModuleBase_Operation::resume()
 {
-  if (myPropertyPanel)
-    connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
-            this,            SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
+  resumeOperation();
   emit resumed();
 }
 
@@ -202,8 +168,6 @@ void ModuleBase_Operation::abort()
 {
   abortOperation();
   emit aborted();
-  if (myPropertyPanel)
-    disconnect(myPropertyPanel, 0, this, 0);
 
   stopOperation();
 
@@ -215,12 +179,6 @@ bool ModuleBase_Operation::commit()
 {
   if (canBeCommitted()) {
     commitOperation();
-    emit committed();
-
-  if (myPropertyPanel)
-    disconnect(myPropertyPanel, 0, this, 0);
-
-    stopOperation();
     // check whether there are modifications performed during the current operation
     // in the model
     // in case if there are no modifications, do not increase the undo/redo stack
@@ -229,7 +187,9 @@ bool ModuleBase_Operation::commit()
     else
       ModelAPI_Session::get()->abortOperation();
 
+    stopOperation();
     emit stopped();
+    emit committed();
 
     afterCommitOperation();
     return true;
@@ -239,37 +199,32 @@ bool ModuleBase_Operation::commit()
 
 void ModuleBase_Operation::setRunning(bool theState)
 {
-  if (!theState) {
-    abort();
-  }
+  emit triggered(theState);
 }
 
-bool ModuleBase_Operation::activateByPreselection()
+void ModuleBase_Operation::activateByPreselection()
 {
-  if (!myPropertyPanel)
-    return false;
-  if (myPreSelection.empty())
-    return false;
+  if (!myPropertyPanel || myPreSelection.empty()) {
+    myPropertyPanel->activateNextWidget(NULL);
+    return;
+  }
   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
-  if (aWidgets.empty())
-    return false;
+  if (aWidgets.empty()) {
+    myPropertyPanel->activateNextWidget(NULL);
+    return;
+  }
   
   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
-  ModuleBase_ViewerPrs aPrs;
   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
-  QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
   bool isSet = false;
-  for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
-       (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
-       ++aWIt, ++aPIt) {
+  // 1. apply the selection to controls
+  int aCurrentPosition = 0;
+  for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
     aWgt = (*aWIt);
-    aPrs = (*aPIt);
-    ModuleBase_WidgetValueFeature aValue;
-    aValue.setObject(aPrs.object());
-    // Check if the selection has a selected point
-    // for today it is impossible to do because
-    // the selected point demands convertation to Sketch plane 2d
-    if (!aWgt->setValue(&aValue)) {
+    if (!aWgt->canSetValue())
+      continue;
+
+    if (!aWgt->setSelection(myPreSelection, aCurrentPosition/*aValue*/)) {
       isSet = false;
       break;
     } else {
@@ -277,84 +232,118 @@ bool ModuleBase_Operation::activateByPreselection()
       aFilledWgt = aWgt;
     }
   }
-  if (isSet && canBeCommitted()) {
-    // if all widgets are filled with selection
-    commit();
-    return true;
-  }
-  else {
-    //activate next widget
-    if (aFilledWgt) {
-      myPropertyPanel->activateNextWidget(aFilledWgt);
-      return true;
-    }
-  }
+  // 2. ignore not obligatory widgets
+  /*for (; aWIt != aWidgets.constEnd(); ++aWIt) {
+    aWgt = (*aWIt);
+    if (aWgt && aWgt->isObligatory())
+      continue;
+    aFilledWgt = aWgt;
+  }*/
 
-  //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
-  //if ((myPreSelection.size() > 0) && aActiveWgt) {
-  //  const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
-  //  ModuleBase_WidgetValueFeature aValue;
-  //  aValue.setObject(aPrs.object());
-  //  if (aActiveWgt->setValue(&aValue)) {
-  //    myPreSelection.removeOne(aPrs);
-  //    myPropertyPanel->activateNextWidget();
-  //  }
-  //  // If preselection is enough to make a valid feature - apply it immediately
-  //}
-  return false;
+  // 3. activate the next obligatory widget
+  myPropertyPanel->activateNextWidget(aFilledWgt);
+  if (aFilledWgt)
+    emit activatedByPreselection();
+
+}
+
+void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
+{
+  myParentFeature = theParent;
+}
+
+CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
+{
+  return myParentFeature;
 }
 
 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
-                                         ModuleBase_IViewer* /*theViewer*/)
+                                         ModuleBase_IViewer* theViewer)
 {
-  myPreSelection.clear();
+  clearPreselection();
 
+  QList<ModuleBase_ViewerPrs> aPreSelected;
   // Check that the selected result are not results of operation feature
-  QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
   FeaturePtr aFeature = feature();
   if (aFeature) {
+    QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
+
     std::list<ResultPtr> aResults = aFeature->results();
-    QList<ObjectPtr> aResList;
+    QObjectPtrList aResList;
     std::list<ResultPtr>::const_iterator aIt;
     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
       aResList.append(*aIt);
 
     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
-        myPreSelection.append(aPrs);
+        aPreSelected.append(aPrs);
     }
   } else
-    myPreSelection = aSelected;
-}
+    aPreSelected = theSelection->getSelected();
 
-void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
-{
-  //activateByPreselection();
-  //if (theWidget && myPropertyPanel) {
-  //  myPropertyPanel->activateNextWidget();
-  ////  //emit activateNextWidget(myActiveWidget);
+  // convert the selection values to the values, which are set to the operation widgets
+
+  //Handle(V3d_View) aView = theViewer->activeView();
+  //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
+  //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
+  //  aValue->setObject(aPrs.object());
+
+  //  double aX, anY;
+  //  if (getViewerPoint(aPrs, theViewer, aX, anY))
+  //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
+  //  myPreSelection.append(aValue);
   //}
+  myPreSelection = aPreSelected;
 }
 
-bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
+//void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
+//{
+//  //activateByPreselection();
+//  //if (theWidget && myPropertyPanel) {
+//  //  myPropertyPanel->activateNextWidget();
+//  ////  //emit activateNextWidget(myActiveWidget);
+//  //}
+//}
+
+//bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
+//{
+//  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
+//  if (!aActiveWgt)
+//    return false;
+//  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
+//  aValue->setObject(theFeature);
+//  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
+//  bool isApplyed = aActiveWgt->setValue(aValue);
+//
+//  delete aValue;
+//  myIsModified = (myIsModified || isApplyed);
+//  return isApplyed;
+//}
+
+bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
+                                               ModuleBase_IViewer* theViewer,
+                                               double& theX, double& theY)
 {
-  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
-  if (!aActiveWgt)
-    return false;
-  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
-  aValue->setObject(theFeature);
-  aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
-  bool isApplyed = aActiveWgt->setValue(aValue);
-
-  delete aValue;
-  myIsModified = (myIsModified || isApplyed);
-  return isApplyed;
+  return false;
 }
 
+void ModuleBase_Operation::clearPreselection()
+{
+  myPreSelection.clear();
+}
 
 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
 { 
   myPropertyPanel = theProp; 
-  connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
-          SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
+  myPropertyPanel->setEditingMode(isEditOperation());
+
+  // Do not activate widgets by default if the current operation is editing operation
+  // Because we don't know which widget is going to be edited. 
+  if (!isEditOperation())
+    activateByPreselection();
+}
+
+bool ModuleBase_Operation::isGranted(QString theId) const
+{
+  return myNestedFeatures.contains(theId);
 }