Scripting : Show script errors in message window

When assigning values from Python to a Pdm object, make sure the errors are displayed in the message window.
This commit is contained in:
Magne Sjaastad 2021-10-24 10:21:17 +02:00
parent 295186f99b
commit e711d94d49
3 changed files with 29 additions and 15 deletions

View File

@ -186,9 +186,8 @@ struct PdmFieldScriptingCapabilityIOHandler<AppEnum<T>>
{
// Unexpected enum value
// Error message
errorMessageContainer->addError( "Argument must be valid enum value. " +
errorMessageContainer->currentArgument + "\" argument of the command: \"" +
errorMessageContainer->currentCommand + "\"" );
errorMessageContainer->addError( "Failed to set enum text value \"" + accumulatedFieldValue +
"\" for the command: \"" + errorMessageContainer->currentCommand + "\"" );
}
}

View File

@ -15,7 +15,9 @@
// for more details.
//
//////////////////////////////////////////////////////////////////////////////////
#include "RiaGrpcServiceInterface.h"
#include "RiaLogging.h"
#include "RimCase.h"
#include "RimProject.h"
@ -31,9 +33,9 @@
#include "cafPdmScriptIOMessages.h"
#include "cafPdmXmlFieldHandle.h"
#include <PdmObject.pb.h>
#include <grpcpp/grpcpp.h>
#include <PdmObject.pb.h>
#include <QDebug>
#include <QXmlStreamReader>
@ -172,6 +174,8 @@ void RiaGrpcServiceInterface::copyPdmObjectFromRipsToCaf( const rips::PdmObject*
}
}
caf::PdmScriptIOMessages messages;
for ( auto field : fields )
{
auto scriptability = field->template capability<caf::PdmAbstractFieldScriptingCapability>();
@ -187,32 +191,39 @@ void RiaGrpcServiceInterface::copyPdmObjectFromRipsToCaf( const rips::PdmObject*
QString value = QString::fromStdString( parametersMap[keyword.toStdString()] );
QVariant oldValue, newValue;
if ( assignFieldValue( value, field, &oldValue, &newValue ) )
messages.currentCommand = "Assign value to field " + keyword;
messages.currentArgument = value;
if ( assignFieldValue( value, field, &oldValue, &newValue, &messages ) )
{
destination->uiCapability()->fieldChangedByUi( field, oldValue, newValue );
}
}
}
for ( const auto message : messages.m_messages )
{
RiaLogging::error( message.second );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaGrpcServiceInterface::assignFieldValue( const QString& stringValue,
caf::PdmFieldHandle* field,
QVariant* oldValue,
QVariant* newValue )
bool RiaGrpcServiceInterface::assignFieldValue( const QString& stringValue,
caf::PdmFieldHandle* field,
QVariant* oldValue,
QVariant* newValue,
caf::PdmScriptIOMessages* messages )
{
CAF_ASSERT( oldValue && newValue );
auto scriptability = field->template capability<caf::PdmAbstractFieldScriptingCapability>();
if ( field && scriptability != nullptr )
{
auto* valueField = dynamic_cast<caf::PdmValueField*>( field );
QTextStream stream( stringValue.toLatin1() );
caf::PdmScriptIOMessages messages;
auto* valueField = dynamic_cast<caf::PdmValueField*>( field );
QTextStream stream( stringValue.toLatin1() );
if ( valueField ) *oldValue = valueField->toQVariant();
scriptability->writeToField( stream, nullptr, &messages, false, RimProject::current() );
scriptability->writeToField( stream, nullptr, messages, false, RimProject::current() );
if ( valueField ) *newValue = valueField->toQVariant();
return true;
}

View File

@ -31,6 +31,7 @@ class PdmChildFieldHandle;
class PdmFieldHandle;
class PdmObject;
class PdmObjectHandle;
class PdmScriptIOMessages;
} // namespace caf
namespace rips
@ -57,8 +58,11 @@ public:
static void copyPdmObjectFromCafToRips( const caf::PdmObjectHandle* source, rips::PdmObject* destination );
static void copyPdmObjectFromRipsToCaf( const rips::PdmObject* source, caf::PdmObjectHandle* destination );
static bool
assignFieldValue( const QString& stringValue, caf::PdmFieldHandle* field, QVariant* oldValue, QVariant* newValue );
static bool assignFieldValue( const QString& stringValue,
caf::PdmFieldHandle* field,
QVariant* oldValue,
QVariant* newValue,
caf::PdmScriptIOMessages* messages );
static caf::PdmObjectHandle*
emplaceChildField( caf::PdmObject* parent, const QString& fieldKeyword, const QString& keywordForClassToCreate );