Skip to content

Commit 7679863

Browse files
committedSep 10, 2018
Add API for custom map tools in 3D map canvas
1 parent 41184bf commit 7679863

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
 

‎src/app/3d/qgs3dmapcanvas.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgscameracontroller.h"
2323
#include "qgs3dmapsettings.h"
2424
#include "qgs3dmapscene.h"
25+
#include "qgs3dmaptool.h"
2526
#include "qgswindow3dengine.h"
2627

2728

@@ -108,3 +109,42 @@ void Qgs3DMapCanvas::saveAsImage( const QString fileName, const QString fileForm
108109
mEngine->requestCaptureImage();
109110
}
110111
}
112+
113+
void Qgs3DMapCanvas::setMapTool( Qgs3DMapTool *tool )
114+
{
115+
if ( mMapTool && !tool )
116+
{
117+
mEngine->window()->removeEventFilter( this );
118+
mScene->cameraController()->setEnabled( true );
119+
}
120+
else if ( !mMapTool && tool )
121+
{
122+
mEngine->window()->installEventFilter( this );
123+
mScene->cameraController()->setEnabled( false );
124+
}
125+
126+
mMapTool = tool;
127+
}
128+
129+
bool Qgs3DMapCanvas::eventFilter( QObject *watched, QEvent *event )
130+
{
131+
if ( !mMapTool )
132+
return false;
133+
134+
Q_UNUSED( watched );
135+
switch ( event->type() )
136+
{
137+
case QEvent::MouseButtonPress:
138+
mMapTool->mousePressEvent( static_cast<QMouseEvent *>( event ) );
139+
break;
140+
case QEvent::MouseButtonRelease:
141+
mMapTool->mouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
142+
break;
143+
case QEvent::MouseMove:
144+
mMapTool->mouseMoveEvent( static_cast<QMouseEvent *>( event ) );
145+
break;
146+
default:
147+
break;
148+
}
149+
return false;
150+
}

‎src/app/3d/qgs3dmapcanvas.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Qt3DExtras
2626

2727
class Qgs3DMapSettings;
2828
class Qgs3DMapScene;
29+
class Qgs3DMapTool;
2930
class QgsWindow3DEngine;
3031
class QgsCameraController;
3132
class QgsPointXY;
@@ -59,12 +60,25 @@ class Qgs3DMapCanvas : public QWidget
5960
//! Saves the current scene as an image
6061
void saveAsImage( QString fileName, QString fileFormat );
6162

63+
/**
64+
* Sets the active map tool that will receive events from the 3D canvas. Does not transfer ownership.
65+
* If the tool is null, events will be used for camera manipulation.
66+
*/
67+
void setMapTool( Qgs3DMapTool *tool );
68+
69+
/**
70+
* Returns the active map tool that will receive events from the 3D canvas.
71+
* If the tool is null, events will be used for camera manipulation.
72+
*/
73+
Qgs3DMapTool *mapTool() const { return mMapTool; }
74+
6275
signals:
6376
//! Emitted when the 3D map canvas was successfully saved as image
6477
void savedAsImage( QString fileName );
6578

6679
protected:
6780
void resizeEvent( QResizeEvent *ev ) override;
81+
bool eventFilter( QObject *watched, QEvent *event ) override;
6882

6983
private:
7084
QgsWindow3DEngine *mEngine = nullptr;
@@ -78,6 +92,9 @@ class Qgs3DMapCanvas : public QWidget
7892
Qgs3DMapSettings *mMap = nullptr;
7993
//! Root entity of the 3D scene
8094
Qgs3DMapScene *mScene = nullptr;
95+
96+
//! Active map tool that receives events (if null then mouse/keyboard events are used for camera manipulation)
97+
Qgs3DMapTool *mMapTool = nullptr;
8198
};
8299

83100
#endif // QGS3DMAPCANVAS_H

‎src/app/3d/qgs3dmaptool.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/***************************************************************************
2+
qgs3dmaptool.cpp
3+
--------------------------------------
4+
Date : Sep 2018
5+
Copyright : (C) 2018 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgs3dmaptool.h"
17+
18+
Qgs3DMapTool::Qgs3DMapTool( Qgs3DMapCanvas *canvas )
19+
: mCanvas( canvas )
20+
{
21+
}
22+
23+
void Qgs3DMapTool::mousePressEvent( QMouseEvent *event )
24+
{
25+
Q_UNUSED( event );
26+
}
27+
28+
void Qgs3DMapTool::mouseReleaseEvent( QMouseEvent *event )
29+
{
30+
Q_UNUSED( event );
31+
}
32+
33+
void Qgs3DMapTool::mouseMoveEvent( QMouseEvent *event )
34+
{
35+
Q_UNUSED( event );
36+
}

‎src/app/3d/qgs3dmaptool.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***************************************************************************
2+
qgs3dmaptool.h
3+
--------------------------------------
4+
Date : Sep 2018
5+
Copyright : (C) 2018 by Martin Dobias
6+
Email : wonder dot sk at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGS3DMAPTOOL_H
17+
#define QGS3DMAPTOOL_H
18+
19+
#include <QObject>
20+
21+
class Qgs3DMapCanvas;
22+
class QMouseEvent;
23+
24+
/**
25+
* Base class for map tools operating on 3D map canvas.
26+
*/
27+
class Qgs3DMapTool : public QObject
28+
{
29+
Q_OBJECT
30+
31+
public:
32+
Qgs3DMapTool( Qgs3DMapCanvas *canvas );
33+
34+
virtual void mousePressEvent( QMouseEvent *event );
35+
virtual void mouseReleaseEvent( QMouseEvent *event );
36+
virtual void mouseMoveEvent( QMouseEvent *event );
37+
38+
protected:
39+
Qgs3DMapCanvas *mCanvas = nullptr;
40+
};
41+
42+
#endif // QGS3DMAPTOOL_H

‎src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ IF (WITH_3D)
445445
3d/qgs3dmapcanvas.cpp
446446
3d/qgs3dmapcanvasdockwidget.cpp
447447
3d/qgs3dmapconfigwidget.cpp
448+
3d/qgs3dmaptool.cpp
448449
3d/qgsline3dsymbolwidget.cpp
449450
3d/qgspoint3dsymbolwidget.cpp
450451
3d/qgspolygon3dsymbolwidget.cpp
@@ -459,6 +460,7 @@ IF (WITH_3D)
459460
3d/qgs3dmapcanvas.h
460461
3d/qgs3dmapcanvasdockwidget.h
461462
3d/qgs3dmapconfigwidget.h
463+
3d/qgs3dmaptool.h
462464
3d/qgsline3dsymbolwidget.h
463465
3d/qgspoint3dsymbolwidget.h
464466
3d/qgspolygon3dsymbolwidget.h

0 commit comments

Comments
 (0)
Please sign in to comment.