Skip to content
Permalink
Browse files
Allow showing main canvas extent in extra map views
Another step toward multicanvas replacing map overview panel
  • Loading branch information
nyalldawson committed Mar 23, 2017
1 parent 1ffa9ee commit 4d42d769e63bfac4aedd4056b208cd6dd09d888f
@@ -3141,8 +3141,16 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating, const QRect &dockGeometry, Qt::DockWidgetArea area,
bool synced, bool showCursor, bool scaleSynced, double scaleFactor )
QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name )
{
QgsMapCanvasDockWidget *dock = createNewMapCanvasDock( name );
if ( !dock )
return nullptr;
else
return dock->mapCanvas();
}

QgsMapCanvasDockWidget *QgisApp::createNewMapCanvasDock( const QString &name, bool isFloating, const QRect &dockGeometry, Qt::DockWidgetArea area )
{
Q_FOREACH ( QgsMapCanvas *canvas, mapCanvases() )
{
@@ -3209,13 +3217,7 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating,
addDockWidget( area, mapCanvasWidget );
}
}

mapCanvasWidget->setViewCenterSynchronized( synced );
mapCanvasWidget->setCursorMarkerVisible( showCursor );
mapCanvasWidget->setScaleFactor( scaleFactor );
mapCanvasWidget->setViewScaleSynchronized( scaleSynced );

return mapCanvas;
return mapCanvasWidget;
}

void QgisApp::closeMapCanvas( const QString &name )
@@ -9815,7 +9817,7 @@ void QgisApp::newMapCanvas()
}
}

createNewMapCanvas( name, true );
createNewMapCanvasDock( name, true );
}

void QgisApp::setExtent( const QgsRectangle &rect )
@@ -11854,6 +11856,7 @@ void QgisApp::writeProject( QDomDocument &doc )
node.setAttribute( QStringLiteral( "area" ), dockWidgetArea( w ) );
node.setAttribute( QStringLiteral( "synced" ), w->isViewCenterSynchronized() );
node.setAttribute( QStringLiteral( "showCursor" ), w->isCursorMarkerVisible() );
node.setAttribute( QStringLiteral( "showExtent" ), w->isMainCanvasExtentVisible() );
node.setAttribute( QStringLiteral( "scaleSynced" ), w->isViewScaleSynchronized() );
node.setAttribute( QStringLiteral( "scaleFactor" ), w->scaleFactor() );
mapViewNode.appendChild( node );
@@ -11892,12 +11895,20 @@ void QgisApp::readProject( const QDomDocument &doc )
bool floating = elementNode.attribute( QStringLiteral( "floating" ), QStringLiteral( "0" ) ).toInt();
bool synced = elementNode.attribute( QStringLiteral( "synced" ), QStringLiteral( "0" ) ).toInt();
bool showCursor = elementNode.attribute( QStringLiteral( "showCursor" ), QStringLiteral( "0" ) ).toInt();
bool showExtent = elementNode.attribute( QStringLiteral( "showExtent" ), QStringLiteral( "0" ) ).toInt();
bool scaleSynced = elementNode.attribute( QStringLiteral( "scaleSynced" ), QStringLiteral( "0" ) ).toInt();
double scaleFactor = elementNode.attribute( QStringLiteral( "scaleFactor" ), QStringLiteral( "1" ) ).toDouble();
Qt::DockWidgetArea area = static_cast< Qt::DockWidgetArea >( elementNode.attribute( QStringLiteral( "area" ), QString::number( Qt::RightDockWidgetArea ) ).toInt() );

QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ), area, synced, showCursor, scaleSynced, scaleFactor );
QgsMapCanvasDockWidget *mapCanvasDock = createNewMapCanvasDock( mapName, floating, QRect( x, y, w, h ), area );
QgsMapCanvas *mapCanvas = mapCanvasDock->mapCanvas();
mapCanvas->readProject( doc );

mapCanvasDock->setViewCenterSynchronized( synced );
mapCanvasDock->setCursorMarkerVisible( showCursor );
mapCanvasDock->setScaleFactor( scaleFactor );
mapCanvasDock->setViewScaleSynchronized( scaleSynced );
mapCanvasDock->setMainCanvasExtentVisible( showExtent );
}
}
}
@@ -62,6 +62,7 @@ class QgsGeometry;
class QgsLayerTreeMapCanvasBridge;
class QgsLayerTreeView;
class QgsMapCanvas;
class QgsMapCanvasDockWidget;
class QgsMapLayer;
class QgsMapLayerConfigWidgetFactory;
class QgsMapOverviewCanvas;
@@ -241,13 +242,17 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QList< QgsMapCanvas * > mapCanvases();

/**
* Create a new map canvas with the specified unique \a name. The \a isFloating
* Create a new map canvas with the specified unique \a name.
*/
QgsMapCanvas *createNewMapCanvas( const QString &name );

/**
* Create a new map canvas dock widget with the specified unique \a name. The \a isFloating
* and \a dockGeometry arguments can be used to specify an initial floating state
* and widget geometry rect for the dock.
*/
QgsMapCanvas *createNewMapCanvas( const QString &name, bool isFloating = false, const QRect &dockGeometry = QRect(),
Qt::DockWidgetArea area = Qt::RightDockWidgetArea, bool synced = false, bool showCursor = true, bool scaleSynced = false,
double scaleFactor = 1.0 );
QgsMapCanvasDockWidget *createNewMapCanvasDock( const QString &name, bool isFloating = false, const QRect &dockGeometry = QRect(),
Qt::DockWidgetArea area = Qt::RightDockWidgetArea );

/**
* Closes the additional map canvas with matching \a name.
@@ -27,6 +27,7 @@
#include "qgslayertreeviewdefaultactions.h"
#include "qgisapp.h"
#include "qgsvertexmarker.h"
#include "qgsrubberband.h"
#include <QMessageBox>
#include <QMenu>
#include <QToolBar>
@@ -49,6 +50,10 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
mXyMarker->setIconSize( 6 );
mXyMarker->setColor( QColor( 30, 30, 30, 225 ) );
mXyMarker->setFillColor( QColor( 255, 255, 255, 225 ) );

mExtentRubberBand = new QgsRubberBand( mMapCanvas, QgsWkbTypes::PolygonGeometry );
mExtentRubberBand->setStrokeColor( Qt::red );

mPanTool = new QgsMapToolPan( mMapCanvas );
mMapCanvas->setMapTool( mPanTool );

@@ -85,6 +90,7 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa

connect( mActionSetCrs, &QAction::triggered, this, &QgsMapCanvasDockWidget::setMapCrs );
connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMapCanvasDockWidget::mapCrsChanged );
connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMapCanvasDockWidget::updateExtentRect );
connect( mActionZoomFullExtent, &QAction::triggered, mMapCanvas, &QgsMapCanvas::zoomToFullExtent );
connect( mActionZoomToLayer, &QAction::triggered, mMapCanvas, [ = ] { QgisApp::instance()->layerTreeView()->defaultActions()->zoomToLayer( mMapCanvas ); } );
connect( mActionZoomToSelected, &QAction::triggered, mMapCanvas, [ = ] { mMapCanvas->zoomToSelected(); } );
@@ -94,9 +100,11 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
settingsMenu->addAction( settingsAction );

settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionShowAnnotations );
settingsMenu->addAction( mActionShowCursor );
settingsMenu->addAction( mActionShowExtent );
settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionRename );

connect( settingsMenu, &QMenu::aboutToShow, this, &QgsMapCanvasDockWidget::settingsMenuAboutToShow );
@@ -106,6 +114,8 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
connect( mActionShowAnnotations, &QAction::toggled, this, [ = ]( bool checked ) { mMapCanvas->setAnnotationsVisible( checked ); } );
mActionShowCursor->setChecked( true );
connect( mActionShowCursor, &QAction::toggled, this, [ = ]( bool checked ) { mXyMarker->setVisible( checked ); } );
mActionShowExtent->setChecked( false );
connect( mActionShowExtent, &QAction::toggled, this, [ = ]( bool checked ) { mExtentRubberBand->setVisible( checked ); updateExtentRect(); } );

mScaleCombo = settingsAction->scaleCombo();
mRotationEdit = settingsAction->rotationSpinBox();
@@ -197,13 +207,18 @@ void QgsMapCanvasDockWidget::setMainCanvas( QgsMapCanvas *canvas )
disconnect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
disconnect( mMainCanvas, &QgsMapCanvas::scaleChanged, this, &QgsMapCanvasDockWidget::mapScaleChanged );
disconnect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged );
disconnect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::updateExtentRect );
disconnect( mMainCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMapCanvasDockWidget::updateExtentRect );
}

mMainCanvas = canvas;
connect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
connect( mMainCanvas, &QgsMapCanvas::scaleChanged, this, &QgsMapCanvasDockWidget::mapScaleChanged );
connect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged );
connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::mapExtentChanged, Qt::UniqueConnection );
connect( mMainCanvas, &QgsMapCanvas::extentsChanged, this, &QgsMapCanvasDockWidget::updateExtentRect );
connect( mMainCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsMapCanvasDockWidget::updateExtentRect );
updateExtentRect();
}

QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
@@ -231,6 +246,16 @@ bool QgsMapCanvasDockWidget::isCursorMarkerVisible() const
return mXyMarker->isVisible();
}

void QgsMapCanvasDockWidget::setMainCanvasExtentVisible( bool visible )
{
mActionShowExtent->setChecked( visible );
}

bool QgsMapCanvasDockWidget::isMainCanvasExtentVisible() const
{
return mExtentRubberBand->isVisible();
}

void QgsMapCanvasDockWidget::setScaleFactor( double factor )
{
mScaleFactorWidget->setValue( factor );
@@ -392,6 +417,28 @@ void QgsMapCanvasDockWidget::mapScaleChanged()
mBlockExtentSync = prev;
}

void QgsMapCanvasDockWidget::updateExtentRect()
{
if ( !mExtentRubberBand->isVisible() )
return;

QPolygonF mainCanvasPoly = mMainCanvas->mapSettings().visiblePolygon();
// close polygon
mainCanvasPoly << mainCanvasPoly.at( 0 );
QgsGeometry g = QgsGeometry::fromQPolygonF( mainCanvasPoly );
// reproject extent
QgsCoordinateTransform ct( mMainCanvas->mapSettings().destinationCrs(),
mMapCanvas->mapSettings().destinationCrs() );
try
{
g.transform( ct );
}
catch ( QgsCsException & )
{
}
mExtentRubberBand->setToGeometry( g, nullptr );
}

QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
: QWidgetAction( parent )
{
@@ -30,6 +30,7 @@ class QgsDoubleSpinBox;
class QgsStatusBarMagnifierWidget;
class QgsMapToolPan;
class QgsVertexMarker;
class QgsRubberBand;
class QCheckBox;

/**
@@ -77,6 +78,18 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
*/
bool isCursorMarkerVisible() const;

/**
* Sets whether the main canvas extent is visible.
* @see isMainCanvasExtentVisible()
*/
void setMainCanvasExtentVisible( bool visible );

/**
* Returns true if the main canvas extent is visible.
* @see setMainCanvasExtentVisible()
*/
bool isMainCanvasExtentVisible() const;

/**
* Returns the scaling factor for main canvas scale to view scale.
* @see setScaleFactor()
@@ -122,6 +135,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
void settingsMenuAboutToShow();
void syncMarker( const QgsPoint &p );
void mapScaleChanged();
void updateExtentRect();

private:

@@ -141,6 +155,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
QgsMapToolPan *mPanTool = nullptr;
QTimer mResizeTimer;
QgsVertexMarker *mXyMarker = nullptr;
QgsRubberBand *mExtentRubberBand = nullptr;
void syncViewCenter( QgsMapCanvas *sourceCanvas );
};

@@ -151,6 +151,14 @@
<string>Show Cursor Position</string>
</property>
</action>
<action name="mActionShowExtent">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Main Canvas Extent</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit 4d42d76

Please sign in to comment.