Skip to content

Commit

Permalink
Add registry for annotation item classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2020
1 parent 96cf240 commit b0e2a8b
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 0 deletions.
@@ -0,0 +1,161 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationitemregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsAnnotationItemAbstractMetadata
{
%Docstring
Stores metadata about one annotation item class.

A companion class, QgsAnnotationItemAbstractGuiMetadata, handles the
GUI behavior of QgsAnnotationItems.

.. note::

In C++ you can use QgsAnnotationItemMetadata convenience class.

.. versionadded:: 3.12
%End

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

QgsAnnotationItemAbstractMetadata( const QString &type, const QString &visibleName, const QString &visiblePluralName = QString() );
%Docstring
Constructor for QgsAnnotationItemAbstractMetadata with the specified class ``type``
and ``visibleName``.

The optional ``visiblePluralName`` argument can be used to specify a plural variant of the item type.
%End

virtual ~QgsAnnotationItemAbstractMetadata();

QString type() const;
%Docstring
Returns the unique item type string for the annotation item class.
%End

QString visibleName() const;
%Docstring
Returns a translated, user visible name for the annotation item class.

.. seealso:: :py:func:`visiblePluralName`
%End

QString visiblePluralName() const;
%Docstring
Returns a translated, user visible name for plurals of the annotation item class (e.g. "Labels" for a "Label" item).
%End

virtual QgsAnnotationItem *createItem( const QDomElement &element, const QgsReadWriteContext &context ) = 0 /Factory/;
%Docstring
Creates an annotation item of this class using the state present in the specified
DOM ``element``.
%End

virtual QgsAnnotationItem *createItem() = 0 /Factory/;
%Docstring
Creates a new, default, annotation item of this class.
%End

};





class QgsAnnotationItemRegistry : QObject
{
%Docstring
Registry of available annotation item types.

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

A companion class, :py:class:`QgsAnnotationItemGuiRegistry`, handles the GUI behavior
of annotation items.

.. versionadded:: 3.12
%End

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

QgsAnnotationItemRegistry( QObject *parent = 0 );
%Docstring
Creates a new empty item registry.

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

.. seealso:: :py:func:`populate`
%End

~QgsAnnotationItemRegistry();

bool populate();
%Docstring
Populates the registry with standard item types. If called on a non-empty registry
then this will have no effect and will return ``False``.
%End


QgsAnnotationItemAbstractMetadata *itemMetadata( const QString &type ) const;
%Docstring
Returns the metadata for the specified item ``type``. Returns ``None`` if
a corresponding type was not found in the registry.
%End

bool addItemType( QgsAnnotationItemAbstractMetadata *metadata /Transfer/ );
%Docstring
Registers a new annotation item type. Takes ownership of the metadata instance.
%End

QgsAnnotationItem *createItem( const QString &type ) const /Factory/;
%Docstring
Creates a new instance of a annotation item given the item ``type``.
%End

QgsAnnotationItem *createItem( const QDomElement &element, const QgsReadWriteContext &context ) const /Factory/;
%Docstring
Creates a new instance of a annotation item given a DOM ``element`` corresponding
to an item.
%End

QMap< QString, QString> itemTypes() const;
%Docstring
Returns a map of available item types to translated name.
%End

signals:

void typeAdded( const QString &type, const QString &name );
%Docstring
Emitted whenever a new item type is added to the registry, with the specified
``type`` and visible ``name``.
%End

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




/************************************************************************
* This file has been generated automatically from *
* *
* src/core/annotations/qgsannotationitemregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -256,6 +256,7 @@
%Include auto_generated/./3d/qgsabstract3drenderer.sip
%Include auto_generated/annotations/qgsannotation.sip
%Include auto_generated/annotations/qgsannotationitem.sip
%Include auto_generated/annotations/qgsannotationitemregistry.sip
%Include auto_generated/annotations/qgsannotationlayer.sip
%Include auto_generated/annotations/qgsannotationmanager.sip
%Include auto_generated/annotations/qgshtmlannotation.sip
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -126,6 +126,7 @@ SET(QGIS_CORE_SRCS

annotations/qgsannotation.cpp
annotations/qgsannotationitem.cpp
annotations/qgsannotationitemregistry.cpp
annotations/qgsannotationlayer.cpp
annotations/qgsannotationlayerrenderer.cpp
annotations/qgsannotationmanager.cpp
Expand Down Expand Up @@ -1049,6 +1050,7 @@ SET(QGIS_CORE_HDRS

annotations/qgsannotation.h
annotations/qgsannotationitem.h
annotations/qgsannotationitemregistry.h
annotations/qgsannotationlayer.h
annotations/qgsannotationlayerrenderer.h
annotations/qgsannotationmanager.h
Expand Down
68 changes: 68 additions & 0 deletions src/core/annotations/qgsannotationitemregistry.cpp
@@ -0,0 +1,68 @@
/***************************************************************************
qgsannotationitemregistry.cpp
-------------------------
begin : October 2019
copyright : (C) 2019 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 "qgsannotationitemregistry.h"

QgsAnnotationItemRegistry::QgsAnnotationItemRegistry( QObject *parent )
: QObject( parent )
{
}

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

bool QgsAnnotationItemRegistry::populate()
{
if ( !mMetadata.isEmpty() )
return false;

return true;
}

QgsAnnotationItemAbstractMetadata *QgsAnnotationItemRegistry::itemMetadata( const QString &type ) const
{
return mMetadata.value( type );
}

bool QgsAnnotationItemRegistry::addItemType( QgsAnnotationItemAbstractMetadata *metadata )
{
if ( !metadata || mMetadata.contains( metadata->type() ) )
return false;

mMetadata[metadata->type()] = metadata;
emit typeAdded( metadata->type(), metadata->visibleName() );
return true;
}

QgsAnnotationItem *QgsAnnotationItemRegistry::createItem( const QString &type ) const
{
if ( !mMetadata.contains( type ) )
return nullptr;

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

QMap<QString, QString> QgsAnnotationItemRegistry::itemTypes() const
{
QMap<QString, QString> types;
for ( auto it = mMetadata.constBegin(); it != mMetadata.constEnd(); ++it )
{
types.insert( it.key(), it.value()->visibleName() );
}
return types;
}

0 comments on commit b0e2a8b

Please sign in to comment.