Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
index c3a0bd6fec3cef5b19649dc6fc7ba1f7b22689a9..bfe58446d370168a50092075ea228460c6ca7c25 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * ModuleBase_Operation.cpp
  *
 
 #include "ModuleBase_Operation.h"
 
+#include "ModuleBase_OperationDescription.h"
+#include "ModuleBase_ModelWidget.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>
 #include <ModelAPI_Feature.h>
-#include <ModelAPI_Object.h>
-#include <ModelAPI_PluginManager.h>
+#include <ModelAPI_Data.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>
+
+#include <QTimer>
 
 #ifdef _DEBUG
 #include <QDebug>
 #endif
 
-/*!
- \brief Constructor
- \param XGUI_Workshop - workshop for this operation
-
- Constructs an empty operation. Constructor should work very fast because many
- operators may be created after starting workshop but only several from them
- may be used. As result this constructor stores given workshop in myApp field
- and set Waiting status.
- */
-ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* parent)
-    : QObject(parent),
-      myFlags(Transaction),
-      myState(Waiting),
-      myExecStatus(Rejected),
-      myOperationId(theId)
+ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
+    : QObject(theParent),
+      myIsEditing(false),
+      myIsModified(false),
+      myPropertyPanel(NULL)
 {
+  myDescription = new ModuleBase_OperationDescription(theId);
 }
 
-/*!
- * \brief Destructor
- */
 ModuleBase_Operation::~ModuleBase_Operation()
 {
-
+  delete myDescription;
+  clearPreselection();
 }
 
-/*!
- * \brief Unique name of the operation
- *
- *  Returns string name of the operation.
- */
-QString ModuleBase_Operation::operationId() const
+QString ModuleBase_Operation::id() const
 {
-  return myOperationId;
+  return getDescription()->operationId();
 }
 
-/*!
- * \brief Gets state of operation
- * \return Value from OperationState enumeration
- *
- * Gets state of operation (see OperationState enumeration)
- */
-ModuleBase_Operation::OperationState ModuleBase_Operation::state() const
+FeaturePtr ModuleBase_Operation::feature() const
 {
-  return myState;
+  return myFeature;
 }
 
-/*!
- * \brief Verifies whether operation is an ran one (state()==Running)
- * \return TRUE if operation is active, FALSE otherwise
- *
- * Verifies whether operation is an running. Returns TRUE if state of operator
- * is Running
- */
-bool ModuleBase_Operation::isRunning() const
+bool ModuleBase_Operation::isValid() const
 {
-  return state() == Running;
+  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);
 }
 
-/*!
- * \brief Verifies whether given operator is valid for this one
- * \param theOtherOp - other operation
- * \return Returns TRUE if the given operator is valid for this one
- *
- * Verifies whether given operator is valid for this one (i.e. can be started "above"
- * this operator)
- */
-bool ModuleBase_Operation::isValid(ModuleBase_Operation*) const
-{
-  return false;
-}
 
-/*!
- * \brief Verifies whether this operator can be always started above any already running one
- * \return Returns TRUE if current operation must not be checked for ActiveOperation->IsValid( this )
- *
- * This method must be redefined in derived operation if operation of derived class
- * must be always can start above any launched one. Default implementation returns FALSE,
- * so it is being checked for IsValid, but some operations may overload IsGranted()
- * In this case they will always start, no matter what operation is running.
- */
-bool ModuleBase_Operation::isGranted() const
+bool ModuleBase_Operation::canBeCommitted() const
 {
-  return false;
+  return isValid();
 }
 
-/*
- * Returns pointer to the root document.
- */
-std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
+void ModuleBase_Operation::flushUpdated()
 {
-  return ModelAPI_PluginManager::get()->rootDocument();
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
 }
 
-/*!
- * \brief Sets slot which is called when operation is started
- * \param theReceiver - object containing slot
- * \param theSlot - slot of theReceiver object
- * \return TR if slot was connected successfully, FALSE otherwise
- *
- * Sets slot which is called when operation is started. There is no point in
- * using this method. It would be better to inherit own operator from base
- * one and redefine startOperation method
- */
-bool ModuleBase_Operation::setSlot(const QObject* theReceiver, const char* theSlot)
+void ModuleBase_Operation::flushCreated()
 {
-  return connect(this, SIGNAL(callSlot()), theReceiver, theSlot);
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
 }
 
-/*!
- * \brief Sets the flags of operation
- * \param f - flags of operation to be set
- *
- *  Sets flags of operation (see Flags enumeration)
- */
-void ModuleBase_Operation::setFlags(const int f)
+FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
 {
-  myFlags = myFlags | f;
-}
+  if (myParentFeature.get()) {
+    myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
+  } else {
+    std::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.
+    //myFeature->execute();
+    // Init default values
+    /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
+     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
+     for (; anIt != aLast; anIt++) {
+     (*anIt)->storeValue(aFeature);
+     }*/
+  }
 
-/*!
- * \brief Clears the flags of operation
- * \param f - flags of operation to be cleared
- *
- *  Clears flags of operation (see Flags enumeration)
- */
-void ModuleBase_Operation::clearFlags(const int f)
-{
-  myFlags = myFlags & ~f;
+  if (theFlushMessage)
+    flushCreated();
+  return myFeature;
 }
 
-/*!
- * \brief Test the flags of operation
- * \param f - flags of operation to be tested
- *
- *  Returns TRUE if the specified flags set in the operation (see Flags enumeration)
- */
-bool ModuleBase_Operation::testFlags(const int f) const
+void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
 {
-  return (myFlags & f) == f;
+  myFeature = theFeature;
+  myIsEditing = true;
 }
 
-/*!
- * \brief Gets execution status
- * \return Execution status
- *
- * Gets execution status
- */
-int ModuleBase_Operation::execStatus() const
+bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
 {
-  return myExecStatus;
+  FeaturePtr aFeature = feature();
+  if (aFeature) {
+    if (aFeature == theObj)
+      return true;
+    std::list<ResultPtr> aResults = aFeature->results();
+    std::list<ResultPtr>::const_iterator aIt;
+    for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+      if (theObj == (*aIt))
+        return true;
+    }
+  }
+  return false;
 }
 
-/*!
- *  \brief Returns XML representation of the operation's widget.
- *  \return XML QString
- *
- *  Returns XML representation of the operation's widget.
- */
-const QString& ModuleBase_Operation::xmlRepresentation() const
-{
-  return myXmlRepr;
-}
 
-/*!
- *  \brief Sets XML representation of the operation's widget.
- *  \param xmlRepr - XML QString
- *
- *  Sets XML representation of the operation's widget.
- */
-void ModuleBase_Operation::setXmlRepresentation(const QString& xmlRepr)
+std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
 {
-  myXmlRepr = xmlRepr;
+  return ModelAPI_Session::get()->moduleDocument();
 }
 
-/*!
- * \brief Starts operation
- *
- * Public slot. Verifies whether operation can be started and starts operation.
- * This slot is not virtual and cannot be redefined. Redefine startOperation method
- * to change behavior of operation. There is no point in using this method. It would
- * be better to inherit own operator from base one and redefine startOperation method
- * instead.
- */
+
 void ModuleBase_Operation::start()
 {
-  //document()->start(this);
-  document()->startOperation();
+  QString anId = getDescription()->operationId();
+  ModelAPI_Session::get()->startOperation(anId.toStdString());
+
+  if (!myIsEditing)
+    createFeature();
 
   startOperation();
   emit started();
+
+}
+
+void ModuleBase_Operation::postpone()
+{
+  postponeOperation();
+  emit postponed();
+}
+
+void ModuleBase_Operation::resume()
+{
+  resumeOperation();
+  emit resumed();
 }
 
-/*!
- * \brief Aborts operation
- *
- * Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
- * Redefine abortOperation method to change behavior of operation instead
- */
 void ModuleBase_Operation::abort()
 {
   abortOperation();
-  myState = Waiting;
   emit aborted();
 
   stopOperation();
-  emit stopped();
 
-  document()->abortOperation();
+  ModelAPI_Session::get()->abortOperation();
+  emit stopped();
 }
 
-/*!
- * \brief Commits operation
- *
- * Public slot. Commits operation. This slot is not virtual and cannot be redefined.
- * Redefine commitOperation method to change behavior of operation instead
- */
-void ModuleBase_Operation::commit()
+bool ModuleBase_Operation::commit()
 {
-  commitOperation();
-  myState = Waiting;
-  emit committed();
-
-  stopOperation();
-  emit stopped();
+  if (canBeCommitted()) {
+    commitOperation();
+    // 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
+    if (ModelAPI_Session::get()->isModified())
+      ModelAPI_Session::get()->finishOperation();
+    else
+      ModelAPI_Session::get()->abortOperation();
+
+    stopOperation();
+    emit stopped();
+    emit committed();
+
+    afterCommitOperation();
+    return true;
+  }
+  return false;
+}
 
-  document()->finishOperation();
+void ModuleBase_Operation::setRunning(bool theState)
+{
+  if (!theState) {
+    abort();
+  }
 }
 
-/*!
- * \brief Stores a real value in model.
- * \param theValue - to store
- *
- * Public slot. Passes theValue into the model.
- */
-void ModuleBase_Operation::storeReal(double theValue)
+void ModuleBase_Operation::activateByPreselection()
 {
-  if(!myFeature){
-    #ifdef _DEBUG
-    qDebug() << "ModuleBase_Operation::storeReal: " <<
-        "trying to store value without opening a transaction.";
-    #endif
+  if (!myPropertyPanel || myPreSelection.empty()) {
+    myPropertyPanel->activateNextWidget(NULL);
     return;
   }
-  QString anId = sender()->objectName();
-  std::shared_ptr<ModelAPI_Object> aData = myFeature->data();
-  std::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
-  aReal->setValue(theValue);
+  const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
+  if (aWidgets.empty()) {
+    myPropertyPanel->activateNextWidget(NULL);
+    return;
+  }
+  
+  ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
+  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) {
+    aWgt = (*aWIt);
+    ModuleBase_ViewerPrs aValue = (*aPIt);
+    if (!aWgt->canSetValue())
+      continue;
+
+    ++aPIt;
+    if (!aWgt->setSelection(aValue)) {
+      isSet = false;
+      break;
+    } else {
+      isSet = true;
+      aFilledWgt = aWgt;
+    }
+  }
+
+  myPropertyPanel->activateNextWidget(aFilledWgt);
+  if (aFilledWgt)
+    emit activatedByPreselection();
+
 }
 
-/*!
- * \brief Verifies whether operator is ready to start.
- * \return TRUE if operation is ready to start
- *
- * Default implementation returns TRUE. Redefine this method to add own verifications
- */
-bool ModuleBase_Operation::isReadyToStart() const
+void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
 {
-  return true;
+  myParentFeature = theParent;
 }
 
-/*!
- * \brief Virtual method called when operation is started
- *
- * Virtual method called when operation started (see start() method for more description)
- * Default implementation calls corresponding slot and commits immediately.
- */
-void ModuleBase_Operation::startOperation()
+CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
 {
-  std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
-  myFeature = aDoc->addFeature(myOperationId.toStdString());
-  myFeature->execute();
-  //emit callSlot();
-  //commit();
+  return myParentFeature;
 }
 
-/*!
- * \brief Virtual method called when operation is started
- *
- * Virtual method called when operation stopped - committed or aborted.
- */
-void ModuleBase_Operation::stopOperation()
+void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
+                                         ModuleBase_IViewer* theViewer)
 {
+  clearPreselection();
+
+  QList<ModuleBase_ViewerPrs> aPreSelected;
+  // Check that the selected result are not results of operation feature
+  FeaturePtr aFeature = feature();
+  if (aFeature) {
+    QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
+
+    std::list<ResultPtr> aResults = aFeature->results();
+    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))
+        aPreSelected.append(aPrs);
+    }
+  } else
+    aPreSelected = theSelection->getSelected();
+
+  // 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;
 }
 
-/*!
- * \brief Virtual method called when operation aborted
- *
- * Virtual method called when operation aborted (see abort() method for more description)
- */
-void ModuleBase_Operation::abortOperation()
+//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)
 {
+  return false;
 }
 
-/*!
- * \brief Virtual method called when operation committed
- *
- * Virtual method called when operation committed (see commit() method for more description)
- */
-void ModuleBase_Operation::commitOperation()
+void ModuleBase_Operation::clearPreselection()
 {
-  myFeature->execute();
+  myPreSelection.clear();
 }
 
-/*!
- * \brief Sets execution status
- * \param theStatus - execution status
- *
- * Sets myExecStatus to the given value
- */
-void ModuleBase_Operation::setExecStatus(const int theVal)
-{
-  myExecStatus = (ExecStatus) theVal;
+void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
+{ 
+  myPropertyPanel = theProp; 
+  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();
 }
 
-/*!
- * \brief Sets state of operation
- * \param theState - state of operation to be set
- *
- *  Sets state of operation (see OperationState enumeration)
- */
-void ModuleBase_Operation::setState(const ModuleBase_Operation::OperationState theState)
+bool ModuleBase_Operation::isGranted(QString theId) const
 {
-  myState = theState;
+  return myNestedFeatures.contains(theId);
 }