ResInsight/ApplicationLibCode/ProjectDataModel/RimSummaryCalculationCollection.cpp
Magne Sjaastad fd8d38c4f0 #7519 Curve calculator: Assign unique ID after copy of calculation
Avoid duplicate entries in summary selection dialog
2021-03-26 12:05:52 +01:00

179 lines
6.5 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil 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 "RimSummaryCalculationCollection.h"
#include "RimCalculatedSummaryCase.h"
#include "RimProject.h"
#include "RimSummaryCalculation.h"
#include "cafPdmUiGroup.h"
#include "cafPdmUiTreeSelectionEditor.h"
CAF_PDM_SOURCE_INIT( RimSummaryCalculationCollection, "RimSummaryCalculationCollection" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryCalculationCollection::RimSummaryCalculationCollection()
{
CAF_PDM_InitObject( "Calculation Collection", ":/chain.png", "", "" );
CAF_PDM_InitFieldNoDefault( &m_calculations, "Calculations", "Calculations", "", "", "" );
m_calculations.uiCapability()->setUiEditorTypeName( caf::PdmUiTreeSelectionEditor::uiEditorTypeName() );
CAF_PDM_InitFieldNoDefault( &m_calcuationSummaryCase, "CalculationsSummaryCase", "Calculations Summary Case", "", "", "" );
m_calcuationSummaryCase.xmlCapability()->disableIO();
m_calcuationSummaryCase = new RimCalculatedSummaryCase;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryCalculation* RimSummaryCalculationCollection::addCalculation()
{
RimSummaryCalculation* calculation = new RimSummaryCalculation;
RimProject::current()->assignCalculationIdToCalculation( calculation );
QString varName = QString( "Calculation_%1" ).arg( calculation->id() );
calculation->setDescription( varName );
calculation->setExpression( varName + " := x + y" );
calculation->parseExpression();
m_calculations.push_back( calculation );
rebuildCaseMetaData();
return calculation;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryCalculation* RimSummaryCalculationCollection::addCalculationCopy( const RimSummaryCalculation* sourceCalculation )
{
RimSummaryCalculation* calcCopy = dynamic_cast<RimSummaryCalculation*>(
sourceCalculation->xmlCapability()->copyByXmlSerialization( caf::PdmDefaultObjectFactory::instance() ) );
CVF_ASSERT( calcCopy );
std::set<QString> calcNames;
for ( const auto& calc : m_calculations )
{
calcNames.insert( calc->findLeftHandSide( calc->expression() ) );
}
QString expression = calcCopy->expression();
QString currVarName = calcCopy->findLeftHandSide( expression );
QString newVarName = currVarName;
while ( calcNames.count( newVarName ) > 0 )
{
newVarName += "_copy";
}
expression.replace( currVarName, newVarName );
calcCopy->setExpression( expression );
RimProject::current()->assignCalculationIdToCalculation( calcCopy );
m_calculations.push_back( calcCopy );
calcCopy->resolveReferencesRecursively();
rebuildCaseMetaData();
calcCopy->parseExpression();
return calcCopy;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryCalculationCollection::deleteCalculation( RimSummaryCalculation* calculation )
{
m_calculations.removeChildObject( calculation );
rebuildCaseMetaData();
delete calculation;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimSummaryCalculation*> RimSummaryCalculationCollection::calculations() const
{
return m_calculations.childObjects();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryCalculation* RimSummaryCalculationCollection::findCalculationById( int id ) const
{
for ( RimSummaryCalculation* calc : m_calculations )
{
if ( calc->id() == id )
{
return calc;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSummaryCase* RimSummaryCalculationCollection::calculationSummaryCase()
{
return m_calcuationSummaryCase();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryCalculationCollection::deleteAllContainedObjects()
{
m_calculations.deleteAllChildObjects();
rebuildCaseMetaData();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryCalculationCollection::rebuildCaseMetaData()
{
for ( RimSummaryCalculation* calculation : m_calculations )
{
if ( calculation->id() == -1 )
{
RimProject::current()->assignCalculationIdToCalculation( calculation );
}
}
m_calcuationSummaryCase->buildMetaData();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSummaryCalculationCollection::initAfterRead()
{
rebuildCaseMetaData();
}