Python doc improvements (#5669)

* Add tests to xml data type inspection
* Add baseline for testing of PdmScripting
* #5631 Generate doc for Python
This commit is contained in:
Magne Sjaastad
2020-03-12 07:45:52 +01:00
committed by GitHub
parent 1c75d9b4bd
commit f216bf8d6a
13 changed files with 27653 additions and 12 deletions

View File

@@ -35,6 +35,7 @@
#include "HoloLensCommands/RicHoloLensSessionManager.h"
#include "RicImportGeneralDataFeature.h"
#include "RicfCommandFileExecutor.h"
#include "RicfCommandObject.h"
#include "Rim2dIntersectionViewCollection.h"
#include "RimAnnotationCollection.h"
@@ -95,6 +96,8 @@
#include "cafPdmCodeGenerator.h"
#include "cafPdmDataValueField.h"
#include "cafPdmDefaultObjectFactory.h"
#include "cafPdmMarkdownBuilder.h"
#include "cafPdmMarkdownGenerator.h"
#include "cafPdmScriptIOMessages.h"
#include "cafPdmSettings.h"
#include "cafPdmUiModelChangeDetector.h"
@@ -1704,14 +1707,6 @@ bool RiaApplication::generateCode( const QString& fileName, QString* errMsg )
{
CAF_ASSERT( errMsg );
QFile outputFile( fileName );
if ( !outputFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
*errMsg = QString( "Could not open file %1 for writing" ).arg( fileName );
return false;
}
QTextStream out( &outputFile );
std::string fileExt = QFileInfo( fileName ).suffix().toStdString();
std::unique_ptr<caf::PdmCodeGenerator> generator( caf::PdmCodeGeneratorFactory::instance()->create( fileExt ) );
@@ -1720,6 +1715,87 @@ bool RiaApplication::generateCode( const QString& fileName, QString* errMsg )
*errMsg = QString( "No code generator matches the provided file extension" );
return false;
}
out << generator->generate( caf::PdmDefaultObjectFactory::instance() );
auto markdownGenerator = dynamic_cast<caf::PdmMarkdownGenerator*>( generator.get() );
if ( markdownGenerator )
{
QFileInfo fi( fileName );
QDir dir( fi.absoluteDir() );
QString baseName = fi.baseName();
{
QString outputFileName = dir.absoluteFilePath( baseName + "_class.md" );
QFile outputFile( outputFileName );
if ( !outputFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
*errMsg = QString( "Could not open file %1 for writing" ).arg( outputFileName );
return false;
}
QTextStream out( &outputFile );
{
out << "+++ \n";
out << "title = \"Python Classes\" \n";
out << "published = true \n";
out << "weight = 42 \n";
out << "+++ \n";
}
out << generator->generate( caf::PdmDefaultObjectFactory::instance() );
}
{
QString outputFileName = dir.absoluteFilePath( baseName + "_commands.md" );
QFile outputFile( outputFileName );
if ( !outputFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
*errMsg = QString( "Could not open file %1 for writing" ).arg( outputFileName );
return false;
}
QTextStream out( &outputFile );
{
out << "+++ \n";
out << "title = \"Command Reference\" \n";
out << "published = true \n";
out << "weight = 43 \n";
out << "+++ \n";
}
std::vector<std::shared_ptr<const caf::PdmObject>> commandObjects;
QStringList excludedClassNames{"TestCommand1", "TC2"}; // See RifCommandCore-Text.cpp
auto allObjects = caf::PdmMarkdownBuilder::createAllObjects( caf::PdmDefaultObjectFactory::instance() );
for ( auto classObject : allObjects )
{
if ( dynamic_cast<const RicfCommandObject*>( classObject.get() ) )
{
if ( !excludedClassNames.contains( classObject->classKeyword(), Qt::CaseInsensitive ) )
{
commandObjects.push_back( classObject );
}
}
}
out << caf::PdmMarkdownBuilder::generateDocCommandObjects( commandObjects );
}
}
else
{
QFile outputFile( fileName );
if ( !outputFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
*errMsg = QString( "Could not open file %1 for writing" ).arg( fileName );
return false;
}
QTextStream out( &outputFile );
out << generator->generate( caf::PdmDefaultObjectFactory::instance() );
}
return true;
}

View File

@@ -432,6 +432,9 @@ if (RESINSIGHT_INCLUDE_APPFWK_TESTS)
add_subdirectory(Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafPdmXml_UnitTests)
set_property(TARGET cafPdmXml_UnitTests PROPERTY FOLDER "AppFwkTests")
add_subdirectory(Fwk/AppFwk/cafPdmScripting/cafPdmScripting_UnitTests)
set_property(TARGET cafPdmScripting_UnitTests PROPERTY FOLDER "AppFwkTests")
# Executables
add_subdirectory(Fwk/AppFwk/cafTests/cafTestApplication)
set_property(TARGET cafTestApplication PROPERTY FOLDER "AppFwkTests")

View File

@@ -20,10 +20,19 @@ set( PROJECT_FILES
cafPdmObjectScriptability.cpp
cafPdmScriptResponse.h
cafPdmScriptResponse.cpp
cafPdmMarkdownGenerator.h
cafPdmMarkdownGenerator.cpp
cafPdmMarkdownBuilder.h
cafPdmMarkdownBuilder.cpp
)
add_library( ${PROJECT_NAME}
# NOTE! Adding the library as a cmake "OBJECT" library
# to make sure the linker is not pruning the seemingly unused features,
# and to make sure that the static initialization based registration of the features into the factory is done properly
# see https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Object-Library
# and https://cmake.org/cmake/help/v3.15/command/add_library.html?highlight=add_library#object-libraries
add_library( ${PROJECT_NAME} OBJECT
${PROJECT_FILES}
)
@@ -32,7 +41,24 @@ target_include_directories(${PROJECT_NAME}
${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries( ${PROJECT_NAME} LibCore cafProjectDataModel ${QT_LIBRARIES} )
target_link_libraries ( ${PROJECT_NAME}
cafProjectDataModel
LibCore
)
# Before cmake 3.12 OBJECT libraries could not use the target_link_libraries command,
# So we need to set the POSITION_INDEPENDENT_CODE option manually
set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
# Not to be used until we can use cmake 3.12 or above
#target_link_libraries ( ${PROJECT_NAME}
# cafCommand
# cafUserInterface
# ${QT_LIBRARIES}
#)
source_group("" FILES ${PROJECT_FILES})

View File

@@ -0,0 +1,416 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmMarkdownBuilder.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmChildField.h"
#include "cafPdmFieldScriptability.h"
#include "cafPdmObject.h"
#include "cafPdmObjectFactory.h"
#include "cafPdmObjectScriptabilityRegister.h"
#include "cafPdmProxyValueField.h"
#include "cafPdmPythonGenerator.h"
#include "cafPdmXmlFieldHandle.h"
#include <QRegularExpression>
#include <QTextStream>
#include <algorithm>
#include <list>
#include <memory>
#include <set>
using namespace caf;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
;
QString caf::PdmMarkdownBuilder::generateDocDataModelObjects( std::vector<std::shared_ptr<const PdmObject>>& dataModelObjects )
{
QString generatedCode;
QTextStream out( &generatedCode );
// Sort to make sure super classes get created before sub classes
std::sort( dataModelObjects.begin(),
dataModelObjects.end(),
[]( std::shared_ptr<const PdmObject> lhs, std::shared_ptr<const PdmObject> rhs ) {
auto lhsStack = lhs->classInheritanceStack();
auto rhsStack = rhs->classInheritanceStack();
auto maxItems = std::min( lhsStack.size(), rhsStack.size() );
auto lhsIt = lhsStack.begin();
auto rhsIt = rhsStack.begin();
for ( size_t i = 0; i < maxItems; i++ )
{
if ( *lhsIt != *rhsIt )
{
return ( *lhsIt < *rhsIt );
}
lhsIt++;
rhsIt++;
}
return lhsStack.size() < rhsStack.size();
} );
std::map<QString, std::map<QString, std::pair<QString, QString>>> classAttributesGenerated;
std::map<QString, std::map<QString, QString>> classMethodsGenerated;
std::map<QString, QString> classCommentsGenerated;
// First generate all attributes and comments to go into each object
for ( std::shared_ptr<const PdmObject> object : dataModelObjects )
{
const std::list<QString>& classInheritanceStack = object->classInheritanceStack();
for ( auto it = classInheritanceStack.begin(); it != classInheritanceStack.end(); ++it )
{
const QString& classKeyword = *it;
QString scriptClassComment = PdmObjectScriptabilityRegister::scriptClassComment( classKeyword );
std::map<QString, QString> attributesGenerated;
if ( !scriptClassComment.isEmpty() ) classCommentsGenerated[classKeyword] = scriptClassComment;
if ( classKeyword == object->classKeyword() )
{
std::vector<PdmFieldHandle*> fields;
object->fields( fields );
for ( auto field : fields )
{
auto scriptability = field->template capability<PdmFieldScriptability>();
if ( scriptability != nullptr )
{
QString snake_field_name = PdmPythonGenerator::camelToSnakeCase( scriptability->scriptFieldName() );
QString comment;
{
QStringList commentComponents;
commentComponents << field->capability<PdmUiFieldHandle>()->uiName();
commentComponents << field->capability<PdmUiFieldHandle>()->uiWhatsThis();
commentComponents.removeAll( QString( "" ) );
comment = commentComponents.join( ". " );
}
auto pdmValueField = dynamic_cast<const PdmValueField*>( field );
auto pdmChildField = dynamic_cast<const PdmChildFieldHandle*>( field );
auto pdmChildArrayField = dynamic_cast<const PdmChildArrayFieldHandle*>( field );
if ( pdmValueField )
{
QString dataType = PdmPythonGenerator::dataTypeString( field, true );
if ( field->xmlCapability()->isVectorField() )
{
dataType = QString( "List of %1" ).arg( dataType );
}
bool shouldBeMethod = false;
auto proxyField = dynamic_cast<const PdmProxyFieldHandle*>( field );
if ( proxyField && proxyField->isStreamingField() ) shouldBeMethod = true;
if ( classAttributesGenerated[field->ownerClass()].count( snake_field_name ) ) continue;
if ( classMethodsGenerated[field->ownerClass()].count( snake_field_name ) ) continue;
QVariant valueVariant = pdmValueField->toQVariant();
if ( shouldBeMethod )
{
if ( proxyField->hasGetter() )
{
QString fullComment =
QString( " \"\"\"%1\n Returns:\n %2\n \"\"\"" )
.arg( comment )
.arg( dataType );
QString fieldCode = QString( " def %1(self):\n%2\n return "
"self._call_get_method(\"%3\")\n" )
.arg( snake_field_name )
.arg( fullComment )
.arg( scriptability->scriptFieldName() );
classMethodsGenerated[field->ownerClass()][snake_field_name] = fieldCode;
}
if ( proxyField->hasSetter() )
{
QString fullComment = QString( " \"\"\"Set %1\n Arguments:\n"
" values (%2): data\n \"\"\"" )
.arg( comment )
.arg( dataType );
QString fieldCode = QString( " def set_%1(self, values):\n%2\n "
"self._call_set_method(\"%3\", values)\n" )
.arg( snake_field_name )
.arg( fullComment )
.arg( scriptability->scriptFieldName() );
classMethodsGenerated[field->ownerClass()][QString( "set_%1" ).arg( snake_field_name )] =
fieldCode;
}
}
else
{
QString valueString;
QTextStream valueStream( &valueString );
scriptability->readFromField( valueStream, true, true );
if ( valueString.isEmpty() ) valueString = QString( "\"\"" );
valueString = PdmPythonGenerator::pythonifyDataValue( valueString );
QString fieldCode =
QString( " self.%1 = %2\n" ).arg( snake_field_name ).arg( valueString );
QString fullComment =
QString( "%1|%2|%3" ).arg( snake_field_name ).arg( dataType ).arg( comment );
classAttributesGenerated[field->ownerClass()][snake_field_name].first = fieldCode;
classAttributesGenerated[field->ownerClass()][snake_field_name].second = fullComment;
}
}
else if ( pdmChildField || pdmChildArrayField )
{
QString dataType = PdmPythonGenerator::dataTypeString( field, false );
QString scriptDataType =
PdmObjectScriptabilityRegister::scriptClassNameFromClassKeyword( dataType );
QString commentDataType = field->xmlCapability()->isVectorField()
? QString( "List of %1" ).arg( scriptDataType )
: scriptDataType;
QString fullComment =
QString( " \"\"\"%1\n Returns:\n %2\n \"\"\"" )
.arg( comment )
.arg( commentDataType );
if ( pdmChildField )
{
QString fieldCode =
QString( " def %1(self):\n%2\n return "
"self.children(\"%3\", %4)[0] if len(self.children) > 0 else None\n" )
.arg( snake_field_name )
.arg( fullComment )
.arg( scriptability->scriptFieldName() )
.arg( scriptDataType );
classMethodsGenerated[field->ownerClass()][snake_field_name] = fieldCode;
}
else
{
QString fieldCode = QString( " def %1(self):\n%2\n return "
"self.children(\"%3\", %4)\n" )
.arg( snake_field_name )
.arg( fullComment )
.arg( scriptability->scriptFieldName() )
.arg( scriptDataType );
classMethodsGenerated[field->ownerClass()][snake_field_name] = fieldCode;
}
}
}
}
}
}
}
// Write out classes
std::set<QString> classesWritten;
for ( std::shared_ptr<const caf::PdmObject> object : dataModelObjects )
{
const std::list<QString>& classInheritanceStack = object->classInheritanceStack();
std::list<QString> scriptSuperClassNames;
size_t inheritanceLevel = 0;
for ( auto it = classInheritanceStack.begin(); it != classInheritanceStack.end(); ++it )
{
const QString& classKeyword = *it;
QString scriptClassName = PdmObjectScriptabilityRegister::scriptClassNameFromClassKeyword( classKeyword );
if ( scriptClassName.isEmpty() ) scriptClassName = classKeyword;
if ( !classesWritten.count( scriptClassName ) )
{
QString classCode;
{
size_t headingLevel = inheritanceLevel + 1;
size_t maxHeadingLevel = 4;
size_t minHeadingLevel = 2;
headingLevel = std::max( headingLevel, minHeadingLevel );
headingLevel = std::min( headingLevel, maxHeadingLevel );
for ( size_t level = 0; level < headingLevel; level++ )
{
classCode += "#";
}
}
classCode += QString( " %1\n" ).arg( scriptClassName );
if ( !scriptSuperClassNames.empty() )
{
classCode += QString( "**Inherits %1**\n\n" ).arg( scriptSuperClassNames.back() );
}
if ( !classCommentsGenerated[classKeyword].isEmpty() || !classAttributesGenerated[classKeyword].empty() )
{
if ( !classCommentsGenerated[classKeyword].isEmpty() )
{
classCode += QString( "%1\n\n" ).arg( classCommentsGenerated[classKeyword] );
}
if ( !classAttributesGenerated[classKeyword].empty() )
{
classCode += "Attribute | Type | Description\n";
classCode += "--------- | ---- | -----------\n";
for ( auto keyWordValuePair : classAttributesGenerated[classKeyword] )
{
QStringList items = keyWordValuePair.second.second.split( "|" );
if ( !items.empty() )
{
classCode += items[0];
for ( int i = 1; i < items.size(); i++ )
{
classCode += " | ";
classCode += items[i];
}
classCode += "\n";
}
}
}
}
out << classCode << "\n";
classesWritten.insert( scriptClassName );
}
scriptSuperClassNames.push_back( scriptClassName );
inheritanceLevel++;
}
}
return generatedCode;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::shared_ptr<const PdmObject>> caf::PdmMarkdownBuilder::createAllObjects( caf::PdmObjectFactory* factory )
{
std::vector<std::shared_ptr<const PdmObject>> objects;
std::vector<QString> classKeywords = factory->classKeywords();
for ( QString classKeyword : classKeywords )
{
auto objectHandle = factory->create( classKeyword );
PdmObject* object = dynamic_cast<PdmObject*>( objectHandle );
CAF_ASSERT( object );
std::shared_ptr<PdmObject> sharedObject( object );
objects.push_back( sharedObject );
}
return objects;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString caf::PdmMarkdownBuilder::generateDocCommandObjects( std::vector<std::shared_ptr<const PdmObject>>& commandObjects )
{
QString generatedText;
QTextStream out( &generatedText );
struct CommandDocData
{
QString snakeClassName;
QString description;
std::vector<AttributeItem> parameters;
};
std::vector<CommandDocData> objs;
// First generate all attributes and comments to go into each object
for ( std::shared_ptr<const PdmObject> object : commandObjects )
{
QString snakeCommandName = PdmPythonGenerator::camelToSnakeCase( object->classKeyword() );
std::vector<AttributeItem> attributes;
std::vector<PdmFieldHandle*> fields;
object->fields( fields );
for ( auto field : fields )
{
QString snake_field_name = PdmPythonGenerator::camelToSnakeCase( field->keyword() );
QString pythonDataType = PdmPythonGenerator::dataTypeString( field, true );
QString nativeDataType = field->xmlCapability()->dataTypeName();
if ( nativeDataType.contains( "std::vector" ) )
{
pythonDataType = QString( "List of %1" ).arg( pythonDataType );
}
QString comment;
{
QStringList commentComponents;
commentComponents << field->capability<PdmUiFieldHandle>()->uiName();
commentComponents << field->capability<PdmUiFieldHandle>()->uiWhatsThis();
commentComponents.removeAll( QString( "" ) );
comment = commentComponents.join( ". " );
}
attributes.push_back( {snake_field_name, comment, pythonDataType} );
}
QString comment = caf::PdmObjectScriptabilityRegister::scriptClassComment( object->classKeyword() );
objs.push_back( {snakeCommandName, comment, attributes} );
// objectsAndAttributes[snakeCommandName] = attributes;
}
for ( auto object : objs )
{
out << QString( "## %1\n\n" ).arg( object.snakeClassName );
if ( !object.description.isEmpty() )
{
out << object.description << "\n\n";
}
out << "Parameter | Type | Description\n";
out << "--------- | ---- | -----------\n";
for ( auto keyWordValuePair : object.parameters )
{
out << keyWordValuePair.name << " | ";
out << keyWordValuePair.type << " | ";
out << keyWordValuePair.description << "\n";
}
out << "\n";
}
return generatedText;
}

View File

@@ -0,0 +1,63 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmObject.h"
namespace caf
{
//==================================================================================================
///
//==================================================================================================
class PdmMarkdownBuilder
{
public:
static QString generateDocCommandObjects( std::vector<std::shared_ptr<const PdmObject>>& commandObjects );
static QString generateDocDataModelObjects( std::vector<std::shared_ptr<const PdmObject>>& dataModelObjects );
static std::vector<std::shared_ptr<const PdmObject>> createAllObjects( caf::PdmObjectFactory* factory );
private:
struct AttributeItem
{
QString name;
QString description;
QString type;
};
};
} // namespace caf

View File

@@ -0,0 +1,71 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmMarkdownGenerator.h"
#include "cafPdmMarkdownBuilder.h"
#include "cafPdmObjectScriptabilityRegister.h"
using namespace caf;
CAF_PDM_CODE_GENERATOR_SOURCE_INIT( PdmMarkdownGenerator, "md" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString caf::PdmMarkdownGenerator::generate( PdmObjectFactory* factory ) const
{
QString generatedCode;
QTextStream out( &generatedCode );
std::vector<QString> classKeywords = factory->classKeywords();
std::vector<std::shared_ptr<const PdmObject>> scriptableObjects;
{
std::vector<std::shared_ptr<const PdmObject>> allObjects = caf::PdmMarkdownBuilder::createAllObjects( factory );
for ( auto obj : allObjects )
{
if ( PdmObjectScriptabilityRegister::isScriptable( obj.get() ) )
{
scriptableObjects.push_back( obj );
}
}
}
out << caf::PdmMarkdownBuilder::generateDocDataModelObjects( scriptableObjects );
return generatedCode;
}

View File

@@ -0,0 +1,64 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmCodeGenerator.h"
#include "cafPdmObject.h"
namespace caf
{
class PdmFieldHandle;
//==================================================================================================
///
//==================================================================================================
class PdmMarkdownGenerator : public PdmCodeGenerator
{
CAF_PDM_CODE_GENERATOR_HEADER_INIT;
struct AttributeItem
{
QString name;
QString description;
QString type;
};
public:
QString generate( PdmObjectFactory* factory ) const override;
};
} // namespace caf

View File

@@ -0,0 +1,59 @@
cmake_minimum_required (VERSION 2.8.12)
project ( cafPdmScripting_UnitTests )
if (NOT DEFINED(CEE_USE_QT5))
option(CEE_USE_QT5 "Use Qt5" OFF)
endif(NOT DEFINED(CEE_USE_QT5))
if (CEE_USE_QT5)
find_package(Qt5 COMPONENTS REQUIRED Core Xml Gui)
set(QT_LIBRARIES Qt5::Core Qt5::Xml Qt5::Gui)
else()
find_package(Qt4 COMPONENTS REQUIRED QtCore QtXml QtGui)
include(${QT_USE_FILE})
endif(CEE_USE_QT5)
if (MSVC AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 19.11))
# VS 2017 : Disable warnings from from gtest code, using deprecated code related to TR1
add_definitions(-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
message(STATUS "Add flag to disable warings from gtest - _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING")
endif()
include_directories (
${CMAKE_CURRENT_SOURCE_DIR} # required for gtest-all.cpp
)
# add the executable
add_executable (${PROJECT_NAME}
cafPdmScripting_UnitTests.cpp
gtest/gtest-all.cpp
cafPdmScriptingBasicTest.cpp
)
target_link_libraries ( ${PROJECT_NAME}
cafPdmScripting
${QT_LIBRARIES}
${THREAD_LIBRARY}
)
source_group("" FILES ${PROJECT_FILES})
# Copy Qt Dlls
if (Qt5Core_FOUND)
foreach (qtlib ${QT_LIBRARIES})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:${qtlib}> $<TARGET_FILE_DIR:${PROJECT_NAME}>
)
endforeach(qtlib)
else()
if (MSVC)
set (QTLIBLIST QtCore QtXml)
foreach (qtlib ${QTLIBLIST})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_BINARY_DIR}/$<IF:$<CONFIG:Debug>,${qtlib}d4.dll,${qtlib}4.dll> $<TARGET_FILE_DIR:${PROJECT_NAME}>
)
endforeach( qtlib )
endif(MSVC)
endif(Qt5Core_FOUND)

View File

@@ -0,0 +1,251 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "gtest/gtest.h"
#include "cafAppEnum.h"
#include "cafPdmChildArrayField.h"
#include "cafPdmChildField.h"
#include "cafPdmCodeGenerator.h"
#include "cafPdmDocument.h"
#include "cafPdmField.h"
#include "cafPdmFieldIOScriptability.h"
#include "cafPdmObject.h"
#include "cafPdmObjectGroup.h"
#include "cafPdmPointer.h"
#include "cafPdmProxyValueField.h"
#include "cafPdmReferenceHelper.h"
#include <QFile>
#include "cafPdmObjectScriptability.h"
#include <memory>
/// Demo objects to show the usage of the Pdm system
class SimpleObj : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
SimpleObj()
: PdmObject()
, m_doubleMember( 0.0 )
{
CAF_PDM_InitObject( "SimpleObj", "", "Tooltip SimpleObj", "WhatsThis SimpleObj" );
CAF_PDM_InitField( &m_position, "Position", 8765.2, "Position", "", "Tooltip", "WhatsThis" );
CAF_PDM_InitField( &m_dir, "Dir", 123.56, "Direction", "", "Tooltip", "WhatsThis" );
CAF_PDM_InitField( &m_up, "Up", 0.0, "Up value", "", "Tooltip", "WhatsThis" );
CAF_PDM_InitFieldNoDefault( &m_numbers, "Numbers", "Important Numbers", "", "Tooltip", "WhatsThis" );
#if 1
m_proxyDouble.registerSetMethod( this, &SimpleObj::setDoubleMember );
m_proxyDouble.registerGetMethod( this, &SimpleObj::doubleMember );
AddUiCapabilityToField( &m_proxyDouble );
AddXmlCapabilityToField( &m_proxyDouble );
CAF_PDM_InitFieldNoDefault( &m_proxyDouble, "ProxyDouble", "ProxyDouble", "", "", "" );
#endif
}
/// Assignment and copying of PDM objects is not focus for the features. This is only a
/// "would it work" test
SimpleObj( const SimpleObj& other )
: PdmObject()
{
CAF_PDM_InitField( &m_position, "Position", 8765.2, "Position", "", "", "WhatsThis" );
CAF_PDM_InitField( &m_dir, "Dir", 123.56, "Direction", "", "", "WhatsThis" );
CAF_PDM_InitField( &m_up, "Up", 0.0, "Up value", "", "", "WhatsThis" );
CAF_PDM_InitFieldNoDefault( &m_numbers, "Numbers", "Important Numbers", "", "", "WhatsThis" );
m_position = other.m_position;
m_dir = other.m_dir;
m_up = other.m_up;
m_numbers = other.m_numbers;
m_doubleMember = other.m_doubleMember;
}
~SimpleObj() {}
caf::PdmField<double> m_position;
caf::PdmField<double> m_dir;
caf::PdmField<double> m_up;
caf::PdmField<std::vector<double>> m_numbers;
caf::PdmProxyValueField<double> m_proxyDouble;
void setDoubleMember( const double& d )
{
m_doubleMember = d;
std::cout << "setDoubleMember" << std::endl;
}
double doubleMember() const
{
std::cout << "doubleMember" << std::endl;
return m_doubleMember;
}
double m_doubleMember;
};
CAF_PDM_SOURCE_INIT( SimpleObj, "SimpleObj" );
class DemoPdmObject : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
DemoPdmObject()
{
CAF_PDM_InitObject( "DemoPdmObject", "", "Tooltip DemoPdmObject", "WhatsThis DemoPdmObject" );
CAF_PDM_InitField( &m_doubleField,
"BigNumber",
0.0,
"",
"",
"Enter a big number here",
"This is a place you can enter a big real value if you want" );
CAF_PDM_InitField( &m_intField,
"IntNumber",
0,
"",
"",
"Enter some small number here",
"This is a place you can enter a small integer value if you want" );
CAF_PDM_InitField( &m_textField, "TextField", QString( "<EFBFBD><EFBFBD><EFBFBD> Test text end" ), "TextField", "", "Tooltip", "WhatsThis" );
CAF_PDM_InitFieldNoDefault( &m_simpleObjPtrField, "SimpleObjPtrField", "SimpleObjPtrField", "", "Tooltip", "WhatsThis" );
CAF_PDM_InitFieldNoDefault( &m_simpleObjPtrField2, "SimpleObjPtrField2", "SimpleObjPtrField2", "", "Tooltip", "WhatsThis" );
m_simpleObjPtrField2 = new SimpleObj;
}
~DemoPdmObject()
{
delete m_simpleObjPtrField();
delete m_simpleObjPtrField2();
}
// Fields
caf::PdmField<double> m_doubleField;
caf::PdmField<int> m_intField;
caf::PdmField<QString> m_textField;
caf::PdmChildField<SimpleObj*> m_simpleObjPtrField;
caf::PdmChildField<SimpleObj*> m_simpleObjPtrField2;
};
CAF_PDM_SOURCE_INIT( DemoPdmObject, "DemoPdmObject" );
class InheritedDemoObj : public DemoPdmObject
{
CAF_PDM_HEADER_INIT;
public:
enum TestEnumType
{
T1,
T2,
T3
};
InheritedDemoObj()
{
CAF_PDM_InitScriptableObjectWithNameAndComment( "InheritedDemoObj",
"",
"ToolTip InheritedDemoObj",
"Whatsthis InheritedDemoObj",
"ScriptClassName_InheritedDemoObj",
"Script comment test" );
CAF_PDM_InitScriptableFieldWithIONoDefault( &m_texts, "Texts", "Some words", "", "", "" );
CAF_PDM_InitFieldNoDefault( &m_testEnumField, "TestEnumValue", "An Enum", "", "", "" );
CAF_PDM_InitFieldNoDefault( &m_simpleObjectsField,
"SimpleObjects",
"SimpleObjectsField",
"",
"ToolTip SimpleObjectsField",
"Whatsthis SimpleObjectsField" );
}
~InheritedDemoObj() { m_simpleObjectsField.deleteAllChildObjects(); }
caf::PdmField<std::vector<QString>> m_texts;
caf::PdmField<caf::AppEnum<TestEnumType>> m_testEnumField;
caf::PdmChildArrayField<SimpleObj*> m_simpleObjectsField;
};
CAF_PDM_SOURCE_INIT( InheritedDemoObj, "InheritedDemoObj" );
class MyPdmDocument : public caf::PdmDocument
{
CAF_PDM_HEADER_INIT;
public:
MyPdmDocument()
{
CAF_PDM_InitObject( "PdmObjectCollection", "", "", "" );
CAF_PDM_InitFieldNoDefault( &objects, "PdmObjects", "", "", "", "" )
}
~MyPdmDocument() { objects.deleteAllChildObjects(); }
caf::PdmChildArrayField<PdmObjectHandle*> objects;
};
CAF_PDM_SOURCE_INIT( MyPdmDocument, "MyPdmDocument" );
namespace caf
{
template <>
void AppEnum<InheritedDemoObj::TestEnumType>::setUp()
{
addItem( InheritedDemoObj::T1, "T1", "An A letter" );
addItem( InheritedDemoObj::T2, "T2", "A B letter" );
addItem( InheritedDemoObj::T3, "T3", "A B letter" );
setDefault( InheritedDemoObj::T1 );
}
} // namespace caf
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
TEST( PdmScriptingTest, BasicUse )
{
std::unique_ptr<MyPdmDocument> document( new MyPdmDocument );
std::string fileExt = "py";
std::unique_ptr<caf::PdmCodeGenerator> generator( caf::PdmCodeGeneratorFactory::instance()->create( fileExt ) );
auto generatedText = generator->generate( caf::PdmDefaultObjectFactory::instance() );
}

View File

@@ -0,0 +1,54 @@
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) Ceetron Solutions AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library 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.
//
// This library 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "gtest/gtest.h"
#include <iostream>
#include <stdio.h>
#include <string>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
testing::InitGoogleTest( &argc, argv );
int result = RUN_ALL_TESTS();
char text[5];
std::cin.getline( text, 5 );
return result;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -201,11 +201,15 @@ public:
CAF_PDM_XML_InitField(&m_singleFilePath, "m_singleFilePath");
CAF_PDM_XML_InitField(&m_multipleFilePath, "m_multipleFilePath");
CAF_PDM_XML_InitField(&m_proxyDouble, "m_proxyDouble");
m_proxyDouble.registerSetMethod(this, &SimpleObj::setDoubleMember);
m_proxyDouble.registerGetMethod(this, &SimpleObj::doubleMember);
}
caf::PdmDataValueField<double> m_position;
caf::PdmDataValueField<double> m_dir;
caf::PdmDataValueField<double> m_up;
caf::PdmDataValueField<int> m_up;
caf::PdmProxyValueField<double> m_proxyDouble;
caf::PdmDataValueField<caf::FilePath> m_singleFilePath;
@@ -390,3 +394,40 @@ TEST(BaseTest, FilePathSerializing)
delete s1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(BaseTest, TestDataType)
{
SimpleObj* s1 = new SimpleObj;
{
auto dataTypeNameDouble = s1->m_position.xmlCapability()->dataTypeName();
EXPECT_EQ("double", dataTypeNameDouble.toStdString());
}
{
auto dataTypeNameDouble = s1->m_proxyDouble.xmlCapability()->dataTypeName();
EXPECT_EQ("double", dataTypeNameDouble.toStdString());
}
{
auto dataTypeNameDouble = s1->m_up.xmlCapability()->dataTypeName();
EXPECT_EQ("int", dataTypeNameDouble.toStdString());
}
{
auto dataTypeNameDouble = s1->m_singleFilePath.xmlCapability()->dataTypeName();
EXPECT_EQ("class caf::FilePath", dataTypeNameDouble.toStdString());
}
{
InheritedDemoObj* obj = new InheritedDemoObj;
auto dataTypeNameDouble = obj->m_texts.xmlCapability()->dataTypeName();
EXPECT_EQ("class QString", dataTypeNameDouble.toStdString());
}
delete s1;
}