Skip to content
Permalink
Browse files

Correctly restore materials using registry

  • Loading branch information
nyalldawson committed Jul 28, 2020
1 parent d579d67 commit e541fe68d7f74ea720783e3e58254b1608c7aecb
@@ -78,6 +78,11 @@ Abstract base class for material settings.

virtual ~QgsAbstractMaterialSettings();

virtual QString type() const = 0;
%Docstring
Returns the unique type name for the material.
%End

virtual QgsAbstractMaterialSettings *clone() const = 0 /Factory/;
%Docstring
Clones the material settings.
@@ -34,6 +34,9 @@ with three color components: ambient, diffuse and specular.
Constructor for QgsPhongMaterialSettings.
%End

virtual QString type() const;


static QgsAbstractMaterialSettings *create() /Factory/;
%Docstring
Returns a new instance of QgsPhongMaterialSettings.
@@ -90,6 +90,11 @@ class _3D_EXPORT QgsAbstractMaterialSettings SIP_ABSTRACT

virtual ~QgsAbstractMaterialSettings() = default;

/**
* Returns the unique type name for the material.
*/
virtual QString type() const = 0;

/**
* Clones the material settings.
*
@@ -27,6 +27,11 @@
#include <Qt3DRender/QEffect>


QString QgsPhongMaterialSettings::type() const
{
return QStringLiteral( "phong" );
}

QgsAbstractMaterialSettings *QgsPhongMaterialSettings::create()
{
return new QgsPhongMaterialSettings();
@@ -42,6 +42,8 @@ class _3D_EXPORT QgsPhongMaterialSettings : public QgsAbstractMaterialSettings
*/
QgsPhongMaterialSettings() = default;

QString type() const override;

/**
* Returns a new instance of QgsPhongMaterialSettings.
*/
@@ -14,8 +14,9 @@
***************************************************************************/

#include "qgsline3dsymbol.h"
#include "qgsphongmaterialsettings.h"
#include "qgs3dutils.h"
#include "qgs3d.h"
#include "qgsmaterialregistry.h"

QgsLine3DSymbol::QgsLine3DSymbol()
: mMaterial( qgis::make_unique< QgsPhongMaterialSettings >() )
@@ -54,6 +55,7 @@ void QgsLine3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &co
elemDataProperties.setAttribute( QStringLiteral( "width" ), mWidth );
elem.appendChild( elemDataProperties );

elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
mMaterial->writeXml( elemMaterial, context );
elem.appendChild( elemMaterial );
@@ -71,7 +73,11 @@ void QgsLine3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteContex
mWidth = elemDataProperties.attribute( QStringLiteral( "width" ) ).toFloat();
mRenderAsSimpleLines = elemDataProperties.attribute( QStringLiteral( "simple-lines" ), QStringLiteral( "0" ) ).toInt();

QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
if ( !mMaterial )
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
mMaterial->readXml( elemMaterial, context );
}

@@ -19,7 +19,8 @@
#include "qgsreadwritecontext.h"
#include "qgsxmlutils.h"
#include "qgssymbollayerutils.h"

#include "qgs3d.h"
#include "qgsmaterialregistry.h"

QgsAbstract3DSymbol *QgsPoint3DSymbol::clone() const
{
@@ -56,6 +57,7 @@ void QgsPoint3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext &c
elemDataProperties.setAttribute( QStringLiteral( "alt-clamping" ), Qgs3DUtils::altClampingToString( mAltClamping ) );
elem.appendChild( elemDataProperties );

elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
mMaterial->writeXml( elemMaterial, context );
elem.appendChild( elemMaterial );
@@ -87,6 +89,10 @@ void QgsPoint3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteConte
mAltClamping = Qgs3DUtils::altClampingFromString( elemDataProperties.attribute( QStringLiteral( "alt-clamping" ) ) );

QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
if ( !mMaterial )
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
mMaterial->readXml( elemMaterial, context );

mShape = shapeFromString( elem.attribute( QStringLiteral( "shape" ) ) );
@@ -17,6 +17,8 @@

#include "qgs3dutils.h"
#include "qgssymbollayerutils.h"
#include "qgs3d.h"
#include "qgsmaterialregistry.h"

QgsPolygon3DSymbol::QgsPolygon3DSymbol()
: mMaterial( qgis::make_unique< QgsPhongMaterialSettings >() )
@@ -62,6 +64,7 @@ void QgsPolygon3DSymbol::writeXml( QDomElement &elem, const QgsReadWriteContext
elemDataProperties.setAttribute( QStringLiteral( "rendered-facade" ), mRenderedFacade );
elem.appendChild( elemDataProperties );

elem.setAttribute( QStringLiteral( "material_type" ), mMaterial->type() );
QDomElement elemMaterial = doc.createElement( QStringLiteral( "material" ) );
mMaterial->writeXml( elemMaterial, context );
elem.appendChild( elemMaterial );
@@ -91,7 +94,11 @@ void QgsPolygon3DSymbol::readXml( const QDomElement &elem, const QgsReadWriteCon
mAddBackFaces = elemDataProperties.attribute( QStringLiteral( "add-back-faces" ) ).toInt();
mRenderedFacade = elemDataProperties.attribute( QStringLiteral( "rendered-facade" ), "3" ).toInt();

QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
const QDomElement elemMaterial = elem.firstChildElement( QStringLiteral( "material" ) );
const QString materialType = elem.attribute( QStringLiteral( "material_type" ), QStringLiteral( "phong" ) );
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( materialType ) );
if ( !mMaterial )
mMaterial.reset( Qgs3D::materialRegistry()->createMaterialSettings( QStringLiteral( "phong" ) ) );
mMaterial->readXml( elemMaterial, context );

QDomElement elemDDP = elem.firstChildElement( QStringLiteral( "data-defined-properties" ) );
@@ -27,6 +27,7 @@ class DummyMaterialSettings : public QgsAbstractMaterialSettings
{
public:
DummyMaterialSettings() = default;
QString type() const override { return QStringLiteral( "Dummy" ); }
static QgsAbstractMaterialSettings *create() { return new DummyMaterialSettings(); }
DummyMaterialSettings *clone() const override { return new DummyMaterialSettings(); }
void readXml( const QDomElement &, const QgsReadWriteContext & ) override { }

0 comments on commit e541fe6

Please sign in to comment.
You can’t perform that action at this time.