Skip to content

Commit dbf0160

Browse files
committed
Rework atlas layer action to handle temporary composer windows
1 parent 2e94f7d commit dbf0160

File tree

5 files changed

+101
-73
lines changed

5 files changed

+101
-73
lines changed

src/app/composer/qgscomposer.cpp

+3-54
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ QgsComposer::QgsComposer( QgsComposition *composition )
109109
, mQgis( QgisApp::instance() )
110110
{
111111
setupUi( this );
112-
setTitle( mComposition->name() );
112+
setWindowTitle( mComposition->name() );
113113
setAttribute( Qt::WA_DeleteOnClose );
114114
#if QT_VERSION >= 0x050600
115115
setDockOptions( dockOptions() | QMainWindow::GroupedDragging ) ;
@@ -647,9 +647,9 @@ QgsComposer::QgsComposer( QgsComposition *composition )
647647
mActionExportAtlasAsPDF->setEnabled( false );
648648
QgsAtlasComposition *atlasMap = &mComposition->atlasComposition();
649649
connect( atlasMap, &QgsAtlasComposition::toggled, this, &QgsComposer::toggleAtlasControls );
650-
connect( atlasMap, &QgsAtlasComposition::coverageLayerChanged, this, [ = ]( QgsVectorLayer * layer ) { updateAtlasMapLayerAction( layer ); } );
651650
connect( atlasMap, &QgsAtlasComposition::numberFeaturesChanged, this, &QgsComposer::updateAtlasPageComboBox );
652651
connect( atlasMap, &QgsAtlasComposition::featureChanged, this, &QgsComposer::atlasFeatureChanged );
652+
toggleAtlasControls( atlasMap->enabled() && atlasMap->coverageLayer() );
653653

654654
// Create size grip (needed by Mac OS X for QMainWindow if QStatusBar is not visible)
655655
//should not be needed now that composer has a status bar?
@@ -772,7 +772,7 @@ void QgsComposer::connectCompositionSlots()
772772
return;
773773
}
774774

775-
connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setTitle );
775+
connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setWindowTitle );
776776
connect( mComposition, &QgsComposition::selectedItemChanged, this, &QgsComposer::showItemOptions );
777777
connect( mComposition, &QgsComposition::composerArrowAdded, this, &QgsComposer::addComposerArrow );
778778
connect( mComposition, &QgsComposition::composerPolygonAdded, this, &QgsComposer::addComposerPolygon );
@@ -838,18 +838,6 @@ void QgsComposer::activate()
838838
}
839839
}
840840

841-
void QgsComposer::setTitle( const QString &title )
842-
{
843-
mTitle = title;
844-
setWindowTitle( mTitle );
845-
846-
//update atlas map layer action name if required
847-
if ( mAtlasFeatureAction )
848-
{
849-
mAtlasFeatureAction->setText( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ) );
850-
}
851-
}
852-
853841
bool QgsComposer::loadFromTemplate( const QDomDocument &templateDoc, bool clearExisting )
854842
{
855843
// provide feedback, since composer will be hidden when loading template (much faster)
@@ -1001,8 +989,6 @@ void QgsComposer::toggleAtlasControls( bool atlasEnabled )
1001989
mActionExportAtlasAsImage->setEnabled( atlasEnabled );
1002990
mActionExportAtlasAsSVG->setEnabled( atlasEnabled );
1003991
mActionExportAtlasAsPDF->setEnabled( atlasEnabled );
1004-
1005-
updateAtlasMapLayerAction( atlasEnabled );
1006992
}
1007993

1008994
void QgsComposer::updateAtlasPageComboBox( int pageCount )
@@ -3801,24 +3787,6 @@ void QgsComposer::setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat )
38013787
emit atlasPreviewFeatureChanged();
38023788
}
38033789

3804-
void QgsComposer::updateAtlasMapLayerAction( QgsVectorLayer *coverageLayer )
3805-
{
3806-
if ( mAtlasFeatureAction )
3807-
{
3808-
delete mAtlasFeatureAction;
3809-
mAtlasFeatureAction = nullptr;
3810-
}
3811-
3812-
if ( coverageLayer )
3813-
{
3814-
mAtlasFeatureAction = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ),
3815-
this, coverageLayer, QgsMapLayerAction::SingleFeature,
3816-
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
3817-
QgsMapLayerActionRegistry::instance()->addMapLayerAction( mAtlasFeatureAction );
3818-
connect( mAtlasFeatureAction, &QgsMapLayerAction::triggeredForFeature, this, &QgsComposer::setAtlasFeature );
3819-
}
3820-
}
3821-
38223790
void QgsComposer::pageOrientationChanged( const QString & )
38233791
{
38243792
mSetPageOrientation = false;
@@ -3845,25 +3813,6 @@ void QgsComposer::setPrinterPageOrientation()
38453813
}
38463814
}
38473815

3848-
void QgsComposer::updateAtlasMapLayerAction( bool atlasEnabled )
3849-
{
3850-
if ( mAtlasFeatureAction )
3851-
{
3852-
delete mAtlasFeatureAction;
3853-
mAtlasFeatureAction = nullptr;
3854-
}
3855-
3856-
if ( atlasEnabled )
3857-
{
3858-
QgsAtlasComposition &atlas = mComposition->atlasComposition();
3859-
mAtlasFeatureAction = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ),
3860-
this, atlas.coverageLayer(), QgsMapLayerAction::SingleFeature,
3861-
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
3862-
QgsMapLayerActionRegistry::instance()->addMapLayerAction( mAtlasFeatureAction );
3863-
connect( mAtlasFeatureAction, &QgsMapLayerAction::triggeredForFeature, this, &QgsComposer::setAtlasFeature );
3864-
}
3865-
}
3866-
38673816
void QgsComposer::loadAtlasPredefinedScalesFromProject()
38683817
{
38693818
if ( !mComposition )

src/app/composer/qgscomposer.h

+4-18
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
122122
//! Restore the window and toolbar state
123123
void restoreWindowState();
124124

125-
QString title() const {return mTitle;}
126-
void setTitle( const QString &title );
127-
128125
/** Loads the contents of a template document into the composer's composition.
129126
* @param templateDoc template document to load
130127
* @param clearExisting set to true to remove all existing composition settings and items before loading template
131128
* @returns true if template load was successful
132129
*/
133130
bool loadFromTemplate( const QDomDocument &templateDoc, bool clearExisting );
134131

132+
//! Sets the specified feature as the current atlas feature
133+
//! @note added in 2.1
134+
void setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat );
135+
135136
public slots:
136137

137138
/**
@@ -526,19 +527,13 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
526527
//! Exports either either the whole atlas or just the current feature as a PDF, depending on mode
527528
void exportCompositionAsPDF( QgsComposer::OutputMode mode );
528529

529-
//! Updates the "set as atlas feature" map layer action, removing it if atlas is disabled
530-
void updateAtlasMapLayerAction( bool atlasEnabled );
531-
532530
//! Load predefined scales from the project's properties
533531
void loadAtlasPredefinedScalesFromProject();
534532

535533
QPrinter *printer();
536534

537535
QgsAppComposerInterface *mInterface = nullptr;
538536

539-
//! Composer title
540-
QString mTitle;
541-
542537
//! Labels in status bar which shows current mouse position
543538
QLabel *mStatusCursorXLabel = nullptr;
544539
QLabel *mStatusCursorYLabel = nullptr;
@@ -615,8 +610,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
615610
//! Help menu as mirror of main app's (on Mac)
616611
QMenu *mHelpMenu = nullptr;
617612

618-
QgsMapLayerAction *mAtlasFeatureAction = nullptr;
619-
620613
struct PanelStatus
621614
{
622615
PanelStatus( bool visible = true, bool active = false )
@@ -653,13 +646,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
653646
//! @note added in 2.1
654647
void toggleAtlasControls( bool atlasEnabled );
655648

656-
//! Sets the specified feature as the current atlas feature
657-
//! @note added in 2.1
658-
void setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat );
659-
660-
//! Updates the "set as atlas feature" map layer action when atlas coverage layer changes
661-
void updateAtlasMapLayerAction( QgsVectorLayer *coverageLayer );
662-
663649
//! Sets the printer page orientation when the page orientation changes
664650
void pageOrientationChanged( const QString &orientation );
665651

src/app/qgisapp.cpp

+84-1
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
11621162

11631163
QgsProviderRegistry::instance()->registerGuis( this );
11641164

1165+
setupLayoutManagerConnections();
1166+
11651167
// update windows
11661168
qApp->processEvents();
11671169

@@ -7031,7 +7033,7 @@ QgsComposer *QgisApp::duplicateComposer( QgsComposer *currentComposer, QString t
70317033
if ( title.isEmpty() )
70327034
{
70337035
// TODO: inject a bit of randomness in auto-titles?
7034-
title = currentComposer->title() + tr( " copy" );
7036+
title = currentComposer->composition()->name() + tr( " copy" );
70357037
}
70367038

70377039
QgsComposition *newComposition = QgsProject::instance()->layoutManager()->duplicateComposition( currentComposer->composition()->name(),
@@ -7059,6 +7061,87 @@ void QgisApp::deletePrintComposers()
70597061
}
70607062
}
70617063

7064+
void QgisApp::setupLayoutManagerConnections()
7065+
{
7066+
QgsLayoutManager *manager = QgsProject::instance()->layoutManager();
7067+
connect( manager, &QgsLayoutManager::compositionAdded, this, [ = ]( const QString & name )
7068+
{
7069+
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
7070+
if ( c )
7071+
{
7072+
mAtlasFeatureActions.insert( c, nullptr );
7073+
}
7074+
7075+
connect( c, &QgsComposition::nameChanged, this, [this, c]( const QString & name )
7076+
{
7077+
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
7078+
if ( action )
7079+
{
7080+
action->setText( QString( tr( "Set as atlas feature for %1" ) ).arg( name ) );
7081+
}
7082+
} );
7083+
7084+
connect( &c->atlasComposition(), &QgsAtlasComposition::coverageLayerChanged, this, [this, c]( QgsVectorLayer * coverageLayer )
7085+
{
7086+
setupAtlasMapLayerAction( c, static_cast< bool >( coverageLayer ) );
7087+
} );
7088+
7089+
connect( &c->atlasComposition(), &QgsAtlasComposition::toggled, this, [this, c]( bool enabled )
7090+
{
7091+
setupAtlasMapLayerAction( c, enabled );
7092+
} );
7093+
7094+
setupAtlasMapLayerAction( c, c->atlasComposition().enabled() && c->atlasComposition().coverageLayer() );
7095+
} );
7096+
7097+
connect( manager, &QgsLayoutManager::compositionAboutToBeRemoved, this, [ = ]( const QString & name )
7098+
{
7099+
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
7100+
if ( c )
7101+
{
7102+
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
7103+
if ( action )
7104+
{
7105+
QgsMapLayerActionRegistry::instance()->removeMapLayerAction( action );
7106+
delete action;
7107+
mAtlasFeatureActions.remove( c );
7108+
}
7109+
}
7110+
} );
7111+
}
7112+
7113+
void QgisApp::setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction )
7114+
{
7115+
QgsMapLayerAction *action = mAtlasFeatureActions.value( composition );
7116+
if ( action )
7117+
{
7118+
QgsMapLayerActionRegistry::instance()->removeMapLayerAction( action );
7119+
delete action;
7120+
action = nullptr;
7121+
mAtlasFeatureActions.remove( composition );
7122+
}
7123+
7124+
if ( enableAction )
7125+
{
7126+
action = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( composition->name() ),
7127+
this, composition->atlasComposition().coverageLayer(), QgsMapLayerAction::SingleFeature,
7128+
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
7129+
mAtlasFeatureActions.insert( composition, action );
7130+
QgsMapLayerActionRegistry::instance()->addMapLayerAction( action );
7131+
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this, composition]( QgsMapLayer * layer, const QgsFeature & feat )
7132+
{
7133+
setCompositionAtlasFeature( composition, layer, feat );
7134+
}
7135+
);
7136+
}
7137+
}
7138+
7139+
void QgisApp::setCompositionAtlasFeature( QgsComposition *composition, QgsMapLayer *layer, const QgsFeature &feat )
7140+
{
7141+
QgsComposer *composer = openComposer( composition );
7142+
composer->setAtlasFeature( layer, feat );
7143+
}
7144+
70627145
void QgisApp::composerMenuAboutToShow()
70637146
{
70647147
populateComposerMenu( mPrintComposersMenu );

src/app/qgisapp.h

+9
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class QgsDiagramProperties;
131131
#include "qgswelcomepageitemsmodel.h"
132132
#include "qgsraster.h"
133133
#include "qgsrasterminmaxorigin.h"
134+
#include "qgsmaplayeractionregistry.h"
134135

135136
#include "ui_qgisapp.h"
136137
#include "qgis_app.h"
@@ -1564,6 +1565,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
15641565
//! Deletes all the composer objects and clears mPrintComposers
15651566
void deletePrintComposers();
15661567

1568+
void setupLayoutManagerConnections();
1569+
1570+
void setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction );
1571+
1572+
void setCompositionAtlasFeature( QgsComposition *composition, QgsMapLayer *layer, const QgsFeature &feat );
1573+
15671574
void saveAsVectorFileGeneral( QgsVectorLayer *vlayer = nullptr, bool symbologyOption = true );
15681575

15691576
/** Paste features from clipboard into a new memory layer.
@@ -1934,6 +1941,8 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
19341941

19351942
QStackedWidget *mCentralContainer = nullptr;
19361943

1944+
QHash< QgsComposition *, QgsMapLayerAction * > mAtlasFeatureActions;
1945+
19371946
int mProjOpen;
19381947

19391948
bool gestureEvent( QGestureEvent *event );

src/app/qgsprojectproperties.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgscomposer.h"
2626
#include "qgscontexthelp.h"
2727
#include "qgscoordinatetransform.h"
28+
#include "qgslayoutmanager.h"
2829
#include "qgslogger.h"
2930
#include "qgsmapcanvas.h"
3031
#include "qgsmaplayer.h"

0 commit comments

Comments
 (0)