Skip to content

Commit

Permalink
Swap 'set atlas feature' action from compositions to layouts
Browse files Browse the repository at this point in the history
Time to start making a permenant switch...
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 39ae0ee commit a730eb7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
23 changes: 23 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3600,4 +3600,27 @@ QgsMessageBar *QgsLayoutDesignerDialog::messageBar()
return mMessageBar;
}

void QgsLayoutDesignerDialog::setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat )
{
QgsLayoutAtlas *layoutAtlas = atlas();
if ( !layoutAtlas || !layoutAtlas->enabled() || layoutAtlas->coverageLayer() != layer )
{
//either atlas isn't enabled, or layer doesn't match
return;
}

if ( !mActionAtlasPreview->isChecked() )
{
//update gui controls
whileBlocking( mActionAtlasPreview )->setChecked( true );
atlasPreviewTriggered( true );
}

//set current preview feature id
layoutAtlas->seekTo( feat );

//bring layout window to foreground
activate();
}


5 changes: 5 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
*/
QgsMessageBar *messageBar();

/**
* Sets the specified feature as the current atlas feature
*/
void setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat );

public slots:

/**
Expand Down
68 changes: 38 additions & 30 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgslayertreeview.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgslayout.h"
#include "qgslayoutatlas.h"
#include "qgslayoutcustomdrophandler.h"
#include "qgslayoutdesignerdialog.h"
#include "qgslayoutmanager.h"
Expand Down Expand Up @@ -7560,81 +7561,88 @@ void QgisApp::deleteLayoutDesigners()
void QgisApp::setupLayoutManagerConnections()
{
QgsLayoutManager *manager = QgsProject::instance()->layoutManager();
connect( manager, &QgsLayoutManager::compositionAdded, this, [ = ]( const QString & name )
connect( manager, &QgsLayoutManager::layoutAdded, this, [ = ]( const QString & name )
{
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
if ( !c )
QgsMasterLayoutInterface *l = QgsProject::instance()->layoutManager()->layoutByName( name );
if ( !l )
return;
QgsPrintLayout *pl = dynamic_cast< QgsPrintLayout *>( l );
if ( !pl )
return;

mAtlasFeatureActions.insert( c, nullptr );
connect( c, &QgsComposition::nameChanged, this, [this, c]( const QString & name )
mAtlasFeatureActions.insert( pl, nullptr );
connect( pl, &QgsPrintLayout::nameChanged, this, [this, pl]( const QString & name )
{
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
QgsMapLayerAction *action = mAtlasFeatureActions.value( pl );
if ( action )
{
action->setText( QString( tr( "Set as atlas feature for %1" ) ).arg( name ) );
}
} );

connect( &c->atlasComposition(), &QgsAtlasComposition::coverageLayerChanged, this, [this, c]( QgsVectorLayer * coverageLayer )
connect( pl->atlas(), &QgsLayoutAtlas::coverageLayerChanged, this, [this, pl]( QgsVectorLayer * coverageLayer )
{
setupAtlasMapLayerAction( c, static_cast< bool >( coverageLayer ) );
setupAtlasMapLayerAction( pl, static_cast< bool >( coverageLayer ) );
} );

connect( &c->atlasComposition(), &QgsAtlasComposition::toggled, this, [this, c]( bool enabled )
connect( pl->atlas(), &QgsLayoutAtlas::toggled, this, [this, pl]( bool enabled )
{
setupAtlasMapLayerAction( c, enabled );
setupAtlasMapLayerAction( pl, enabled );
} );

setupAtlasMapLayerAction( c, c->atlasComposition().enabled() && c->atlasComposition().coverageLayer() );
setupAtlasMapLayerAction( pl, pl->atlas()->enabled() && pl->atlas()->coverageLayer() );
} );

connect( manager, &QgsLayoutManager::compositionAboutToBeRemoved, this, [ = ]( const QString & name )
connect( manager, &QgsLayoutManager::layoutAboutToBeRemoved, this, [ = ]( const QString & name )
{
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
if ( c )
QgsMasterLayoutInterface *l = QgsProject::instance()->layoutManager()->layoutByName( name );
if ( l )
{
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
if ( action )
QgsPrintLayout *pl = dynamic_cast< QgsPrintLayout * >( l );
if ( pl )
{
QgsGui::mapLayerActionRegistry()->removeMapLayerAction( action );
delete action;
mAtlasFeatureActions.remove( c );
QgsMapLayerAction *action = mAtlasFeatureActions.value( pl );
if ( action )
{
QgsGui::mapLayerActionRegistry()->removeMapLayerAction( action );
delete action;
mAtlasFeatureActions.remove( pl );
}
}
}
} );
}

void QgisApp::setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction )
void QgisApp::setupAtlasMapLayerAction( QgsPrintLayout *layout, bool enableAction )
{
QgsMapLayerAction *action = mAtlasFeatureActions.value( composition );
QgsMapLayerAction *action = mAtlasFeatureActions.value( layout );
if ( action )
{
QgsGui::mapLayerActionRegistry()->removeMapLayerAction( action );
delete action;
action = nullptr;
mAtlasFeatureActions.remove( composition );
mAtlasFeatureActions.remove( layout );
}

if ( enableAction )
{
action = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( composition->name() ),
this, composition->atlasComposition().coverageLayer(), QgsMapLayerAction::SingleFeature,
action = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( layout->name() ),
this, layout->atlas()->coverageLayer(), QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
mAtlasFeatureActions.insert( composition, action );
mAtlasFeatureActions.insert( layout, action );
QgsGui::mapLayerActionRegistry()->addMapLayerAction( action );
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this, composition]( QgsMapLayer * layer, const QgsFeature & feat )
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this, layout]( QgsMapLayer * layer, const QgsFeature & feat )
{
setCompositionAtlasFeature( composition, layer, feat );
setLayoutAtlasFeature( layout, layer, feat );
}
);
}
}

void QgisApp::setCompositionAtlasFeature( QgsComposition *composition, QgsMapLayer *layer, const QgsFeature &feat )
void QgisApp::setLayoutAtlasFeature( QgsPrintLayout *layout, QgsMapLayer *layer, const QgsFeature &feat )
{
QgsComposer *composer = openComposer( composition );
composer->setAtlasFeature( layer, feat );
QgsLayoutDesignerDialog *designer = openLayoutDesignerDialog( layout );
designer->setAtlasFeature( layer, feat );
}

void QgisApp::composerMenuAboutToShow()
Expand Down
7 changes: 4 additions & 3 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class QgsPluginLayer;
class QgsPluginLayer;
class QgsPluginManager;
class QgsPointXY;
class QgsPrintLayout;
class QgsProviderRegistry;
class QgsPythonUtils;
class QgsRasterLayer;
Expand Down Expand Up @@ -1780,9 +1781,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

void setupLayoutManagerConnections();

void setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction );
void setupAtlasMapLayerAction( QgsPrintLayout *layout, bool enableAction );

void setCompositionAtlasFeature( QgsComposition *composition, QgsMapLayer *layer, const QgsFeature &feat );
void setLayoutAtlasFeature( QgsPrintLayout *layout, QgsMapLayer *layer, const QgsFeature &feat );

void saveAsVectorFileGeneral( QgsVectorLayer *vlayer = nullptr, bool symbologyOption = true );

Expand Down Expand Up @@ -2187,7 +2188,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

QStackedWidget *mCentralContainer = nullptr;

QHash< QgsComposition *, QgsMapLayerAction * > mAtlasFeatureActions;
QHash< QgsPrintLayout *, QgsMapLayerAction * > mAtlasFeatureActions;

int mProjOpen = 0;

Expand Down

0 comments on commit a730eb7

Please sign in to comment.