QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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( "labelingCalloutCurved.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 }
QgsCalloutWidget
Base class for widgets which allow control over the properties of callouts.
Definition: qgscalloutwidget.h:34
QgsCalloutMetadata
Convenience metadata class that uses static functions to create callouts and their widgets.
Definition: qgscalloutsregistry.h:110
QgsCalloutRegistry::defaultCallout
static QgsCallout * defaultCallout()
Create a new instance of a callout with default settings.
Definition: qgscalloutsregistry.cpp:88
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsCallout
Abstract base class for callout renderers.
Definition: qgscallout.h:52
QgsSimpleLineCallout::create
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
QgsCurvedLineCallout::create
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
qgsapplication.h
QgsCalloutMetadata::mWidgetFunc
QgsCalloutWidgetFunc mWidgetFunc
Definition: qgscalloutsregistry.h:137
QgsCalloutMetadata::mCreateFunc
QgsCalloutCreateFunc mCreateFunc
Definition: qgscalloutsregistry.h:136
QgsCalloutAbstractMetadata
Stores metadata about one callout renderer class.
Definition: qgscalloutsregistry.h:39
QgsCalloutRegistry::calloutTypes
QStringList calloutTypes() const
Returns a list of all available callout types.
Definition: qgscalloutsregistry.cpp:78
QgsXmlUtils::readVariant
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Definition: qgsxmlutils.cpp:251
qgscalloutsregistry.h
QgsCalloutMetadata::createCalloutWidget
QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *vl) override
Creates a widget for configuring callouts of this type.
Definition: qgscalloutsregistry.cpp:39
QgsSimpleLineCallout
A simple direct line callout style.
Definition: qgscallout.h:510
QgsCalloutRegistry::createCallout
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.
Definition: qgscalloutsregistry.cpp:93
qgsxmlutils.h
QgsCalloutRegistry::calloutMetadata
QgsCalloutAbstractMetadata * calloutMetadata(const QString &type) const
Returns the metadata for specified the specified callout type.
Definition: qgscalloutsregistry.cpp:83
qgscallout.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsCalloutAbstractMetadata::createCalloutWidget
virtual QgsCalloutWidget * createCalloutWidget(QgsVectorLayer *)
Creates a widget for configuring callouts of this type.
Definition: qgscalloutsregistry.cpp:25
QgsCalloutRegistry::QgsCalloutRegistry
QgsCalloutRegistry()
Definition: qgscalloutsregistry.cpp:49
QgsCalloutMetadata::createCallout
QgsCallout * createCallout(const QVariantMap &properties, const QgsReadWriteContext &context) override
Create a callout of this type given the map of properties.
Definition: qgscalloutsregistry.cpp:34
QgsCalloutRegistry::addCalloutType
bool addCalloutType(QgsCalloutAbstractMetadata *metadata)
Registers a new callout type.
Definition: qgscalloutsregistry.cpp:63
QgsCalloutAbstractMetadata::name
QString name() const
Returns the unique name of the callout type.
Definition: qgscalloutsregistry.h:62
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
QgsManhattanLineCallout::create
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
QgsBalloonCallout::create
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...
Definition: qgscallout.cpp:1059
QgsCalloutRegistry::~QgsCalloutRegistry
~QgsCalloutRegistry()
Definition: qgscalloutsregistry.cpp:58