QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsapplication.h"
19#include "qgscalloutsregistry.h"
20#include "qgscalloutwidget.h"
21#include "qgsgui.h"
22
23#include "moc_qgscalloutpanelwidget.cpp"
24
26 : QgsPanelWidget( parent )
27 , mLayer( layer )
28{
30
31 setupUi( this );
32
33 const QStringList calloutTypes = QgsApplication::calloutRegistry()->calloutTypes();
34 for ( const QString &type : calloutTypes )
35 {
36 mCalloutStyleComboBox->addItem( QgsApplication::calloutRegistry()->calloutMetadata( type )->icon(), QgsApplication::calloutRegistry()->calloutMetadata( type )->visibleName(), type );
37 }
38
39 connect( mCalloutStyleComboBox, qOverload<int>( &QComboBox::currentIndexChanged ), this, &QgsCalloutPanelWidget::calloutTypeChanged );
40 calloutTypeChanged();
41}
42
44{
45 mGeometryType = type;
46}
47
49{
50 mContext = context;
51 if ( QgsCalloutWidget *cw = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
52 {
53 cw->setContext( context );
54 }
55}
56
58{
59 return mContext;
60}
61
63{
64 if ( callout )
65 {
66 whileBlocking( mCalloutStyleComboBox )->setCurrentIndex( mCalloutStyleComboBox->findData( callout->type() ) );
67 updateCalloutWidget( callout );
68 emit calloutChanged();
69 }
70}
71
73{
74 const QString calloutType = mCalloutStyleComboBox->currentData().toString();
75 std::unique_ptr<QgsCallout> callout;
76 if ( QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
77 {
78 callout.reset( pew->callout()->clone() );
79 }
80 if ( !callout )
81 callout.reset( QgsApplication::calloutRegistry()->createCallout( calloutType ) );
82
83 callout->setEnabled( true );
84 return callout.release();
85}
86
87void QgsCalloutPanelWidget::calloutTypeChanged()
88{
89 const QString newCalloutType = mCalloutStyleComboBox->currentData().toString();
90 QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() );
91 if ( pew )
92 {
93 if ( pew->callout() && pew->callout()->type() == newCalloutType )
94 return;
95 }
96
97 // get creation function for new callout from registry
98 QgsCalloutRegistry *registry = QgsApplication::calloutRegistry();
99 QgsCalloutAbstractMetadata *am = registry->calloutMetadata( newCalloutType );
100 if ( !am ) // check whether the metadata is assigned
101 return;
102
103 // change callout to a new one (with different type)
104 // base new callout on existing callout's properties
105 const std::unique_ptr<QgsCallout> newCallout( am->createCallout( pew && pew->callout() ? pew->callout()->properties( QgsReadWriteContext() ) : QVariantMap(), QgsReadWriteContext() ) );
106 if ( !newCallout )
107 return;
108
109 updateCalloutWidget( newCallout.get() );
110 emit calloutChanged();
111}
112
113void QgsCalloutPanelWidget::updateCalloutWidget( const QgsCallout *callout )
114{
115 if ( !callout )
116 {
117 mCalloutStackedWidget->setCurrentWidget( pageDummy );
118 return;
119 }
120
121 if ( mCalloutStackedWidget->currentWidget() != pageDummy )
122 {
123 // stop updating from the original widget
124 if ( QgsCalloutWidget *pew = qobject_cast<QgsCalloutWidget *>( mCalloutStackedWidget->currentWidget() ) )
126 }
127
128 QgsCalloutRegistry *registry = QgsApplication::calloutRegistry();
129 if ( QgsCalloutAbstractMetadata *am = registry->calloutMetadata( callout->type() ) )
130 {
131 if ( QgsCalloutWidget *w = am->createCalloutWidget( mLayer ) )
132 {
133 Qgis::GeometryType geometryType = mGeometryType;
134 if ( QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( mLayer ) )
135 geometryType = vLayer->geometryType();
136 w->setGeometryType( geometryType );
137 w->setCallout( callout );
138
139 w->setContext( context() );
140 mCalloutStackedWidget->addWidget( w );
141 mCalloutStackedWidget->setCurrentWidget( w );
142 // start receiving updates from widget
144 return;
145 }
146 }
147 // When anything is not right
148 mCalloutStackedWidget->setCurrentWidget( pageDummy );
149}
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:358
static QgsCalloutRegistry * calloutRegistry()
Returns the application's callout registry, used for managing callout types.
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.
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.
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:55
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:428
Base class for all map layer types.
Definition qgsmaplayer.h:80
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
Contains settings which reflect the context in which a symbol (or renderer) widget is shown,...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6511