#15008 Python: Typed add_folder() and collection_name() for nested collections

The Python generator emits add_folder() again on a concrete collection
class when the folder type narrows: SurfaceCollection.add_folder()
returns SurfaceCollection and PolygonCollection.add_folder() returns
PolygonCollection, while PdmNestedCollectionBase keeps the declared type.
PdmObjectMethod::classKeywordReturnedTypeForSelf() reports the narrowed
type, and the super class method check in the generator accepts such an
override (same arguments, subclass return type).

PdmNestedCollectionBase: subCollectionClassKeyword() names the folder
type without creating one, addNewSubCollection() is a template method
over createSubCollection() and returns PdmNestedCollectionBase*, and the
base virtuals are pure. The leaf polygon containers only override
canAddSubCollection().

collection_name() in rips returns the folder name for every nested
collection; the class specific name attributes are kept.
This commit is contained in:
Stefan Crooijmans authored and Kristian Bendiksen committed 2026-11-05 08:50:36 +01:00
1 parent 6be8efb752
commit c0dcb78824
24 files changed
+408 -71

No files matched your search

@@ -281,14 +281,6 @@ bool RimPolygonCloudAddress::canAddSubCollection() const
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonCloudAddress::addNewSubCollection()
{
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -83,8 +83,7 @@ public:
QString name() const;
bool canAddSubCollection() const override;
RimPolygonContainer* addNewSubCollection() override;
bool canAddSubCollection() const override;
protected:
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
@@ -54,11 +54,3 @@ bool RimPolygonCloudFolder::canAddSubCollection() const
{
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonCloudFolder::addNewSubCollection()
{
return nullptr;
}
@@ -35,6 +35,5 @@ public:
RimPolygonCloudFolder();
explicit RimPolygonCloudFolder( const QString& folderName );
bool canAddSubCollection() const override;
RimPolygonContainer* addNewSubCollection() override;
bool canAddSubCollection() const override;
};
@@ -201,14 +201,6 @@ bool RimPolygonCloudSource::canAddSubCollection() const
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonCloudSource::addNewSubCollection()
{
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -78,8 +78,7 @@ public:
// other than this source's own applied base one.
QString nameForRealization( int realization ) const;
bool canAddSubCollection() const override;
RimPolygonContainer* addNewSubCollection() override;
bool canAddSubCollection() const override;
bool supportsRealizationOverride() const override;
std::vector<int> availableRealizationIdsForOverride() const override;
@@ -40,7 +40,16 @@ RimPolygonContainer::RimPolygonContainer()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonContainer::addNewSubCollection()
QString RimPolygonContainer::subCollectionClassKeyword() const
{
if ( !canAddSubCollection() ) return {};
return RimPolygonCollection::classKeywordStatic();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonContainer::createSubCollection()
{
auto* sub = new RimPolygonCollection();
addSubCollection( sub );
@@ -39,9 +39,8 @@ class RimPolygonContainer : public caf::PdmNestedCollection<RimPolygonContainer,
public:
RimPolygonContainer();
// "Add Folder" should produce a real RimPolygonCollection, not another container shell.
// Return type stays RimPolygonContainer* to avoid an include cycle.
RimPolygonContainer* addNewSubCollection() override;
// "Add Folder" produces a real RimPolygonCollection, not another container shell.
QString subCollectionClassKeyword() const override;
// Default recurses into sub-collections; leaf containers override to load their own data.
virtual void loadData();
@@ -63,6 +62,9 @@ public:
void ensureUniquePolygonName( RimPolygon* polygon );
protected:
// Return type stays RimPolygonContainer* to avoid an include cycle.
RimPolygonContainer* createSubCollection() override;
// Enforces name uniqueness among sibling folders when the folder is renamed.
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;
};
@@ -155,14 +155,6 @@ bool RimPolygonFile::canAddSubCollection() const
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygonContainer* RimPolygonFile::addNewSubCollection()
{
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -42,8 +42,7 @@ public:
QString name() const;
// A file is a leaf folder. Refuse to grow sub-folders inside it.
bool canAddSubCollection() const override;
RimPolygonContainer* addNewSubCollection() override;
bool canAddSubCollection() const override;
protected:
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
@@ -20,6 +20,8 @@
#include "RiaNameUniquenessTools.h"
#include "RimcDataContainerString.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmNestedCollectionBase.h"
@@ -50,6 +52,7 @@ RimcNestedCollectionBase::RimcNestedCollectionBase( caf::PdmObjectHandle* self )
std::expected<caf::PdmObjectHandle*, QString> RimcNestedCollectionBase::execute()
{
auto* coll = self<caf::PdmNestedCollectionBase>();
// Checked before the name conflict policy runs, since OVERWRITE deletes an existing folder.
if ( !coll || !coll->canAddSubCollection() )
{
return std::unexpected( "Cannot add subfolder" );
@@ -69,16 +72,13 @@ std::expected<caf::PdmObjectHandle*, QString> RimcNestedCollectionBase::execute(
}
}
caf::PdmObject* added = coll->addNewSubCollection();
caf::PdmNestedCollectionBase* added = coll->addNewSubCollection();
if ( !added )
{
return std::unexpected( "Failed to add subfolder" );
}
if ( auto* asNested = dynamic_cast<caf::PdmNestedCollectionBase*>( added ) )
{
asNested->setCollectionName( resolution.nameToUse );
}
added->setCollectionName( resolution.nameToUse );
coll->uiCapability()->updateConnectedEditors();
return added;
}
@@ -88,10 +88,54 @@ std::expected<caf::PdmObjectHandle*, QString> RimcNestedCollectionBase::execute(
//--------------------------------------------------------------------------------------------------
QString RimcNestedCollectionBase::classKeywordReturnedType() const
{
// Returned at codegen time. The Python generator emits AddFolder on the
// PdmNestedCollectionBase Python class (since that is where the method is registered),
// so the declared return type must be stable across all concrete nested collections.
// The actual runtime instance is whatever the concrete addNewSubCollection() returns;
// callers can use descendants() / class_from_keyword to recover the precise type.
// The method is registered on PdmNestedCollectionBase, so this is the type the Python generator
// declares for PdmNestedCollectionBase.add_folder(). It must not depend on self(): the generator
// creates the method once per concrete collection and would otherwise keep the type of whichever
// collection it visited last. The concrete type is reported by classKeywordReturnedTypeForSelf()
// and emitted as a typed override on the concrete class.
return caf::PdmNestedCollectionBase::classKeywordStatic();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimcNestedCollectionBase::classKeywordReturnedTypeForSelf() const
{
auto* coll = self<caf::PdmNestedCollectionBase>();
if ( !coll ) return classKeywordReturnedType();
QString keyword = coll->subCollectionClassKeyword();
return keyword.isEmpty() ? classKeywordReturnedType() : keyword;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( caf::PdmNestedCollectionBase, RimcNestedCollectionBase_collectionName, "CollectionNameInternal" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcNestedCollectionBase_collectionName::RimcNestedCollectionBase_collectionName( caf::PdmObjectHandle* self )
: caf::PdmObjectMethod( self, PdmObjectMethod::NullPointerType::NULL_IS_INVALID, PdmObjectMethod::ResultType::PERSISTENT_FALSE )
{
CAF_PDM_InitObject( "Collection Name", "", "", "Name of the folder" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<caf::PdmObjectHandle*, QString> RimcNestedCollectionBase_collectionName::execute()
{
auto* coll = self<caf::PdmNestedCollectionBase>();
if ( !coll ) return std::unexpected( "Not a folder" );
auto* result = new RimcDataContainerString();
result->m_stringValues = { coll->collectionName() };
return result;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimcNestedCollectionBase_collectionName::classKeywordReturnedType() const
{
return RimcDataContainerString::classKeywordStatic();
}
@@ -43,8 +43,26 @@ public:
std::expected<caf::PdmObjectHandle*, QString> execute() override;
QString classKeywordReturnedType() const override;
QString classKeywordReturnedTypeForSelf() const override;
private:
caf::PdmField<QString> m_folderName;
caf::PdmField<caf::AppEnum<RiaDefines::NameConflictPolicy>> m_onNameConflict;
};
//==================================================================================================
///
/// Name of the folder, the same for every nested collection whichever class owns the name field.
/// Exposed in Python as PdmNestedCollectionBase.collection_name() (rips/nested_collection.py).
///
//==================================================================================================
class RimcNestedCollectionBase_collectionName : public caf::PdmObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcNestedCollectionBase_collectionName( caf::PdmObjectHandle* self );
std::expected<caf::PdmObjectHandle*, QString> execute() override;
QString classKeywordReturnedType() const override;
};