QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgscalloutpanelwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscalloutpanelwidget.cpp
3 ---------------------
4 begin : July 2024
5 copyright : (C) 2024 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
17#include "moc_qgscalloutpanelwidget.cpp"
18#include "qgsapplication.h"
19#include "qgscalloutsregistry.h"
20#include "qgscalloutwidget.h"
21#include "qgsgui.h"
22
24 : QgsPanelWidget( parent )
25 , mLayer( layer )
26{
28
29 setupUi( this );
30
31 const QStringList calloutTypes = QgsApplication::calloutRegistry()->calloutTypes();
32 for ( const QString &type : calloutTypes )
33 {
34 mCalloutStyleComboBox->addItem( QgsApplication::calloutRegistry()->calloutMetadata( type )->icon(),
36 }
37
38 connect( mCalloutStyleComboBox, qOverload< int >( &QComboBox::currentIndexChanged ), this, &QgsCalloutPanelWidget::calloutTypeChanged );
39 calloutTypeChanged();
40}
41
43{
44 mGeometryType = type;
45}
46
48{
49 mContext = context;
50 if ( QgsCalloutWidget *cw = qobject_cast< QgsCalloutWidget * >( mCalloutStackedWidget->currentWidget() ) )
51 {
52 cw->setContext( context );
53 }
54}
55
57{
58 return mContext;
59}
60
62{
63 if ( callout )
64 {
65 whileBlocking( mCalloutStyleComboBox )->setCurrentIndex( mCalloutStyleComboBox->findData( callout->type() ) );
66 updateCalloutWidget( callout );
67 emit calloutChanged();
68 }
69}
70
72{
73 const QString calloutType = mCalloutStyleComboBox->currentData().toString();
74 std::unique_ptr< QgsCallout > callout;
75 if ( QgsCalloutWidget *pew = qobject_cast< QgsCalloutWidget * >( mCalloutStackedWidget->currentWidget() ) )
76 {
77 callout.reset( pew->callout()->clone() );
78 }
79 if ( !callout )
80 callout.reset( QgsApplication::calloutRegistry()->createCallout( calloutType ) );
81
82 callout->setEnabled( true );
83 return callout.release();
84}
85
86void QgsCalloutPanelWidget::calloutTypeChanged()
87{
88 const QString newCalloutType = mCalloutStyleComboBox->currentData().toString();
89 QgsCalloutWidget *pew = qobject_cast< QgsCalloutWidget * >( mCalloutStackedWidget->currentWidget() );
90 if ( pew )
91 {
92 if ( pew->callout() && pew->callout()->type() == newCalloutType )
93 return;
94 }
95
96 // get creation function for new callout from registry
98 QgsCalloutAbstractMetadata *am = registry->calloutMetadata( newCalloutType );
99 if ( !am ) // check whether the metadata is assigned
100 return;
101
102 // change callout to a new one (with different type)
103 // base new callout on existing callout's properties
104 const std::unique_ptr< QgsCallout > newCallout( am->createCallout( pew && pew->callout() ? pew->callout()->properties( QgsReadWriteContext() ) : QVariantMap(), QgsReadWriteContext() ) );
105 if ( !newCallout )
106 return;
107
108 updateCalloutWidget( newCallout.get() );
109 emit calloutChanged();
110}
111
112void QgsCalloutPanelWidget::updateCalloutWidget( const QgsCallout *callout )
113{
114 if ( !callout )
115 {
116 mCalloutStackedWidget->setCurrentWidget( pageDummy );
117 return;
118 }
119
120 if ( mCalloutStackedWidget->currentWidget() != pageDummy )
121 {
122 // stop updating from the original widget
123 if ( QgsCalloutWidget *pew = qobject_cast< QgsCalloutWidget * >( mCalloutStackedWidget->currentWidget() ) )
125 }
126
128 if ( QgsCalloutAbstractMetadata *am = registry->calloutMetadata( callout->type() ) )
129 {
130 if ( QgsCalloutWidget *w = am->createCalloutWidget( mLayer ) )
131 {
132 Qgis::GeometryType geometryType = mGeometryType;
133 if ( QgsVectorLayer *vLayer = qobject_cast< QgsVectorLayer * >( mLayer ) )
134 geometryType = vLayer->geometryType();
135 w->setGeometryType( geometryType );
136 w->setCallout( callout );
137
138 w->setContext( context() );
139 mCalloutStackedWidget->addWidget( w );
140 mCalloutStackedWidget->setCurrentWidget( w );
141 // start receiving updates from widget
143 return;
144 }
145 }
146 // When anything is not right
147 mCalloutStackedWidget->setCurrentWidget( pageDummy );
148}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
Stores metadata about one callout renderer class.
QString visibleName() const
Returns a friendly display name of the callout type.
virtual QgsCalloutWidget * createCalloutWidget(QgsMapLayer *)
Creates a widget for configuring callouts of this type.
virtual QgsCallout * createCallout(const QVariantMap &properties, const QgsReadWriteContext &context)=0
Create a callout of this type given the map of properties.
QIcon icon() const
Returns an icon representing the callout.
void setContext(const QgsSymbolWidgetContext &context)
Sets the context in which the widget is shown, e.g., the associated map canvas and expression context...
Qgis::GeometryType geometryType() const
Returns the geometry type for the objects associated with the callouts.
QgsCallout * callout()
Returns a new callout, respecting the configuration from the widget.
QgsSymbolWidgetContext context() const
Returns the context in which the widget is shown, e.g., the associated map canvas and expression cont...
void calloutChanged()
Emitted when the callout defined by the widget changes.
void setGeometryType(Qgis::GeometryType type)
Sets the geometry type for the objects associated with the callouts.
QgsCalloutPanelWidget(QWidget *parent=nullptr, QgsMapLayer *layer=nullptr)
Constructor for QgsCalloutPanelWidget, with the specified parent widget.
void setCallout(const QgsCallout *callout)
Sets the widget state to match the specified callout.
Registry of available callout classes.
QgsCalloutAbstractMetadata * calloutMetadata(const QString &type) const
Returns the metadata for specified the specified callout type.
QStringList calloutTypes() const
Returns a list of all available callout types.
Base class for widgets which allow control over the properties of callouts.
virtual QgsCallout * callout()=0
Returns the callout defined by the current settings in the widget.
void changed()
Should be emitted whenever configuration changes happened on this symbol layer configuration.
Abstract base class for callout renderers.
Definition qgscallout.h:54
void setEnabled(bool enabled)
Sets whether the callout is enabled.
virtual QString type() const =0
Returns a unique string representing the callout type.
virtual QVariantMap properties(const QgsReadWriteContext &context) const
Returns the properties describing the callout encoded in a string format.
static void initCalloutWidgets()
Initializes callout widgets.
Definition qgsgui.cpp:497
Base class for all map layer types.
Definition qgsmaplayer.h:76
Base class for any widget that can be shown as a inline panel.
The class is used as a container of context for various read/write operations on other objects.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
Represents a vector layer which manages a vector based data sets.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:5862