Skip to content
Permalink
Browse files

Make extra map views either follow the main canvas layers/styles,

or allow them to be set to follow a map theme preset

I.e. all styling and configuration operations for secondary
views are done in the main map canvas. Theme presets can be made
in the main canvas, and then assigned to the secondary views.
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent 419e53b commit 95806bb59be455c6f3b0f2871aa4871f3f0c72fd
@@ -3143,6 +3143,7 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating,
mapCanvas->freeze( true );
mapCanvas->setObjectName( name );
connect( mapCanvas, &QgsMapCanvas::messageEmitted, this, &QgisApp::displayMessage );
connect( mLayerTreeCanvasBridge, &QgsLayerTreeMapCanvasBridge::canvasLayersChanged, mapCanvas, &QgsMapCanvas::setLayers );

applyProjectSettingsToCanvas( mapCanvas );
applyDefaultSettingsToCanvas( mapCanvas );
@@ -21,6 +21,9 @@
#include "qgsdoublespinbox.h"
#include "qgssettings.h"
#include "qgsmaptoolpan.h"
#include "qgsmapthemecollection.h"
#include "qgsproject.h"
#include "qgsmapthemes.h"
#include <QMessageBox>
#include <QMenu>
#include <QToolBar>
@@ -49,24 +52,36 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa

connect( mActionSyncView, &QAction::toggled, this, &QgsMapCanvasDockWidget::syncView );

QMenu *menu = new QMenu();

QToolButton *toolButton = new QToolButton();
toolButton->setMenu( menu );
toolButton->setPopupMode( QToolButton::InstantPopup );
toolButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapSettings.svg" ) ) );
mToolbar->addWidget( toolButton );
mMenu = new QMenu();
connect( mMenu, &QMenu::aboutToShow, this, &QgsMapCanvasDockWidget::menuAboutToShow );

QToolButton *btnMapThemes = new QToolButton;
btnMapThemes->setAutoRaise( true );
btnMapThemes->setToolTip( tr( "Set View Theme" ) );
btnMapThemes->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) ) );
btnMapThemes->setPopupMode( QToolButton::InstantPopup );
btnMapThemes->setMenu( mMenu );
mToolbar->addWidget( btnMapThemes );

QMenu *settingsMenu = new QMenu();
QToolButton *settingsButton = new QToolButton();
btnMapThemes->setAutoRaise( true );
settingsButton->setMenu( settingsMenu );
settingsButton->setPopupMode( QToolButton::InstantPopup );
settingsButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapSettings.svg" ) ) );
mToolbar->addWidget( settingsButton );

connect( mActionSetCrs, &QAction::triggered, this, &QgsMapCanvasDockWidget::setMapCrs );
connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMapCanvasDockWidget::mapCrsChanged );
mapCrsChanged();

QgsMapSettingsAction *settingsAction = new QgsMapSettingsAction( menu );
menu->addAction( settingsAction );
QgsMapSettingsAction *settingsAction = new QgsMapSettingsAction( settingsMenu );
settingsMenu->addAction( settingsAction );

menu->addSeparator();
menu->addAction( mActionSetCrs );
menu->addAction( mActionRename );
settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionRename );
settingsMenu->addSeparator();
connect( mActionRename, &QAction::triggered, this, &QgsMapCanvasDockWidget::renameTriggered );

mScaleCombo = settingsAction->scaleCombo();
@@ -219,6 +234,45 @@ void QgsMapCanvasDockWidget::mapCrsChanged()
tr( "No projection" ) ) );
}

void QgsMapCanvasDockWidget::menuAboutToShow()
{
qDeleteAll( mMenuPresetActions );
mMenuPresetActions.clear();

QString currentTheme = mMapCanvas->theme();

QAction *actionFollowMain = new QAction( tr( "(default)" ), mMenu );
actionFollowMain->setCheckable( true );
if ( currentTheme.isEmpty() || !QgsProject::instance()->mapThemeCollection()->hasMapTheme( currentTheme ) )
{
actionFollowMain->setChecked( true );
}
connect( actionFollowMain, &QAction::triggered, this, [ = ]
{
mMapCanvas->setTheme( QString() );
mMapCanvas->refresh();
} );
mMenuPresetActions.append( actionFollowMain );

Q_FOREACH ( const QString &grpName, QgsProject::instance()->mapThemeCollection()->mapThemes() )
{
QAction *a = new QAction( grpName, mMenu );
a->setCheckable( true );
if ( grpName == currentTheme )
{
a->setChecked( true );
}
connect( a, &QAction::triggered, this, [a, this]
{
mMapCanvas->setTheme( a->text() );
mMapCanvas->refresh();
} );
mMenuPresetActions.append( a );
}
mMenu->addActions( mMenuPresetActions );
}


QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
: QWidgetAction( parent )
{
@@ -68,12 +68,15 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
void syncView( bool enabled );
void mapExtentChanged();
void mapCrsChanged();
void menuAboutToShow();

private:

QgsMapCanvas *mMapCanvas = nullptr;
QgsMapCanvas *mMainCanvas = nullptr;
bool mShowCloseWarning = true;
QMenu *mMenu = nullptr;
QList<QAction *> mMenuPresetActions;
QgsScaleComboBox *mScaleCombo = nullptr;
QgsDoubleSpinBox *mRotationEdit = nullptr;
QgsDoubleSpinBox *mMagnificationEdit = nullptr;
@@ -1864,7 +1864,7 @@ void QgsMapCanvas::readProject( const QDomDocument &doc )
{
QDomElement elementNode = nodes.at( i ).toElement();

if ( elementNode.hasAttribute( "name" ) && elementNode.attribute( "name" ) == objectName() )
if ( elementNode.hasAttribute( QStringLiteral( "name" ) ) && elementNode.attribute( QStringLiteral( "name" ) ) == objectName() )
{
node = nodes.at( i );
break;
@@ -1881,6 +1881,15 @@ void QgsMapCanvas::readProject( const QDomDocument &doc )
enableMapTileRendering( tmpSettings.testFlag( QgsMapSettings::RenderMapTile ) );

clearExtentHistory(); // clear the extent history on project load

QDomElement elem = node.toElement();
if ( elem.hasAttribute( QStringLiteral( "theme" ) ) )
{
if ( QgsProject::instance()->mapThemeCollection()->hasMapTheme( elem.attribute( QStringLiteral( "theme" ) ) ) )
{
setTheme( elem.attribute( QStringLiteral( "theme" ) ) );
}
}
}
else
{
@@ -1902,6 +1911,8 @@ void QgsMapCanvas::writeProject( QDomDocument &doc )

QDomElement mapcanvasNode = doc.createElement( QStringLiteral( "mapcanvas" ) );
mapcanvasNode.setAttribute( "name", objectName() );
if ( !mTheme.isEmpty() )
mapcanvasNode.setAttribute( "theme", mTheme );
qgisNode.appendChild( mapcanvasNode );

mSettings.writeXml( mapcanvasNode, doc );
@@ -211,5 +211,6 @@ def testMasterVisibleLayers(self):
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, layer2, layer3])



if __name__ == '__main__':
unittest.main()

0 comments on commit 95806bb

Please sign in to comment.
You can’t perform that action at this time.