Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rework atlas layer action to handle temporary composer windows
  • Loading branch information
nyalldawson committed Mar 21, 2017
1 parent 2e94f7d commit dbf0160
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 73 deletions.
57 changes: 3 additions & 54 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -109,7 +109,7 @@ QgsComposer::QgsComposer( QgsComposition *composition )
, mQgis( QgisApp::instance() )
{
setupUi( this );
setTitle( mComposition->name() );
setWindowTitle( mComposition->name() );
setAttribute( Qt::WA_DeleteOnClose );
#if QT_VERSION >= 0x050600
setDockOptions( dockOptions() | QMainWindow::GroupedDragging ) ;
Expand Down Expand Up @@ -647,9 +647,9 @@ QgsComposer::QgsComposer( QgsComposition *composition )
mActionExportAtlasAsPDF->setEnabled( false );
QgsAtlasComposition *atlasMap = &mComposition->atlasComposition();
connect( atlasMap, &QgsAtlasComposition::toggled, this, &QgsComposer::toggleAtlasControls );
connect( atlasMap, &QgsAtlasComposition::coverageLayerChanged, this, [ = ]( QgsVectorLayer * layer ) { updateAtlasMapLayerAction( layer ); } );
connect( atlasMap, &QgsAtlasComposition::numberFeaturesChanged, this, &QgsComposer::updateAtlasPageComboBox );
connect( atlasMap, &QgsAtlasComposition::featureChanged, this, &QgsComposer::atlasFeatureChanged );
toggleAtlasControls( atlasMap->enabled() && atlasMap->coverageLayer() );

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

connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setTitle );
connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setWindowTitle );
connect( mComposition, &QgsComposition::selectedItemChanged, this, &QgsComposer::showItemOptions );
connect( mComposition, &QgsComposition::composerArrowAdded, this, &QgsComposer::addComposerArrow );
connect( mComposition, &QgsComposition::composerPolygonAdded, this, &QgsComposer::addComposerPolygon );
Expand Down Expand Up @@ -838,18 +838,6 @@ void QgsComposer::activate()
}
}

void QgsComposer::setTitle( const QString &title )
{
mTitle = title;
setWindowTitle( mTitle );

//update atlas map layer action name if required
if ( mAtlasFeatureAction )
{
mAtlasFeatureAction->setText( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ) );
}
}

bool QgsComposer::loadFromTemplate( const QDomDocument &templateDoc, bool clearExisting )
{
// provide feedback, since composer will be hidden when loading template (much faster)
Expand Down Expand Up @@ -1001,8 +989,6 @@ void QgsComposer::toggleAtlasControls( bool atlasEnabled )
mActionExportAtlasAsImage->setEnabled( atlasEnabled );
mActionExportAtlasAsSVG->setEnabled( atlasEnabled );
mActionExportAtlasAsPDF->setEnabled( atlasEnabled );

updateAtlasMapLayerAction( atlasEnabled );
}

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

void QgsComposer::updateAtlasMapLayerAction( QgsVectorLayer *coverageLayer )
{
if ( mAtlasFeatureAction )
{
delete mAtlasFeatureAction;
mAtlasFeatureAction = nullptr;
}

if ( coverageLayer )
{
mAtlasFeatureAction = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ),
this, coverageLayer, QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
QgsMapLayerActionRegistry::instance()->addMapLayerAction( mAtlasFeatureAction );
connect( mAtlasFeatureAction, &QgsMapLayerAction::triggeredForFeature, this, &QgsComposer::setAtlasFeature );
}
}

void QgsComposer::pageOrientationChanged( const QString & )
{
mSetPageOrientation = false;
Expand All @@ -3845,25 +3813,6 @@ void QgsComposer::setPrinterPageOrientation()
}
}

void QgsComposer::updateAtlasMapLayerAction( bool atlasEnabled )
{
if ( mAtlasFeatureAction )
{
delete mAtlasFeatureAction;
mAtlasFeatureAction = nullptr;
}

if ( atlasEnabled )
{
QgsAtlasComposition &atlas = mComposition->atlasComposition();
mAtlasFeatureAction = new QgsMapLayerAction( QString( tr( "Set as atlas feature for %1" ) ).arg( mTitle ),
this, atlas.coverageLayer(), QgsMapLayerAction::SingleFeature,
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
QgsMapLayerActionRegistry::instance()->addMapLayerAction( mAtlasFeatureAction );
connect( mAtlasFeatureAction, &QgsMapLayerAction::triggeredForFeature, this, &QgsComposer::setAtlasFeature );
}
}

void QgsComposer::loadAtlasPredefinedScalesFromProject()
{
if ( !mComposition )
Expand Down
22 changes: 4 additions & 18 deletions src/app/composer/qgscomposer.h
Expand Up @@ -122,16 +122,17 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Restore the window and toolbar state
void restoreWindowState();

QString title() const {return mTitle;}
void setTitle( const QString &title );

/** Loads the contents of a template document into the composer's composition.
* @param templateDoc template document to load
* @param clearExisting set to true to remove all existing composition settings and items before loading template
* @returns true if template load was successful
*/
bool loadFromTemplate( const QDomDocument &templateDoc, bool clearExisting );

//! Sets the specified feature as the current atlas feature
//! @note added in 2.1
void setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat );

public slots:

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

//! Updates the "set as atlas feature" map layer action, removing it if atlas is disabled
void updateAtlasMapLayerAction( bool atlasEnabled );

//! Load predefined scales from the project's properties
void loadAtlasPredefinedScalesFromProject();

QPrinter *printer();

QgsAppComposerInterface *mInterface = nullptr;

//! Composer title
QString mTitle;

//! Labels in status bar which shows current mouse position
QLabel *mStatusCursorXLabel = nullptr;
QLabel *mStatusCursorYLabel = nullptr;
Expand Down Expand Up @@ -615,8 +610,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Help menu as mirror of main app's (on Mac)
QMenu *mHelpMenu = nullptr;

QgsMapLayerAction *mAtlasFeatureAction = nullptr;

struct PanelStatus
{
PanelStatus( bool visible = true, bool active = false )
Expand Down Expand Up @@ -653,13 +646,6 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! @note added in 2.1
void toggleAtlasControls( bool atlasEnabled );

//! Sets the specified feature as the current atlas feature
//! @note added in 2.1
void setAtlasFeature( QgsMapLayer *layer, const QgsFeature &feat );

//! Updates the "set as atlas feature" map layer action when atlas coverage layer changes
void updateAtlasMapLayerAction( QgsVectorLayer *coverageLayer );

//! Sets the printer page orientation when the page orientation changes
void pageOrientationChanged( const QString &orientation );

Expand Down
85 changes: 84 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -1162,6 +1162,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh

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

setupLayoutManagerConnections();

// update windows
qApp->processEvents();

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

QgsComposition *newComposition = QgsProject::instance()->layoutManager()->duplicateComposition( currentComposer->composition()->name(),
Expand Down Expand Up @@ -7059,6 +7061,87 @@ void QgisApp::deletePrintComposers()
}
}

void QgisApp::setupLayoutManagerConnections()
{
QgsLayoutManager *manager = QgsProject::instance()->layoutManager();
connect( manager, &QgsLayoutManager::compositionAdded, this, [ = ]( const QString & name )
{
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
if ( c )
{
mAtlasFeatureActions.insert( c, nullptr );
}

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

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

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

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

connect( manager, &QgsLayoutManager::compositionAboutToBeRemoved, this, [ = ]( const QString & name )
{
QgsComposition *c = QgsProject::instance()->layoutManager()->compositionByName( name );
if ( c )
{
QgsMapLayerAction *action = mAtlasFeatureActions.value( c );
if ( action )
{
QgsMapLayerActionRegistry::instance()->removeMapLayerAction( action );
delete action;
mAtlasFeatureActions.remove( c );
}
}
} );
}

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

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

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

void QgisApp::composerMenuAboutToShow()
{
populateComposerMenu( mPrintComposersMenu );
Expand Down
9 changes: 9 additions & 0 deletions src/app/qgisapp.h
Expand Up @@ -131,6 +131,7 @@ class QgsDiagramProperties;
#include "qgswelcomepageitemsmodel.h"
#include "qgsraster.h"
#include "qgsrasterminmaxorigin.h"
#include "qgsmaplayeractionregistry.h"

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

void setupLayoutManagerConnections();

void setupAtlasMapLayerAction( QgsComposition *composition, bool enableAction );

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

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

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

QStackedWidget *mCentralContainer = nullptr;

QHash< QgsComposition *, QgsMapLayerAction * > mAtlasFeatureActions;

int mProjOpen;

bool gestureEvent( QGestureEvent *event );
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsprojectproperties.cpp
Expand Up @@ -25,6 +25,7 @@
#include "qgscomposer.h"
#include "qgscontexthelp.h"
#include "qgscoordinatetransform.h"
#include "qgslayoutmanager.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
Expand Down

0 comments on commit dbf0160

Please sign in to comment.