Skip to content
Permalink
Browse files

[api] Add a proper registry for 3D symbol types

  • Loading branch information
nyalldawson committed Jul 22, 2020
1 parent f60b79f commit a34eabd1fbcd8631d6863fef9600c39f87ffacb9
@@ -0,0 +1,117 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/./3d/qgs3dsymbolregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/





class Qgs3DSymbolAbstractMetadata
{
%Docstring
Stores metadata about one 3D symbol class.

.. note::

It's necessary to implement :py:func:`~createSymbol` function.
In C++ you can use Qgs3DSymbolMetadata convenience class.

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgs3dsymbolregistry.h"
%End
public:

Qgs3DSymbolAbstractMetadata( const QString &type, const QString &visibleName );
%Docstring
Constructor for Qgs3DSymbolAbstractMetadata, with the specified ``type`` and ``visibleName``.
%End

virtual ~Qgs3DSymbolAbstractMetadata();

QString type() const;
%Docstring
Returns the unique symbol type string.
%End

QString visibleName() const;
%Docstring
Returns the symbol's visible (translated) name.
%End

virtual QgsAbstract3DSymbol *create() = 0 /Factory/;
%Docstring
Creates a new instance of this symbol type.

Caller takes ownership of the returned symbol.
%End


};





class Qgs3DSymbolRegistry
{
%Docstring
Registry of available 3D symbol classes.

Qgs3DSymbolRegistry is not usually directly created, but rather accessed through
:py:func:`QgsApplication.symbol3DRegistry()`.

.. versionadded:: 3.16
%End

%TypeHeaderCode
#include "qgs3dsymbolregistry.h"
%End
public:

Qgs3DSymbolRegistry();
~Qgs3DSymbolRegistry();


Qgs3DSymbolAbstractMetadata *symbolMetadata( const QString &type ) const;
%Docstring
Returns metadata for specified symbol ``type``. Returns ``None`` if not found
%End

QStringList symbolTypes() const;
%Docstring
Returns a list of all available symbol types.
%End

bool addSymbolType( Qgs3DSymbolAbstractMetadata *metadata /Transfer/ );
%Docstring
Registers a new symbol type. Takes ownership of the ``metadata`` instance.
%End

QgsAbstract3DSymbol *createSymbol( const QString &type ) const /Factory/;
%Docstring
Creates a new instance of a symbol of the specified ``type``.

The caller takes ownership of the returned symbol.

Returns ``None`` if the specified type is not found in the registry.
%End

private:
Qgs3DSymbolRegistry( const Qgs3DSymbolRegistry &rh );
};


/************************************************************************
* This file has been generated automatically from *
* *
* src/core/./3d/qgs3dsymbolregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -850,6 +850,13 @@ Gets the registry of available field formatters.
Returns registry of available 3D renderers.

.. versionadded:: 3.0
%End

static Qgs3DSymbolRegistry *symbol3DRegistry() /KeepReference/;
%Docstring
Returns registry of available 3D symbols.

.. versionadded:: 3.16
%End

static QgsScaleBarRendererRegistry *scaleBarRendererRegistry() /KeepReference/;
@@ -251,6 +251,7 @@
%Include auto_generated/qgsxmlutils.sip
%Include auto_generated/qgsziputils.sip
%Include auto_generated/./3d/qgs3drendererregistry.sip
%Include auto_generated/./3d/qgs3dsymbolregistry.sip
%Include auto_generated/./3d/qgsabstract3dsymbol.sip
%Include auto_generated/./3d/qgsabstract3drenderer.sip
%Include auto_generated/annotations/qgsannotation.sip
@@ -0,0 +1,52 @@
/***************************************************************************
qgs3dsymbolregistry.cpp
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgs3dsymbolregistry.h"

Qgs3DSymbolRegistry::Qgs3DSymbolRegistry()
{
}

Qgs3DSymbolRegistry::~Qgs3DSymbolRegistry()
{
qDeleteAll( mMetadata );
}

bool Qgs3DSymbolRegistry::addSymbolType( Qgs3DSymbolAbstractMetadata *metadata )
{
if ( !metadata || mMetadata.contains( metadata->type() ) )
return false;

mMetadata[metadata->type()] = metadata;
return true;
}

QgsAbstract3DSymbol *Qgs3DSymbolRegistry::createSymbol( const QString &type ) const
{
if ( !mMetadata.contains( type ) )
return nullptr;

return mMetadata[type]->create();
}

Qgs3DSymbolAbstractMetadata *Qgs3DSymbolRegistry::symbolMetadata( const QString &type ) const
{
return mMetadata.value( type );
}

QStringList Qgs3DSymbolRegistry::symbolTypes() const
{
return mMetadata.keys();
}
@@ -0,0 +1,201 @@
/***************************************************************************
qgs3dsymbolregistry.h
--------------------------------------
Date : July 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGS3DSYMBOLREGISTRY_H
#define QGS3DSYMBOLREGISTRY_H

#include "qgis_core.h"
#include "qgis_sip.h"

#include <QDomElement>
#include <QMap>

class QgsAbstract3DSymbol;
class QgsReadWriteContext;
class Qgs3DSymbolWidget;
class QgsVectorLayer;

/**
* \ingroup core
* Stores metadata about one 3D symbol class.
*
* \note It's necessary to implement createSymbol() function.
* In C++ you can use Qgs3DSymbolMetadata convenience class.
*
* \since QGIS 3.16
*/
class CORE_EXPORT Qgs3DSymbolAbstractMetadata
{
public:

/**
* Constructor for Qgs3DSymbolAbstractMetadata, with the specified \a type and \a visibleName.
*/
Qgs3DSymbolAbstractMetadata( const QString &type, const QString &visibleName )
: mType( type )
, mVisibleName( visibleName )
{}

virtual ~Qgs3DSymbolAbstractMetadata() = default;

/**
* Returns the unique symbol type string.
*/
QString type() const { return mType; }

/**
* Returns the symbol's visible (translated) name.
*/
QString visibleName() const { return mVisibleName; }

/**
* Creates a new instance of this symbol type.
*
* Caller takes ownership of the returned symbol.
*/
virtual QgsAbstract3DSymbol *create() = 0 SIP_FACTORY;

#ifndef SIP_RUN

/**
* Create a widget for configuring a symbol of this type.
*
* Can return NULLPTR if there's no GUI.
*
* \note Not available in Python bindings
*/
virtual Qgs3DSymbolWidget *createSymbolWidget( QgsVectorLayer * ) SIP_FACTORY { return nullptr; }
#endif

private:
QString mType;
QString mVisibleName;
};

//! 3D symbol creation function
typedef QgsAbstract3DSymbol *( *Qgs3DSymbolCreateFunc )() SIP_SKIP;

//! 3D symbol widget creation function
typedef Qgs3DSymbolWidget *( *Qgs3DSymbolWidgetFunc )( QgsVectorLayer * ) SIP_SKIP;

#ifndef SIP_RUN

/**
* \ingroup core
* Convenience metadata class that uses static functions to create a 3D symbol and its widget.
*
* \note Not available in Python bindings.
*
* \since QGIS 3.16
*/
class CORE_EXPORT Qgs3DSymbolMetadata : public Qgs3DSymbolAbstractMetadata
{
public:

/**
* Constructor for Qgs3DSymbolMetadata, with the specified \a type and \a visibleName.
*
* The \a pfCreate and \a pfWidget arguments are used to specify
* static functions for creating the symbol type and configuration widget.
*/
Qgs3DSymbolMetadata( const QString &type, const QString &visibleName,
Qgs3DSymbolCreateFunc pfCreate,
Qgs3DSymbolWidgetFunc pfWidget = nullptr ) SIP_SKIP
: Qgs3DSymbolAbstractMetadata( type, visibleName )
, mCreateFunc( pfCreate )
, mWidgetFunc( pfWidget )
{}

/**
* Returns the symbol type's creation function.
*/
Qgs3DSymbolCreateFunc createFunction() const { return mCreateFunc; }

/**
* Returns the symbol type's widget creation function.
*
* \see setWidgetFunction()
*/
Qgs3DSymbolWidgetFunc widgetFunction() const { return mWidgetFunc; }

/**
* Sets the symbol type's widget creation \a function.
*
* \see widgetFunction()
*/
void setWidgetFunction( Qgs3DSymbolWidgetFunc function ) { mWidgetFunc = function; }

QgsAbstract3DSymbol *create() override SIP_FACTORY { return mCreateFunc ? mCreateFunc() : nullptr; }
Qgs3DSymbolWidget *createSymbolWidget( QgsVectorLayer *vl ) override SIP_FACTORY { return mWidgetFunc ? mWidgetFunc( vl ) : nullptr; }

private:
Qgs3DSymbolCreateFunc mCreateFunc;
Qgs3DSymbolWidgetFunc mWidgetFunc;

};
#endif


/**
* \ingroup core
* Registry of available 3D symbol classes.
*
* Qgs3DSymbolRegistry is not usually directly created, but rather accessed through
* QgsApplication::symbol3DRegistry().
*
* \since QGIS 3.16
*/
class CORE_EXPORT Qgs3DSymbolRegistry
{
public:

Qgs3DSymbolRegistry();
~Qgs3DSymbolRegistry();

//! Qgs3DSymbolRegistry cannot be copied.
Qgs3DSymbolRegistry( const Qgs3DSymbolRegistry &rh ) = delete;
//! Qgs3DSymbolRegistry cannot be copied.
Qgs3DSymbolRegistry &operator=( const Qgs3DSymbolRegistry &rh ) = delete;

//! Returns metadata for specified symbol \a type. Returns NULLPTR if not found
Qgs3DSymbolAbstractMetadata *symbolMetadata( const QString &type ) const;

/**
* Returns a list of all available symbol types.
*/
QStringList symbolTypes() const;

//! Registers a new symbol type. Takes ownership of the \a metadata instance.
bool addSymbolType( Qgs3DSymbolAbstractMetadata *metadata SIP_TRANSFER );

/**
* Creates a new instance of a symbol of the specified \a type.
*
* The caller takes ownership of the returned symbol.
*
* Returns NULLPTR if the specified type is not found in the registry.
*/
QgsAbstract3DSymbol *createSymbol( const QString &type ) const SIP_FACTORY;

private:
#ifdef SIP_RUN
Qgs3DSymbolRegistry( const Qgs3DSymbolRegistry &rh );
#endif

QMap<QString, Qgs3DSymbolAbstractMetadata *> mMetadata;
};


#endif // QGS3DSYMBOLREGISTRY_H

0 comments on commit a34eabd

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