Salome HOME
Improve Model package coverage: remove unused methods, open/save tests improvements
[modules/shaper.git] / src / Model / Model_Data.h
index c41f1a5ab58b34f4ac22eac1baf811149d8341ba..b4cc048a7929dd15af71583d2fd4f6b930796e5b 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        Model_Data.hxx
-// Created:     21 Mar 2014
-// Author:      Mikhail PONIKAROV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #ifndef Model_Data_H_
 #define Model_Data_H_
 #include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeRefAttrList.h>
 #include <ModelAPI_AttributeString.h>
+#include <ModelAPI_AttributeStringArray.h>
 #include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Feature.h>
+#include <ModelAPI_Folder.h>
 #include <ModelAPI_Object.h>
 
 #include <TDF_Label.hxx>
@@ -43,9 +59,12 @@ class ModelAPI_Attribute;
 
 class Model_Data : public ModelAPI_Data
 {
+  typedef std::map<std::string, std::pair<std::shared_ptr<ModelAPI_Attribute>, int> > AttributeMap;
+
   TDF_Label myLab;  ///< label of the feature in the document
   /// All attributes of the object identified by the attribute ID
-  std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
+  /// (the attribute is stored together with its index in the feature)
+  AttributeMap myAttrs;
   /// Array of flags of this data
   Handle(TDataStd_BooleanArray) myFlags;
 
@@ -76,6 +95,8 @@ class Model_Data : public ModelAPI_Data
   friend class Model_AttributeSelectionList;
   friend class Model_ValidatorsFactory;
   friend class Model_SelectionNaming;
+  friend class Model_ResultConstruction;
+  friend class Model_ResultBody;
 
  public:
   /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
@@ -84,6 +105,8 @@ class Model_Data : public ModelAPI_Data
   MODEL_EXPORT virtual std::string name();
   /// Defines the name of the feature visible by the user in the object browser
   MODEL_EXPORT virtual void setName(const std::string& theName);
+  /// Return \c true if the object has been renamed by the user
+  MODEL_EXPORT virtual bool hasUserDefinedName() const;
   /// Returns the attribute that references to another document
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
   /// Returns the attribute that contains real value with double precision
@@ -101,7 +124,7 @@ class Model_Data : public ModelAPI_Data
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
     selection(const std::string& theID);
   /// Returns the attribute that contains selection to a shape
-  MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
+  MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
     selectionList(const std::string& theID);
   /// Returns the attribute that contains reference to an attribute of a feature
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
@@ -122,6 +145,12 @@ class Model_Data : public ModelAPI_Data
   /// Returns the attribute that contains integer values array
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
     intArray(const std::string& theID);
+  /// Returns the attribute that contains string values array
+  MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeStringArray>
+    stringArray(const std::string& theID);
+  /// Returns the attribute that contains string values array
+  MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeTables>
+    tables(const std::string& theID);
 
   /// Returns the generic attribute by identifier
   /// \param theID identifier of the attribute
@@ -139,13 +168,13 @@ class Model_Data : public ModelAPI_Data
   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
   /// Returns true if data belongs to same features
   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
-  /// Returns true if it is correctly connected t othe data model
+  /// Returns true if it is correctly connected tthe data model
   MODEL_EXPORT virtual bool isValid();
 
   /// Returns the label where the shape must be stored (used in ResultBody)
-  TDF_Label& shapeLab()
+  TDF_Label shapeLab() const
   {
-    return myLab;
+    return myLab.IsNull() ? myLab : myLab.Father().FindChild(2);
   }
 
   /// Initializes object by the attributes: must be called just after the object is created
@@ -153,7 +182,7 @@ class Model_Data : public ModelAPI_Data
   /// \param theID identifier of the attribute that can be referenced by this ID later
   /// \param theAttrType type of the created attribute (received from the type method)
   /// \returns the just created attribute
-  MODEL_EXPORT virtual AttributePtr 
+  MODEL_EXPORT virtual AttributePtr
     addAttribute(const std::string& theID, const std::string theAttrType);
 
   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
@@ -164,7 +193,8 @@ class Model_Data : public ModelAPI_Data
   /// \param theSendMessage if false, it does not send the update message
   ///            even if something is changed
   ///            (normally is it used in attributeChanged because this message will be sent anyway)
-  MODEL_EXPORT virtual void blockSendAttributeUpdated(
+  /// \returns the previous state of block
+  MODEL_EXPORT virtual bool blockSendAttributeUpdated(
     const bool theBlock, const bool theSendMessage = true);
 
   /// Puts feature to the document data sub-structure
@@ -201,15 +231,15 @@ class Model_Data : public ModelAPI_Data
   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
 
   /// returns all references by attributes of this data
-  /// \param theRefs returned list of pairs: 
+  /// \param theRefs returned list of pairs:
   ///                id of referenced attribute and list of referenced objects
   MODEL_EXPORT virtual void referencesToObjects(
     std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
 
-  /// Copies all atributes content into theTarget data
+  /// Copies all attributes content into theTarget data
   MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
 
-  /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
+  /// Returns the invalid data pointer (to avoid working with NULL shared pointers in swig)
   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
 
   /// Returns the invalid data pointer: static method
@@ -233,51 +263,61 @@ protected:
   /// Defines the custom "is in history" behavior
   MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
 
-  /// Returns true if the object is deleted, but some data is still keept in memory
+  /// Returns true if the object is deleted, but some data is still kept in memory
   MODEL_EXPORT virtual bool isDeleted();
 
-  /// Sets true if the object is deleted, but some data is still keept in memory
+  /// Sets true if the object is deleted, but some data is still kept in memory
   MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
 
 private:
-  /// Removes all information about back references
-  void eraseBackReferences();
   /// Removes a back reference (with identifier which attribute references to this object)
   /// \param theFeature feature referenced to this
   /// \param theAttrID identifier of the attribute that is references from theFeature to this
-  void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
+  void removeBackReference(ObjectPtr theObject, std::string theAttrID);
   /// Removes a back reference (by the attribute)
   /// \param theAttr the referenced attribute
   void removeBackReference(AttributePtr theAttr);
   /// Adds a back reference (with identifier which attribute references to this object
   /// \param theFeature feature referenced to this
   /// \param theAttrID identifier of the attribute that is references from theFeature to this
-  /// \param theApplyConcealment applies consealment flag changes
-  void addBackReference(FeaturePtr theFeature, std::string theAttrID, 
+  /// \param theApplyConcealment applies concealment flag changes
+  void addBackReference(FeaturePtr theFeature, std::string theAttrID,
     const bool theApplyConcealment = true);
+  /// Adds a back reference to an object
+  /// \param theObject object referenced to this
+  /// \param theAttrID identifier of the attribute that is references from theFolder to this
+  void addBackReference(ObjectPtr theObject, std::string theAttrID);
 
   /// Makes the concealment flag up to date for this object-owner.
   MODEL_EXPORT virtual void updateConcealmentFlag();
 
   /// Returns true if object must be displayed in the viewer: flag is stored in the
   /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
-  /// the original state i nthe current transaction.
+  /// the original state ithe current transaction.
   MODEL_EXPORT virtual bool isDisplayed();
 
   /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
   /// signal.
   MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
+
+  /// Returns \c true if theAttribute1 is going earlier than theAttribute2 in the data
+  MODEL_EXPORT virtual bool isPrecedingAttribute(const std::string& theAttribute1,
+                                                 const std::string& theAttribute2) const;
 };
 
 /// Generic method to register back reference, used in referencing attributes.
 /// Without concealment change, it will be done later, on synchronization.
 #define ADD_BACK_REF(TARGET) \
   if (TARGET.get() != NULL) { \
+    std::shared_ptr<Model_Data> aTargetData = \
+        std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
     FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
-    if (anAttributeOwnerFeature.get()) { \
-      std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
-        (TARGET)->data()); \
+    if (anAttributeOwnerFeature.get()) \
       aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
+    else { \
+      FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
+      if (anAttributeOwnerFolder.get()) \
+        aTargetData->addBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \
     } \
   }
 
@@ -285,11 +325,15 @@ private:
 /// Without concealment change, it will be done later, on synchronization.
 #define REMOVE_BACK_REF(TARGET) \
   if (TARGET.get() != NULL) { \
+    std::shared_ptr<Model_Data> aTargetData = \
+        std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
     FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
-    if (anAttOwnerFeature.get()) { \
-      std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
-        (TARGET)->data()); \
+    if (anAttOwnerFeature.get()) \
       aTargetData->removeBackReference(anAttOwnerFeature, id()); \
+    else { \
+      FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
+      if (anAttributeOwnerFolder.get()) \
+        aTargetData->removeBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \
     } \
   }