Refactor: reduce duplicated code for collections

This commit is contained in:
Kristian Bendiksen
2026-01-05 13:15:47 +01:00
parent 633edd8bba
commit bd7e5ac1b9
11 changed files with 666 additions and 681 deletions
@@ -1,4 +1,6 @@
set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RimIntervalCollection.h
${CMAKE_CURRENT_LIST_DIR}/RimIntervalCollection.inl
${CMAKE_CURRENT_LIST_DIR}/RimCompletionCellIntersectionCalc.h
${CMAKE_CURRENT_LIST_DIR}/RimFishbonesCollection.h
${CMAKE_CURRENT_LIST_DIR}/RimFishbones.h
@@ -18,14 +18,7 @@
#include "RimCustomSegmentIntervalCollection.h"
#include "RiaLogging.h"
#include "RimCustomSegmentInterval.h"
#include "cafCmdFeatureMenuBuilder.h"
#include "cafPdmUiTableViewEditor.h"
#include <algorithm>
#include <cmath>
CAF_PDM_SOURCE_INIT( RimCustomSegmentIntervalCollection, "CustomSegmentIntervalCollection" );
@@ -35,8 +28,7 @@ CAF_PDM_SOURCE_INIT( RimCustomSegmentIntervalCollection, "CustomSegmentIntervalC
RimCustomSegmentIntervalCollection::RimCustomSegmentIntervalCollection()
{
CAF_PDM_InitObject( "Custom Segment Intervals", ":/WellPathComponent16x16.png" );
CAF_PDM_InitFieldNoDefault( &m_intervals, "Intervals", "Intervals" );
CAF_PDM_InitFieldNoDefault( &m_items, "Intervals", "Intervals" );
}
//--------------------------------------------------------------------------------------------------
@@ -46,124 +38,6 @@ RimCustomSegmentIntervalCollection::~RimCustomSegmentIntervalCollection()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimCustomSegmentInterval*> RimCustomSegmentIntervalCollection::intervals() const
{
return m_intervals.childrenByType();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::addInterval( RimCustomSegmentInterval* interval )
{
if ( interval )
{
m_intervals.push_back( interval );
sortIntervalsByMD();
updateConnectedEditors();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::removeInterval( RimCustomSegmentInterval* interval )
{
if ( interval )
{
m_intervals.removeChild( interval );
delete interval;
updateConnectedEditors();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::removeAllIntervals()
{
m_intervals.deleteChildren();
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCustomSegmentInterval* RimCustomSegmentIntervalCollection::createInterval( double startMD, double endMD )
{
auto* interval = new RimCustomSegmentInterval();
interval->setStartMD( startMD );
interval->setEndMD( endMD );
addInterval( interval );
return interval;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCustomSegmentInterval* RimCustomSegmentIntervalCollection::createDefaultInterval()
{
auto* interval = new RimCustomSegmentInterval();
addInterval( interval );
return interval;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCustomSegmentInterval* RimCustomSegmentIntervalCollection::findIntervalAtMD( double md ) const
{
for ( auto* interval : intervals() )
{
if ( interval && interval->containsMD( md ) )
{
return interval;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimCustomSegmentIntervalCollection::hasValidIntervals() const
{
if ( isEmpty() ) return false;
for ( auto* interval : intervals() )
{
if ( !interval || !interval->isValidInterval() )
{
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimCustomSegmentIntervalCollection::hasOverlappingIntervals() const
{
auto intervalList = intervals();
for ( size_t i = 0; i < intervalList.size(); ++i )
{
for ( size_t j = i + 1; j < intervalList.size(); ++j )
{
if ( intervalList[i]->overlaps( intervalList[j] ) )
{
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -213,142 +87,6 @@ std::map<QString, QString> RimCustomSegmentIntervalCollection::validate( const Q
return errors;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::sortIntervalsByMD()
{
auto intervalList = intervals();
std::sort( intervalList.begin(),
intervalList.end(),
[]( const RimCustomSegmentInterval* a, const RimCustomSegmentInterval* b ) { return *a < *b; } );
// Rebuild the collection in sorted order
m_intervals.clearWithoutDelete();
for ( auto* interval : intervalList )
{
m_intervals.push_back( interval );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimCustomSegmentIntervalCollection::isEmpty() const
{
return m_intervals.empty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RimCustomSegmentIntervalCollection::count() const
{
return m_intervals.size();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::updateConnectedEditors()
{
m_intervals.uiCapability()->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmChildArrayField<RimCustomSegmentInterval*>& RimCustomSegmentIntervalCollection::intervalsField()
{
return m_intervals;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::updateOverlapVisualFeedback()
{
auto intervalList = intervals();
// First, reset all intervals to no overlap
for ( auto* interval : intervalList )
{
interval->updateOverlapVisualFeedback( false );
}
// Then check for overlaps and update visual feedback
for ( size_t i = 0; i < intervalList.size(); ++i )
{
bool hasOverlap = false;
for ( size_t j = 0; j < intervalList.size(); ++j )
{
if ( i != j && intervalList[i]->overlaps( intervalList[j] ) )
{
hasOverlap = true;
break;
}
}
if ( hasOverlap )
{
intervalList[i]->updateOverlapVisualFeedback( true );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
const QVariant& oldValue,
const QVariant& newValue )
{
if ( changedField == &m_intervals )
{
sortIntervalsByMD();
updateOverlapVisualFeedback();
}
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
uiOrdering.add( &m_intervals );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::defineEditorAttribute( const caf::PdmFieldHandle* field,
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute )
{
if ( field == &m_intervals )
{
auto tvAttribute = dynamic_cast<caf::PdmUiTableViewEditorAttribute*>( attribute );
if ( tvAttribute )
{
tvAttribute->resizePolicy = caf::PdmUiTableViewEditorAttribute::RESIZE_TO_FILL_CONTAINER;
tvAttribute->alwaysEnforceResizePolicy = true;
tvAttribute->minimumHeight = 300;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects )
{
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -364,20 +102,3 @@ void RimCustomSegmentIntervalCollection::defineCustomContextMenu( const caf::Pdm
menuBuilder.appendToMenu( menu );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCustomSegmentIntervalCollection::insertInterval( RimCustomSegmentInterval* insertBefore, RimCustomSegmentInterval* interval )
{
if ( !interval ) return;
size_t index = m_intervals.indexOf( insertBefore );
if ( index < m_intervals.size() )
m_intervals.insert( index, interval );
else
m_intervals.push_back( interval );
sortIntervalsByMD();
updateConnectedEditors();
}
@@ -17,21 +17,15 @@
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RiaDefines.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmObject.h"
#include <vector>
class RimCustomSegmentInterval;
#include "RimCustomSegmentInterval.h"
#include "RimIntervalCollection.h"
//==================================================================================================
///
/// Collection to manage custom segment intervals for measured depth ranges
///
//==================================================================================================
class RimCustomSegmentIntervalCollection : public caf::PdmObject
class RimCustomSegmentIntervalCollection : public RimIntervalCollection<RimCustomSegmentInterval>
{
CAF_PDM_HEADER_INIT;
@@ -39,47 +33,9 @@ public:
RimCustomSegmentIntervalCollection();
~RimCustomSegmentIntervalCollection() override;
// Collection management
std::vector<RimCustomSegmentInterval*> intervals() const;
void addInterval( RimCustomSegmentInterval* interval );
void insertInterval( RimCustomSegmentInterval* insertBefore, RimCustomSegmentInterval* interval );
void removeInterval( RimCustomSegmentInterval* interval );
void removeAllIntervals();
// Interval creation
RimCustomSegmentInterval* createInterval( double startMD, double endMD );
RimCustomSegmentInterval* createDefaultInterval();
// Lookup methods
RimCustomSegmentInterval* findIntervalAtMD( double md ) const;
// Validation
std::map<QString, QString> validate( const QString& configName = "" ) const override;
bool hasValidIntervals() const;
bool hasOverlappingIntervals() const;
// Sorting and organization
void sortIntervalsByMD();
// Utility
bool isEmpty() const;
size_t count() const;
void updateConnectedEditors();
// Accessor for UI integration
caf::PdmChildArrayField<RimCustomSegmentInterval*>& intervalsField();
// Visual feedback for overlapping intervals
void updateOverlapVisualFeedback();
protected:
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
void defineCustomContextMenu( const caf::PdmFieldHandle* fieldNeedingMenu, QMenu* menu, QWidget* fieldEditorWidget ) override;
void defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;
private:
void onChildDeleted( caf::PdmChildArrayFieldHandle* childArray, std::vector<caf::PdmObjectHandle*>& referringObjects ) override;
caf::PdmChildArrayField<RimCustomSegmentInterval*> m_intervals;
};
@@ -18,12 +18,10 @@
#include "RimDiameterRoughnessIntervalCollection.h"
#include "RiaLogging.h"
#include "RimDiameterRoughnessInterval.h"
#include "RimMswCompletionParameters.h"
#include "cafCmdFeatureMenuBuilder.h"
#include "cafPdmUiTableViewEditor.h"
#include <algorithm>
#include <cmath>
@@ -36,8 +34,7 @@ CAF_PDM_SOURCE_INIT( RimDiameterRoughnessIntervalCollection, "DiameterRoughnessI
RimDiameterRoughnessIntervalCollection::RimDiameterRoughnessIntervalCollection()
{
CAF_PDM_InitObject( "Diameter Roughness Intervals", ":/WellPathComponent16x16.png" );
CAF_PDM_InitFieldNoDefault( &m_intervals, "Intervals", "Intervals" );
CAF_PDM_InitFieldNoDefault( &m_items, "Intervals", "Intervals" );
}
//--------------------------------------------------------------------------------------------------
@@ -47,49 +44,6 @@ RimDiameterRoughnessIntervalCollection::~RimDiameterRoughnessIntervalCollection(
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimDiameterRoughnessInterval*> RimDiameterRoughnessIntervalCollection::intervals() const
{
return m_intervals.childrenByType();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::addInterval( RimDiameterRoughnessInterval* interval )
{
if ( interval )
{
m_intervals.push_back( interval );
sortIntervalsByMD();
updateConnectedEditors();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::removeInterval( RimDiameterRoughnessInterval* interval )
{
if ( interval )
{
m_intervals.removeChild( interval );
delete interval;
updateConnectedEditors();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::removeAllIntervals()
{
m_intervals.deleteChildren();
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -106,16 +60,6 @@ RimDiameterRoughnessInterval*
return interval;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimDiameterRoughnessInterval* RimDiameterRoughnessIntervalCollection::createDefaultInterval()
{
auto* interval = new RimDiameterRoughnessInterval();
addInterval( interval );
return interval;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -146,58 +90,6 @@ double RimDiameterRoughnessIntervalCollection::getRoughnessAtMD( double md, RiaD
return RimMswCompletionParameters::defaultRoughnessFactor( unitSystem );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimDiameterRoughnessInterval* RimDiameterRoughnessIntervalCollection::findIntervalAtMD( double md ) const
{
for ( auto* interval : intervals() )
{
if ( interval && interval->containsMD( md ) )
{
return interval;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimDiameterRoughnessIntervalCollection::hasValidIntervals() const
{
if ( isEmpty() ) return false;
for ( auto* interval : intervals() )
{
if ( !interval || !interval->isValidInterval() )
{
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimDiameterRoughnessIntervalCollection::hasOverlappingIntervals() const
{
auto intervalList = intervals();
for ( size_t i = 0; i < intervalList.size(); ++i )
{
for ( size_t j = i + 1; j < intervalList.size(); ++j )
{
if ( intervalList[i]->overlaps( intervalList[j] ) )
{
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -223,67 +115,6 @@ bool RimDiameterRoughnessIntervalCollection::coversFullRange( double startMD, do
return currentPos >= endMD - 1e-6; // Allow small tolerance
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<QString> RimDiameterRoughnessIntervalCollection::validateIntervals() const
{
std::vector<QString> issues;
if ( isEmpty() )
{
issues.push_back( "No intervals defined" );
return issues;
}
auto intervalList = intervals();
// Check for invalid intervals
for ( auto* interval : intervalList )
{
if ( !interval->isValidInterval() )
{
issues.push_back( QString( "Invalid interval: MD %.1f-%.1f" ).arg( interval->startMD() ).arg( interval->endMD() ) );
}
}
// Check for overlaps
for ( size_t i = 0; i < intervalList.size(); ++i )
{
for ( size_t j = i + 1; j < intervalList.size(); ++j )
{
if ( intervalList[i]->overlaps( intervalList[j] ) )
{
issues.push_back( QString( "Overlapping intervals: MD %.1f-%.1f and MD %.1f-%.1f" )
.arg( intervalList[i]->startMD() )
.arg( intervalList[i]->endMD() )
.arg( intervalList[j]->startMD() )
.arg( intervalList[j]->endMD() ) );
}
}
}
return issues;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::sortIntervalsByMD()
{
auto intervalList = intervals();
std::sort( intervalList.begin(),
intervalList.end(),
[]( const RimDiameterRoughnessInterval* a, const RimDiameterRoughnessInterval* b ) { return *a < *b; } );
// Rebuild the collection in sorted order
m_intervals.clearWithoutDelete();
for ( auto* interval : intervalList )
{
m_intervals.push_back( interval );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -320,131 +151,13 @@ void RimDiameterRoughnessIntervalCollection::mergeAdjacentIntervals()
}
// Rebuild collection with merged intervals
m_intervals.clearWithoutDelete();
this->m_items.clearWithoutDelete();
for ( auto* interval : mergedIntervals )
{
m_intervals.push_back( interval );
this->m_items.push_back( interval );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimDiameterRoughnessIntervalCollection::isEmpty() const
{
return m_intervals.empty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RimDiameterRoughnessIntervalCollection::count() const
{
return m_intervals.size();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::updateConnectedEditors()
{
m_intervals.uiCapability()->updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmChildArrayField<RimDiameterRoughnessInterval*>& RimDiameterRoughnessIntervalCollection::intervalsField()
{
return m_intervals;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::updateOverlapVisualFeedback()
{
auto intervalList = intervals();
// First, reset all intervals to no overlap
for ( auto* interval : intervalList )
{
interval->updateOverlapVisualFeedback( false );
}
// Then check for overlaps and update visual feedback
for ( size_t i = 0; i < intervalList.size(); ++i )
{
bool hasOverlap = false;
for ( size_t j = 0; j < intervalList.size(); ++j )
{
if ( i != j && intervalList[i]->overlaps( intervalList[j] ) )
{
hasOverlap = true;
break;
}
}
if ( hasOverlap )
{
intervalList[i]->updateOverlapVisualFeedback( true );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
const QVariant& oldValue,
const QVariant& newValue )
{
if ( changedField == &m_intervals )
{
sortIntervalsByMD();
updateOverlapVisualFeedback();
}
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
uiOrdering.add( &m_intervals );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::defineEditorAttribute( const caf::PdmFieldHandle* field,
QString uiConfigName,
caf::PdmUiEditorAttribute* attribute )
{
if ( field == &m_intervals )
{
auto tvAttribute = dynamic_cast<caf::PdmUiTableViewEditorAttribute*>( attribute );
if ( tvAttribute )
{
tvAttribute->resizePolicy = caf::PdmUiTableViewEditorAttribute::RESIZE_TO_FILL_CONTAINER;
tvAttribute->alwaysEnforceResizePolicy = true;
tvAttribute->minimumHeight = 300;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects )
{
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -460,20 +173,3 @@ void RimDiameterRoughnessIntervalCollection::defineCustomContextMenu( const caf:
menuBuilder.appendToMenu( menu );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDiameterRoughnessIntervalCollection::insertInterval( RimDiameterRoughnessInterval* insertBefore, RimDiameterRoughnessInterval* interval )
{
if ( !interval ) return;
size_t index = m_intervals.indexOf( insertBefore );
if ( index < m_intervals.size() )
m_intervals.insert( index, interval );
else
m_intervals.push_back( interval );
sortIntervalsByMD();
updateConnectedEditors();
}
@@ -18,20 +18,15 @@
#pragma once
#include "RiaDefines.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmObject.h"
#include <vector>
class RimDiameterRoughnessInterval;
#include "RimDiameterRoughnessInterval.h"
#include "RimIntervalCollection.h"
//==================================================================================================
///
/// Collection to manage diameter and roughness intervals for measured depth ranges
///
//==================================================================================================
class RimDiameterRoughnessIntervalCollection : public caf::PdmObject
class RimDiameterRoughnessIntervalCollection : public RimIntervalCollection<RimDiameterRoughnessInterval>
{
CAF_PDM_HEADER_INIT;
@@ -39,51 +34,19 @@ public:
RimDiameterRoughnessIntervalCollection();
~RimDiameterRoughnessIntervalCollection() override;
// Collection management
std::vector<RimDiameterRoughnessInterval*> intervals() const;
void addInterval( RimDiameterRoughnessInterval* interval );
void insertInterval( RimDiameterRoughnessInterval* insertBefore, RimDiameterRoughnessInterval* interval );
void removeInterval( RimDiameterRoughnessInterval* interval );
void removeAllIntervals();
// Interval creation
// Domain-specific interval creation
RimDiameterRoughnessInterval* createInterval( double startMD, double endMD, double diameter, double roughness );
RimDiameterRoughnessInterval* createDefaultInterval();
// Lookup methods
double getDiameterAtMD( double md, RiaDefines::EclipseUnitSystem unitSystem ) const;
double getRoughnessAtMD( double md, RiaDefines::EclipseUnitSystem unitSystem ) const;
RimDiameterRoughnessInterval* findIntervalAtMD( double md ) const;
// Domain-specific lookup methods
double getDiameterAtMD( double md, RiaDefines::EclipseUnitSystem unitSystem ) const;
double getRoughnessAtMD( double md, RiaDefines::EclipseUnitSystem unitSystem ) const;
// Validation
bool hasValidIntervals() const;
bool hasOverlappingIntervals() const;
bool coversFullRange( double startMD, double endMD ) const;
std::vector<QString> validateIntervals() const;
// Domain-specific validation
bool coversFullRange( double startMD, double endMD ) const;
// Sorting and organization
void sortIntervalsByMD();
// Domain-specific organization
void mergeAdjacentIntervals();
// Utility
bool isEmpty() const;
size_t count() const;
void updateConnectedEditors();
// Accessor for UI integration
caf::PdmChildArrayField<RimDiameterRoughnessInterval*>& intervalsField();
// Visual feedback for overlapping intervals
void updateOverlapVisualFeedback();
protected:
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
void defineCustomContextMenu( const caf::PdmFieldHandle* fieldNeedingMenu, QMenu* menu, QWidget* fieldEditorWidget ) override;
void defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;
private:
void onChildDeleted( caf::PdmChildArrayFieldHandle* childArray, std::vector<caf::PdmObjectHandle*>& referringObjects ) override;
caf::PdmChildArrayField<RimDiameterRoughnessInterval*> m_intervals;
};
@@ -0,0 +1,78 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2025 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmObjectCollection.h"
#include <QString>
#include <vector>
//==================================================================================================
///
/// Interval-specific templated collection base class
///
/// Extends RimPdmObjectCollection with interval-specific operations for objects that have
/// measured depth (MD) ranges. Provides sorting, overlap detection, and validation.
///
/// Template parameter:
/// - IntervalType: Must inherit from caf::PdmObject and provide interval methods
///
/// IntervalType must provide: startMD(), endMD(), containsMD(double), overlaps(const IntervalType*),
/// isValidInterval(), updateOverlapVisualFeedback(bool), operator<
///
/// Derived classes must call the base constructor with field name and display name.
///
//==================================================================================================
template <typename IntervalType>
class RimIntervalCollection : public caf::PdmObjectCollection<IntervalType>
{
public:
// Interval-specific lookup
IntervalType* findIntervalAtMD( double md ) const;
// Validation
bool hasValidIntervals() const;
bool hasOverlappingIntervals() const;
std::vector<QString> validateIntervals() const;
// Organization
void sortIntervalsByMD();
void updateOverlapVisualFeedback();
// Convenience accessors (delegate to base class)
std::vector<IntervalType*> intervals() const;
void addInterval( IntervalType* interval );
void removeInterval( IntervalType* interval );
void removeAllIntervals();
IntervalType* createDefaultInterval();
caf::PdmChildArrayField<IntervalType*>& intervalsField();
const caf::PdmChildArrayField<IntervalType*>& intervalsField() const;
protected:
// Constructor (protected - only derived classes can instantiate)
// Derived classes MUST initialize m_items field using CAF_PDM_InitFieldNoDefault
RimIntervalCollection();
~RimIntervalCollection() override;
// Override to automatically sort and update visual feedback when items change
void onItemsChanged() override;
};
// Include template implementation
#include "RimIntervalCollection.inl"
@@ -0,0 +1,243 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2025 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight 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 General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
RimIntervalCollection<IntervalType>::RimIntervalCollection()
{
// m_items field must be initialized by derived class using CAF_PDM_InitFieldNoDefault
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
RimIntervalCollection<IntervalType>::~RimIntervalCollection()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
IntervalType* RimIntervalCollection<IntervalType>::findIntervalAtMD( double md ) const
{
for ( auto* interval : intervals() )
{
if ( interval && interval->containsMD( md ) )
{
return interval;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
bool RimIntervalCollection<IntervalType>::hasValidIntervals() const
{
if ( this->isEmpty() ) return false;
for ( auto* interval : intervals() )
{
if ( !interval || !interval->isValidInterval() )
{
return false;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
bool RimIntervalCollection<IntervalType>::hasOverlappingIntervals() const
{
auto intervalList = intervals();
for ( size_t i = 0; i < intervalList.size(); ++i )
{
for ( size_t j = i + 1; j < intervalList.size(); ++j )
{
if ( intervalList[i]->overlaps( intervalList[j] ) )
{
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
std::vector<QString> RimIntervalCollection<IntervalType>::validateIntervals() const
{
std::vector<QString> issues;
if ( this->isEmpty() )
{
issues.push_back( "No intervals defined" );
return issues;
}
auto intervalList = intervals();
// Check for invalid intervals
for ( auto* interval : intervalList )
{
if ( !interval->isValidInterval() )
{
issues.push_back(
QString( "Invalid interval: MD %.1f-%.1f (Start MD must be less than End MD)" ).arg( interval->startMD() ).arg( interval->endMD() ) );
}
}
// Check for overlaps
for ( size_t i = 0; i < intervalList.size(); ++i )
{
for ( size_t j = i + 1; j < intervalList.size(); ++j )
{
if ( intervalList[i]->overlaps( intervalList[j] ) )
{
issues.push_back( QString( "Overlapping intervals: MD %.1f-%.1f and MD %.1f-%.1f" )
.arg( intervalList[i]->startMD() )
.arg( intervalList[i]->endMD() )
.arg( intervalList[j]->startMD() )
.arg( intervalList[j]->endMD() ) );
}
}
}
return issues;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::sortIntervalsByMD()
{
auto intervalList = intervals();
std::sort( intervalList.begin(), intervalList.end(), []( const IntervalType* a, const IntervalType* b ) { return *a < *b; } );
// Rebuild the collection in sorted order
this->m_items.clearWithoutDelete();
for ( auto* interval : intervalList )
{
this->m_items.push_back( interval );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::updateOverlapVisualFeedback()
{
auto intervalList = intervals();
// First, reset all intervals to no overlap
for ( auto* interval : intervalList )
{
interval->updateOverlapVisualFeedback( false );
}
// Then check for overlaps and update visual feedback
for ( size_t i = 0; i < intervalList.size(); ++i )
{
bool hasOverlap = false;
for ( size_t j = 0; j < intervalList.size(); ++j )
{
if ( i != j && intervalList[i]->overlaps( intervalList[j] ) )
{
hasOverlap = true;
break;
}
}
if ( hasOverlap )
{
intervalList[i]->updateOverlapVisualFeedback( true );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::onItemsChanged()
{
sortIntervalsByMD();
updateOverlapVisualFeedback();
}
//--------------------------------------------------------------------------------------------------
/// Convenience accessors (delegate to base class)
//--------------------------------------------------------------------------------------------------
template <typename IntervalType>
std::vector<IntervalType*> RimIntervalCollection<IntervalType>::intervals() const
{
return this->items();
}
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::addInterval( IntervalType* interval )
{
this->addItem( interval );
}
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::removeInterval( IntervalType* interval )
{
this->deleteItem( interval );
}
template <typename IntervalType>
void RimIntervalCollection<IntervalType>::removeAllIntervals()
{
this->deleteAllItems();
}
template <typename IntervalType>
IntervalType* RimIntervalCollection<IntervalType>::createDefaultInterval()
{
return this->createDefaultItem();
}
template <typename IntervalType>
caf::PdmChildArrayField<IntervalType*>& RimIntervalCollection<IntervalType>::intervalsField()
{
return this->itemsField();
}
template <typename IntervalType>
const caf::PdmChildArrayField<IntervalType*>& RimIntervalCollection<IntervalType>::intervalsField() const
{
return this->itemsField();
}