Skip to content

Commit

Permalink
Add message bar to QgsMapLayerActionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2022
1 parent 4afcddf commit c15b960
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 12 deletions.
16 changes: 16 additions & 0 deletions python/gui/auto_generated/qgsmaplayeractionregistry.sip.in
Expand Up @@ -39,6 +39,22 @@ May be ``None`` if the action is not being executed from an attribute dialog.
Sets the attribute ``dialog`` associated with the action's execution.

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

QgsMessageBar *messageBar() const;
%Docstring
Returns the message bar associated with the action's execution.

May be ``None``.

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

void setMessageBar( QgsMessageBar *bar );
%Docstring
Sets the message ``bar`` associated with the action's execution.

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

};
Expand Down
16 changes: 10 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -7481,8 +7481,7 @@ void QgisApp::refreshFeatureActions()
}

//add actions registered in QgsMapLayerActionRegistry
QgsMapLayerActionContext context;
QList<QgsMapLayerAction *> registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::SingleFeature, context );
QList<QgsMapLayerAction *> registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::SingleFeature, createMapLayerActionContext() );
if ( !actions.isEmpty() && !registeredActions.empty() )
{
//add a separator between user defined and standard actions
Expand Down Expand Up @@ -14895,8 +14894,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )

bool isEditable = vlayer->isEditable();
bool layerHasSelection = vlayer->selectedFeatureCount() > 0;
QgsMapLayerActionContext context;
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, context ).isEmpty();
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, createMapLayerActionContext() ).isEmpty();
bool isSpatial = vlayer->isSpatial();

mActionLocalHistogramStretch->setEnabled( false );
Expand Down Expand Up @@ -15528,8 +15526,7 @@ void QgisApp::refreshActionFeatureAction()
if ( !vlayer )
return;

QgsMapLayerActionContext context;
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, context ).isEmpty();
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, createMapLayerActionContext() ).isEmpty();
mActionFeatureAction->setEnabled( layerHasActions );
}

Expand Down Expand Up @@ -15768,6 +15765,13 @@ QgsMapToolIdentifyAction *QgisApp::identifyMapTool() const
return mMapTools->mapTool< QgsMapToolIdentifyAction >( QgsAppMapTools::Identify );
}

QgsMapLayerActionContext QgisApp::createMapLayerActionContext()
{
QgsMapLayerActionContext context;
context.setMessageBar( messageBar() );
return context;
}

void QgisApp::takeAppScreenShots( const QString &saveDirectory, const int categories )
{
QgsAppScreenShots ass( saveDirectory );
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -112,6 +112,7 @@ class QgsAppMapTools;
class QgsMapToolIdentifyAction;
class Qgs3DMapCanvasWidget;
class QgsVertexEditor;
class QgsMapLayerActionContext;

class QDomDocument;
class QNetworkReply;
Expand Down Expand Up @@ -869,6 +870,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Returns pointer to the identify map tool - used by identify tool in 3D view
QgsMapToolIdentifyAction *identifyMapTool() const;

QgsMapLayerActionContext createMapLayerActionContext();

/**
* Take screenshots for user documentation
* @param saveDirectory path were the screenshots will be saved
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -643,7 +643,7 @@ QgsIdentifyResultsFeatureItem *QgsIdentifyResultsDialog::createFeatureItem( QgsV
}

//get valid QgsMapLayerActions for this layer
QgsMapLayerActionContext context;
QgsMapLayerActionContext context = QgisApp::instance()->createMapLayerActionContext();
const QList< QgsMapLayerAction * > registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, context );
const QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Feature" ) );

Expand Down Expand Up @@ -1583,7 +1583,7 @@ void QgsIdentifyResultsDialog::contextMenuEvent( QContextMenuEvent *event )
if ( featItem && featItem->data( 0, Qt::UserRole + 1 ).isValid() && vlayer )
{
//get valid QgsMapLayerActions for this layer
QgsMapLayerActionContext context;
QgsMapLayerActionContext context = QgisApp::instance()->createMapLayerActionContext();
const QList< QgsMapLayerAction * > registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, QgsMapLayerAction::AllActions, context );

if ( !registeredActions.isEmpty() )
Expand Down Expand Up @@ -1751,7 +1751,7 @@ void QgsIdentifyResultsDialog::doMapLayerAction( QTreeWidgetItem *item, QgsMapLa
return;

const QgsFeature feat = featItem->data( 0, FeatureRole ).value< QgsFeature >();
QgsMapLayerActionContext context;
QgsMapLayerActionContext context = QgisApp::instance()->createMapLayerActionContext();
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeature( layer, feat );
Q_NOWARN_DEPRECATED_POP
Expand Down Expand Up @@ -2537,7 +2537,7 @@ void QgsIdentifyResultsDialog::formatChanged( int index )

void QgsIdentifyResultsDialogMapLayerAction::execute()
{
QgsMapLayerActionContext context;
QgsMapLayerActionContext context = QgisApp::instance()->createMapLayerActionContext();
Q_NOWARN_DEPRECATED_PUSH
mAction->triggerForFeature( mLayer, *mFeature );
Q_NOWARN_DEPRECATED_POP
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsattributedialog.cpp
Expand Up @@ -174,6 +174,7 @@ QgsMapLayerActionContext QgsAttributeDialog::createActionContext()
{
QgsMapLayerActionContext context;
context.setAttributeDialog( this );
context.setMessageBar( mMessageBar );
return context;
}

14 changes: 14 additions & 0 deletions src/gui/qgsmaplayeractionregistry.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgsgui.h"
#include "qgsvectorlayer.h"
#include "qgsattributedialog.h"
#include "qgsmessagebar.h"

QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
: QAction( icon, name, parent )
Expand Down Expand Up @@ -224,4 +225,17 @@ void QgsMapLayerActionContext::setAttributeDialog( QgsAttributeDialog *dialog )
mAttributeDialog = dialog;
}

QgsMessageBar *QgsMapLayerActionContext::messageBar() const
{
return mMessageBar;
}

void QgsMapLayerActionContext::setMessageBar( QgsMessageBar *bar )
{
mMessageBar = bar;
}

//
// QgsMapLayerActionContextGenerator
//
QgsMapLayerActionContextGenerator::~QgsMapLayerActionContextGenerator() = default;
20 changes: 18 additions & 2 deletions src/gui/qgsmaplayeractionregistry.h
Expand Up @@ -29,6 +29,7 @@
class QgsFeature;
class QgsMapLayer;
class QgsAttributeDialog;
class QgsMessageBar;

/**
* \ingroup gui
Expand Down Expand Up @@ -57,11 +58,26 @@ class GUI_EXPORT QgsMapLayerActionContext
*/
void setAttributeDialog( QgsAttributeDialog *dialog );

private:
/**
* Returns the message bar associated with the action's execution.
*
* May be NULLPTR.
*
* \see setMessageBar()
*/
QgsMessageBar *messageBar() const;

QPointer< QgsAttributeDialog > mAttributeDialog;
/**
* Sets the message \a bar associated with the action's execution.
*
* \see messageBar()
*/
void setMessageBar( QgsMessageBar *bar );

private:

QPointer< QgsAttributeDialog > mAttributeDialog;
QPointer< QgsMessageBar > mMessageBar;
};

Q_DECLARE_METATYPE( QgsMapLayerActionContext )
Expand Down

0 comments on commit c15b960

Please sign in to comment.