Move some more scriptability to caf

This commit is contained in:
Gaute Lindkvist 2020-03-04 11:30:08 +01:00
parent 17ea69397a
commit 2424d79c5d
14 changed files with 279 additions and 53 deletions

View File

@ -34,16 +34,3 @@ RicfCommandObject::RicfCommandObject()
RicfCommandObject::~RicfCommandObject()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicfCommandObject::pythonHelpString( const QString& existingTooltip, const QString& keyword )
{
QString snake_case = caf::PdmPythonGenerator::camelToSnakeCase( keyword );
QString helpString = QString( "Available through python/rips as the attribute '%1'" ).arg( snake_case );
if ( !existingTooltip.isEmpty() ) return existingTooltip + "\n\n" + helpString;
return helpString;
}

View File

@ -24,6 +24,7 @@
#include "cafCmdFeature.h"
#include "cafPdmObject.h"
#include "cafPdmObjectScriptabilityRegister.h"
#include "cafPdmPythonGenerator.h"
//==================================================================================================
//
@ -37,8 +38,6 @@ public:
~RicfCommandObject() override;
virtual RicfCommandResponse execute() = 0;
static QString pythonHelpString( const QString& existingTooltip, const QString& keyword );
};
#define RICF_InitField( field, keyword, default, uiName, iconResourceName, toolTip, whatsThis ) \
@ -47,7 +46,7 @@ public:
default, \
uiName, \
iconResourceName, \
RicfCommandObject::pythonHelpString( toolTip, keyword ), \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, keyword )
@ -56,7 +55,7 @@ public:
keyword, \
uiName, \
iconResourceName, \
RicfCommandObject::pythonHelpString( toolTip, keyword ), \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, keyword )
@ -66,7 +65,7 @@ public:
default, \
uiName, \
iconResourceName, \
RicfCommandObject::pythonHelpString( toolTip, scriptKeyword ), \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, scriptKeyword )
@ -75,7 +74,7 @@ public:
keyword, \
uiName, \
iconResourceName, \
RicfCommandObject::pythonHelpString( toolTip, scriptKeyword ), \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
AddRicfCapabilityToField( field, scriptKeyword )

View File

@ -31,10 +31,10 @@
template <typename DataType>
struct RicfFieldIOHandler
{
static void writeToField( DataType& fieldValue,
QTextStream& inputStream,
static void writeToField( DataType& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
bool stringsAreQuoted = true )
{
inputStream >> fieldValue;
if ( inputStream.status() == QTextStream::ReadCorruptData )
@ -72,10 +72,10 @@ struct RicfFieldIOHandler<QString>
template <>
struct RicfFieldIOHandler<bool>
{
static void writeToField( bool& fieldValue,
QTextStream& inputStream,
static void writeToField( bool& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
bool stringsAreQuoted = true );
static void readFromField( const bool& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
@ -85,10 +85,10 @@ struct RicfFieldIOHandler<bool>
template <>
struct RicfFieldIOHandler<cvf::Color3f>
{
static void writeToField( cvf::Color3f& fieldValue,
QTextStream& inputStream,
static void writeToField( cvf::Color3f& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
bool stringsAreQuoted = true );
static void readFromField( const cvf::Color3f& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
@ -98,10 +98,10 @@ struct RicfFieldIOHandler<cvf::Color3f>
template <typename T>
struct RicfFieldIOHandler<caf::AppEnum<T>>
{
static void writeToField( caf::AppEnum<T>& fieldValue,
QTextStream& inputStream,
static void writeToField( caf::AppEnum<T>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
bool stringsAreQuoted = true )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QString accumulatedFieldValue;
@ -149,10 +149,10 @@ struct RicfFieldIOHandler<caf::AppEnum<T>>
template <typename T>
struct RicfFieldIOHandler<std::vector<T>>
{
static void writeToField( std::vector<T>& fieldValue,
QTextStream& inputStream,
static void writeToField( std::vector<T>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true )
bool stringsAreQuoted = true )
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
QChar chr = errorMessageContainer->readCharWithLineNumberCount( inputStream );
@ -221,10 +221,10 @@ public:
// Xml Serializing
public:
void readFieldData( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true ) override
void writeToField( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true ) override
{
typename FieldType::FieldDataType value;
RicfFieldIOHandler<typename FieldType::FieldDataType>::writeToField( value,
@ -238,7 +238,7 @@ public:
}
}
void writeFieldData( QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false ) const override
void readFromField( QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false ) const override
{
RicfFieldIOHandler<typename FieldType::FieldDataType>::readFromField( m_field->value(),
outputStream,

View File

@ -41,11 +41,6 @@ public:
RicfFieldHandle( caf::PdmFieldHandle* owner, const QString& scriptFieldName, bool giveOwnership );
~RicfFieldHandle() override;
virtual void readFieldData( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true ) = 0;
private:
caf::PdmFieldHandle* m_owner;
QString m_fieldName;

View File

@ -136,7 +136,7 @@ void RicfObjectCapability::readFields( QTextStream& inputStream,
if ( xmlFieldHandle->isIOReadable() )
{
errorMessageContainer->currentArgument = keyword;
rcfField->readFieldData( inputStream, objectFactory, errorMessageContainer );
rcfField->writeToField( inputStream, objectFactory, errorMessageContainer );
errorMessageContainer->currentArgument = keyword;
}
}
@ -213,7 +213,7 @@ void RicfObjectCapability::writeFields( QTextStream& outputStream ) const
}
outputStream << keyword << " = ";
rcfField->writeFieldData( outputStream );
rcfField->readFromField( outputStream );
writtenFieldCount++;
}

View File

@ -107,7 +107,7 @@ void RiaGrpcServiceInterface::copyPdmObjectFromCafToRips( const caf::PdmObjectHa
{
QString text;
QTextStream outStream( &text );
ricfHandle->writeFieldData( outStream, false );
ricfHandle->readFromField( outStream, false );
( *parametersMap )[ricfHandle->scriptFieldName().toStdString()] = text.toStdString();
}
}
@ -176,7 +176,7 @@ bool RiaGrpcServiceInterface::assignFieldValue( const QString& stringValue,
QTextStream stream( stringValue.toLatin1() );
caf::PdmScriptIOMessages messages;
*oldValue = field->toQVariant();
ricfHandle->readFieldData( stream, nullptr, &messages, false );
ricfHandle->writeToField( stream, nullptr, &messages, false );
*newValue = field->toQVariant();
return true;
}

View File

@ -172,7 +172,7 @@ RimEclipseView::RimEclipseView()
CAF_PDM_InitField( &m_showInactiveCells, "ShowInactiveCells", false, "Show Inactive Cells", "", "", "" );
CAF_PDM_InitField( &m_showInvalidCells, "ShowInvalidCells", false, "Show Invalid Cells", "", "", "" );
RICF_InitFieldNoDefault( &m_cellResultData, "CellResultData", "", "", "", "Current Eclipse Cell Result" );
CAF_PDM_InitScriptableFieldNoDefault( &m_cellResultData, "CellResultData", "", "", "", "Current Eclipse Cell Result" );
m_cellResultData.xmlCapability()->disableIO();
m_cellResultData.registerGetMethod( this, &RimEclipseView::currentCellResultData );
m_cellResultData.registerSetMethod( this, &RimEclipseView::setCurrentCellResultData );

View File

@ -47,7 +47,13 @@ RimWellBoreStabilityPlot::RimWellBoreStabilityPlot()
"",
"A GeoMechanical Well Bore Stabilit Plot" );
CAF_PDM_InitFieldNoDefault( &m_wbsParameters, "WbsParameters", "Well Bore Stability Parameters", "", "", "" );
CAF_PDM_InitScriptableFieldWithKeywordNoDefault( &m_wbsParameters,
"WbsParameters",
"Params",
"Well Bore Stability Parameters",
"",
"",
"" );
m_wbsParameters = new RimWbsParameters;
m_wbsParameters.uiCapability()->setUiTreeHidden( true );
m_wbsParameters.uiCapability()->setUiTreeChildrenHidden( true );

View File

@ -82,3 +82,30 @@ void PdmFieldScriptability::setIOWriteable(bool writeable)
{
m_IOWriteable = writeable;
}
//--------------------------------------------------------------------------------------------------
/// Empty default implementation that doesn't offer any script IO for the field
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptability::readFromField(QTextStream& outputStream, bool quoteStrings /*= true*/, bool quoteNonBuiltins /*= false*/) const
{
}
//--------------------------------------------------------------------------------------------------
/// Empty default implementation that doesn't offer any script IO for the field
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptability::writeToField(QTextStream& inputStream, caf::PdmObjectFactory* objectFactory, caf::PdmScriptIOMessages* errorMessageContainer, bool stringsAreQuoted /*= true*/)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptability::addToField(caf::PdmFieldHandle* field, const QString& fieldName)
{
if (field->template capability<PdmFieldScriptability>() == nullptr)
{
new PdmFieldScriptability(field, fieldName, true);
}
}

View File

@ -43,6 +43,46 @@ class QTextStream;
namespace caf {
class PdmFieldHandle;
class PdmObjectFactory;
class PdmScriptIOMessages;
#define CAF_PDM_InitScriptableField( field, keyword, default, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitField( field, \
keyword, \
default, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
caf::PdmFieldScriptability::addToField( field, keyword )
#define CAF_PDM_InitScriptableFieldNoDefault( field, keyword, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitFieldNoDefault( field, \
keyword, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, keyword ), \
whatsThis ); \
caf::PdmFieldScriptability::addToField( field, keyword )
#define CAF_PDM_InitScriptableFieldWithKeyword( field, keyword, scriptKeyword, default, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitField( field, \
keyword, \
default, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
caf::PdmFieldScriptability::addToField( field, scriptKeyword )
#define CAF_PDM_InitScriptableFieldWithKeywordNoDefault( field, keyword, scriptKeyword, uiName, iconResourceName, toolTip, whatsThis ) \
CAF_PDM_InitFieldNoDefault( field, \
keyword, \
uiName, \
iconResourceName, \
caf::PdmPythonGenerator::pythonHelpString( toolTip, scriptKeyword ), \
whatsThis ); \
caf::PdmFieldScriptability::addToField( field, scriptKeyword )
class PdmFieldScriptability : public PdmFieldCapability
{
@ -55,12 +95,19 @@ public:
bool isIOWriteable() const;
void setIOWriteable(bool writeable);
virtual void writeFieldData(QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false) const = 0;
virtual void readFromField(QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false) const;
virtual void writeToField(QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true);
static void addToField(caf::PdmFieldHandle* field, const QString& fieldName);
private:
caf::PdmFieldHandle* m_owner;
QString m_scriptFieldName;
bool m_IOWriteable;
};
}

View File

@ -177,7 +177,7 @@ QString PdmPythonGenerator::generate(PdmObjectFactory* factory) const
{
QString valueString;
QTextStream valueStream(&valueString);
scriptability->writeFieldData(valueStream, true, true);
scriptability->readFromField(valueStream, true, true);
if (valueString.isEmpty())
valueString = QString("\"\"");
valueString = pythonifyDataValue(valueString);
@ -355,10 +355,23 @@ QString PdmPythonGenerator::dataTypeString(const PdmFieldHandle* field, bool* is
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString caf::PdmPythonGenerator::pythonifyDataValue(const QString& dataValue)
QString PdmPythonGenerator::pythonifyDataValue(const QString& dataValue)
{
QString outValue = dataValue;
outValue.replace("false", "False");
outValue.replace("true", "True");
return outValue;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmPythonGenerator::pythonHelpString(const QString& existingTooltip, const QString& keyword)
{
QString snake_case = caf::PdmPythonGenerator::camelToSnakeCase(keyword);
QString helpString = QString("Available through python/rips as the attribute '%1'").arg(snake_case);
if (!existingTooltip.isEmpty()) return existingTooltip + "\n\n" + helpString;
return helpString;
}

View File

@ -53,6 +53,8 @@ public:
static QString dataTypeString(const PdmFieldHandle* field, bool* isList = nullptr, bool* isBuiltinType = nullptr);
static QString pythonifyDataValue(const QString& dataValue);
static QString pythonHelpString(const QString& existingTooltip, const QString& keyword);
};
}

View File

@ -0,0 +1,92 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 "cafPdmScriptIOMessages.h"
#include <QTextStream>
using namespace caf;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmScriptIOMessages::addWarning( const QString& message )
{
m_messages.push_back(
std::make_pair( MESSAGE_WARNING, "Line " + QString::number( m_currentLineNumber ) + ": " + message ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmScriptIOMessages::addError( const QString& message )
{
m_messages.push_back(
std::make_pair( MESSAGE_ERROR, "Line " + QString::number( m_currentLineNumber ) + ": " + message ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmScriptIOMessages::skipWhiteSpaceWithLineNumberCount( QTextStream& inputStream )
{
while ( !inputStream.atEnd() )
{
QChar ch = readCharWithLineNumberCount( inputStream );
if ( !ch.isSpace() )
{
inputStream.seek( inputStream.pos() - 1 );
break;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QChar PdmScriptIOMessages::readCharWithLineNumberCount( QTextStream& inputStream )
{
QChar ch;
inputStream >> ch;
if ( ch == QChar( '\n' ) )
{
m_currentLineNumber++;
}
return ch;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QChar PdmScriptIOMessages::peekNextChar( QTextStream& inputStream )
{
QChar ch;
if ( !inputStream.atEnd() )
{
inputStream >> ch;
inputStream.seek( inputStream.pos() - 1 );
}
return ch;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmScriptIOMessages::skipLineWithLineNumberCount( QTextStream& inputStream )
{
inputStream.readLine();
m_currentLineNumber++;
}

View File

@ -0,0 +1,58 @@
/////////////////////////////////////////////////////////////////////////////////
//
// 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 <QString>
#include <vector>
class QTextStream;
namespace caf {
class PdmScriptIOMessages
{
public:
PdmScriptIOMessages()
: m_currentLineNumber( 1 )
{
}
enum MessageType
{
MESSAGE_WARNING,
MESSAGE_ERROR
};
void addWarning( const QString& message );
void addError( const QString& message );
void skipWhiteSpaceWithLineNumberCount( QTextStream& inputStream );
void skipLineWithLineNumberCount( QTextStream& inputStream );
QChar readCharWithLineNumberCount( QTextStream& inputStream );
QChar peekNextChar( QTextStream& inputStream );
QString currentCommand;
QString currentArgument;
std::vector<std::pair<MessageType, QString>> m_messages;
private:
int m_currentLineNumber;
};
}