Skip to content
Permalink
Browse files
Always refetch the current item from the layer and then update it
when we are editing annotation items, as properties of the item
which aren't exposed in the widget (e.g. it's geometry) may
have changed in the meantime and we don't want to lose those.
  • Loading branch information
nyalldawson committed Sep 7, 2021
1 parent 8e18629 commit 4f1fc3b1a41785ce833b4c31d6f1ac40d4be4739
@@ -32,6 +32,11 @@ Constructor for QgsAnnotationItemBaseWidget, linked with the specified annotatio
virtual QgsAnnotationItem *createItem() = 0 /Factory/;
%Docstring
Creates a new item matching the settings defined in the widget.
%End

virtual void updateItem( QgsAnnotationItem *item ) = 0;
%Docstring
Updates an existing item to match the settings defined in the widget.
%End

bool setItem( QgsAnnotationItem *item );
@@ -95,10 +95,16 @@ void QgsAnnotationItemPropertiesWidget::onChanged()
if ( !mLayer )
return;

// set the annotation layer's item's properties to match the widget
std::unique_ptr< QgsAnnotationItem > newItem( mItemWidget->createItem() );
// we refetch the item from the layer and update it, as the item's geometry (or some other property)
// may have changed and we always want to use the current properties

mLayer->replaceItem( mMapLayerConfigWidgetContext.annotationId(), newItem.release() );
if ( QgsAnnotationItem *existingItem = mLayer->item( mMapLayerConfigWidgetContext.annotationId() ) )
{
std::unique_ptr< QgsAnnotationItem > newItem( existingItem->clone() );
mItemWidget->updateItem( newItem.get() );

mLayer->replaceItem( mMapLayerConfigWidgetContext.annotationId(), newItem.release() );
}
}

void QgsAnnotationItemPropertiesWidget::setItemId( const QString &itemId )
@@ -47,6 +47,11 @@ class GUI_EXPORT QgsAnnotationItemBaseWidget: public QgsPanelWidget
*/
virtual QgsAnnotationItem *createItem() = 0 SIP_FACTORY;

/**
* Updates an existing item to match the settings defined in the widget.
*/
virtual void updateItem( QgsAnnotationItem *item ) = 0;

/**
* Sets the current \a item to show in the widget. If TRUE is returned, \a item
* was an acceptable type for display in this widget and the widget has been
@@ -52,6 +52,12 @@ QgsAnnotationItem *QgsAnnotationPolygonItemWidget::createItem()
return newItem;
}

void QgsAnnotationPolygonItemWidget::updateItem( QgsAnnotationItem *item )
{
if ( QgsAnnotationPolygonItem *polygonItem = dynamic_cast< QgsAnnotationPolygonItem * >( item ) )
polygonItem->setSymbol( mSymbol->clone() );
}

void QgsAnnotationPolygonItemWidget::setDockMode( bool dockMode )
{
QgsAnnotationItemBaseWidget::setDockMode( dockMode );
@@ -114,6 +120,12 @@ QgsAnnotationItem *QgsAnnotationLineItemWidget::createItem()
return newItem;
}

void QgsAnnotationLineItemWidget::updateItem( QgsAnnotationItem *item )
{
if ( QgsAnnotationLineItem *lineItem = dynamic_cast< QgsAnnotationLineItem * >( item ) )
lineItem->setSymbol( mSymbol->clone() );
}

void QgsAnnotationLineItemWidget::setDockMode( bool dockMode )
{
QgsAnnotationItemBaseWidget::setDockMode( dockMode );
@@ -176,6 +188,12 @@ QgsAnnotationItem *QgsAnnotationMarkerItemWidget::createItem()
return newItem;
}

void QgsAnnotationMarkerItemWidget::updateItem( QgsAnnotationItem *item )
{
if ( QgsAnnotationMarkerItem *markerItem = dynamic_cast< QgsAnnotationMarkerItem * >( item ) )
markerItem->setSymbol( mSymbol->clone() );
}

void QgsAnnotationMarkerItemWidget::setDockMode( bool dockMode )
{
QgsAnnotationItemBaseWidget::setDockMode( dockMode );
@@ -249,6 +267,15 @@ QgsAnnotationItem *QgsAnnotationPointTextItemWidget::createItem()
return newItem;
}

void QgsAnnotationPointTextItemWidget::updateItem( QgsAnnotationItem *item )
{
if ( QgsAnnotationPointTextItem *pointTextItem = dynamic_cast< QgsAnnotationPointTextItem * >( item ) )
{
pointTextItem->setFormat( mTextFormatWidget->format() );
pointTextItem->setText( mTextEdit->toPlainText() );
}
}

void QgsAnnotationPointTextItemWidget::setDockMode( bool dockMode )
{
QgsAnnotationItemBaseWidget::setDockMode( dockMode );
@@ -45,6 +45,7 @@ class QgsAnnotationPolygonItemWidget : public QgsAnnotationItemBaseWidget
QgsAnnotationPolygonItemWidget( QWidget *parent );
~QgsAnnotationPolygonItemWidget() override;
QgsAnnotationItem *createItem() override;
void updateItem( QgsAnnotationItem *item ) override;
void setDockMode( bool dockMode ) override;

protected:
@@ -66,6 +67,7 @@ class QgsAnnotationLineItemWidget : public QgsAnnotationItemBaseWidget
QgsAnnotationLineItemWidget( QWidget *parent );
~QgsAnnotationLineItemWidget() override;
QgsAnnotationItem *createItem() override;
void updateItem( QgsAnnotationItem *item ) override;
void setDockMode( bool dockMode ) override;

protected:
@@ -87,6 +89,7 @@ class QgsAnnotationMarkerItemWidget : public QgsAnnotationItemBaseWidget
QgsAnnotationMarkerItemWidget( QWidget *parent );
~QgsAnnotationMarkerItemWidget() override;
QgsAnnotationItem *createItem() override;
void updateItem( QgsAnnotationItem *item ) override;
void setDockMode( bool dockMode ) override;

protected:
@@ -109,6 +112,7 @@ class QgsAnnotationPointTextItemWidget : public QgsAnnotationItemBaseWidget, pri
QgsAnnotationPointTextItemWidget( QWidget *parent );
~QgsAnnotationPointTextItemWidget() override;
QgsAnnotationItem *createItem() override;
void updateItem( QgsAnnotationItem *item ) override;
void setDockMode( bool dockMode ) override;

protected:
@@ -80,6 +80,7 @@ class TestItemWidget: public QgsAnnotationItemBaseWidget
{}

QgsAnnotationItem *createItem() override { return nullptr; }
void updateItem( QgsAnnotationItem * ) override {}

};

0 comments on commit 4f1fc3b

Please sign in to comment.