QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgscalloutsregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscalloutsregistry.cpp
3  -----------------------
4  begin : July 2019
5  copyright : (C) 2019 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgscalloutsregistry.h"
17 #include "qgscallout.h"
18 #include "qgsxmlutils.h"
19 #include "qgsapplication.h"
20 
21 //
22 // QgsCalloutAbstractMetadata
23 //
24 
26 {
27  return nullptr;
28 }
29 
30 //
31 // QgsCalloutMetadata
32 //
33 
34 QgsCallout *QgsCalloutMetadata::createCallout( const QVariantMap &properties, const QgsReadWriteContext &context )
35 {
36  return mCreateFunc ? mCreateFunc( properties, context ) : nullptr;
37 }
38 
40 {
41  return mWidgetFunc ? mWidgetFunc( vl ) : nullptr;
42 }
43 
44 
45 //
46 // QgsCalloutRegistry
47 //
48 
50 {
51  // init registry with known callouts
52  addCalloutType( new QgsCalloutMetadata( QStringLiteral( "simple" ), QObject::tr( "Simple lines" ), QgsApplication::getThemeIcon( QStringLiteral( "labelingCalloutSimple.svg" ) ), QgsSimpleLineCallout::create ) );
53  addCalloutType( new QgsCalloutMetadata( QStringLiteral( "manhattan" ), QObject::tr( "Manhattan lines" ), QgsApplication::getThemeIcon( QStringLiteral( "labelingCalloutManhattan.svg" ) ), QgsManhattanLineCallout::create ) );
54 }
55 
57 {
58  qDeleteAll( mMetadata );
59 }
60 
62 {
63  if ( !metadata || mMetadata.contains( metadata->name() ) )
64  return false;
65 
66  mMetadata[metadata->name()] = metadata;
67  return true;
68 }
69 
70 QgsCallout *QgsCalloutRegistry::createCallout( const QString &name, const QDomElement &element, const QgsReadWriteContext &context ) const
71 {
72  const QVariantMap props = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
73  return createCallout( name, props, context );
74 }
75 
77 {
78  return mMetadata.keys();
79 }
80 
82 {
83  return mMetadata.value( name );
84 }
85 
87 {
88  return new QgsSimpleLineCallout();
89 }
90 
91 QgsCallout *QgsCalloutRegistry::createCallout( const QString &name, const QVariantMap &properties, const QgsReadWriteContext &context ) const
92 {
93  if ( !mMetadata.contains( name ) )
94  return nullptr;
95 
96  return mMetadata[name]->createCallout( properties, context );
97 }
The class is used as a container of context for various read/write operations on other objects...
static QgsCallout * create(const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext())
Creates a new QgsManhattanLineCallout, using the settings serialized in the properties map (correspon...
Definition: qgscallout.cpp:437
Abstract base class for callout renderers.
Definition: qgscallout.h:46
QStringList calloutTypes() const
Returns a list of all available callout types.
bool addCalloutType(QgsCalloutAbstractMetadata *metadata)
Registers a new callout type.
Stores metadata about one callout renderer class.
QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *vl) override
Creates a widget for configuring callouts of this type.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Base class for widgets which allow control over the properties of callouts.
QgsCallout * createCallout(const QString &type, const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext()) const
Creates a new instance of a callout, given the callout type and properties.
QgsCalloutAbstractMetadata * calloutMetadata(const QString &type) const
Returns the metadata for specified the specified callout type.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
static QgsCallout * defaultCallout()
Create a new instance of a callout with default settings.
A simple direct line callout style.
Definition: qgscallout.h:339
QgsCallout * createCallout(const QVariantMap &properties, const QgsReadWriteContext &context) override
Create a callout of this type given the map of properties.
virtual QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *)
Creates a widget for configuring callouts of this type.
Convenience metadata class that uses static functions to create callouts and their widgets...
QString name() const
Returns the unique name of the callout type.
Represents a vector layer which manages a vector based data sets.
virtual QgsCallout * createCallout(const QVariantMap &properties, const QgsReadWriteContext &context)=0
Create a callout of this type given the map of properties.
static QgsCallout * create(const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext())
Creates a new QgsSimpleLineCallout, using the settings serialized in the properties map (correspondin...
Definition: qgscallout.cpp:216