#13987 PDM: Make loadData() virtual on RimPolygonContainer

Replace the dynamic_cast dispatch in RimPolygonCollection::loadData() with a
virtual loadData() on RimPolygonContainer. The base recurses through
subCollections; RimPolygonFile overrides to load file data. The folder
RimPolygonCollection no longer needs its own loadData().

Move RimPolygonFile::canAddSubCollection() and RimPolygonInView::sourceItem()
implementations from headers to .cpp.

Simplify std::unexpected calls in RimcNestedCollectionBase by removing
redundant explicit QString construction.
This commit is contained in:
Magne Sjaastad committed 2026-05-13 14:53:00 +02:00
1 parent 2010ba99b3
commit 3375e37ffc
9 files changed
+36 -25

No files matched your search

@@ -63,25 +63,6 @@ RimPolygonCollection* RimPolygonCollection::createTopmost()
return coll;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimPolygonCollection::loadData()
{
for ( auto* sub : subCollections() )
{
if ( !sub ) continue;
if ( auto* file = dynamic_cast<RimPolygonFile*>( sub ) )
{
file->loadData();
}
else if ( auto* coll = dynamic_cast<RimPolygonCollection*>( sub ) )
{
coll->loadData();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -39,7 +39,6 @@ public:
// constructor.
static RimPolygonCollection* createTopmost();
void loadData();
RimPolygon* createUserDefinedPolygon();
RimPolygon* appendUserDefinedPolygon();
void addUserDefinedPolygon( RimPolygon* polygon );
@@ -43,3 +43,14 @@ RimPolygonContainer* RimPolygonContainer::addNewSubCollection()
addSubCollection( sub );
return sub;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimPolygonContainer::loadData()
{
for ( auto* sub : subCollections() )
{
if ( sub ) sub->loadData();
}
}
@@ -44,4 +44,8 @@ public:
// the return type stays at RimPolygonContainer* to avoid pulling RimPolygonCollection.h into
// this header (which would create an include cycle).
RimPolygonContainer* addNewSubCollection() override;
// Default behavior recurses into sub-collections. Leaf containers (e.g., file-backed)
// override to load their own data; folder containers inherit the recursion.
virtual void loadData();
};
@@ -140,6 +140,14 @@ QString RimPolygonFile::name() const
return fileInfo.fileName();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimPolygonFile::canAddSubCollection() const
{
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -34,14 +34,14 @@ public:
void setFileName( const QString& fileName );
void loadData();
void loadData() override;
std::vector<RimPolygon*> polygons() const;
QString name() const;
// A file is a leaf folder. Refuse to grow sub-folders inside it.
bool canAddSubCollection() const override { return false; }
bool canAddSubCollection() const override;
RimPolygonContainer* addNewSubCollection() override;
protected:
@@ -82,6 +82,14 @@ RimPolygon* RimPolygonInView::polygon() const
return m_polygon();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimPolygon* RimPolygonInView::sourceItem() const
{
return polygon();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -52,7 +52,7 @@ public:
RimPolygonInView();
RimPolygon* polygon() const;
RimPolygon* sourceItem() const { return polygon(); }
RimPolygon* sourceItem() const;
void setPolygon( RimPolygon* polygon );
void updateTargetsFromPolygon();
@@ -42,13 +42,13 @@ std::expected<caf::PdmObjectHandle*, QString> RimcNestedCollectionBase::execute(
auto* coll = self<caf::PdmNestedCollectionBase>();
if ( !coll || !coll->canAddSubCollection() )
{
return std::unexpected<QString>( QString( "Cannot add subfolder" ) );
return std::unexpected( "Cannot add subfolder" );
}
caf::PdmObject* added = coll->addNewSubCollection();
if ( !added )
{
return std::unexpected<QString>( QString( "Failed to add subfolder" ) );
return std::unexpected( "Failed to add subfolder" );
}
if ( auto* asNested = dynamic_cast<caf::PdmNestedCollectionBase*>( added ) )