Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
index 12eb36e53b40d9b8503c51319d6c7316aa6a0164..cd728aa7adcb2711218549b172b0062bd6b83df1 100644 (file)
@@ -9,6 +9,10 @@
 
 #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 <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Result.h>
+#include <ModelAPI_Object.h>
 #include <ModelAPI_Validator.h>
+#include <ModelAPI_Session.h>
+
+#include <GeomAPI_Pnt2d.h>
 
 #include <Events_Loop.h>
 
 #endif
 
 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
-    : ModuleBase_IOperation(theId, theParent)
+    : QObject(theParent),
+      myIsEditing(false),
+      myIsModified(false),
+      myPropertyPanel(NULL)
 {
+  myDescription = new ModuleBase_OperationDescription(theId);
 }
 
 ModuleBase_Operation::~ModuleBase_Operation()
 {
+  delete myDescription;
 }
 
 QString ModuleBase_Operation::id() const
@@ -44,6 +57,16 @@ FeaturePtr ModuleBase_Operation::feature() const
   return myFeature;
 }
 
+bool ModuleBase_Operation::isValid() const
+{
+  if (!myFeature)
+    return true; // rename operation
+  //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;
@@ -64,16 +87,10 @@ void ModuleBase_Operation::storeCustomValue()
     aCustom->storeValue();
 }
 
-void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
-{
-}
-
 void ModuleBase_Operation::startOperation()
 {
   if (!myIsEditing)
     createFeature();
-  //emit callSlot();
-  //commit();
 }
 
 void ModuleBase_Operation::stopOperation()
@@ -94,26 +111,7 @@ void ModuleBase_Operation::afterCommitOperation()
 
 bool ModuleBase_Operation::canBeCommitted() const
 {
-  if (ModuleBase_IOperation::canBeCommitted()) {
-    /*    FeaturePtr aFeature = feature();
-     std::string aId = aFeature->getKind();
-
-     SessionPtr aMgr = ModelAPI_Session::get();
-     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-     std::list<ModelAPI_Validator*> aValidators;
-     aFactory->validators(aId, aValidators);
-     std::list<ModelAPI_Validator*>::const_iterator aIt;
-     for (aIt = aValidators.cbegin(); aIt != aValidators.cend(); ++aIt) {
-     const ModuleBase_FeatureValidator* aFValidator = 
-     dynamic_cast<const ModuleBase_FeatureValidator*>(*aIt);
-     if (aFValidator) {
-     if (!aFValidator->isValid(aFeature))
-     return false;
-     }
-     }*/
-    return true;
-  }
-  return false;
+  return true;
 }
 
 void ModuleBase_Operation::flushUpdated()
@@ -126,10 +124,15 @@ void ModuleBase_Operation::flushCreated()
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
 }
 
-FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
+FeaturePtr ModuleBase_Operation::createFeature(
+  const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
 {
-  boost::shared_ptr<ModelAPI_Document> aDoc = document();
-  myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
+  if (theCompositeFeature) {
+    myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
+  } else {
+    boost::shared_ptr<ModelAPI_Document> aDoc = document();
+    myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
+  }
   if (myFeature) {  // TODO: generate an error if feature was not created
     myIsModified = true;
     // Model update should call "execute" of a feature.
@@ -150,11 +153,6 @@ FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
 {
   myFeature = theFeature;
-}
-
-void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
-{
-  setFeature(theFeature);
   myIsEditing = true;
 }
 
@@ -174,9 +172,164 @@ bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
   return false;
 }
 
-bool ModuleBase_Operation::keyReleased(const int theKey)
+
+boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
+{
+  return ModelAPI_Session::get()->moduleDocument();
+}
+
+
+void ModuleBase_Operation::start()
+{
+  ModelAPI_Session::get()->startOperation();
+
+  startOperation();
+  emit started();
+}
+
+void ModuleBase_Operation::resume()
+{
+  if (myPropertyPanel)
+    connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
+            this,            SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
+  emit resumed();
+}
+
+void ModuleBase_Operation::abort()
+{
+  abortOperation();
+  emit aborted();
+  if (myPropertyPanel)
+    disconnect(myPropertyPanel, 0, this, 0);
+
+  stopOperation();
+
+  ModelAPI_Session::get()->abortOperation();
+  emit stopped();
+}
+
+bool ModuleBase_Operation::commit()
+{
+  if (canBeCommitted()) {
+    commitOperation();
+    emit committed();
+
+  if (myPropertyPanel)
+    disconnect(myPropertyPanel, 0, this, 0);
+
+    stopOperation();
+    ModelAPI_Session::get()->finishOperation();
+
+    emit stopped();
+
+    afterCommitOperation();
+    return true;
+  }
+  return false;
+}
+
+void ModuleBase_Operation::setRunning(bool theState)
+{
+  if (!theState) {
+    abort();
+  }
+}
+
+bool ModuleBase_Operation::activateByPreselection()
 {
-  // Do nothing...
+  if (!myPropertyPanel)
+    return false;
+  if (myPreSelection.empty())
+    return false;
+  const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
+  if (aWidgets.empty())
+    return false;
+  
+  ModuleBase_ModelWidget* aWgt;
+  ModuleBase_ViewerPrs aPrs;
+  QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
+  QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
+  for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
+       (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
+       ++aWIt, ++aPIt) {
+    aWgt = (*aWIt);
+    aPrs = (*aPIt);
+    ModuleBase_WidgetValueFeature aValue;
+    aValue.setObject(aPrs.object());
+    if (!aWgt->setValue(&aValue))
+      break;
+  }
+  if (canBeCommitted()) {
+    // if all widgets are filled with selection
+    commit();
+    return true;
+  }
+
+  //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;
 }
 
+void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
+                                         ModuleBase_IViewer* /*theViewer*/)
+{
+  myPreSelection.clear();
+
+  // Check that the selected result are not results of operation feature
+  QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
+  FeaturePtr aFeature = feature();
+  if (aFeature) {
+    std::list<ResultPtr> aResults = aFeature->results();
+    QList<ObjectPtr> 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);
+    }
+  } else
+    myPreSelection = aSelected;
+}
+
+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(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
+  bool isApplyed = aActiveWgt->setValue(aValue);
+
+  delete aValue;
+  myIsModified = (myIsModified || isApplyed);
+  return isApplyed;
+}
+
+
+void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
+{ 
+  myPropertyPanel = theProp; 
+  connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
+          SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
+}