mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
WIA: Add better support for list parameters (#8266)
* Add better support for list parameters
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include "RimGenericParameter.h"
|
||||
#include "RimIntegerParameter.h"
|
||||
#include "RimListParameter.h"
|
||||
#include "RimParameterList.h"
|
||||
#include "RimStringParameter.h"
|
||||
|
||||
#include "RimParameterGroup.h"
|
||||
@@ -29,15 +30,24 @@
|
||||
#include <QFile>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifParameterXmlReader::RifParameterXmlReader( QString filename )
|
||||
: m_filename( filename )
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RifParameterXmlReader::~RifParameterXmlReader()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RimGenericParameter* getParameterFromTypeStr( QString typestr )
|
||||
{
|
||||
// check that we have a type we support
|
||||
@@ -53,14 +63,13 @@ RimGenericParameter* getParameterFromTypeStr( QString typestr )
|
||||
{
|
||||
return new RimStringParameter();
|
||||
}
|
||||
else if ( typestr == "list" )
|
||||
{
|
||||
return new RimListParameter();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RifParameterXmlReader::parseFile( QString& outErrorText )
|
||||
{
|
||||
m_parameters.clear();
|
||||
@@ -82,114 +91,149 @@ bool RifParameterXmlReader::parseFile( QString& outErrorText )
|
||||
RimParameterGroup* group = nullptr;
|
||||
|
||||
std::list<QString> reqGroupAttrs = { QString( "name" ) };
|
||||
std::list<QString> reqListAttrs = { QString( "name" ) };
|
||||
std::list<QString> reqParamAttrs = { QString( "name" ), QString( "label" ), QString( "type" ) };
|
||||
|
||||
bool bResult = true;
|
||||
bool bResult = true;
|
||||
RimParameterList* currentList = nullptr;
|
||||
|
||||
while ( !xml.atEnd() )
|
||||
{
|
||||
if ( xml.readNextStartElement() )
|
||||
if ( xml.readNext() )
|
||||
{
|
||||
if ( xml.name() == "group" )
|
||||
if ( xml.isStartElement() )
|
||||
{
|
||||
if ( group != nullptr )
|
||||
if ( xml.name() == "group" )
|
||||
{
|
||||
m_parameters.push_back( group );
|
||||
}
|
||||
|
||||
// check that we have the required attributes
|
||||
for ( auto& reqattr : reqGroupAttrs )
|
||||
{
|
||||
if ( !xml.attributes().hasAttribute( reqattr ) )
|
||||
// check that we have the required attributes
|
||||
for ( auto& reqattr : reqGroupAttrs )
|
||||
{
|
||||
outErrorText += "Missing required attribute \"" + reqattr + "\" for a parameter group.";
|
||||
if ( !xml.attributes().hasAttribute( reqattr ) )
|
||||
{
|
||||
outErrorText += "Missing required attribute \"" + reqattr + "\" for a parameter group.";
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !bResult ) break;
|
||||
|
||||
group = new RimParameterGroup();
|
||||
if ( xml.attributes().hasAttribute( "name" ) )
|
||||
{
|
||||
group->setName( xml.attributes().value( "name" ).toString() );
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "label" ) )
|
||||
{
|
||||
group->setLabel( xml.attributes().value( "label" ).toString() );
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "expanded" ) )
|
||||
{
|
||||
group->setExpanded( xml.attributes().value( "expanded" ).toString().toLower() == "true" );
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "comment" ) )
|
||||
{
|
||||
group->setComment( xml.attributes().value( "comment" ).toString() );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else if ( xml.name() == "parameter" )
|
||||
{
|
||||
if ( group == nullptr ) continue;
|
||||
|
||||
// check that we have the required attributes
|
||||
for ( auto& reqattr : reqParamAttrs )
|
||||
{
|
||||
if ( !xml.attributes().hasAttribute( reqattr ) )
|
||||
{
|
||||
outErrorText += "Missing required attribute \"" + reqattr + "\" for a parameter.";
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !bResult ) break;
|
||||
|
||||
// get a parameter of the required type
|
||||
QString paramtypestr = xml.attributes().value( "type" ).toString().toLower();
|
||||
|
||||
RimGenericParameter* parameter = getParameterFromTypeStr( paramtypestr );
|
||||
if ( parameter == nullptr )
|
||||
{
|
||||
outErrorText += "Unsupported parameter type found: " + paramtypestr;
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !bResult ) break;
|
||||
|
||||
group = new RimParameterGroup();
|
||||
if ( xml.attributes().hasAttribute( "name" ) )
|
||||
{
|
||||
group->setName( xml.attributes().value( "name" ).toString() );
|
||||
parameter->setName( xml.attributes().value( "name" ).toString() );
|
||||
parameter->setLabel( xml.attributes().value( "label" ).toString() );
|
||||
parameter->setAdvanced( false );
|
||||
|
||||
if ( xml.attributes().hasAttribute( "advanced" ) )
|
||||
{
|
||||
if ( xml.attributes().value( "advanced" ).toString().toLower() == "true" )
|
||||
parameter->setAdvanced( true );
|
||||
}
|
||||
|
||||
if ( xml.attributes().hasAttribute( "description" ) )
|
||||
{
|
||||
parameter->setDescription( xml.attributes().value( "description" ).toString() );
|
||||
}
|
||||
|
||||
parameter->setValue( xml.readElementText().trimmed() );
|
||||
if ( !parameter->isValid() )
|
||||
{
|
||||
outErrorText += "Invalid parameter value found for parameter: " + parameter->name();
|
||||
delete parameter;
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
|
||||
group->addParameter( parameter );
|
||||
if ( currentList )
|
||||
{
|
||||
currentList->addParameter( parameter->name() );
|
||||
}
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "label" ) )
|
||||
else if ( xml.name() == "list" )
|
||||
{
|
||||
group->setLabel( xml.attributes().value( "label" ).toString() );
|
||||
// check that we have the required attributes
|
||||
for ( auto& reqattr : reqListAttrs )
|
||||
{
|
||||
if ( !xml.attributes().hasAttribute( reqattr ) )
|
||||
{
|
||||
outErrorText += "Missing required attribute \"" + reqattr + "\" for a list parameter.";
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !bResult ) break;
|
||||
|
||||
currentList = new RimParameterList();
|
||||
currentList->setName( xml.attributes().value( "name" ).toString() );
|
||||
currentList->setLabel( xml.attributes().value( "label" ).toString() );
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "expanded" ) )
|
||||
{
|
||||
group->setExpanded( xml.attributes().value( "expanded" ).toString().toLower() == "true" );
|
||||
}
|
||||
if ( xml.attributes().hasAttribute( "comment" ) )
|
||||
{
|
||||
group->setComment( xml.attributes().value( "comment" ).toString() );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( xml.name() == "parameter" )
|
||||
else if ( xml.isEndElement() )
|
||||
{
|
||||
if ( group == nullptr ) continue;
|
||||
|
||||
// check that we have the required attributes
|
||||
for ( auto& reqattr : reqParamAttrs )
|
||||
if ( xml.name() == "group" )
|
||||
{
|
||||
if ( !xml.attributes().hasAttribute( reqattr ) )
|
||||
if ( group != nullptr )
|
||||
{
|
||||
outErrorText += "Missing required attribute \"" + reqattr + "\" for a parameter.";
|
||||
bResult = false;
|
||||
break;
|
||||
m_parameters.push_back( group );
|
||||
group = nullptr;
|
||||
}
|
||||
}
|
||||
if ( !bResult ) break;
|
||||
|
||||
// get a parameter of the required type
|
||||
QString paramtypestr = xml.attributes().value( "type" ).toString().toLower();
|
||||
|
||||
RimGenericParameter* parameter = getParameterFromTypeStr( paramtypestr );
|
||||
if ( parameter == nullptr )
|
||||
else if ( xml.name() == "list" )
|
||||
{
|
||||
outErrorText += "Unsupported parameter type found: " + paramtypestr;
|
||||
bResult = false;
|
||||
break;
|
||||
if ( group )
|
||||
{
|
||||
group->addList( currentList );
|
||||
}
|
||||
currentList = nullptr;
|
||||
}
|
||||
|
||||
parameter->setName( xml.attributes().value( "name" ).toString() );
|
||||
parameter->setLabel( xml.attributes().value( "label" ).toString() );
|
||||
parameter->setAdvanced( false );
|
||||
|
||||
if ( xml.attributes().hasAttribute( "advanced" ) )
|
||||
{
|
||||
if ( xml.attributes().value( "advanced" ).toString().toLower() == "true" )
|
||||
parameter->setAdvanced( true );
|
||||
}
|
||||
|
||||
if ( xml.attributes().hasAttribute( "description" ) )
|
||||
{
|
||||
parameter->setDescription( xml.attributes().value( "description" ).toString() );
|
||||
}
|
||||
|
||||
parameter->setValue( xml.readElementText().trimmed() );
|
||||
if ( !parameter->isValid() )
|
||||
{
|
||||
outErrorText += "Invalid parameter value found for parameter: " + parameter->name();
|
||||
delete parameter;
|
||||
bResult = false;
|
||||
break;
|
||||
}
|
||||
|
||||
group->addParameter( parameter );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( group != nullptr )
|
||||
{
|
||||
m_parameters.push_back( group );
|
||||
}
|
||||
|
||||
dataFile.close();
|
||||
|
||||
if ( bResult ) outErrorText = "";
|
||||
@@ -197,6 +241,9 @@ bool RifParameterXmlReader::parseFile( QString& outErrorText )
|
||||
return bResult;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
std::list<RimParameterGroup*>& RifParameterXmlReader::parameterGroups()
|
||||
{
|
||||
return m_parameters;
|
||||
|
||||
Reference in New Issue
Block a user