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
@@ -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();
}