Skip to content

Commit

Permalink
Make extra map views either follow the main canvas layers/styles,
Browse files Browse the repository at this point in the history
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 95806bb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
78 changes: 66 additions & 12 deletions src/app/qgsmapcanvasdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 )
{
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsmapcanvasdockwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/test_qgsmapthemecollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.