QGIS API Documentation 3.41.0-Master (af5edcb665c)
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(), QgsApplication::calloutRegistry()->calloutMetadata( type )->visibleName(), type );
35 }
36
37 connect( mCalloutStyleComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsCalloutPanelWidget::calloutTypeChanged );
38 calloutTypeChanged();
39}
40
42{
43 mGeometryType = type;
44}
45
47{
48 mContext = context;
49 if ( QgsCalloutWidget *cw = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
50 {
51 cw->setContext( context );
52 }
53}
54
56{
57 return mContext;
58}
59
61{
62 if ( callout )
63 {
64 whileBlocking( mCalloutStyleComboBox )->setCurrentIndex( mCalloutStyleComboBox->findData( callout->type() ) );
65 updateCalloutWidget( callout );
66 emit calloutChanged();
67 }
68}
69
71{
72 const QString calloutType = mCalloutStyleComboBox->currentData().toString();
73 std::unique_ptr<QgsCallout> callout;
74 if ( QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
75 {
76 callout.reset( pew->callout()->clone() );
77 }
78 if ( !callout )
79 callout.reset( QgsApplication::calloutRegistry()->createCallout( calloutType ) );
80
81 callout->setEnabled( true );
82 return callout.release();
83}
84
85void QgsCalloutPanelWidget::calloutTypeChanged()
86{
87 const QString newCalloutType = mCalloutStyleComboBox->currentData().toString();
88 QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() );
89 if ( pew )
90 {
91 if ( pew->callout() && pew->callout()->type() == newCalloutType )
92 return;
93 }
94
95 // get creation function for new callout from registry
97 QgsCalloutAbstractMetadata *am = registry->calloutMetadata( newCalloutType );
98 if ( !am ) // check whether the metadata is assigned
99 return;
100
101 // change callout to a new one (with different type)
102 // base new callout on existing callout's properties
103 const std::unique_ptr<QgsCallout> newCallout( am->createCallout( pew && pew->callout() ? pew->callout()->properties( QgsReadWriteContext() ) : QVariantMap(), QgsReadWriteContext() ) );
104 if ( !newCallout )
105 return;
106
107 updateCalloutWidget( newCallout.get() );
108 emit calloutChanged();
109}
110
111void QgsCalloutPanelWidget::updateCalloutWidget( const QgsCallout *callout )
112{
113 if ( !callout )
114 {
115 mCalloutStackedWidget->setCurrentWidget( pageDummy );
116 return;
117 }
118
119 if ( mCalloutStackedWidget->currentWidget() != pageDummy )
120 {
121 // stop updating from the original widget
122 if ( QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
124 }
125
127 if ( QgsCalloutAbstractMetadata *am = registry->calloutMetadata( callout->type() ) )
128 {
129 if ( QgsCalloutWidget *w = am->createCalloutWidget( mLayer ) )
130 {
131 Qgis::GeometryType geometryType = mGeometryType;
132 if ( QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( mLayer ) )
133 geometryType = vLayer->geometryType();
134 w->setGeometryType( geometryType );
135 w->setCallout( callout );
136
137 w->setContext( context() );
138 mCalloutStackedWidget->addWidget( w );
139 mCalloutStackedWidget->setCurrentWidget( w );
140 // start receiving updates from widget
142 return;
143 }
144 }
145 // When anything is not right
146 mCalloutStackedWidget->setCurrentWidget( pageDummy );
147}
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:494
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:5970