Skip to content
Permalink
Browse files

Port cursor marker from Aux Window plugin to multi canvas

Shows a marker in map views indicating the position of the cursor
in the main canvas. Can be toggled on/off as desired.
  • Loading branch information
nyalldawson committed Mar 14, 2017
1 parent 946a9d0 commit e21b90808075bc375e9ad3ff684697a5cb1b4252
@@ -3113,7 +3113,7 @@ QgsMapCanvas *QgisApp::mapCanvas()
return mMapCanvas;
}

QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating, const QRect &dockGeometry, bool synced )
QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating, const QRect &dockGeometry, bool synced, bool showCursor )
{
Q_FOREACH ( QgsMapCanvas *canvas, mapCanvases() )
{
@@ -3169,6 +3169,7 @@ QgsMapCanvas *QgisApp::createNewMapCanvas( const QString &name, bool isFloating,
connect( mapCanvasWidget, &QgsMapCanvasDockWidget::renameTriggered, this, &QgisApp::renameView );

mapCanvasWidget->setViewExtentSynchronized( synced );
mapCanvasWidget->setCursorMarkerVisible( showCursor );

return mapCanvas;
}
@@ -11753,6 +11754,7 @@ void QgisApp::writeProject( QDomDocument &doc )
node.setAttribute( QStringLiteral( "height" ), w->height() );
node.setAttribute( QStringLiteral( "floating" ), w->isFloating() );
node.setAttribute( QStringLiteral( "synced" ), w->isViewExtentSynchronized() );
node.setAttribute( QStringLiteral( "showCursor" ), w->isCursorMarkerVisible() );
mapViewNode.appendChild( node );
}
qgisNode.appendChild( mapViewNode );
@@ -11788,8 +11790,9 @@ void QgisApp::readProject( const QDomDocument &doc )
int h = elementNode.attribute( QStringLiteral( "height" ), QStringLiteral( "400" ) ).toInt();
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();

QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ), synced );
QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ), synced, showCursor );
mapCanvas->readProject( doc );
}
}
@@ -242,7 +242,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
* and widget geometry rect for the dock.
*/
QgsMapCanvas *createNewMapCanvas( const QString &name, bool isFloating = false, const QRect &dockGeometry = QRect(),
bool synced = false );
bool synced = false, bool showCursor = true );

/**
* Closes any additional map canvases. The main map canvas will not
@@ -26,6 +26,7 @@
#include "qgslayertreeview.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgisapp.h"
#include "qgsvertexmarker.h"
#include <QMessageBox>
#include <QMenu>
#include <QToolBar>
@@ -43,7 +44,10 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa

setWindowTitle( name );
mMapCanvas = new QgsMapCanvas( this );

mXyMarker = new QgsVertexMarker( mMapCanvas );
mXyMarker->setIconType( QgsVertexMarker::ICON_CIRCLE );
mXyMarker->setIconSize( 6 );
mXyMarker->setColor( QColor( 30, 30, 30, 225 ) );
mPanTool = new QgsMapToolPan( mMapCanvas );
mMapCanvas->setMapTool( mPanTool );

@@ -92,13 +96,16 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
settingsMenu->addSeparator();
settingsMenu->addAction( mActionSetCrs );
settingsMenu->addAction( mActionShowAnnotations );
settingsMenu->addAction( mActionShowCursor );
settingsMenu->addAction( mActionRename );

connect( settingsMenu, &QMenu::aboutToShow, this, &QgsMapCanvasDockWidget::settingsMenuAboutToShow );

connect( mActionRename, &QAction::triggered, this, &QgsMapCanvasDockWidget::renameTriggered );
mActionShowAnnotations->setChecked( mMapCanvas->annotationsVisible() );
connect( mActionShowAnnotations, &QAction::toggled, this, [ = ]( bool checked ) { mMapCanvas->setAnnotationsVisible( checked ); } );
mActionShowCursor->setChecked( true );
connect( mActionShowCursor, &QAction::toggled, this, [ = ]( bool checked ) { mXyMarker->setVisible( checked ); } );

mScaleCombo = settingsAction->scaleCombo();
mRotationEdit = settingsAction->rotationSpinBox();
@@ -172,6 +179,17 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
} );
}

void QgsMapCanvasDockWidget::setMainCanvas( QgsMapCanvas *canvas )
{
if ( mMainCanvas )
{
disconnect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
}

mMainCanvas = canvas;
connect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
}

QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
{
return mMapCanvas;
@@ -187,6 +205,16 @@ bool QgsMapCanvasDockWidget::isViewExtentSynchronized() const
return mActionSyncView->isChecked();
}

void QgsMapCanvasDockWidget::setCursorMarkerVisible( bool visible )
{
mActionShowCursor->setChecked( visible );
}

bool QgsMapCanvasDockWidget::isCursorMarkerVisible() const
{
return mXyMarker->isVisible();
}

void QgsMapCanvasDockWidget::resizeEvent( QResizeEvent * )
{
mBlockExtentSync = true;
@@ -304,6 +332,24 @@ void QgsMapCanvasDockWidget::settingsMenuAboutToShow()
whileBlocking( mActionShowAnnotations )->setChecked( mMapCanvas->annotationsVisible() );
}

void QgsMapCanvasDockWidget::syncMarker( const QgsPoint &p )
{
if ( !mXyMarker->isVisible() )
return;

// reproject point
QgsCoordinateTransform ct( mMainCanvas->mapSettings().destinationCrs(),
mMapCanvas->mapSettings().destinationCrs() );
QgsPoint t = p;
try
{
t = ct.transform( p );
}
catch ( QgsCsException & )
{}

mXyMarker->setCenter( t );
}

QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
: QWidgetAction( parent )
@@ -18,6 +18,7 @@
#include <ui_qgsmapcanvasdockwidgetbase.h>

#include "qgsdockwidget.h"
#include "qgspoint.h"
#include "qgis_app.h"
#include <QWidgetAction>
#include <QTimer>
@@ -28,6 +29,7 @@ class QgsScaleComboBox;
class QgsDoubleSpinBox;
class QgsStatusBarMagnifierWidget;
class QgsMapToolPan;
class QgsVertexMarker;

/**
* \class QgsMapCanvasDockWidget
@@ -43,7 +45,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
/**
* Sets the main app map canvas.
*/
void setMainCanvas( QgsMapCanvas *canvas ) { mMainCanvas = canvas; }
void setMainCanvas( QgsMapCanvas *canvas );

/**
* Returns the map canvas contained in the dock widget.
@@ -52,11 +54,28 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM

/**
* Sets whether the view extent should be synchronized with the main canvas extent.
* @see isViewExtentSynchronized()
*/
void setViewExtentSynchronized( bool enabled );

/**
* Returns true if the view extent is synchronized with the main canvas extent.
* @see setViewExtentSynchronized()
*/
bool isViewExtentSynchronized() const;

/**
* Sets whether the cursor position marker is visible.
* @see isCursorMarkerVisible()
*/
void setCursorMarkerVisible( bool visible );

/**
* Returns true if the cursor position marker is visible.
* @see setCursorMarkerVisible()
*/
bool isCursorMarkerVisible() const;

signals:

void renameTriggered();
@@ -73,6 +92,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
void mapCrsChanged();
void menuAboutToShow();
void settingsMenuAboutToShow();
void syncMarker( const QgsPoint &p );

private:

@@ -89,6 +109,7 @@ class APP_EXPORT QgsMapCanvasDockWidget : public QgsDockWidget, private Ui::QgsM
bool mBlockExtentSync = false;
QgsMapToolPan *mPanTool = nullptr;
QTimer mResizeTimer;
QgsVertexMarker *mXyMarker = nullptr;
void syncViewExtent( QgsMapCanvas *sourceCanvas );
};

@@ -143,6 +143,14 @@
<string>Show Annotations</string>
</property>
</action>
<action name="mActionShowCursor">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Show Cursor Position</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

0 comments on commit e21b908

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