QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsCalloutWidget
Definition: qgscalloutwidget.h:34
QgsCalloutMetadata
Definition: qgscalloutsregistry.h:110
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:605
QgsCalloutRegistry::defaultCallout
static QgsCallout * defaultCallout()
Create a new instance of a callout with default settings.
Definition: qgscalloutsregistry.cpp:86
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsCallout
Abstract base class for callout renderers.
Definition: qgscallout.h:46
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:336
qgsapplication.h
QgsCalloutMetadata::mWidgetFunc
QgsCalloutWidgetFunc mWidgetFunc
Definition: qgscalloutsregistry.h:137
QgsCalloutMetadata::mCreateFunc
QgsCalloutCreateFunc mCreateFunc
Definition: qgscalloutsregistry.h:136
QgsCalloutAbstractMetadata
Definition: qgscalloutsregistry.h:39
QgsCalloutRegistry::calloutTypes
QStringList calloutTypes() const
Returns a list of all available callout types.
Definition: qgscalloutsregistry.cpp:76
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:401
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:91
qgsxmlutils.h
QgsCalloutRegistry::calloutMetadata
QgsCalloutAbstractMetadata * calloutMetadata(const QString &type) const
Returns the metadata for specified the specified callout type.
Definition: qgscalloutsregistry.cpp:81
qgscallout.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
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:61
QgsCalloutAbstractMetadata::name
QString name() const
Returns the unique name of the callout type.
Definition: qgscalloutsregistry.h:62
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:565
QgsCalloutRegistry::~QgsCalloutRegistry
~QgsCalloutRegistry()
Definition: qgscalloutsregistry.cpp:56