Skip to content

Commit e21b908

Browse files
committed
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.
1 parent 946a9d0 commit e21b908

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

src/app/qgisapp.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,7 +3113,7 @@ QgsMapCanvas *QgisApp::mapCanvas()
31133113
return mMapCanvas;
31143114
}
31153115

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

31713171
mapCanvasWidget->setViewExtentSynchronized( synced );
3172+
mapCanvasWidget->setCursorMarkerVisible( showCursor );
31723173

31733174
return mapCanvas;
31743175
}
@@ -11753,6 +11754,7 @@ void QgisApp::writeProject( QDomDocument &doc )
1175311754
node.setAttribute( QStringLiteral( "height" ), w->height() );
1175411755
node.setAttribute( QStringLiteral( "floating" ), w->isFloating() );
1175511756
node.setAttribute( QStringLiteral( "synced" ), w->isViewExtentSynchronized() );
11757+
node.setAttribute( QStringLiteral( "showCursor" ), w->isCursorMarkerVisible() );
1175611758
mapViewNode.appendChild( node );
1175711759
}
1175811760
qgisNode.appendChild( mapViewNode );
@@ -11788,8 +11790,9 @@ void QgisApp::readProject( const QDomDocument &doc )
1178811790
int h = elementNode.attribute( QStringLiteral( "height" ), QStringLiteral( "400" ) ).toInt();
1178911791
bool floating = elementNode.attribute( QStringLiteral( "floating" ), QStringLiteral( "0" ) ).toInt();
1179011792
bool synced = elementNode.attribute( QStringLiteral( "synced" ), QStringLiteral( "0" ) ).toInt();
11793+
bool showCursor = elementNode.attribute( QStringLiteral( "showCursor" ), QStringLiteral( "0" ) ).toInt();
1179111794

11792-
QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ), synced );
11795+
QgsMapCanvas *mapCanvas = createNewMapCanvas( mapName, floating, QRect( x, y, w, h ), synced, showCursor );
1179311796
mapCanvas->readProject( doc );
1179411797
}
1179511798
}

src/app/qgisapp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
242242
* and widget geometry rect for the dock.
243243
*/
244244
QgsMapCanvas *createNewMapCanvas( const QString &name, bool isFloating = false, const QRect &dockGeometry = QRect(),
245-
bool synced = false );
245+
bool synced = false, bool showCursor = true );
246246

247247
/**
248248
* Closes any additional map canvases. The main map canvas will not

src/app/qgsmapcanvasdockwidget.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgslayertreeview.h"
2727
#include "qgslayertreeviewdefaultactions.h"
2828
#include "qgisapp.h"
29+
#include "qgsvertexmarker.h"
2930
#include <QMessageBox>
3031
#include <QMenu>
3132
#include <QToolBar>
@@ -43,7 +44,10 @@ QgsMapCanvasDockWidget::QgsMapCanvasDockWidget( const QString &name, QWidget *pa
4344

4445
setWindowTitle( name );
4546
mMapCanvas = new QgsMapCanvas( this );
46-
47+
mXyMarker = new QgsVertexMarker( mMapCanvas );
48+
mXyMarker->setIconType( QgsVertexMarker::ICON_CIRCLE );
49+
mXyMarker->setIconSize( 6 );
50+
mXyMarker->setColor( QColor( 30, 30, 30, 225 ) );
4751
mPanTool = new QgsMapToolPan( mMapCanvas );
4852
mMapCanvas->setMapTool( mPanTool );
4953

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

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

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

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

182+
void QgsMapCanvasDockWidget::setMainCanvas( QgsMapCanvas *canvas )
183+
{
184+
if ( mMainCanvas )
185+
{
186+
disconnect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
187+
}
188+
189+
mMainCanvas = canvas;
190+
connect( mMainCanvas, &QgsMapCanvas::xyCoordinates, this, &QgsMapCanvasDockWidget::syncMarker );
191+
}
192+
175193
QgsMapCanvas *QgsMapCanvasDockWidget::mapCanvas()
176194
{
177195
return mMapCanvas;
@@ -187,6 +205,16 @@ bool QgsMapCanvasDockWidget::isViewExtentSynchronized() const
187205
return mActionSyncView->isChecked();
188206
}
189207

208+
void QgsMapCanvasDockWidget::setCursorMarkerVisible( bool visible )
209+
{
210+
mActionShowCursor->setChecked( visible );
211+
}
212+
213+
bool QgsMapCanvasDockWidget::isCursorMarkerVisible() const
214+
{
215+
return mXyMarker->isVisible();
216+
}
217+
190218
void QgsMapCanvasDockWidget::resizeEvent( QResizeEvent * )
191219
{
192220
mBlockExtentSync = true;
@@ -304,6 +332,24 @@ void QgsMapCanvasDockWidget::settingsMenuAboutToShow()
304332
whileBlocking( mActionShowAnnotations )->setChecked( mMapCanvas->annotationsVisible() );
305333
}
306334

335+
void QgsMapCanvasDockWidget::syncMarker( const QgsPoint &p )
336+
{
337+
if ( !mXyMarker->isVisible() )
338+
return;
339+
340+
// reproject point
341+
QgsCoordinateTransform ct( mMainCanvas->mapSettings().destinationCrs(),
342+
mMapCanvas->mapSettings().destinationCrs() );
343+
QgsPoint t = p;
344+
try
345+
{
346+
t = ct.transform( p );
347+
}
348+
catch ( QgsCsException & )
349+
{}
350+
351+
mXyMarker->setCenter( t );
352+
}
307353

308354
QgsMapSettingsAction::QgsMapSettingsAction( QWidget *parent )
309355
: QWidgetAction( parent )

src/app/qgsmapcanvasdockwidget.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <ui_qgsmapcanvasdockwidgetbase.h>
1919

2020
#include "qgsdockwidget.h"
21+
#include "qgspoint.h"
2122
#include "qgis_app.h"
2223
#include <QWidgetAction>
2324
#include <QTimer>
@@ -28,6 +29,7 @@ class QgsScaleComboBox;
2829
class QgsDoubleSpinBox;
2930
class QgsStatusBarMagnifierWidget;
3031
class QgsMapToolPan;
32+
class QgsVertexMarker;
3133

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

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

5355
/**
5456
* Sets whether the view extent should be synchronized with the main canvas extent.
57+
* @see isViewExtentSynchronized()
5558
*/
5659
void setViewExtentSynchronized( bool enabled );
5760

61+
/**
62+
* Returns true if the view extent is synchronized with the main canvas extent.
63+
* @see setViewExtentSynchronized()
64+
*/
5865
bool isViewExtentSynchronized() const;
5966

67+
/**
68+
* Sets whether the cursor position marker is visible.
69+
* @see isCursorMarkerVisible()
70+
*/
71+
void setCursorMarkerVisible( bool visible );
72+
73+
/**
74+
* Returns true if the cursor position marker is visible.
75+
* @see setCursorMarkerVisible()
76+
*/
77+
bool isCursorMarkerVisible() const;
78+
6079
signals:
6180

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

7797
private:
7898

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

src/ui/qgsmapcanvasdockwidgetbase.ui

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@
143143
<string>Show Annotations</string>
144144
</property>
145145
</action>
146+
<action name="mActionShowCursor">
147+
<property name="checkable">
148+
<bool>true</bool>
149+
</property>
150+
<property name="text">
151+
<string>Show Cursor Position</string>
152+
</property>
153+
</action>
146154
</widget>
147155
<customwidgets>
148156
<customwidget>

0 commit comments

Comments
 (0)