Skip to content

Commit

Permalink
QgsMapLayerActionRegistry is no longer a singleton
Browse files Browse the repository at this point in the history
Moved the global instance to QgsGui::QgsMapLayerActionRegistry()
  • Loading branch information
nyalldawson committed May 14, 2017
1 parent a86b93e commit 436844b
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 57 deletions.
7 changes: 7 additions & 0 deletions doc/api_break.dox
Expand Up @@ -1445,6 +1445,13 @@ screenUpdateRequested() were removed. These members have had no effect for a num
- the invalidTransformInput() slot was removed - calling this slot had no effect
- metadata() was renamed to htmlMetadata()


QgsMapLayerActionRegistry {#qgis_api_break_3_0_QgsMapLayerActionRegistry}
-------------------------

- This class is no longer a singleton and instance() has been removed. Instead use QgsGui::mapLayerActionRegistry().


QgsMapOverviewCanvas {#qgis_api_break_3_0_QgsMapOverviewCanvas}
--------------------

Expand Down
6 changes: 6 additions & 0 deletions python/gui/qgsgui.sip
Expand Up @@ -49,6 +49,12 @@ class QgsGui
:rtype: QgsLayerTreeEmbeddedWidgetRegistry
%End

static QgsMapLayerActionRegistry *mapLayerActionRegistry();
%Docstring
Returns the global map layer action registry, used for registering map layer actions.
:rtype: QgsMapLayerActionRegistry
%End

~QgsGui();

private:
Expand Down
8 changes: 2 additions & 6 deletions python/gui/qgsmaplayeractionregistry.sip
Expand Up @@ -64,8 +64,8 @@ class QgsMapLayerActionRegistry : QObject
%End

public:
//! Returns the instance pointer, creating the object on the first call
static QgsMapLayerActionRegistry *instance();

QgsMapLayerActionRegistry( QObject *parent = 0 );

~QgsMapLayerActionRegistry();

Expand All @@ -83,10 +83,6 @@ class QgsMapLayerActionRegistry : QObject
/** Returns the default action for a layer*/
QgsMapLayerAction *defaultActionForLayer( QgsMapLayer *layer );

protected:
//! protected constructor
QgsMapLayerActionRegistry( QObject *parent = 0 );

signals:
/** Triggered when an action is added or removed from the registry */
void changed();
Expand Down
22 changes: 11 additions & 11 deletions src/app/qgisapp.cpp
Expand Up @@ -942,7 +942,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh

activateDeactivateLayerRelatedActions( nullptr ); // after members were created

connect( QgsMapLayerActionRegistry::instance(), &QgsMapLayerActionRegistry::changed, this, &QgisApp::refreshActionFeatureAction );
connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgisApp::refreshActionFeatureAction );

// set application's caption
QString caption = tr( "QGIS - %1 ('%2')" ).arg( Qgis::QGIS_VERSION, Qgis::QGIS_RELEASE_NAME );
Expand Down Expand Up @@ -6288,7 +6288,7 @@ void QgisApp::updateDefaultFeatureAction( QAction *action )
if ( qgsAction.isValid() )
{
vlayer->actions()->setDefaultAction( QStringLiteral( "Canvas" ), qgsAction.id() );
QgsMapLayerActionRegistry::instance()->setDefaultActionForLayer( vlayer, nullptr );
QgsGui::mapLayerActionRegistry()->setDefaultActionForLayer( vlayer, nullptr );

mActionFeatureAction->setToolTip( tr( "Run feature action<br><b>%1</b>" ).arg( qgsAction.name() ) );

Expand All @@ -6303,7 +6303,7 @@ void QgisApp::updateDefaultFeatureAction( QAction *action )
QgsMapLayerAction *mapLayerAction = qobject_cast<QgsMapLayerAction *>( action );
if ( mapLayerAction )
{
QgsMapLayerActionRegistry::instance()->setDefaultActionForLayer( vlayer, mapLayerAction );
QgsGui::mapLayerActionRegistry()->setDefaultActionForLayer( vlayer, mapLayerAction );

if ( !mapLayerAction->text().isEmpty() )
mActionFeatureAction->setToolTip( tr( "Run feature action<br><b>%1</b>" ).arg( mapLayerAction->text() ) );
Expand All @@ -6313,7 +6313,7 @@ void QgisApp::updateDefaultFeatureAction( QAction *action )
}
else
{
QgsMapLayerActionRegistry::instance()->setDefaultActionForLayer( vlayer, nullptr );
QgsGui::mapLayerActionRegistry()->setDefaultActionForLayer( vlayer, nullptr );
}
}
}
Expand All @@ -6340,7 +6340,7 @@ void QgisApp::refreshFeatureActions()
}

//add actions registered in QgsMapLayerActionRegistry
QList<QgsMapLayerAction *> registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer );
QList<QgsMapLayerAction *> registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer );
if ( !actions.isEmpty() && registeredActions.size() > 0 )
{
//add a separator between user defined and standard actions
Expand All @@ -6350,7 +6350,7 @@ void QgisApp::refreshFeatureActions()
for ( int i = 0; i < registeredActions.size(); i++ )
{
mFeatureActionMenu->addAction( registeredActions.at( i ) );
if ( registeredActions.at( i ) == QgsMapLayerActionRegistry::instance()->defaultActionForLayer( vlayer ) )
if ( registeredActions.at( i ) == QgsGui::mapLayerActionRegistry()->defaultActionForLayer( vlayer ) )
{
mFeatureActionMenu->setActiveAction( registeredActions.at( i ) );
}
Expand Down Expand Up @@ -7296,7 +7296,7 @@ void QgisApp::setupLayoutManagerConnections()
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
if ( action )
{
QgsMapLayerActionRegistry::instance()->removeMapLayerAction( action );
QgsGui::mapLayerActionRegistry()->removeMapLayerAction( action );
delete action;
mAtlasFeatureActions.remove( c );
}
Expand All @@ -7309,7 +7309,7 @@ void QgisApp::setupAtlasMapLayerAction( QgsComposition *composition, bool enable
QgsMapLayerAction *action = mAtlasFeatureActions.value( composition );
if ( action )
{
QgsMapLayerActionRegistry::instance()->removeMapLayerAction( action );
QgsGui::mapLayerActionRegistry()->removeMapLayerAction( action );
delete action;
action = nullptr;
mAtlasFeatureActions.remove( composition );
Expand All @@ -7321,7 +7321,7 @@ void QgisApp::setupAtlasMapLayerAction( QgsComposition *composition, bool enable
this, composition->atlasComposition().coverageLayer(), QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
mAtlasFeatureActions.insert( composition, action );
QgsMapLayerActionRegistry::instance()->addMapLayerAction( action );
QgsGui::mapLayerActionRegistry()->addMapLayerAction( action );
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this, composition]( QgsMapLayer * layer, const QgsFeature & feat )
{
setCompositionAtlasFeature( composition, layer, feat );
Expand Down Expand Up @@ -11160,7 +11160,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )

bool isEditable = vlayer->isEditable();
bool layerHasSelection = vlayer->selectedFeatureCount() > 0;
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer ).isEmpty();
bool isSpatial = vlayer->isSpatial();

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

bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer ).isEmpty();
mActionFeatureAction->setEnabled( layerHasActions );
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -483,7 +483,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
}

//get valid QgsMapLayerActions for this layer
QList< QgsMapLayerAction * > registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer );
QList< QgsMapLayerAction * > registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer );
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Feature" ) );

if ( !vlayer->fields().isEmpty() || !actions.isEmpty() || !registeredActions.isEmpty() )
Expand Down Expand Up @@ -1096,7 +1096,7 @@ void QgsIdentifyResultsDialog::contextMenuEvent( QContextMenuEvent *event )
if ( featItem && vlayer )
{
//get valid QgsMapLayerActions for this layer
QList< QgsMapLayerAction * > registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer );
QList< QgsMapLayerAction * > registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer );

if ( !registeredActions.isEmpty() )
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/qgsmaptoolfeatureaction.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgsproject.h"
#include "qgsmaplayeractionregistry.h"
#include "qgisapp.h"
#include "qgsgui.h"

#include <QSettings>
#include <QMouseEvent>
Expand Down Expand Up @@ -72,7 +73,7 @@ void QgsMapToolFeatureAction::canvasReleaseEvent( QgsMapMouseEvent *e )
}

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() && QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty() )
if ( vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() && QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer ).isEmpty() )
{
emit messageEmitted( tr( "The active vector layer has no defined actions" ), QgsMessageBar::INFO );
return;
Expand Down Expand Up @@ -146,7 +147,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
}
else
{
QgsMapLayerAction *mapLayerAction = QgsMapLayerActionRegistry::instance()->defaultActionForLayer( layer );
QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry()->defaultActionForLayer( layer );
if ( mapLayerAction )
{
mapLayerAction->triggerForFeature( layer, &feat );
Expand Down
7 changes: 4 additions & 3 deletions src/gui/attributetable/qgsattributetableview.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgsfeatureselectionmodel.h"
#include "qgsmaplayeractionregistry.h"
#include "qgsfeatureiterator.h"
#include "qgsgui.h"

QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
: QTableView( parent )
Expand Down Expand Up @@ -67,7 +68,7 @@ QgsAttributeTableView::QgsAttributeTableView( QWidget *parent )
connect( verticalHeader(), &QHeaderView::sectionEntered, this, &QgsAttributeTableView::_q_selectRow );
connect( horizontalHeader(), &QHeaderView::sectionResized, this, &QgsAttributeTableView::columnSizeChanged );
connect( horizontalHeader(), &QHeaderView::sortIndicatorChanged, this, &QgsAttributeTableView::showHorizontalSortIndicator );
connect( QgsMapLayerActionRegistry::instance(), &QgsMapLayerActionRegistry::changed, this, &QgsAttributeTableView::recreateActionWidgets );
connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsAttributeTableView::recreateActionWidgets );
}

bool QgsAttributeTableView::eventFilter( QObject *object, QEvent *event )
Expand Down Expand Up @@ -196,7 +197,7 @@ QWidget *QgsAttributeTableView::createActionWidget( QgsFeatureId fid )

// next add any registered actions for this layer
Q_FOREACH ( QgsMapLayerAction *mapLayerAction,
QgsMapLayerActionRegistry::instance()->mapLayerActions( mFilterModel->layer(),
QgsGui::mapLayerActionRegistry()->mapLayerActions( mFilterModel->layer(),
QgsMapLayerAction::SingleFeature ) )
{
QAction *action = new QAction( mapLayerAction->icon(), mapLayerAction->text(), container );
Expand All @@ -208,7 +209,7 @@ QWidget *QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
actionList << action;

if ( !defaultAction &&
QgsMapLayerActionRegistry::instance()->defaultActionForLayer( mFilterModel->layer() ) == mapLayerAction )
QgsGui::mapLayerActionRegistry()->defaultActionForLayer( mFilterModel->layer() ) == mapLayerAction )
defaultAction = action;
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -479,7 +479,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
}

//add actions from QgsMapLayerActionRegistry to context menu
QList<QgsMapLayerAction *> registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer );
QList<QgsMapLayerAction *> registeredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( mLayer );
if ( !registeredActions.isEmpty() )
{
//add a separator between user defined and standard actions
Expand Down
5 changes: 3 additions & 2 deletions src/gui/qgsactionmenu.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsmaplayeractionregistry.h"
#include "qgsactionmanager.h"
#include "qgsfeatureiterator.h"
#include "qgsgui.h"

QgsActionMenu::QgsActionMenu( QgsVectorLayer *layer, const QgsFeature &feature, const QString &actionScope, QWidget *parent )
: QMenu( parent )
Expand All @@ -42,7 +43,7 @@ void QgsActionMenu::init()
{
setTitle( tr( "&Actions" ) );

connect( QgsMapLayerActionRegistry::instance(), &QgsMapLayerActionRegistry::changed, this, &QgsActionMenu::reloadActions );
connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsActionMenu::reloadActions );

reloadActions();
}
Expand Down Expand Up @@ -123,7 +124,7 @@ void QgsActionMenu::reloadActions()
addAction( qAction );
}

QList<QgsMapLayerAction *> mapLayerActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer, QgsMapLayerAction::SingleFeature );
QList<QgsMapLayerAction *> mapLayerActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( mLayer, QgsMapLayerAction::SingleFeature );

if ( !mapLayerActions.isEmpty() )
{
Expand Down
10 changes: 9 additions & 1 deletion src/gui/qgsgui.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsgui.h"
#include "qgseditorwidgetregistry.h"
#include "qgslayertreeembeddedwidgetregistry.h"
#include "qgsmaplayeractionregistry.h"
#include "qgsshortcutsmanager.h"

QgsGui *QgsGui::instance()
Expand All @@ -41,16 +42,23 @@ QgsLayerTreeEmbeddedWidgetRegistry *QgsGui::layerTreeEmbeddedWidgetRegistry()
return instance()->mLayerTreeEmbeddedWidgetRegistry;
}

QgsMapLayerActionRegistry *QgsGui::mapLayerActionRegistry()
{
return instance()->mMapLayerActionRegistry;
}

QgsGui::~QgsGui()
{
delete mLayerTreeEmbeddedWidgetRegistry;
delete mEditorWidgetRegistry;
delete mMapLayerActionRegistry;
delete mShortcutsManager;
delete mLayerTreeEmbeddedWidgetRegistry;
}

QgsGui::QgsGui()
{
mEditorWidgetRegistry = new QgsEditorWidgetRegistry();
mShortcutsManager = new QgsShortcutsManager();
mLayerTreeEmbeddedWidgetRegistry = new QgsLayerTreeEmbeddedWidgetRegistry();
mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
}
7 changes: 7 additions & 0 deletions src/gui/qgsgui.h
Expand Up @@ -23,6 +23,7 @@
class QgsEditorWidgetRegistry;
class QgsShortcutsManager;
class QgsLayerTreeEmbeddedWidgetRegistry;
class QgsMapLayerActionRegistry;

/**
* \ingroup gui
Expand Down Expand Up @@ -60,6 +61,11 @@ class GUI_EXPORT QgsGui
*/
static QgsLayerTreeEmbeddedWidgetRegistry *layerTreeEmbeddedWidgetRegistry();

/**
* Returns the global map layer action registry, used for registering map layer actions.
*/
static QgsMapLayerActionRegistry *mapLayerActionRegistry();

~QgsGui();

private:
Expand All @@ -69,6 +75,7 @@ class GUI_EXPORT QgsGui
QgsEditorWidgetRegistry *mEditorWidgetRegistry = nullptr;
QgsShortcutsManager *mShortcutsManager = nullptr;
QgsLayerTreeEmbeddedWidgetRegistry *mLayerTreeEmbeddedWidgetRegistry = nullptr;
QgsMapLayerActionRegistry *mMapLayerActionRegistry = nullptr;

#ifdef SIP_RUN
QgsGui( const QgsGui &other );
Expand Down
11 changes: 5 additions & 6 deletions src/gui/qgsidentifymenu.cpp
Expand Up @@ -24,11 +24,11 @@
#include "qgsvectorlayer.h"
#include "qgslogger.h"
#include "qgssettings.h"

#include "qgsgui.h"

/// @cond PRIVATE
CustomActionRegistry::CustomActionRegistry( QObject *parent )
: QgsMapLayerActionRegistry( parent )
CustomActionRegistry::CustomActionRegistry()
: QgsMapLayerActionRegistry()
{
}
///@endcond
Expand All @@ -43,7 +43,6 @@ QgsIdentifyMenu::QgsIdentifyMenu( QgsMapCanvas *canvas )
, mMaxLayerDisplay( 10 )
, mMaxFeatureDisplay( 10 )
, mDefaultActionName( tr( "Identify" ) )
, mCustomActionRegistry( CustomActionRegistry::instance() )
{
}

Expand Down Expand Up @@ -178,7 +177,7 @@ void QgsIdentifyMenu::addRasterLayer( QgsMapLayer *layer )
}
if ( mShowFeatureActions )
{
layerActions.append( QgsMapLayerActionRegistry::instance()->mapLayerActions( layer, QgsMapLayerAction::Layer ) );
layerActions.append( QgsGui::mapLayerActionRegistry()->mapLayerActions( layer, QgsMapLayerAction::Layer ) );
if ( layerActions.count() > nCustomActions )
{
separators.append( layerActions[nCustomActions] );
Expand Down Expand Up @@ -245,7 +244,7 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapT
}
if ( mShowFeatureActions )
{
layerActions << QgsMapLayerActionRegistry::instance()->mapLayerActions( layer, targets );
layerActions << QgsGui::mapLayerActionRegistry()->mapLayerActions( layer, targets );

if ( layerActions.count() > nCustomActions )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsidentifymenu.h
Expand Up @@ -30,7 +30,7 @@ class CustomActionRegistry : public QgsMapLayerActionRegistry
Q_OBJECT

public:
explicit CustomActionRegistry( QObject *parent );
CustomActionRegistry();
// remove all actions
void clear() { mMapLayerActionList.clear(); }
};
Expand Down

0 comments on commit 436844b

Please sign in to comment.