/
qgs3dmapcanvas.cpp
91 lines (71 loc) · 2.6 KB
/
qgs3dmapcanvas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************************************************************
qgs3dmapcanvas.cpp
--------------------------------------
Date : July 2017
Copyright : (C) 2017 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 "qgs3dmapcanvas.h"
#include <QBoxLayout>
#include <Qt3DExtras/Qt3DWindow>
#include "qgscameracontroller.h"
#include "qgs3dmapsettings.h"
#include "qgs3dmapscene.h"
Qgs3DMapCanvas::Qgs3DMapCanvas( QWidget *parent )
: QWidget( parent )
{
mWindow3D = new Qt3DExtras::Qt3DWindow;
mContainer = QWidget::createWindowContainer( mWindow3D );
QHBoxLayout *hLayout = new QHBoxLayout( this );
hLayout->setMargin( 0 );
hLayout->addWidget( mContainer, 1 );
mWindow3D->setCursor( Qt::OpenHandCursor );
}
Qgs3DMapCanvas::~Qgs3DMapCanvas()
{
delete mMap;
}
void Qgs3DMapCanvas::resizeEvent( QResizeEvent *ev )
{
QWidget::resizeEvent( ev );
if ( !mScene )
return;
QRect viewportRect( QPoint( 0, 0 ), size() );
mScene->cameraController()->setViewport( viewportRect );
}
void Qgs3DMapCanvas::setMap( Qgs3DMapSettings *map )
{
// TODO: eventually we want to get rid of this
Q_ASSERT( !mMap );
Q_ASSERT( !mScene );
QRect viewportRect( QPoint( 0, 0 ), size() );
Qgs3DMapScene *newScene = new Qgs3DMapScene( *map, mWindow3D->defaultFrameGraph(), mWindow3D->renderSettings(), mWindow3D->camera(), viewportRect );
mWindow3D->setRootEntity( newScene );
if ( mScene )
mScene->deleteLater();
mScene = newScene;
delete mMap;
mMap = map;
resetView();
}
QgsCameraController *Qgs3DMapCanvas::cameraController()
{
return mScene ? mScene->cameraController() : nullptr;
}
void Qgs3DMapCanvas::resetView()
{
mScene->viewZoomFull();
}
void Qgs3DMapCanvas::setViewFromTop( const QgsPointXY ¢er, float distance, float rotation )
{
float worldX = center.x() - mMap->originX();
float worldY = center.y() - mMap->originY();
mScene->cameraController()->setViewFromTop( worldX, -worldY, distance, rotation );
}