Move some more scriptability to caf

This commit is contained in:
Gaute Lindkvist committed 2020-03-05 08:40:46 +01:00
1 parent 17ea69397a
commit 2424d79c5d
14 files changed
+279 -53

No files matched your search

@@ -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);
}
}
@@ -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;
};
}
@@ -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;
}
@@ -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);
};
}
@@ -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++;
}
@@ -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;
};
}