Skip to content
Permalink
Browse files

Allow canvas extents to be synchronized

  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent e4469c8 commit 271ed6cbace2ea4067258a3abed5996be1584535
@@ -3126,6 +3126,7 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating,

QgsMapCanvasDockWidget *mapCanvasWidget = new QgsMapCanvasDockWidget( name, this );
mapCanvasWidget->setAllowedAreas( Qt::AllDockWidgetAreas );
mapCanvasWidget->setMainCanvas( mMapCanvas );

mapCanvasWidget->setFloating( isFloating );
if ( dockGeometry.isEmpty() )
@@ -37,6 +37,7 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
mMainWidget->layout()->addWidget( mMapCanvas );

connect( mActionSetCrs, &QAction::triggered, this, &QgsMapCanvasDockWidget::setMapCrs );
connect( mActionSyncView, &QAction::toggled, this, &QgsMapCanvasDockWidget::syncView );
}

QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
@@ -75,3 +76,33 @@ void QgsMapCanvasDockWidget::setMapCrs()
mMapCanvas->setDestinationCrs( dlg.crs() );
}
}

void QgsMapCanvasDockWidget::syncView( bool enabled )
{
if ( enabled )
{
connect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged, Qt::UniqueConnection );
connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged, Qt::UniqueConnection );
}
else
{
disconnect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged );
disconnect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged );
}
}

void QgsMapCanvasDockWidget::mapExtentChanged()
{
QgsMapCanvas *sourceCanvas = qobject_cast< QgsMapCanvas * >( sender() );
if ( !sourceCanvas )
return;

// avoid infinite recursion
syncView( false );

QgsMapCanvas *destCanvas = sourceCanvas == mMapCanvas ? mMainCanvas : mMapCanvas;
destCanvas->setExtent( sourceCanvas->extent() );
destCanvas->refresh();

syncView( true );
}
@@ -28,6 +28,11 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
public:
explicit QgsMapCanvasDockWidget( const QString &name, QWidget *parent = nullptr );

/**
* Sets the main app map canvas.
*/
void setMainCanvas( QgsMapCanvas *canvas ) { mMainCanvas = canvas; }

/**
* Returns the map canvas contained in the dock widget.
*/
@@ -45,10 +50,13 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
private slots:

void setMapCrs();
void syncView( bool enabled );
void mapExtentChanged();

private:

QgsMapCanvas *mMapCanvas = nullptr;
QgsMapCanvas *mMainCanvas = nullptr;
bool mShowCloseWarning = true;


@@ -42,6 +42,7 @@
<bool>false</bool>
</property>
<addaction name="mActionSetCrs"/>
<addaction name="mActionSyncView"/>
</widget>
</item>
<item>
@@ -79,6 +80,21 @@
<string>Set Map CRS</string>
</property>
</action>
<action name="mActionSyncView">
<property name="checkable">
<bool>true</bool>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/sync_views.svg</normaloff>:/images/themes/default/sync_views.svg</iconset>
</property>
<property name="text">
<string>Synchronize view</string>
</property>
<property name="toolTip">
<string>Synchronize View with Main Map</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit 271ed6c

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