Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Automatically open annotation properties and focus an appropriate
widget after creating a new annotation
  • Loading branch information
nyalldawson committed Sep 8, 2021
1 parent 303cb45 commit 24089aa
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 8 deletions.
Expand Up @@ -61,6 +61,13 @@ Sets the ``context`` in which the widget is shown, e.g., the associated map canv
Returns the context in which the widget is shown, e.g., the associated map canvas and expression contexts.

.. seealso:: :py:func:`setContext`
%End

public slots:

virtual void focusDefaultWidget();
%Docstring
Focuses the default widget for the page.
%End

signals:
Expand Down
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsmaplayerconfigwidget.sip.in
Expand Up @@ -127,6 +127,13 @@ Subclasses should take care to call the base class implementation when overridin
%Docstring
Called when changes to the layer need to be made.
Will be called when live update is enabled.
%End

virtual void focusDefaultWidget();
%Docstring
Focuses the default widget for the page.

.. versionadded:: 3.22
%End

signals:
Expand Down
6 changes: 6 additions & 0 deletions src/app/annotations/qgsannotationitempropertieswidget.cpp
Expand Up @@ -98,6 +98,12 @@ void QgsAnnotationItemPropertiesWidget::apply()
mLayer->triggerRepaint();
}

void QgsAnnotationItemPropertiesWidget::focusDefaultWidget()
{
if ( mItemWidget )
mItemWidget->focusDefaultWidget();
}

void QgsAnnotationItemPropertiesWidget::onChanged()
{
if ( !mLayer )
Expand Down
1 change: 1 addition & 0 deletions src/app/annotations/qgsannotationitempropertieswidget.h
Expand Up @@ -37,6 +37,7 @@ class QgsAnnotationItemPropertiesWidget : public QgsMapLayerConfigWidget

public slots:
void apply() override;
void focusDefaultWidget() override;

private slots:

Expand Down
18 changes: 10 additions & 8 deletions src/app/qgisapp.cpp
Expand Up @@ -810,14 +810,16 @@ void QgisApp::annotationItemTypeAdded( int id )
connect( tool, &QgsCreateAnnotationItemMapTool::itemCreated, this, [ = ]
{
QgsAnnotationItem *item = tool->takeCreatedItem();
if ( QgsAnnotationLayer *layer = qobject_cast< QgsAnnotationLayer * >( activeLayer() ) )
{
layer->addItem( item );
}
else
{
QgsProject::instance()->mainAnnotationLayer()->addItem( item );
}
QgsAnnotationLayer *targetLayer = qobject_cast< QgsAnnotationLayer * >( activeLayer() );
if ( !targetLayer )
targetLayer = QgsProject::instance()->mainAnnotationLayer();

const QString itemId = targetLayer->addItem( item );
// automatically select item in layer styling panel
mMapStyleWidget->setAnnotationItem( targetLayer, itemId );
mMapStylingDock->setUserVisible( true );
mMapStyleWidget->focusDefaultWidget();

// TODO -- possibly automatically deactive the tool now?
} );
} );
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgslayerstylingwidget.cpp
Expand Up @@ -712,6 +712,14 @@ void QgsLayerStylingWidget::setAnnotationItem( QgsAnnotationLayer *layer, const
}
}

void QgsLayerStylingWidget::focusDefaultWidget()
{
if ( QgsMapLayerConfigWidget *configWidget = qobject_cast< QgsMapLayerConfigWidget * >( mWidgetStack->mainPanel() ) )
{
configWidget->focusDefaultWidget();
}
}

void QgsLayerStylingWidget::layerAboutToBeRemoved( QgsMapLayer *layer )
{
if ( layer == mCurrentLayer )
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgslayerstylingwidget.h
Expand Up @@ -135,6 +135,11 @@ class APP_EXPORT QgsLayerStylingWidget : public QWidget, private Ui::QgsLayerSty
*/
void setAnnotationItem( QgsAnnotationLayer *layer, const QString &itemId );

/**
* Focuses the default widget for the current page.
*/
void focusDefaultWidget();

private slots:

void layerAboutToBeRemoved( QgsMapLayer *layer );
Expand Down
4 changes: 4 additions & 0 deletions src/gui/annotations/qgsannotationitemwidget.cpp
Expand Up @@ -36,6 +36,10 @@ QgsSymbolWidgetContext QgsAnnotationItemBaseWidget::context() const
return mContext;
}

void QgsAnnotationItemBaseWidget::focusDefaultWidget()
{
}

bool QgsAnnotationItemBaseWidget::setNewItem( QgsAnnotationItem * )
{
return false;
Expand Down
7 changes: 7 additions & 0 deletions src/gui/annotations/qgsannotationitemwidget.h
Expand Up @@ -75,6 +75,13 @@ class GUI_EXPORT QgsAnnotationItemBaseWidget: public QgsPanelWidget
*/
QgsSymbolWidgetContext context() const;

public slots:

/**
* Focuses the default widget for the page.
*/
virtual void focusDefaultWidget();

signals:

/**
Expand Down
6 changes: 6 additions & 0 deletions src/gui/annotations/qgsannotationitemwidget_impl.cpp
Expand Up @@ -360,6 +360,12 @@ void QgsAnnotationPointTextItemWidget::setContext( const QgsSymbolWidgetContext
mPropertiesWidget->setContext( context );
}

void QgsAnnotationPointTextItemWidget::focusDefaultWidget()
{
mTextEdit->selectAll();
mTextEdit->setFocus();
}

QgsAnnotationPointTextItemWidget::~QgsAnnotationPointTextItemWidget() = default;

bool QgsAnnotationPointTextItemWidget::setNewItem( QgsAnnotationItem *item )
Expand Down
4 changes: 4 additions & 0 deletions src/gui/annotations/qgsannotationitemwidget_impl.h
Expand Up @@ -120,6 +120,10 @@ class QgsAnnotationPointTextItemWidget : public QgsAnnotationItemBaseWidget, pri
void setDockMode( bool dockMode ) override;
void setContext( const QgsSymbolWidgetContext &context ) override;

public slots:

void focusDefaultWidget() override;

protected:
bool setNewItem( QgsAnnotationItem *item ) override;

Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsmaplayerconfigwidget.cpp
Expand Up @@ -27,3 +27,7 @@ void QgsMapLayerConfigWidget::setMapLayerConfigWidgetContext( const QgsMapLayerC
{
mMapLayerConfigWidgetContext = context;
}

void QgsMapLayerConfigWidget::focusDefaultWidget()
{
}
7 changes: 7 additions & 0 deletions src/gui/qgsmaplayerconfigwidget.h
Expand Up @@ -137,6 +137,13 @@ class GUI_EXPORT QgsMapLayerConfigWidget : public QgsPanelWidget
*/
virtual void apply() = 0;

/**
* Focuses the default widget for the page.
*
* \since QGIS 3.22
*/
virtual void focusDefaultWidget();

signals:

#ifndef SIP_RUN
Expand Down

0 comments on commit 24089aa

Please sign in to comment.