From 08af2e7c17c196a2d7f9ea7710ee49da6bf35747 Mon Sep 17 00:00:00 2001 From: jonjenssen <69144954+jonjenssen@users.noreply.github.com> Date: Tue, 9 Nov 2021 18:17:47 +0100 Subject: [PATCH] WIA: Add better support for list parameters (#8266) * Add better support for list parameters --- .../FileInterface/RifParameterXmlReader.cpp | 215 +++++++++++------- .../Parameters/CMakeLists_files.cmake | 2 + .../Parameters/RimParameterGroup.cpp | 42 +++- .../Parameters/RimParameterGroup.h | 6 + .../Parameters/RimParameterList.cpp | 150 ++++++++++++ .../Parameters/RimParameterList.h | 61 +++++ 6 files changed, 391 insertions(+), 85 deletions(-) create mode 100644 ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.cpp create mode 100644 ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.h diff --git a/ApplicationLibCode/FileInterface/RifParameterXmlReader.cpp b/ApplicationLibCode/FileInterface/RifParameterXmlReader.cpp index 790e439bab..16115616f5 100644 --- a/ApplicationLibCode/FileInterface/RifParameterXmlReader.cpp +++ b/ApplicationLibCode/FileInterface/RifParameterXmlReader.cpp @@ -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 #include +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- 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 reqGroupAttrs = { QString( "name" ) }; + std::list reqListAttrs = { QString( "name" ) }; std::list 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& RifParameterXmlReader::parameterGroups() { return m_parameters; diff --git a/ApplicationLibCode/ProjectDataModel/Parameters/CMakeLists_files.cmake b/ApplicationLibCode/ProjectDataModel/Parameters/CMakeLists_files.cmake index 28426a65b7..8cd3f11149 100644 --- a/ApplicationLibCode/ProjectDataModel/Parameters/CMakeLists_files.cmake +++ b/ApplicationLibCode/ProjectDataModel/Parameters/CMakeLists_files.cmake @@ -6,6 +6,7 @@ set(SOURCE_GROUP_HEADER_FILES ${CMAKE_CURRENT_LIST_DIR}/RimListParameter.h ${CMAKE_CURRENT_LIST_DIR}/RimParameterGroup.h ${CMAKE_CURRENT_LIST_DIR}/RimParameterGroups.h + ${CMAKE_CURRENT_LIST_DIR}/RimParameterList.h ) set(SOURCE_GROUP_SOURCE_FILES @@ -16,6 +17,7 @@ set(SOURCE_GROUP_SOURCE_FILES ${CMAKE_CURRENT_LIST_DIR}/RimListParameter.cpp ${CMAKE_CURRENT_LIST_DIR}/RimParameterGroup.cpp ${CMAKE_CURRENT_LIST_DIR}/RimParameterGroups.cpp + ${CMAKE_CURRENT_LIST_DIR}/RimParameterList.cpp ) list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES}) diff --git a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.cpp b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.cpp index b077badd8c..feeaadc807 100644 --- a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.cpp +++ b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.cpp @@ -25,6 +25,8 @@ #include "RimDoubleParameter.h" #include "RimGenericParameter.h" #include "RimIntegerParameter.h" +#include "RimListParameter.h" +#include "RimParameterList.h" #include "RimStringParameter.h" #include @@ -67,6 +69,11 @@ RimParameterGroup::RimParameterGroup() m_labelProxy.uiCapability()->setUiReadOnly( true ); m_labelProxy.uiCapability()->setUiHidden( true ); m_labelProxy.xmlCapability()->disableIO(); + + CAF_PDM_InitFieldNoDefault( &m_lists, "ParameterLists", "Parameter Lists", "", "", "" ); + m_lists.uiCapability()->setUiHidden( true ); + m_lists.uiCapability()->setUiTreeHidden( true ); + m_lists.uiCapability()->setUiTreeChildrenHidden( true ); } //-------------------------------------------------------------------------------------------------- @@ -140,6 +147,14 @@ void RimParameterGroup::addParameter( QString name, double value ) addParameter( p ); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimParameterGroup::addList( RimParameterList* paramList ) +{ + m_lists.push_back( paramList ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -246,12 +261,37 @@ QString RimParameterGroup::label() const return labelOrName(); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimParameterGroup::isListParameter( QString paramName ) const +{ + for ( auto& list : m_lists ) + { + if ( list->containsParameter( paramName ) ) return true; + } + return false; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimParameterGroup::parameters() const { - return m_parameters.childObjects(); + std::vector retParams; + + for ( const auto& p : m_parameters.childObjects() ) + { + if ( isListParameter( p->name() ) ) continue; + retParams.push_back( p ); + } + + for ( const auto& list : m_lists ) + { + retParams.push_back( list->getAsListParameter( m_parameters.childObjects() ) ); + } + + return retParams; } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.h b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.h index 0a6f5b3e07..bf7876b84f 100644 --- a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.h +++ b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterGroup.h @@ -28,6 +28,7 @@ #include class RimGenericParameter; +class RimParameterList; //================================================================================================== /// @@ -46,6 +47,8 @@ public: void addParameter( QString name, QString value ); void addParameter( QString name, double value ); + void addList( RimParameterList* paramList ); + void appendParametersToList( std::list& parameterList ); void setName( QString name ); @@ -76,6 +79,8 @@ private: caf::PdmFieldHandle* userDescriptionField() override; QString labelOrName() const; + bool isListParameter( QString paramName ) const; + private: caf::PdmChildArrayField m_parameters; caf::PdmField m_showExpanded; @@ -83,4 +88,5 @@ private: caf::PdmField m_label; caf::PdmField m_comment; caf::PdmProxyValueField m_labelProxy; + caf::PdmChildArrayField m_lists; }; diff --git a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.cpp b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.cpp new file mode 100644 index 0000000000..1b3a0d9560 --- /dev/null +++ b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.cpp @@ -0,0 +1,150 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2021 - 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#include "RimParameterList.h" + +#include "RimListParameter.h" + +#include + +CAF_PDM_SOURCE_INIT( RimParameterList, "ParameterList" ); + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimParameterList::RimParameterList() +{ + CAF_PDM_InitObject( "Parameter List", ":/Bullet.png", "", "" ); + uiCapability()->setUiTreeChildrenHidden( true ); + + CAF_PDM_InitFieldNoDefault( &m_parameterNames, "ParameterNames", "Parameters", "", "", "" ); + m_parameterNames.uiCapability()->setUiHidden( true ); + m_parameterNames.uiCapability()->setUiTreeHidden( true ); + m_parameterNames.uiCapability()->setUiTreeChildrenHidden( true ); + + CAF_PDM_InitField( &m_name, "Name", QString(), "Name", "", "", "" ); + m_name.uiCapability()->setUiHidden( true ); + + CAF_PDM_InitField( &m_label, "Label", QString(), "Name", "", "", "" ); + m_label.uiCapability()->setUiHidden( true ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimParameterList::~RimParameterList() +{ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimParameterList::addParameter( QString paramName ) +{ + m_parameterNames.v().push_back( paramName ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RimParameterList::containsParameter( QString paramName ) +{ + for ( auto& param : m_parameterNames.v() ) + { + if ( param == paramName ) return true; + } + + return false; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimParameterList::setName( QString name ) +{ + m_name = name; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimParameterList::setLabel( QString labelText ) +{ + m_label = labelText; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimParameterList::name() const +{ + return m_name; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimParameterList::label() const +{ + return m_label; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimParameterList::parameterValue( QString paramName, std::vector& parameters ) +{ + for ( auto& param : parameters ) + { + if ( paramName == param->name() ) return param->stringValue(); + } + return ""; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RimListParameter* RimParameterList::getAsListParameter( std::vector parameters ) +{ + QString stringValue = "["; + + bool skipComma = true; + + for ( auto& paramName : m_parameterNames.v() ) + { + if ( skipComma ) + { + stringValue += " "; + skipComma = false; + } + else + { + stringValue += ", "; + } + stringValue += parameterValue( paramName, parameters ); + } + + stringValue += " ]"; + + RimListParameter* param = new RimListParameter(); + param->setName( name() ); + param->setLabel( label() ); + param->setValue( stringValue ); + + return param; +} diff --git a/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.h b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.h new file mode 100644 index 0000000000..80438df784 --- /dev/null +++ b/ApplicationLibCode/ProjectDataModel/Parameters/RimParameterList.h @@ -0,0 +1,61 @@ +///////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2021 - 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "cafPdmChildArrayField.h" +#include "cafPdmField.h" +#include "cafPdmObject.h" + +#include +#include + +class RimListParameter; +class RimGenericParameter; + +//================================================================================================== +/// +/// +//================================================================================================== +class RimParameterList : public caf::PdmObject +{ + CAF_PDM_HEADER_INIT; + +public: + RimParameterList(); + ~RimParameterList() override; + + void addParameter( QString paramName ); + bool containsParameter( QString paramName ); + + RimListParameter* getAsListParameter( std::vector parameters ); + + void setName( QString name ); + void setLabel( QString labelText ); + + QString name() const; + QString label() const; + +private: + QString parameterValue( QString paramName, std::vector& parameters ); + +private: + caf::PdmField m_name; + caf::PdmField m_label; + caf::PdmField> m_parameterNames; +};