QGIS API Documentation  3.20.0-Odense (decaadbb31)
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  addCalloutType( new QgsCalloutMetadata( QStringLiteral( "curved" ), QObject::tr( "Curved lines" ), QgsApplication::getThemeIcon( QStringLiteral( "labelingCalloutSimple.svg" ) ), QgsCurvedLineCallout::create ) );
55  addCalloutType( new QgsCalloutMetadata( QStringLiteral( "balloon" ), QObject::tr( "Balloons" ), QgsApplication::getThemeIcon( QStringLiteral( "labelingCalloutManhattan.svg" ) ), QgsBalloonCallout::create ) );
56 }
57 
59 {
60  qDeleteAll( mMetadata );
61 }
62 
64 {
65  if ( !metadata || mMetadata.contains( metadata->name() ) )
66  return false;
67 
68  mMetadata[metadata->name()] = metadata;
69  return true;
70 }
71 
72 QgsCallout *QgsCalloutRegistry::createCallout( const QString &name, const QDomElement &element, const QgsReadWriteContext &context ) const
73 {
74  const QVariantMap props = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
75  return createCallout( name, props, context );
76 }
77 
79 {
80  return mMetadata.keys();
81 }
82 
84 {
85  return mMetadata.value( name );
86 }
87 
89 {
90  return new QgsSimpleLineCallout();
91 }
92 
93 QgsCallout *QgsCalloutRegistry::createCallout( const QString &name, const QVariantMap &properties, const QgsReadWriteContext &context ) const
94 {
95  if ( !mMetadata.contains( name ) )
96  return nullptr;
97 
98  return mMetadata[name]->createCallout( properties, context );
99 }
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsCallout * create(const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext())
Creates a new QgsBalloonCallout, using the settings serialized in the properties map (corresponding t...
Stores metadata about one callout renderer class.
virtual QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *)
Creates a widget for configuring callouts of this type.
QString name() const
Returns the unique name of the callout type.
Convenience metadata class that uses static functions to create callouts and their widgets.
QgsCalloutWidgetFunc mWidgetFunc
QgsCalloutCreateFunc mCreateFunc
QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *vl) override
Creates a widget for configuring callouts of this type.
QgsCallout * createCallout(const QVariantMap &properties, const QgsReadWriteContext &context) override
Create a callout of this type given the map of properties.
QgsCalloutAbstractMetadata * calloutMetadata(const QString &type) const
Returns the metadata for specified the specified callout type.
static QgsCallout * defaultCallout()
Create a new instance of a callout with default settings.
bool addCalloutType(QgsCalloutAbstractMetadata *metadata)
Registers a new callout type.
QStringList calloutTypes() const
Returns a list of all available callout types.
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.
Base class for widgets which allow control over the properties of callouts.
Abstract base class for callout renderers.
Definition: qgscallout.h:52
static QgsCallout * create(const QVariantMap &properties=QVariantMap(), const QgsReadWriteContext &context=QgsReadWriteContext())
Creates a new QgsCurvedLineCallout, using the settings serialized in the properties map (correspondin...
Definition: qgscallout.cpp:803
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:763
The class is used as a container of context for various read/write operations on other objects.
A simple direct line callout style.
Definition: qgscallout.h:510
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:554
Represents a vector layer which manages a vector based data sets.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.