From 27816701ee9b526a56e24a564efa319e66ff5a78 Mon Sep 17 00:00:00 2001 From: mpv Date: Tue, 11 Jun 2019 17:03:22 +0300 Subject: [PATCH] Support of open/save and undo/redo for filters feature --- src/FiltersPlugin/FiltersPlugin_Selection.cpp | 28 +++++++++++++------ src/FiltersPlugin/FiltersPlugin_Selection.h | 7 ++--- src/Model/Model_Data.cpp | 28 +++++++++++++++++-- src/Model/Model_Data.h | 5 ++++ src/Model/Model_Objects.cpp | 18 ++++++++---- 5 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/FiltersPlugin/FiltersPlugin_Selection.cpp b/src/FiltersPlugin/FiltersPlugin_Selection.cpp index 4b656d847..e2dd91f01 100644 --- a/src/FiltersPlugin/FiltersPlugin_Selection.cpp +++ b/src/FiltersPlugin/FiltersPlugin_Selection.cpp @@ -83,14 +83,6 @@ std::list FiltersPlugin_Selection::filterArgs(const std::string th void FiltersPlugin_Selection::setAttribute(const AttributePtr& theAttr) { - if (myBase != theAttr) { // clear all filters if the attribute is changed or defined a new - std::list aFilters; - data()->allGroups(aFilters); - std::list::iterator aFIter = aFilters.begin(); - for(; aFIter != aFilters.end(); aFIter++) { - data()->removeAttributes(*aFIter); - } - } myBase = theAttr; if (myBase.get()) { // check that the type of sub-elements is supported by all existing filters std::shared_ptr aSelList = @@ -116,3 +108,23 @@ const AttributePtr& FiltersPlugin_Selection::baseAttribute() const { return myBase; } + +void FiltersPlugin_Selection::initAttributes() +{ + ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters(); + std::list aFilters; + data()->allGroups(aFilters); + for(std::list::iterator aFIt = aFilters.begin(); aFIt != aFilters.end(); aFIt++) { + FilterPtr aFilter = aFactory->filter(*aFIt); + if (aFilter.get()) { + std::shared_ptr aBool = + std::dynamic_pointer_cast(data()->addFloatingAttribute( + kReverseAttrID, ModelAPI_AttributeBoolean::typeId(), *aFIt)); + aBool->setValue(false); // not reversed by default + ModelAPI_FiltersArgs anArgs; + anArgs.setFeature(std::dynamic_pointer_cast(data()->owner())); + anArgs.setFilter(*aFIt); + aFilter->initAttributes(anArgs); + } + } +} diff --git a/src/FiltersPlugin/FiltersPlugin_Selection.h b/src/FiltersPlugin/FiltersPlugin_Selection.h index 0e68ac9bb..a9e62517f 100644 --- a/src/FiltersPlugin/FiltersPlugin_Selection.h +++ b/src/FiltersPlugin/FiltersPlugin_Selection.h @@ -51,12 +51,12 @@ public: /// Computes a selection? FILTERS_EXPORT virtual void execute() {} - /// Initially there are no filters selected, so, no attributes - virtual void initAttributes() {} - /// Feature is created in the plugin manager FiltersPlugin_Selection() {} + /// This method initializes all filters on open of document + FILTERS_EXPORT virtual void initAttributes() override; + // methods related to the filters management /// Adds a filter to the feature. Also initializes arguments of this filter. @@ -89,7 +89,6 @@ public: protected: AttributePtr myBase; ///< the attribute related to this filter - }; #endif diff --git a/src/Model/Model_Data.cpp b/src/Model/Model_Data.cpp index 16355161a..dad2068da 100644 --- a/src/Model/Model_Data.cpp +++ b/src/Model/Model_Data.cpp @@ -240,6 +240,21 @@ AttributePtr Model_Data::addFloatingAttribute( int anIndex; TDF_Label aLab; if (myLab.IsAttribute(TDF_TagSource::GetID())) { + // check this is re-init of attributes, so, check attribute with this name already there + TDF_ChildIDIterator anIter(myLab, kGroupAttributeID, false); + for(; anIter.More(); anIter.Next()) { + TCollection_AsciiString aThisName(Handle(TDataStd_Name)::DownCast(anIter.Value())->Get()); + if (theID == aThisName.ToCString()) { + TDF_Label aLab = anIter.Value()->Label(); + Handle(TDataStd_Name) aGName; + if (aLab.FindAttribute(kGroupAttributeGroupID, aGName)) { + TCollection_AsciiString aGroupName(aGName->Get()); + if (theGroup == aGroupName.ToCString()) { + return addAttribute(theGroup + "__" + theID, theAttrType, aLab.Tag()); + } + } + } + } aLab = myLab.NewChild(); // already exists a floating attribute, create the next anIndex = aLab.Tag(); } else { // put the first floating attribute, quite far from other standard attributes @@ -289,11 +304,12 @@ void Model_Data::removeAttributes(const std::string& theGroup) Handle(TDataStd_Name) aGroupID = Handle(TDataStd_Name)::DownCast(aGroup.Value()); if (aGroupID->Get().IsEqual(theGroup.c_str())) { Handle(TDataStd_Name) anID; - if (aGroup.Value()->Label().FindAttribute(kGroupAttributeID, anID)) { - TCollection_AsciiString anAsciiID(aGroupID->Get() + "__" + anID->Get()); - myAttrs.erase(anAsciiID.ToCString()); + if (!aGroup.Value()->Label().IsNull() && + aGroup.Value()->Label().FindAttribute(kGroupAttributeID, anID)) { aLabsToRemove.Append(aGroup.Value()->Label()); } + TCollection_AsciiString anAsciiID(aGroupID->Get() + "__" + anID->Get()); + myAttrs.erase(anAsciiID.ToCString()); } } for(TDF_LabelList::Iterator aLab(aLabsToRemove); aLab.More(); aLab.Next()) { @@ -301,6 +317,12 @@ void Model_Data::removeAttributes(const std::string& theGroup) } } +void Model_Data::clearAttributes() +{ + myAttrs.clear(); +} + + // macro for the generic returning of the attribute by the ID #define GET_ATTRIBUTE_BY_ID(ATTR_TYPE, METHOD_NAME) \ diff --git a/src/Model/Model_Data.h b/src/Model/Model_Data.h index 58c97bd4e..f2698eef9 100644 --- a/src/Model/Model_Data.h +++ b/src/Model/Model_Data.h @@ -288,6 +288,9 @@ protected: /// Sets true if the object is deleted, but some data is still kept in memory MODEL_EXPORT virtual void setIsDeleted(const bool theFlag); + /// Erases all attributes from myAttrs, but keeping them in the data structure + void clearAttributes(); + private: /// Removes a back reference (with identifier which attribute references to this object) /// \param theFeature feature referenced to this @@ -322,6 +325,8 @@ private: /// 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; + + friend class Model_Objects; }; /// Generic method to register back reference, used in referencing attributes. diff --git a/src/Model/Model_Objects.cpp b/src/Model/Model_Objects.cpp index 971f8a305..e95b44321 100644 --- a/src/Model/Model_Objects.cpp +++ b/src/Model/Model_Objects.cpp @@ -32,6 +32,8 @@ #include #include #include +#include + #include #include @@ -874,11 +876,17 @@ void Model_Objects::synchronizeFeatures( if (anUpdatedMap.Contains(aFeatureLabel)) { if (!theOpen) { // on abort/undo/redo reinitialize attributes if something is changed - std::list > anAttrs = - anObject->data()->attributes(""); - std::list >::iterator anAttr = anAttrs.begin(); - for(; anAttr != anAttrs.end(); anAttr++) - (*anAttr)->reinit(); + FiltersFeaturePtr aFilter = std::dynamic_pointer_cast(anObject); + if (aFilter.get()) { // for filters attributes may be added/removed on undo/redo + std::dynamic_pointer_cast(aFilter->data())->clearAttributes(); + aFilter->initAttributes(); + } else { + std::list > anAttrs = + anObject->data()->attributes(""); + std::list >::iterator anAttr = anAttrs.begin(); + for(; anAttr != anAttrs.end(); anAttr++) + (*anAttr)->reinit(); + } } ModelAPI_EventCreator::get()->sendUpdated(anObject, anUpdateEvent); if (aFeature && aFeature->getKind() == "Parameter") { -- 2.39.2