/
qgslayoutitem3dmap.cpp
173 lines (135 loc) · 4.95 KB
/
qgslayoutitem3dmap.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************
qgslayoutitem3dmap.cpp
--------------------------------------
Date : August 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 "qgslayoutitem3dmap.h"
#include "qgs3dmapscene.h"
#include "qgs3dutils.h"
#include "qgscameracontroller.h"
#include "qgslayout.h"
#include "qgslayoutitemregistry.h"
#include "qgsoffscreen3dengine.h"
QgsLayoutItem3DMap::QgsLayoutItem3DMap( QgsLayout *layout )
: QgsLayoutItem( layout )
{
connect( this, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItem3DMap::onSizePositionChanged );
}
QgsLayoutItem3DMap::~QgsLayoutItem3DMap() = default;
QgsLayoutItem3DMap *QgsLayoutItem3DMap::create( QgsLayout *layout )
{
return new QgsLayoutItem3DMap( layout );
}
int QgsLayoutItem3DMap::type() const
{
return QgsLayoutItemRegistry::Layout3DMap;
}
void QgsLayoutItem3DMap::draw( QgsLayoutItemRenderContext &context )
{
QgsRenderContext &ctx = context.renderContext();
QPainter *painter = ctx.painter();
int w = static_cast<int>( std::ceil( rect().width() * ctx.scaleFactor() ) );
int h = static_cast<int>( std::ceil( rect().height() * ctx.scaleFactor() ) );
QRect r( 0, 0, w, h );
painter->save();
if ( !mSettings )
{
painter->drawText( r, Qt::AlignCenter, tr( "Scene not set" ) );
painter->restore();
return;
}
if ( !mCapturedImage.isNull() )
{
painter->drawImage( r, mCapturedImage );
painter->restore();
return;
}
// we do not have a cached image of the rendered scene - let's request one from the engine
painter->drawText( r, Qt::AlignCenter, tr( "Loading" ) );
painter->restore();
QSizeF sizePixels = mLayout->renderContext().measurementConverter().convert( sizeWithUnits(), QgsUnitTypes::LayoutPixels ).toQSizeF();
QSize sizePixelsInt = QSize( static_cast<int>( std::ceil( sizePixels.width() ) ),
static_cast<int>( std::ceil( sizePixels.height() ) ) );
if ( !mEngine )
{
mEngine.reset( new QgsOffscreen3DEngine );
connect( mEngine.get(), &QgsAbstract3DEngine::imageCaptured, this, &QgsLayoutItem3DMap::onImageCaptured );
mEngine->setSize( sizePixelsInt );
mScene = new Qgs3DMapScene( *mSettings, mEngine.get() );
connect( mScene, &Qgs3DMapScene::sceneStateChanged, this, &QgsLayoutItem3DMap::onSceneStateChanged );
mEngine->setRootEntity( mScene );
}
if ( mEngine->size() != sizePixelsInt )
mEngine->setSize( sizePixelsInt );
mScene->cameraController()->setCameraPose( mCameraPose );
onSceneStateChanged();
}
void QgsLayoutItem3DMap::onImageCaptured( const QImage &img )
{
mCapturedImage = img;
update();
}
void QgsLayoutItem3DMap::onSceneStateChanged()
{
if ( mCapturedImage.isNull() && mScene->sceneState() == Qgs3DMapScene::Ready )
{
mEngine->requestCaptureImage();
}
}
void QgsLayoutItem3DMap::onSizePositionChanged()
{
mCapturedImage = QImage();
update();
}
bool QgsLayoutItem3DMap::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
if ( mSettings )
{
QDomElement elemSettings = mSettings->writeXml( document, context );
element.appendChild( elemSettings );
}
QDomElement elemCameraPose = mCameraPose.writeXml( document );
element.appendChild( elemCameraPose );
return true;
}
bool QgsLayoutItem3DMap::readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context )
{
Q_UNUSED( document );
QDomElement elemSettings = element.firstChildElement( "qgis3d" );
if ( !elemSettings.isNull() )
{
mSettings.reset( new Qgs3DMapSettings );
mSettings->readXml( elemSettings, context );
if ( mLayout->project() )
mSettings->resolveReferences( *mLayout->project() );
}
QDomElement elemCameraPose = element.firstChildElement( "camera-pose" );
if ( !elemCameraPose.isNull() )
mCameraPose.readXml( elemCameraPose );
return true;
}
void QgsLayoutItem3DMap::setMapSettings( Qgs3DMapSettings *settings )
{
mSettings.reset( settings );
mEngine.reset();
mScene = nullptr;
mCapturedImage = QImage();
update();
}
void QgsLayoutItem3DMap::setCameraPose( const QgsCameraPose &pose )
{
if ( mCameraPose == pose )
return;
mCameraPose = pose;
mCapturedImage = QImage();
update();
}