Make API more flexible, allow any layer type for callout widget functions

This commit is contained in:
Nyall Dawson 2024-07-15 11:04:48 +10:00
parent e4335d216d
commit 51b747efce
8 changed files with 83 additions and 44 deletions

View File

@ -74,7 +74,7 @@ Create a callout of this type given the map of ``properties``.
Ownership of the callout is transferred to the caller.
%End
virtual QgsCalloutWidget *createCalloutWidget( QgsVectorLayer * );
virtual QgsCalloutWidget *createCalloutWidget( QgsMapLayer * );
%Docstring
Creates a widget for configuring callouts of this type. Can return ``None`` if there's no GUI required.
@ -103,7 +103,7 @@ Convenience metadata class that uses static functions to create callouts and the
virtual QgsCallout *createCallout( const QVariantMap &properties, const QgsReadWriteContext &context ) /Factory/;
virtual QgsCalloutWidget *createCalloutWidget( QgsVectorLayer *vl ) /Factory/;
virtual QgsCalloutWidget *createCalloutWidget( QgsMapLayer *vl ) /Factory/;
protected:

View File

@ -21,11 +21,11 @@ Base class for widgets which allow control over the properties of callouts.
%End
public:
QgsCalloutWidget( QWidget *parent /TransferThis/, QgsVectorLayer *vl = 0 );
QgsCalloutWidget( QWidget *parent /TransferThis/, QgsMapLayer *vl = 0 );
%Docstring
Constructor for QgsCalloutWidget.
:param vl: associated vector layer
:param vl: associated map layer
:param parent: parent widget
%End
@ -60,9 +60,19 @@ Returns the context in which the symbol widget is shown, e.g., the associated ma
.. seealso:: :py:func:`setContext`
%End
const QgsVectorLayer *vectorLayer() const;
const QgsVectorLayer *vectorLayer() const /Deprecated/;
%Docstring
Returns the vector layer associated with the widget.
.. deprecated::
QGIS 3.40, use :py:func:`~QgsCalloutWidget.layer` instead.
%End
const QgsMapLayer *layer() const;
%Docstring
Returns the vector layer associated with the widget.
.. versionadded:: 3.40
%End
virtual void setGeometryType( Qgis::GeometryType type ) = 0;

View File

@ -74,7 +74,7 @@ Create a callout of this type given the map of ``properties``.
Ownership of the callout is transferred to the caller.
%End
virtual QgsCalloutWidget *createCalloutWidget( QgsVectorLayer * );
virtual QgsCalloutWidget *createCalloutWidget( QgsMapLayer * );
%Docstring
Creates a widget for configuring callouts of this type. Can return ``None`` if there's no GUI required.
@ -103,7 +103,7 @@ Convenience metadata class that uses static functions to create callouts and the
virtual QgsCallout *createCallout( const QVariantMap &properties, const QgsReadWriteContext &context ) /Factory/;
virtual QgsCalloutWidget *createCalloutWidget( QgsVectorLayer *vl ) /Factory/;
virtual QgsCalloutWidget *createCalloutWidget( QgsMapLayer *vl ) /Factory/;
protected:

View File

@ -21,11 +21,11 @@ Base class for widgets which allow control over the properties of callouts.
%End
public:
QgsCalloutWidget( QWidget *parent /TransferThis/, QgsVectorLayer *vl = 0 );
QgsCalloutWidget( QWidget *parent /TransferThis/, QgsMapLayer *vl = 0 );
%Docstring
Constructor for QgsCalloutWidget.
:param vl: associated vector layer
:param vl: associated map layer
:param parent: parent widget
%End
@ -60,9 +60,19 @@ Returns the context in which the symbol widget is shown, e.g., the associated ma
.. seealso:: :py:func:`setContext`
%End
const QgsVectorLayer *vectorLayer() const;
const QgsVectorLayer *vectorLayer() const /Deprecated/;
%Docstring
Returns the vector layer associated with the widget.
.. deprecated::
QGIS 3.40, use :py:func:`~QgsCalloutWidget.layer` instead.
%End
const QgsMapLayer *layer() const;
%Docstring
Returns the vector layer associated with the widget.
.. versionadded:: 3.40
%End
virtual void setGeometryType( Qgis::GeometryType type ) = 0;

View File

@ -22,7 +22,7 @@
// QgsCalloutAbstractMetadata
//
QgsCalloutWidget *QgsCalloutAbstractMetadata::createCalloutWidget( QgsVectorLayer * )
QgsCalloutWidget *QgsCalloutAbstractMetadata::createCalloutWidget( QgsMapLayer * )
{
return nullptr;
}
@ -36,7 +36,7 @@ QgsCallout *QgsCalloutMetadata::createCallout( const QVariantMap &properties, co
return mCreateFunc ? mCreateFunc( properties, context ) : nullptr;
}
QgsCalloutWidget *QgsCalloutMetadata::createCalloutWidget( QgsVectorLayer *vl )
QgsCalloutWidget *QgsCalloutMetadata::createCalloutWidget( QgsMapLayer *vl )
{
return mWidgetFunc ? mWidgetFunc( vl ) : nullptr;
}

View File

@ -22,7 +22,7 @@
#include <QIcon>
class QgsPathResolver;
class QgsVectorLayer;
class QgsMapLayer;
class QgsCalloutWidget SIP_EXTERNAL;
class QgsCallout;
class QDomElement;
@ -91,7 +91,7 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
*
* Ownership of the widget is transferred to the caller.
*/
virtual QgsCalloutWidget *createCalloutWidget( QgsVectorLayer * );
virtual QgsCalloutWidget *createCalloutWidget( QgsMapLayer * );
protected:
QString mName;
@ -100,7 +100,7 @@ class CORE_EXPORT QgsCalloutAbstractMetadata
};
typedef QgsCallout *( *QgsCalloutCreateFunc )( const QVariantMap &, const QgsReadWriteContext & ) SIP_SKIP;
typedef QgsCalloutWidget *( *QgsCalloutWidgetFunc )( QgsVectorLayer * ) SIP_SKIP;
typedef QgsCalloutWidget *( *QgsCalloutWidgetFunc )( QgsMapLayer * ) SIP_SKIP;
/**
* \ingroup core
@ -130,7 +130,7 @@ class CORE_EXPORT QgsCalloutMetadata : public QgsCalloutAbstractMetadata
void setWidgetFunction( QgsCalloutWidgetFunc f ) { mWidgetFunc = f; } SIP_SKIP
QgsCallout *createCallout( const QVariantMap &properties, const QgsReadWriteContext &context ) override SIP_FACTORY;
QgsCalloutWidget *createCalloutWidget( QgsVectorLayer *vl ) override SIP_FACTORY;
QgsCalloutWidget *createCalloutWidget( QgsMapLayer *vl ) override SIP_FACTORY;
protected:
QgsCalloutCreateFunc mCreateFunc;

View File

@ -29,7 +29,7 @@ QgsExpressionContext QgsCalloutWidget::createExpressionContext() const
if ( auto *lExpressionContext = mContext.expressionContext() )
return *lExpressionContext;
QgsExpressionContext expContext( mContext.globalProjectAtlasMapLayerScopes( vectorLayer() ) );
QgsExpressionContext expContext( mContext.globalProjectAtlasMapLayerScopes( layer() ) );
QgsExpressionContextScope *symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr, new QgsExpressionContextScope() );
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, QColor(), true ) );
expContext << symbolScope;
@ -72,7 +72,7 @@ QgsSymbolWidgetContext QgsCalloutWidget::context() const
void QgsCalloutWidget::registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsCallout::Property key )
{
button->init( static_cast< int >( key ), callout()->dataDefinedProperties(), QgsCallout::propertyDefinitions(), mVectorLayer, true );
button->init( static_cast< int >( key ), callout()->dataDefinedProperties(), QgsCallout::propertyDefinitions(), qobject_cast< QgsVectorLayer * >( mLayer ), true );
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsCalloutWidget::updateDataDefinedProperty );
connect( button, &QgsPropertyOverrideButton::createAuxiliaryField, this, &QgsCalloutWidget::createAuxiliaryField );
@ -82,14 +82,18 @@ void QgsCalloutWidget::registerDataDefinedButton( QgsPropertyOverrideButton *but
void QgsCalloutWidget::createAuxiliaryField()
{
// try to create an auxiliary layer if not yet created
if ( !mVectorLayer->auxiliaryLayer() )
QgsVectorLayer *vectorLayer = qobject_cast< QgsVectorLayer * >( mLayer );
if ( !vectorLayer )
return;
if ( !vectorLayer->auxiliaryLayer() )
{
QgsNewAuxiliaryLayerDialog dlg( mVectorLayer, this );
QgsNewAuxiliaryLayerDialog dlg( vectorLayer, this );
dlg.exec();
}
// return if still not exists
if ( !mVectorLayer->auxiliaryLayer() )
if ( !vectorLayer->auxiliaryLayer() )
return;
QgsPropertyOverrideButton *button = qobject_cast<QgsPropertyOverrideButton *>( sender() );
@ -97,9 +101,9 @@ void QgsCalloutWidget::createAuxiliaryField()
const QgsPropertyDefinition def = QgsCallout::propertyDefinitions()[static_cast< int >( key )];
// create property in auxiliary storage if necessary
if ( !mVectorLayer->auxiliaryLayer()->exists( def ) )
if ( !vectorLayer->auxiliaryLayer()->exists( def ) )
{
mVectorLayer->auxiliaryLayer()->addAuxiliaryField( def );
vectorLayer->auxiliaryLayer()->addAuxiliaryField( def );
}
// update property with join field name from auxiliary storage
@ -128,7 +132,7 @@ void QgsCalloutWidget::updateDataDefinedProperty()
// QgsSimpleLineCalloutWidget
//
QgsSimpleLineCalloutWidget::QgsSimpleLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent )
QgsSimpleLineCalloutWidget::QgsSimpleLineCalloutWidget( QgsMapLayer *vl, QWidget *parent )
: QgsCalloutWidget( parent, vl )
{
setupUi( this );
@ -138,7 +142,7 @@ QgsSimpleLineCalloutWidget::QgsSimpleLineCalloutWidget( QgsVectorLayer *vl, QWid
mCalloutLineStyleButton->setDialogTitle( tr( "Callout Symbol" ) );
mCalloutLineStyleButton->registerExpressionContextGenerator( this );
mCalloutLineStyleButton->setLayer( vl );
mCalloutLineStyleButton->setLayer( qobject_cast< QgsVectorLayer * >( vl ) );
mMinCalloutWidthUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
<< Qgis::RenderUnit::Points << Qgis::RenderUnit::Inches );
mOffsetFromAnchorUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
@ -178,6 +182,8 @@ QgsSimpleLineCalloutWidget::QgsSimpleLineCalloutWidget( QgsVectorLayer *vl, QWid
connect( mCalloutLineStyleButton, &QgsSymbolButton::changed, this, &QgsSimpleLineCalloutWidget::lineSymbolChanged );
connect( mCalloutBlendComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsSimpleLineCalloutWidget::mCalloutBlendComboBox_currentIndexChanged );
mPlacementDDGroupBox->setVisible( qobject_cast< QgsVectorLayer * >( vl ) );
}
void QgsSimpleLineCalloutWidget::setCallout( QgsCallout *callout )
@ -320,7 +326,7 @@ void QgsSimpleLineCalloutWidget::drawToAllPartsToggled( bool active )
// QgsManhattanLineCalloutWidget
//
QgsManhattanLineCalloutWidget::QgsManhattanLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent )
QgsManhattanLineCalloutWidget::QgsManhattanLineCalloutWidget( QgsMapLayer *vl, QWidget *parent )
: QgsSimpleLineCalloutWidget( vl, parent )
{
@ -331,7 +337,7 @@ QgsManhattanLineCalloutWidget::QgsManhattanLineCalloutWidget( QgsVectorLayer *vl
// QgsCurvedLineCalloutWidget
//
QgsCurvedLineCalloutWidget::QgsCurvedLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent )
QgsCurvedLineCalloutWidget::QgsCurvedLineCalloutWidget( QgsMapLayer *vl, QWidget *parent )
: QgsCalloutWidget( parent, vl )
{
setupUi( this );
@ -341,7 +347,7 @@ QgsCurvedLineCalloutWidget::QgsCurvedLineCalloutWidget( QgsVectorLayer *vl, QWid
mCalloutLineStyleButton->setDialogTitle( tr( "Callout Symbol" ) );
mCalloutLineStyleButton->registerExpressionContextGenerator( this );
mCalloutLineStyleButton->setLayer( vl );
mCalloutLineStyleButton->setLayer( qobject_cast< QgsVectorLayer * >( vl ) );
mMinCalloutWidthUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
<< Qgis::RenderUnit::Points << Qgis::RenderUnit::Inches );
mOffsetFromAnchorUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
@ -398,6 +404,8 @@ QgsCurvedLineCalloutWidget::QgsCurvedLineCalloutWidget( QgsVectorLayer *vl, QWid
} );
connect( mCalloutBlendComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsCurvedLineCalloutWidget::mCalloutBlendComboBox_currentIndexChanged );
mPlacementDDGroupBox->setVisible( qobject_cast< QgsVectorLayer * >( vl ) );
}
void QgsCurvedLineCalloutWidget::setCallout( QgsCallout *callout )
@ -547,7 +555,7 @@ void QgsCurvedLineCalloutWidget::drawToAllPartsToggled( bool active )
// QgsBalloonCalloutWidget
//
QgsBalloonCalloutWidget::QgsBalloonCalloutWidget( QgsVectorLayer *vl, QWidget *parent )
QgsBalloonCalloutWidget::QgsBalloonCalloutWidget( QgsMapLayer *vl, QWidget *parent )
: QgsCalloutWidget( parent, vl )
{
setupUi( this );
@ -557,7 +565,7 @@ QgsBalloonCalloutWidget::QgsBalloonCalloutWidget( QgsVectorLayer *vl, QWidget *p
mCalloutFillStyleButton->setDialogTitle( tr( "Balloon Symbol" ) );
mCalloutFillStyleButton->registerExpressionContextGenerator( this );
mCalloutFillStyleButton->setLayer( vl );
mCalloutFillStyleButton->setLayer( qobject_cast< QgsVectorLayer * >( vl ) );
mOffsetFromAnchorUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
<< Qgis::RenderUnit::Points << Qgis::RenderUnit::Inches );
mMarginUnitWidget->setUnits( QgsUnitTypes::RenderUnitList() << Qgis::RenderUnit::Millimeters << Qgis::RenderUnit::MetersInMapUnits << Qgis::RenderUnit::MapUnits << Qgis::RenderUnit::Pixels
@ -645,6 +653,8 @@ QgsBalloonCalloutWidget::QgsBalloonCalloutWidget( QgsVectorLayer *vl, QWidget *p
} );
connect( mCalloutBlendComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsBalloonCalloutWidget::mCalloutBlendComboBox_currentIndexChanged );
mPlacementDDGroupBox->setVisible( qobject_cast< QgsVectorLayer * >( vl ) );
}
void QgsBalloonCalloutWidget::setCallout( QgsCallout *callout )

View File

@ -19,10 +19,10 @@
#include "qgis_sip.h"
#include "qgssymbolwidgetcontext.h"
#include "qgscallout.h"
#include "qgsvectorlayer.h"
#include <QWidget>
#include <QStandardItemModel>
class QgsVectorLayer;
class QgsMapCanvas;
/**
@ -39,12 +39,12 @@ class GUI_EXPORT QgsCalloutWidget : public QWidget, protected QgsExpressionConte
/**
* Constructor for QgsCalloutWidget.
* \param vl associated vector layer
* \param vl associated map layer
* \param parent parent widget
*/
QgsCalloutWidget( QWidget *parent SIP_TRANSFERTHIS, QgsVectorLayer *vl = nullptr )
QgsCalloutWidget( QWidget *parent SIP_TRANSFERTHIS, QgsMapLayer *vl = nullptr )
: QWidget( parent )
, mVectorLayer( vl )
, mLayer( vl )
{}
/**
@ -75,8 +75,17 @@ class GUI_EXPORT QgsCalloutWidget : public QWidget, protected QgsExpressionConte
/**
* Returns the vector layer associated with the widget.
*
* \deprecated QGIS 3.40, use layer() instead.
*/
const QgsVectorLayer *vectorLayer() const { return mVectorLayer; }
Q_DECL_DEPRECATED const QgsVectorLayer *vectorLayer() const SIP_DEPRECATED { return qobject_cast< QgsVectorLayer * >( mLayer ); }
/**
* Returns the vector layer associated with the widget.
*
* \since QGIS 3.40
*/
const QgsMapLayer *layer() const { return mLayer; }
/**
* Sets the geometry \a type of the features to customize the widget accordingly.
@ -95,7 +104,7 @@ class GUI_EXPORT QgsCalloutWidget : public QWidget, protected QgsExpressionConte
QgsExpressionContext createExpressionContext() const override;
private:
QgsVectorLayer *mVectorLayer = nullptr;
QgsMapLayer *mLayer = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
@ -132,9 +141,9 @@ class GUI_EXPORT QgsSimpleLineCalloutWidget : public QgsCalloutWidget, private U
public:
QgsSimpleLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
QgsSimpleLineCalloutWidget( QgsMapLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
static QgsCalloutWidget *create( QgsVectorLayer *vl ) SIP_FACTORY { return new QgsSimpleLineCalloutWidget( vl ); }
static QgsCalloutWidget *create( QgsMapLayer *vl ) SIP_FACTORY { return new QgsSimpleLineCalloutWidget( vl ); }
void setCallout( QgsCallout *callout ) override;
@ -167,9 +176,9 @@ class GUI_EXPORT QgsManhattanLineCalloutWidget : public QgsSimpleLineCalloutWidg
public:
QgsManhattanLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
QgsManhattanLineCalloutWidget( QgsMapLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
static QgsCalloutWidget *create( QgsVectorLayer *vl ) SIP_FACTORY { return new QgsManhattanLineCalloutWidget( vl ); }
static QgsCalloutWidget *create( QgsMapLayer *vl ) SIP_FACTORY { return new QgsManhattanLineCalloutWidget( vl ); }
};
@ -186,9 +195,9 @@ class GUI_EXPORT QgsCurvedLineCalloutWidget : public QgsCalloutWidget, private U
public:
QgsCurvedLineCalloutWidget( QgsVectorLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
QgsCurvedLineCalloutWidget( QgsMapLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
static QgsCalloutWidget *create( QgsVectorLayer *vl ) SIP_FACTORY { return new QgsCurvedLineCalloutWidget( vl ); }
static QgsCalloutWidget *create( QgsMapLayer *vl ) SIP_FACTORY { return new QgsCurvedLineCalloutWidget( vl ); }
void setCallout( QgsCallout *callout ) override;
@ -228,9 +237,9 @@ class GUI_EXPORT QgsBalloonCalloutWidget : public QgsCalloutWidget, private Ui::
public:
QgsBalloonCalloutWidget( QgsVectorLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
QgsBalloonCalloutWidget( QgsMapLayer *vl, QWidget *parent SIP_TRANSFERTHIS = nullptr );
static QgsCalloutWidget *create( QgsVectorLayer *vl ) SIP_FACTORY { return new QgsBalloonCalloutWidget( vl ); }
static QgsCalloutWidget *create( QgsMapLayer *vl ) SIP_FACTORY { return new QgsBalloonCalloutWidget( vl ); }
void setCallout( QgsCallout *callout ) override;