#8180 Python : Generate enum texts for documentation

This commit is contained in:
Magne Sjaastad 2021-10-22 18:58:01 +02:00
parent 9e051d2769
commit fbc8300210
4 changed files with 83 additions and 2 deletions

View File

@ -38,6 +38,8 @@
#include "cafPdmFieldHandle.h"
#include "cafPdmPythonGenerator.h"
#include <QStringList>
using namespace caf;
//--------------------------------------------------------------------------------------------------
@ -96,3 +98,11 @@ QString PdmAbstractFieldScriptingCapability::helpString( const QString& existing
if ( !existingTooltip.isEmpty() ) return existingTooltip + "\n\n" + helpString;
return helpString;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList PdmAbstractFieldScriptingCapability::enumScriptTexts() const
{
return QStringList();
}

View File

@ -39,6 +39,7 @@
#include <QString>
class QTextStream;
class QStringList;
namespace caf
{
@ -68,6 +69,8 @@ public:
static QString helpString( const QString& existingTooltip, const QString& keyword );
virtual QStringList enumScriptTexts() const;
protected:
PdmFieldHandle* m_owner;
QString m_scriptFieldName;

View File

@ -39,6 +39,7 @@
#include "cafPdmAbstractFieldScriptingCapability.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmChildField.h"
#include "cafPdmField.h"
#include "cafPdmPtrArrayField.h"
#include "cafPdmPtrField.h"
#include "cafPdmScriptIOMessages.h"
@ -631,6 +632,60 @@ private:
PdmChildArrayField<DataType*>* m_field;
};
template <typename DataType>
class PdmFieldScriptingCapability<PdmField<caf::AppEnum<DataType>>> : public PdmAbstractFieldScriptingCapability
{
public:
PdmFieldScriptingCapability( PdmField<caf::AppEnum<DataType>>* field, const QString& fieldName, bool giveOwnership )
: PdmAbstractFieldScriptingCapability( field, fieldName, giveOwnership )
{
m_field = field;
}
void writeToField( QTextStream& inputStream,
PdmObjectFactory* objectFactory,
PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true,
caf::PdmObjectHandle* existingObjectsRoot = nullptr ) override
{
if ( this->isIOWriteable() )
{
AppEnum<DataType> value;
PdmFieldScriptingCapabilityIOHandler<caf::AppEnum<DataType>>::writeToField( value,
inputStream,
errorMessageContainer,
stringsAreQuoted );
m_field->setValue( value );
}
}
void readFromField( QTextStream& outputStream, bool quoteStrings = true, bool quoteNonBuiltins = false ) const override
{
PdmFieldScriptingCapabilityIOHandler<caf::AppEnum<DataType>>::readFromField( m_field->value(),
outputStream,
quoteStrings,
quoteNonBuiltins );
}
QStringList enumScriptTexts() const override
{
QStringList enumTexts;
for ( size_t i = 0; i < caf::AppEnum<DataType>::size(); i++ )
{
auto enumText = caf::AppEnum<DataType>::text( caf::AppEnum<DataType>::fromIndex( i ) );
enumTexts.push_back( enumText );
}
return enumTexts;
}
// Xml Serializing
private:
PdmField<caf::AppEnum<DataType>>* m_field;
};
template <typename FieldType>
void AddScriptingCapabilityToField( FieldType* field, const QString& fieldName )
{

View File

@ -187,8 +187,21 @@ QString caf::PdmPythonGenerator::generate( PdmObjectFactory* factory, std::vecto
QString fieldCode =
QString( " self.%1 = %2\n" ).arg( snake_field_name ).arg( valueString );
QString fullComment =
QString( "%1 (%2): %3\n" ).arg( snake_field_name ).arg( dataType ).arg( comment );
QString fullComment;
{
QString commentAndEnum = comment;
QStringList enumTexts = scriptability->enumScriptTexts();
if ( !enumTexts.empty() )
{
// Replace the comment text with enum values
// The space is limited for the generation of documentation
commentAndEnum = "One of [" + enumTexts.join( ", " ) + "]";
}
fullComment =
QString( "%1 (%2): %3\n" ).arg( snake_field_name ).arg( dataType ).arg( commentAndEnum );
}
classAttributesGenerated[field->ownerClass()][snake_field_name].first = fieldCode;
classAttributesGenerated[field->ownerClass()][snake_field_name].second = fullComment;