Skip to content
Permalink
Browse files

Add API for custom map tools in 3D map canvas

  • Loading branch information
wonder-sk committed Sep 10, 2018
1 parent 41184bf commit 76798638f91e6a9f593a6cfee9d07b573379cde9
Showing with 137 additions and 0 deletions.
  1. +40 −0 src/app/3d/qgs3dmapcanvas.cpp
  2. +17 −0 src/app/3d/qgs3dmapcanvas.h
  3. +36 −0 src/app/3d/qgs3dmaptool.cpp
  4. +42 −0 src/app/3d/qgs3dmaptool.h
  5. +2 −0 src/app/CMakeLists.txt
@@ -22,6 +22,7 @@
#include "qgscameracontroller.h"
#include "qgs3dmapsettings.h"
#include "qgs3dmapscene.h"
#include "qgs3dmaptool.h"
#include "qgswindow3dengine.h"


@@ -108,3 +109,42 @@ void Qgs3DMapCanvas::saveAsImage( const QString fileName, const QString fileForm
mEngine->requestCaptureImage();
}
}

void Qgs3DMapCanvas::setMapTool( Qgs3DMapTool *tool )
{
if ( mMapTool && !tool )
{
mEngine->window()->removeEventFilter( this );
mScene->cameraController()->setEnabled( true );
}
else if ( !mMapTool && tool )
{
mEngine->window()->installEventFilter( this );
mScene->cameraController()->setEnabled( false );
}

mMapTool = tool;
}

bool Qgs3DMapCanvas::eventFilter( QObject *watched, QEvent *event )
{
if ( !mMapTool )
return false;

Q_UNUSED( watched );
switch ( event->type() )
{
case QEvent::MouseButtonPress:
mMapTool->mousePressEvent( static_cast<QMouseEvent *>( event ) );
break;
case QEvent::MouseButtonRelease:
mMapTool->mouseReleaseEvent( static_cast<QMouseEvent *>( event ) );
break;
case QEvent::MouseMove:
mMapTool->mouseMoveEvent( static_cast<QMouseEvent *>( event ) );
break;
default:
break;
}
return false;
}
@@ -26,6 +26,7 @@ namespace Qt3DExtras

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

/**
* Sets the active map tool that will receive events from the 3D canvas. Does not transfer ownership.
* If the tool is null, events will be used for camera manipulation.
*/
void setMapTool( Qgs3DMapTool *tool );

/**
* Returns the active map tool that will receive events from the 3D canvas.
* If the tool is null, events will be used for camera manipulation.
*/
Qgs3DMapTool *mapTool() const { return mMapTool; }

signals:
//! Emitted when the 3D map canvas was successfully saved as image
void savedAsImage( QString fileName );

protected:
void resizeEvent( QResizeEvent *ev ) override;
bool eventFilter( QObject *watched, QEvent *event ) override;

private:
QgsWindow3DEngine *mEngine = nullptr;
@@ -78,6 +92,9 @@ class Qgs3DMapCanvas : public QWidget
Qgs3DMapSettings *mMap = nullptr;
//! Root entity of the 3D scene
Qgs3DMapScene *mScene = nullptr;

//! Active map tool that receives events (if null then mouse/keyboard events are used for camera manipulation)
Qgs3DMapTool *mMapTool = nullptr;
};

#endif // QGS3DMAPCANVAS_H
@@ -0,0 +1,36 @@
/***************************************************************************
qgs3dmaptool.cpp
--------------------------------------
Date : Sep 2018
Copyright : (C) 2018 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgs3dmaptool.h"

Qgs3DMapTool::Qgs3DMapTool( Qgs3DMapCanvas *canvas )
: mCanvas( canvas )
{
}

void Qgs3DMapTool::mousePressEvent( QMouseEvent *event )
{
Q_UNUSED( event );
}

void Qgs3DMapTool::mouseReleaseEvent( QMouseEvent *event )
{
Q_UNUSED( event );
}

void Qgs3DMapTool::mouseMoveEvent( QMouseEvent *event )
{
Q_UNUSED( event );
}
@@ -0,0 +1,42 @@
/***************************************************************************
qgs3dmaptool.h
--------------------------------------
Date : Sep 2018
Copyright : (C) 2018 by Martin Dobias
Email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGS3DMAPTOOL_H
#define QGS3DMAPTOOL_H

#include <QObject>

class Qgs3DMapCanvas;
class QMouseEvent;

/**
* Base class for map tools operating on 3D map canvas.
*/
class Qgs3DMapTool : public QObject
{
Q_OBJECT

public:
Qgs3DMapTool( Qgs3DMapCanvas *canvas );

virtual void mousePressEvent( QMouseEvent *event );
virtual void mouseReleaseEvent( QMouseEvent *event );
virtual void mouseMoveEvent( QMouseEvent *event );

protected:
Qgs3DMapCanvas *mCanvas = nullptr;
};

#endif // QGS3DMAPTOOL_H
@@ -445,6 +445,7 @@ IF (WITH_3D)
3d/qgs3dmapcanvas.cpp
3d/qgs3dmapcanvasdockwidget.cpp
3d/qgs3dmapconfigwidget.cpp
3d/qgs3dmaptool.cpp
3d/qgsline3dsymbolwidget.cpp
3d/qgspoint3dsymbolwidget.cpp
3d/qgspolygon3dsymbolwidget.cpp
@@ -459,6 +460,7 @@ IF (WITH_3D)
3d/qgs3dmapcanvas.h
3d/qgs3dmapcanvasdockwidget.h
3d/qgs3dmapconfigwidget.h
3d/qgs3dmaptool.h
3d/qgsline3dsymbolwidget.h
3d/qgspoint3dsymbolwidget.h
3d/qgspolygon3dsymbolwidget.h

0 comments on commit 7679863

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