#5632 Python method commands (#5649)

* General PdmObjectMethods for scripting.
This commit is contained in:
Gaute Lindkvist
2020-03-10 14:11:22 +01:00
committed by GitHub
parent 587478cbd1
commit c51aa91c42
145 changed files with 2646 additions and 1587 deletions

View File

@@ -1,19 +1,11 @@
set (SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicfCommandObject.h
${CMAKE_CURRENT_LIST_DIR}/RicfCommandResponse.h
${CMAKE_CURRENT_LIST_DIR}/RicfFieldCapability.h
${CMAKE_CURRENT_LIST_DIR}/RicfFieldHandle.h
${CMAKE_CURRENT_LIST_DIR}/RicfObjectCapability.h
${CMAKE_CURRENT_LIST_DIR}/RifcCommandFileReader.h
)
set (SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicfCommandObject.cpp
${CMAKE_CURRENT_LIST_DIR}/RicfCommandResponse.cpp
${CMAKE_CURRENT_LIST_DIR}/RicfFieldCapability.cpp
${CMAKE_CURRENT_LIST_DIR}/RicfFieldHandle.cpp
${CMAKE_CURRENT_LIST_DIR}/RicfObjectCapability.cpp
${CMAKE_CURRENT_LIST_DIR}/RifcCommandFileReader.cpp
)

View File

@@ -18,13 +18,11 @@
#include "RicfCommandObject.h"
#include "cafPdmPythonGenerator.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfCommandObject::RicfCommandObject()
: RicfObjectCapability( this, false )
: PdmObjectScriptability( this, false )
{
}

View File

@@ -17,66 +17,11 @@
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RicfCommandResponse.h"
#include "RicfFieldCapability.h"
#include "RicfObjectCapability.h"
#include "cafCmdFeature.h"
#include "cafPdmObject.h"
#include "cafPdmObjectScriptabilityRegister.h"
#include "cafPdmPythonGenerator.h"
//==================================================================================================
//
//
//
//==================================================================================================
class RicfCommandObject : public caf::PdmObject, public RicfObjectCapability
{
public:
RicfCommandObject();
~RicfCommandObject() override;
virtual RicfCommandResponse execute() = 0;
};
#define RICF_InitField( field, keyword, default, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitField( field, \
keyword, \
default, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, keyword )
#define RICF_InitFieldNoDefault( field, keyword, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitFieldNoDefault( field, \
keyword, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, keyword )
#define RICF_InitFieldTranslated( field, keyword, scriptKeyword, default, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitField( field, \
keyword, \
default, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, scriptKeyword )
#define RICF_InitFieldNoDefaultTranslated( field, keyword, scriptKeyword, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitFieldNoDefault( field, \
keyword, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, scriptKeyword )
#include "cafPdmObjectScriptability.h"
#include "cafPdmScriptResponse.h"
#define RICF_HEADER_INIT \
CAF_CMD_HEADER_INIT; \
@@ -91,3 +36,17 @@ public:
} \
CAF_FACTORY_REGISTER2( caf::CmdFeature, ClassName, std::string, ClassName::idNameStatic() ); \
CAF_PDM_SOURCE_INIT( ClassName, CommandKeyword )
//==================================================================================================
//
//
//
//==================================================================================================
class RicfCommandObject : public caf::PdmObject, public caf::PdmObjectScriptability
{
public:
RicfCommandObject();
~RicfCommandObject() override;
virtual caf::PdmScriptResponse execute() = 0;
};

View File

@@ -1,106 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- 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 "RicfCommandResponse.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfCommandResponse::RicfCommandResponse( Status status, const QString& message )
: m_status( COMMAND_OK )
{
updateStatus( status, message );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfCommandResponse::RicfCommandResponse( caf::PdmObject* ok_result )
: m_status( COMMAND_OK )
, m_result( ok_result )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfCommandResponse::Status RicfCommandResponse::status() const
{
return m_status;
}
//--------------------------------------------------------------------------------------------------
/// The resulting message is sent in HTTP metadata and must not have any newlines.
//--------------------------------------------------------------------------------------------------
QString RicfCommandResponse::sanitizedResponseMessage() const
{
QString completeMessage = m_messages.join( ";;" );
completeMessage.replace( '\n', ";;" );
return completeMessage;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RicfCommandResponse::messages() const
{
return m_messages;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmObject* RicfCommandResponse::result() const
{
return m_result.get();
}
//--------------------------------------------------------------------------------------------------
/// Takes ownership of the result object
//--------------------------------------------------------------------------------------------------
void RicfCommandResponse::setResult( caf::PdmObject* result )
{
m_result.reset( result );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfCommandResponse::updateStatus( Status status, const QString& message )
{
m_status = std::max( m_status, status );
if ( !message.isEmpty() )
{
m_messages.push_back( QString( "%1: %2" ).arg( statusLabel( status ) ).arg( message ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicfCommandResponse::statusLabel( Status status )
{
switch ( status )
{
case COMMAND_WARNING:
return "Warning";
case COMMAND_ERROR:
return "Error";
default:
return "";
}
}

View File

@@ -1,62 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- 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 "cafPdmObject.h"
#include "cafPdmPointer.h"
#include <QString>
#include <QStringList>
#include <memory>
//==================================================================================================
//
// Command response which contains status and possibly a result
//
//==================================================================================================
class RicfCommandResponse
{
public:
// Status in order of severity from ok to critical
enum Status
{
COMMAND_OK,
COMMAND_WARNING,
COMMAND_ERROR
};
public:
RicfCommandResponse( Status status = COMMAND_OK, const QString& message = "" );
explicit RicfCommandResponse( caf::PdmObject* ok_result );
Status status() const;
QString sanitizedResponseMessage() const;
QStringList messages() const;
caf::PdmObject* result() const;
void setResult( caf::PdmObject* result );
void updateStatus( Status status, const QString& message );
private:
static QString statusLabel( Status status );
private:
Status m_status;
QStringList m_messages;
std::unique_ptr<caf::PdmObject> m_result;
};

View File

@@ -1,198 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RicfFieldCapability.h"
#include "RiaColorTools.h"
#include <QColor>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<QString>::writeToField( QString& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted )
{
fieldValue = "";
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QString accumulatedFieldValue;
QChar currentChar;
bool validStringStart = !stringsAreQuoted;
bool validStringEnd = !stringsAreQuoted;
if ( stringsAreQuoted )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( currentChar == QChar( '"' ) )
{
validStringStart = true;
}
}
if ( validStringStart )
{
while ( !inputStream.atEnd() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( currentChar != QChar( '\\' ) )
{
if ( currentChar == QChar( '"' ) ) // End Quote
{
// Reached end of string
validStringEnd = true;
break;
}
else
{
accumulatedFieldValue += currentChar;
}
}
else
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
accumulatedFieldValue += currentChar;
}
}
}
if ( !validStringStart )
{
// Unexpected start of string, Missing '"'
// Error message
errorMessageContainer->addError(
"String argument does not seem to be quoted. Missing the start '\"' in the \"" +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
// Could interpret as unquoted text
}
else if ( !validStringEnd )
{
// Unexpected end of string, Missing '"'
// Error message
errorMessageContainer->addError( "String argument does not seem to be quoted. Missing the end '\"' in the \"" +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
// Could interpret as unquoted text
}
fieldValue = accumulatedFieldValue;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<QString>::readFromField( const QString& fieldValue,
QTextStream& outputStream,
bool quoteStrings,
bool quoteNonBuiltin )
{
outputStream << "\"";
for ( int i = 0; i < fieldValue.size(); ++i )
{
if ( fieldValue[i] == QChar( '"' ) || fieldValue[i] == QChar( '\\' ) )
{
outputStream << "\\";
}
outputStream << fieldValue[i];
}
outputStream << "\"";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<bool>::writeToField( bool& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QString accumulatedFieldValue;
QChar nextChar;
QChar currentChar;
while ( !inputStream.atEnd() )
{
nextChar = errorMessageContainer->peekNextChar( inputStream );
if ( nextChar.isLetter() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
accumulatedFieldValue += currentChar;
}
else
{
break;
}
}
// Accept TRUE or False in any case combination.
bool evaluatesToTrue = QString::compare( accumulatedFieldValue, QString( "true" ), Qt::CaseInsensitive ) == 0;
bool evaluatesToFalse = QString::compare( accumulatedFieldValue, QString( "false" ), Qt::CaseInsensitive ) == 0;
if ( evaluatesToTrue == evaluatesToFalse )
{
QString formatString(
"Boolean argument \"%1\" for the command \"%2\" does not evaluate to either true or false" );
QString errorMessage =
formatString.arg( errorMessageContainer->currentArgument ).arg( errorMessageContainer->currentCommand );
errorMessageContainer->addError( errorMessage );
}
fieldValue = evaluatesToTrue;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<bool>::readFromField( const bool& fieldValue,
QTextStream& outputStream,
bool quoteStrings,
bool quoteNonBuiltin )
{
// Lower-case true/false is used in the documentation.
outputStream << ( fieldValue ? "true" : "false" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<cvf::Color3f>::writeToField( cvf::Color3f& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted )
{
QString fieldStringValue;
RicfFieldIOHandler<QString>::writeToField( fieldStringValue, inputStream, errorMessageContainer, stringsAreQuoted );
QColor qColor( fieldStringValue );
if ( qColor.isValid() )
{
fieldValue = RiaColorTools::fromQColorTo3f( qColor );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfFieldIOHandler<cvf::Color3f>::readFromField( const cvf::Color3f& fieldValue,
QTextStream& outputStream,
bool quoteStrings,
bool quoteNonBuiltin )
{
QColor qColor = RiaColorTools::toQColor( fieldValue );
QString fieldStringValue = qColor.name();
RicfFieldIOHandler<QString>::readFromField( fieldStringValue, outputStream, quoteStrings );
}

View File

@@ -1,260 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RicfFieldHandle.h"
#include "cafAppEnum.h"
#include "cafPdmScriptIOMessages.h"
#include "cvfColor3.h"
#include <QString>
#include <QTextStream>
template <typename DataType>
struct RicfFieldIOHandler
{
static void writeToField( DataType& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
inputStream >> fieldValue;
if ( inputStream.status() == QTextStream::ReadCorruptData )
{
errorMessageContainer->addError( "Argument value is unreadable in the argument: \"" +
errorMessageContainer->currentArgument + "\" in the command: \"" +
errorMessageContainer->currentCommand + "\"" );
inputStream.setStatus( QTextStream::Ok );
}
}
static void readFromField( const DataType& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false )
{
outputStream << fieldValue;
}
};
template <>
struct RicfFieldIOHandler<QString>
{
static void writeToField( QString& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
static void readFromField( const QString& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};
template <>
struct RicfFieldIOHandler<bool>
{
static void writeToField( bool& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
static void readFromField( const bool& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};
template <>
struct RicfFieldIOHandler<cvf::Color3f>
{
static void writeToField( cvf::Color3f& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
static void readFromField( const cvf::Color3f& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};
template <typename T>
struct RicfFieldIOHandler<caf::AppEnum<T>>
{
static void writeToField( caf::AppEnum<T>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QString accumulatedFieldValue;
QChar nextChar;
QChar currentChar;
while ( !inputStream.atEnd() )
{
nextChar = errorMessageContainer->peekNextChar( inputStream );
if ( nextChar.isLetterOrNumber() || nextChar == QChar( '_' ) )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
accumulatedFieldValue += currentChar;
}
else
{
break;
}
}
if ( !fieldValue.setFromText( accumulatedFieldValue ) )
{
// Unexpected enum value
// Error message
errorMessageContainer->addError( "Argument must be valid enum value. " +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
}
static void readFromField( const caf::AppEnum<T>& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false )
{
if ( quoteNonBuiltins )
{
outputStream << "\"" << fieldValue << "\"";
}
else
{
outputStream << fieldValue;
}
}
};
template <typename T>
struct RicfFieldIOHandler<std::vector<T>>
{
static void writeToField( std::vector<T>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QChar chr = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( chr == QChar( '[' ) )
{
while ( !inputStream.atEnd() )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QChar nextChar = errorMessageContainer->peekNextChar( inputStream );
if ( nextChar == QChar( ']' ) )
{
nextChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
break;
}
else if ( nextChar == QChar( ',' ) )
{
nextChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
}
T value;
RicfFieldIOHandler<T>::writeToField( value, inputStream, errorMessageContainer, true );
fieldValue.push_back( value );
}
}
else
{
errorMessageContainer->addError( "Array argument is missing start '['. " +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
}
static void readFromField( const std::vector<T>& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false )
{
outputStream << "[";
for ( size_t i = 0; i < fieldValue.size(); ++i )
{
RicfFieldIOHandler<T>::readFromField( fieldValue[i], outputStream, quoteNonBuiltins );
if ( i < fieldValue.size() - 1 )
{
outputStream << ", ";
}
}
outputStream << "]";
}
};
//==================================================================================================
//
//
//
//==================================================================================================
template <typename FieldType>
class RicfFieldCapability : public RicfFieldHandle
{
public:
RicfFieldCapability( FieldType* field, const QString& fieldName, bool giveOwnership )
: RicfFieldHandle( field, fieldName, giveOwnership )
{
m_field = field;
}
// Xml Serializing
public:
void writeToField( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true ) override
{
typename FieldType::FieldDataType value;
RicfFieldIOHandler<typename FieldType::FieldDataType>::writeToField( value,
inputStream,
errorMessageContainer,
stringsAreQuoted );
if ( this->isIOWriteable() )
{
m_field->setValue( value );
}
}
void readFromField( QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false ) const override
{
RicfFieldIOHandler<typename FieldType::FieldDataType>::readFromField( m_field->value(),
outputStream,
quoteStrings,
quoteNonBuiltins );
}
private:
FieldType* m_field;
};
template <typename FieldType>
void AddRicfCapabilityToField( FieldType* field, const QString& fieldName )
{
if ( field->template capability<RicfFieldCapability<FieldType>>() == nullptr )
{
new RicfFieldCapability<FieldType>( field, fieldName, true );
}
}

View File

@@ -1,35 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RicfFieldHandle.h"
#include "cafPdmFieldHandle.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfFieldHandle::RicfFieldHandle( caf::PdmFieldHandle* owner, const QString& scriptFieldName, bool giveOwnership )
: caf::PdmFieldScriptability( owner, scriptFieldName, giveOwnership )
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfFieldHandle::~RicfFieldHandle()
{
}

View File

@@ -1,48 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmFieldScriptability.h"
#include <QString>
namespace caf
{
class PdmObjectFactory;
class PdmFieldHandle;
class PdmScriptIOMessages;
} // namespace caf
class QTextStream;
//==================================================================================================
//
//
//
//==================================================================================================
class RicfFieldHandle : public caf::PdmFieldScriptability
{
public:
RicfFieldHandle( caf::PdmFieldHandle* owner, const QString& scriptFieldName, bool giveOwnership );
~RicfFieldHandle() override;
private:
caf::PdmFieldHandle* m_owner;
QString m_fieldName;
bool m_IOWriteable;
};

View File

@@ -1,221 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RicfObjectCapability.h"
#include "RicfFieldHandle.h"
#include "cafPdmObject.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmScriptIOMessages.h"
#include "cafPdmXmlFieldHandle.h"
#include <QTextStream>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfObjectCapability::RicfObjectCapability( caf::PdmObjectHandle* owner, bool giveOwnership )
: m_owner( owner )
{
m_owner->addCapability( this, giveOwnership );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfObjectCapability::~RicfObjectCapability()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfObjectCapability::readFields( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer )
{
std::set<QString> readFields;
bool isLastArgumentRead = false;
while ( !inputStream.atEnd() && !isLastArgumentRead )
{
// Read field keyword
bool fieldDataFound = false;
bool isEndOfArgumentFound = false;
QString keyword;
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
{
QChar currentChar;
while ( !inputStream.atEnd() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( currentChar == QChar( '=' ) || currentChar == QChar( ')' ) || currentChar == QChar( ',' ) ||
currentChar.isSpace() )
{
break;
}
else
{
keyword += currentChar;
}
}
if ( currentChar.isSpace() )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
}
if ( currentChar == QChar( '=' ) )
{
fieldDataFound = true;
}
else if ( currentChar == QChar( ')' ) )
{
if ( !keyword.isNull() )
{
errorMessageContainer->addError(
QString( "Can't find the '=' after the argument named '%1' in the command '%2'" )
.arg( keyword )
.arg( errorMessageContainer->currentCommand ) );
}
isLastArgumentRead = true;
}
else if ( currentChar == QChar( ',' ) )
{
errorMessageContainer->addError(
QString( "Can't find the '=' after the argument named '%1' in the command '%2'" )
.arg( keyword )
.arg( errorMessageContainer->currentCommand ) );
isEndOfArgumentFound = true;
}
else
{
errorMessageContainer->addError(
QString( "Can't find the '=' after the argument named '%1' in the command '%2'" )
.arg( keyword )
.arg( errorMessageContainer->currentCommand ) );
}
}
if ( readFields.count( keyword ) )
{
// Warning message: Referenced the same argument several times
errorMessageContainer->addWarning( "The argument: \"" + keyword +
"\" is referenced several times in the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
}
if ( fieldDataFound )
{
// Make field read its data
caf::PdmFieldHandle* fieldHandle = m_owner->findField( keyword );
if ( fieldHandle && fieldHandle->xmlCapability() && fieldHandle->capability<RicfFieldHandle>() )
{
caf::PdmXmlFieldHandle* xmlFieldHandle = fieldHandle->xmlCapability();
RicfFieldHandle* rcfField = fieldHandle->capability<RicfFieldHandle>();
if ( xmlFieldHandle->isIOReadable() )
{
errorMessageContainer->currentArgument = keyword;
rcfField->writeToField( inputStream, objectFactory, errorMessageContainer );
errorMessageContainer->currentArgument = keyword;
}
}
else
{
// Error message: Unknown argument name
errorMessageContainer->addWarning( "The argument: \"" + keyword + "\" does not exist in the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
}
// Skip to end of argument ',' or end of call ')'
if ( !( isLastArgumentRead || isEndOfArgumentFound ) )
{
QChar currentChar;
bool isOutsideQuotes = true;
while ( !inputStream.atEnd() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( isOutsideQuotes )
{
if ( currentChar == QChar( ',' ) )
{
break;
}
if ( currentChar == QChar( ')' ) )
{
isLastArgumentRead = true;
break;
}
if ( currentChar == QChar( '\"' ) )
{
isOutsideQuotes = false;
}
}
else
{
if ( currentChar == QChar( '\"' ) )
{
isOutsideQuotes = true;
}
if ( currentChar == QChar( '\\' ) )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
}
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfObjectCapability::writeFields( QTextStream& outputStream ) const
{
std::vector<caf::PdmFieldHandle*> fields;
m_owner->fields( fields );
int writtenFieldCount = 0;
for ( size_t it = 0; it < fields.size(); ++it )
{
const caf::PdmXmlFieldHandle* xmlField = fields[it]->xmlCapability();
const RicfFieldHandle* rcfField = fields[it]->capability<RicfFieldHandle>();
if ( rcfField && xmlField && xmlField->isIOWritable() )
{
QString keyword = xmlField->fieldHandle()->keyword();
CAF_ASSERT( caf::PdmXmlObjectHandle::isValidXmlElementName( keyword ) );
if ( writtenFieldCount >= 1 )
{
outputStream << ", ";
}
outputStream << keyword << " = ";
rcfField->readFromField( outputStream );
writtenFieldCount++;
}
}
}

View File

@@ -1,55 +0,0 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmObjectCapability.h"
#include <QString>
#include <map>
namespace caf
{
class PdmObject;
class PdmObjectHandle;
class PdmObjectFactory;
class PdmScriptIOMessages;
} // namespace caf
class QTextStream;
//==================================================================================================
//
//
//
//==================================================================================================
class RicfObjectCapability : public caf::PdmObjectCapability
{
public:
RicfObjectCapability( caf::PdmObjectHandle* owner, bool giveOwnership );
~RicfObjectCapability() override;
void readFields( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer );
void writeFields( QTextStream& outputStream ) const;
private:
caf::PdmObjectHandle* m_owner;
};

View File

@@ -19,9 +19,9 @@
#include "RifcCommandFileReader.h"
#include "RicfCommandObject.h"
#include "RicfObjectCapability.h"
#include "cafPdmObjectFactory.h"
#include "cafPdmObjectScriptability.h"
#include "cafPdmScriptIOMessages.h"
#include <QTextStream>
@@ -127,7 +127,7 @@ std::vector<RicfCommandObject*> RicfCommandFileReader::readCommands( QTextStream
else
{
readCommands.push_back( cObj );
auto rcfCap = cObj->capability<RicfObjectCapability>();
auto rcfCap = cObj->capability<caf::PdmObjectScriptability>();
errorMessageContainer->currentCommand = commandName;
rcfCap->readFields( inputStream, objectFactory, errorMessageContainer );
errorMessageContainer->currentCommand = "";
@@ -144,7 +144,7 @@ void RicfCommandFileReader::writeCommands( QTextStream& outputStream, const std:
{
for ( const auto& cmdObj : commandsToWrite )
{
auto rcfCap = cmdObj->capability<RicfObjectCapability>();
auto rcfCap = cmdObj->capability<caf::PdmObjectScriptability>();
if ( !rcfCap ) continue;
outputStream << cmdObj->classKeyword();