Skip to content

Commit

Permalink
Merge pull request #40947 from 3nids/rel-reg-f2
Browse files Browse the repository at this point in the history
move reading from XML logic from QgsEditFormConfig to QgsAttributeEditorElement
  • Loading branch information
3nids committed Jan 12, 2021
2 parents ea5ce01 + 9e5b614 commit ca37a56
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 136 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsattributeeditorelement.sip.in
Expand Up @@ -64,6 +64,13 @@ Constructor

virtual ~QgsAttributeEditorElement();

static QgsAttributeEditorElement *create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent = 0 ) /Factory/;
%Docstring
Constructs the editor element from the given element

.. versionadded:: 3.18
%End

QString name() const;
%Docstring
Returns the name of this element
Expand Down
5 changes: 4 additions & 1 deletion python/core/auto_generated/qgseditformconfig.sip.in
Expand Up @@ -276,9 +276,12 @@ Write XML information
Serialize on project save
%End

QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = QString(), const QgsReadWriteContext &context = QgsReadWriteContext() );
QgsAttributeEditorElement *attributeEditorElementFromDomElement( QDomElement &elem, QgsAttributeEditorElement *parent, const QString &layerId = QString(), const QgsReadWriteContext &context = QgsReadWriteContext() ) /Deprecated/;
%Docstring
Deserialize drag and drop designer elements.

.. deprecated:: QGIS 3.18
use QgsAttributeEditorElement.create instead
%End

explicit QgsEditFormConfig();
Expand Down
156 changes: 156 additions & 0 deletions src/core/qgsattributeeditorelement.cpp
Expand Up @@ -124,6 +124,42 @@ void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem, QDomDocu
}
}

void QgsAttributeEditorContainer::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
mBackgroundColor = element.attribute( QStringLiteral( "backgroundColor" ), QString() );
bool ok;
int cc = element.attribute( QStringLiteral( "columnCount" ) ).toInt( &ok );
if ( !ok )
cc = 0;
setColumnCount( cc );

bool isGroupBox = element.attribute( QStringLiteral( "groupBox" ) ).toInt( &ok );
if ( ok )
setIsGroupBox( isGroupBox );
else
setIsGroupBox( mParent );

bool visibilityExpressionEnabled = element.attribute( QStringLiteral( "visibilityExpressionEnabled" ) ).toInt( &ok );
QgsOptionalExpression visibilityExpression;
if ( ok )
{
visibilityExpression.setEnabled( visibilityExpressionEnabled );
visibilityExpression.setData( QgsExpression( element.attribute( QStringLiteral( "visibilityExpression" ) ) ) );
}
setVisibilityExpression( visibilityExpression );

QDomNodeList childNodeList = element.childNodes();

for ( int i = 0; i < childNodeList.size(); i++ )
{
QDomElement childElem = childNodeList.at( i ).toElement();

QgsAttributeEditorElement *myElem = create( childElem, layerId, fields, context, this );
if ( myElem )
addChildElement( myElem );
}
}

QString QgsAttributeEditorContainer::typeIdentifier() const
{
return QStringLiteral( "attributeEditorContainer" );
Expand Down Expand Up @@ -161,11 +197,63 @@ void QgsAttributeEditorField::saveConfiguration( QDomElement &elem, QDomDocument
elem.setAttribute( QStringLiteral( "index" ), mIdx );
}

void QgsAttributeEditorField::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( element )
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )
}

QString QgsAttributeEditorField::typeIdentifier() const
{
return QStringLiteral( "attributeEditorField" );
}

QgsAttributeEditorElement *QgsAttributeEditorElement::create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent )
{
QgsAttributeEditorElement *newElement = nullptr;

QString name = element.attribute( QStringLiteral( "name" ) );

if ( element.tagName() == QLatin1String( "attributeEditorContainer" ) )
{
newElement = new QgsAttributeEditorContainer( context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:formcontainers" ).arg( layerId ),
name ), parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorField" ) )
{
int idx = fields.lookupField( name );
newElement = new QgsAttributeEditorField( name, idx, parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorRelation" ) )
{
// At this time, the relations are not loaded
// So we only grab the id and delegate the rest to onRelationsLoaded()
newElement = new QgsAttributeEditorRelation( element.attribute( QStringLiteral( "relation" ), QStringLiteral( "[None]" ) ), parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorQmlElement" ) )
{
newElement = new QgsAttributeEditorQmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
}
else if ( element.tagName() == QLatin1String( "attributeEditorHtmlElement" ) )
{
newElement = new QgsAttributeEditorHtmlElement( element.attribute( QStringLiteral( "name" ) ), parent );
}

if ( newElement )
{
if ( element.hasAttribute( QStringLiteral( "showLabel" ) ) )
newElement->setShowLabel( element.attribute( QStringLiteral( "showLabel" ) ).toInt() );
else
newElement->setShowLabel( true );

newElement->loadConfiguration( element, layerId, context, fields );
}

return newElement;
}

QDomElement QgsAttributeEditorElement::toDomElement( QDomDocument &doc ) const
{
QDomElement elem = doc.createElement( typeIdentifier() );
Expand Down Expand Up @@ -198,6 +286,58 @@ void QgsAttributeEditorRelation::saveConfiguration( QDomElement &elem, QDomDocum
elem.appendChild( elemConfig );
}

void QgsAttributeEditorRelation::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )

QVariantMap config = QgsXmlUtils::readVariant( element.firstChildElement( "editor_configuration" ) ).toMap();

// load defaults
if ( config.isEmpty() )
config = relationEditorConfiguration();

// pre QGIS 3.18 compatibility
if ( ! config.contains( QStringLiteral( "buttons" ) ) )
{
if ( element.hasAttribute( "buttons" ) )
{
QString buttonString = element.attribute( QStringLiteral( "buttons" ), qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
config.insert( "buttons", qgsFlagValueToKeys( qgsFlagKeysToValue( buttonString, QgsAttributeEditorRelation::Button::AllButtons ) ) );
}
else
{
// pre QGIS 3.16 compatibility
QgsAttributeEditorRelation::Buttons buttons = QgsAttributeEditorRelation::Button::AllButtons;
buttons.setFlag( QgsAttributeEditorRelation::Button::Link, element.attribute( QStringLiteral( "showLinkButton" ), QStringLiteral( "1" ) ).toInt() );
buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, element.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, element.attribute( QStringLiteral( "showSaveChildEditsButton" ), QStringLiteral( "1" ) ).toInt() );
config.insert( "buttons", qgsFlagValueToKeys( buttons ) );
}
}

setRelationEditorConfiguration( config );

setForceSuppressFormPopup( element.attribute( QStringLiteral( "forceSuppressFormPopup" ), 0 ).toInt() );

if ( element.hasAttribute( QStringLiteral( "nmRelationId" ) ) )
{
setNmRelationId( element.attribute( QStringLiteral( "nmRelationId" ) ) );
}

if ( element.hasAttribute( "label" ) )
{
QString label = element.attribute( QStringLiteral( "label" ) );
setLabel( label );
}
if ( element.hasAttribute( "relationWidgetTypeId" ) )
{
QString relationWidgetTypeId = element.attribute( QStringLiteral( "relationWidgetTypeId" ) );
setRelationWidgetTypeId( relationWidgetTypeId );
}
}

QString QgsAttributeEditorRelation::typeIdentifier() const
{
return QStringLiteral( "attributeEditorRelation" );
Expand Down Expand Up @@ -277,6 +417,14 @@ void QgsAttributeEditorQmlElement::saveConfiguration( QDomElement &elem, QDomDoc
elem.appendChild( codeElem );
}

void QgsAttributeEditorQmlElement::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )
setQmlCode( element.text() );
}

QString QgsAttributeEditorQmlElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorQmlElement" );
Expand Down Expand Up @@ -306,6 +454,14 @@ void QgsAttributeEditorHtmlElement::saveConfiguration( QDomElement &elem, QDomDo
elem.appendChild( codeElem );
}

void QgsAttributeEditorHtmlElement::loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields )
{
Q_UNUSED( layerId )
Q_UNUSED( context )
Q_UNUSED( fields )
setHtmlCode( element.text() );
}

QString QgsAttributeEditorHtmlElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorHtmlElement" );
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsattributeeditorelement.h
Expand Up @@ -85,6 +85,13 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT

virtual ~QgsAttributeEditorElement() = default;

/**
* Constructs the editor element from the given element
*
* \since QGIS 3.18
*/
static QgsAttributeEditorElement *create( const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent = nullptr ) SIP_FACTORY;

/**
* Returns the name of this element
*
Expand Down Expand Up @@ -152,6 +159,12 @@ class CORE_EXPORT QgsAttributeEditorElement SIP_ABSTRACT
*/
virtual void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const = 0;

/**
* Should be implemented by subclasses to read specific configuration
* \since QGIS 3.18
*/
virtual void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) = 0;

/**
* All subclasses need to overwrite this method and return a type specific identifier.
* Needs to be XML key compatible.
Expand Down Expand Up @@ -285,6 +298,7 @@ class CORE_EXPORT QgsAttributeEditorContainer : public QgsAttributeEditorElement

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;

bool mIsGroupBox;
Expand Down Expand Up @@ -323,6 +337,7 @@ class CORE_EXPORT QgsAttributeEditorField : public QgsAttributeEditorElement

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
int mIdx;
};
Expand Down Expand Up @@ -481,6 +496,7 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
QString mRelationId;
QgsRelation mRelation;
Expand Down Expand Up @@ -531,6 +547,7 @@ class CORE_EXPORT QgsAttributeEditorQmlElement : public QgsAttributeEditorElemen

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
QString mQmlCode;
};
Expand Down Expand Up @@ -572,6 +589,7 @@ class CORE_EXPORT QgsAttributeEditorHtmlElement : public QgsAttributeEditorEleme

private:
void saveConfiguration( QDomElement &elem, QDomDocument &doc ) const override;
void loadConfiguration( const QDomElement &element, const QString &layerId, const QgsReadWriteContext &context, const QgsFields &fields ) override;
QString typeIdentifier() const override;
QString mHtmlCode;
};
Expand Down

0 comments on commit ca37a56

Please sign in to comment.