QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsrubberband3d.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrubberband3d.cpp
3 --------------------------------------
4 Date : June 2021
5 Copyright : (C) 2021 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 "qgsrubberband3d.h"
17
20#include "qgsmarkersymbol.h"
21#include "qgswindow3dengine.h"
22#include "qgslinevertexdata_p.h"
23#include "qgslinematerial_p.h"
24#include "qgsvertexid.h"
25#include "qgslinestring.h"
26#include "qgssymbollayer.h"
27#include "qgs3dmapsettings.h"
28
29#include <Qt3DCore/QEntity>
30
31#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
32#include <Qt3DRender/QAttribute>
33#include <Qt3DRender/QBuffer>
34#include <Qt3DRender/QGeometry>
35#else
36#include <Qt3DCore/QAttribute>
37#include <Qt3DCore/QBuffer>
38#include <Qt3DCore/QGeometry>
39#endif
40
41#include <Qt3DRender/QGeometryRenderer>
42#include <QColor>
43
44
46
47
48QgsRubberBand3D::QgsRubberBand3D( Qgs3DMapSettings &map, QgsWindow3DEngine *engine, Qt3DCore::QEntity *parentEntity, Qgis::GeometryType geometryType )
49 : mMapSettings( &map )
50 , mEngine( engine )
51 , mGeometryType( geometryType )
52{
53 if ( mGeometryType == Qgis::GeometryType::Line )
54 {
55 // Rubberband line
56 mLineEntity = new Qt3DCore::QEntity( parentEntity );
57
58 QgsLineVertexData dummyLineData;
59 mGeometry = dummyLineData.createGeometry( mLineEntity );
60
61 Q_ASSERT( mGeometry->attributes().count() == 2 );
62 mPositionAttribute = mGeometry->attributes().at( 0 );
63 mIndexAttribute = mGeometry->attributes().at( 1 );
64
65 mLineGeomRenderer = new Qt3DRender::QGeometryRenderer;
66 mLineGeomRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::LineStripAdjacency );
67 mLineGeomRenderer->setGeometry( mGeometry );
68 mLineGeomRenderer->setPrimitiveRestartEnabled( true );
69 mLineGeomRenderer->setRestartIndexValue( 0 );
70
71 mLineEntity->addComponent( mLineGeomRenderer );
72
73 mLineMaterial = new QgsLineMaterial;
74 mLineMaterial->setLineWidth( mWidth );
75 mLineMaterial->setLineColor( mColor );
76
77 QObject::connect( engine, &QgsAbstract3DEngine::sizeChanged, mLineMaterial, [this, engine] {
78 mLineMaterial->setViewportSize( engine->size() );
79 } );
80 mLineMaterial->setViewportSize( engine->size() );
81
82 mLineEntity->addComponent( mLineMaterial );
83 }
84
85 // Rubberband vertex markers
86 mMarkerEntity = new Qt3DCore::QEntity( parentEntity );
87 mMarkerGeometry = new QgsBillboardGeometry();
88 mMarkerGeometryRenderer = new Qt3DRender::QGeometryRenderer;
89 mMarkerGeometryRenderer->setPrimitiveType( Qt3DRender::QGeometryRenderer::Points );
90 mMarkerGeometryRenderer->setGeometry( mMarkerGeometry );
91 mMarkerGeometryRenderer->setVertexCount( mMarkerGeometry->count() );
92
93 setMarkerType( mMarkerType );
94 mMarkerEntity->addComponent( mMarkerGeometryRenderer );
95}
96
97QgsRubberBand3D::~QgsRubberBand3D()
98{
99 if ( mLineEntity )
100 mLineEntity->deleteLater();
101 mMarkerEntity->deleteLater();
102}
103
104float QgsRubberBand3D::width() const
105{
106 return mWidth;
107}
108
109void QgsRubberBand3D::setWidth( float width )
110{
111 mWidth = width;
112
113 if ( mGeometryType == Qgis::GeometryType::Line )
114 {
115 // when highlighting lines, the vertex markers should be wider
116 mLineMaterial->setLineWidth( width );
117 width *= 3;
118 }
119
120 mMarkerSymbol->setSize( width );
121 updateMarkerMaterial();
122}
123
124QColor QgsRubberBand3D::color() const
125{
126 return mColor;
127}
128
129void QgsRubberBand3D::setColor( QColor color )
130{
131 mColor = color;
132
133 if ( mGeometryType == Qgis::GeometryType::Line )
134 {
135 mLineMaterial->setLineColor( color );
136 mMarkerSymbol->setColor( color.lighter( 130 ) );
137 }
138 else
139 {
140 mMarkerSymbol->setColor( color );
141 }
142
143 if ( mMarkerSymbol->symbolLayerCount() > 0 && mMarkerSymbol->symbolLayer( 0 )->layerType() == QLatin1String( "SimpleMarker" ) )
144 {
145 static_cast<QgsMarkerSymbolLayer *>( mMarkerSymbol->symbolLayer( 0 ) )->setStrokeColor( color );
146 }
147 updateMarkerMaterial();
148}
149
150void QgsRubberBand3D::setMarkerType( MarkerType marker )
151{
152 mMarkerType = marker;
153
154 const QVariantMap props {
155 { QStringLiteral( "color" ), mGeometryType == Qgis::GeometryType::Line ? mColor.lighter( 130 ).name() : mColor.name() },
156 { QStringLiteral( "size_unit" ), QStringLiteral( "pixel" ) },
157 { QStringLiteral( "size" ), QString::number( mGeometryType == Qgis::GeometryType::Line ? mWidth * 3.f : mWidth ) },
158 { QStringLiteral( "outline_color" ), mColor.name() },
159 { QStringLiteral( "outline_width" ), QString::number( mGeometryType == Qgis::GeometryType::Line ? 0.5 : 1 ) },
160 { QStringLiteral( "name" ), mMarkerType == Square ? QStringLiteral( "square" ) : QStringLiteral( "circle" ) }
161 };
162
163 mMarkerSymbol.reset( QgsMarkerSymbol::createSimple( props ) );
164 updateMarkerMaterial();
165}
166
167QgsRubberBand3D::MarkerType QgsRubberBand3D::markerType() const
168{
169 return mMarkerType;
170}
171
172void QgsRubberBand3D::reset()
173{
174 mLineString.clear();
175 updateGeometry();
176}
177
178void QgsRubberBand3D::addPoint( const QgsPoint &pt )
179{
180 mLineString.addVertex( pt );
181 updateGeometry();
182}
183
184void QgsRubberBand3D::removeLastPoint()
185{
186 const int lastVertexIndex = mLineString.numPoints() - 1;
187 mLineString.deleteVertex( QgsVertexId( 0, 0, lastVertexIndex ) );
188 updateGeometry();
189}
190
191void QgsRubberBand3D::moveLastPoint( const QgsPoint &pt )
192{
193 const int lastVertexIndex = mLineString.numPoints() - 1;
194 mLineString.moveVertex( QgsVertexId( 0, 0, lastVertexIndex ), pt );
195 updateGeometry();
196}
197
198void QgsRubberBand3D::updateGeometry()
199{
200 QgsLineVertexData lineData;
201 lineData.withAdjacency = true;
203 lineData.addLineString( mLineString );
204
205 if ( mGeometryType == Qgis::GeometryType::Line )
206 {
207 mPositionAttribute->buffer()->setData( lineData.createVertexBuffer() );
208 mIndexAttribute->buffer()->setData( lineData.createIndexBuffer() );
209 mLineGeomRenderer->setVertexCount( lineData.indexes.count() );
210 }
211
212 // first entry is empty for primitive restart
213 lineData.vertices.pop_front();
214
215 // we may not want a marker on the last point as it's tracked by the mouse cursor
216 if ( mHideLastMarker && !lineData.vertices.isEmpty() )
217 lineData.vertices.pop_back();
218
219 mMarkerGeometry->setPoints( lineData.vertices );
220 mMarkerGeometryRenderer->setVertexCount( lineData.vertices.count() );
221}
222
223void QgsRubberBand3D::updateMarkerMaterial()
224{
225 mMarkerMaterial = new QgsPoint3DBillboardMaterial();
226 mMarkerMaterial->setTexture2DFromSymbol( mMarkerSymbol.get(), Qgs3DRenderContext::fromMapSettings( mMapSettings ) );
227 mMarkerEntity->addComponent( mMarkerMaterial );
228
229 //TODO: QgsAbstract3DEngine::sizeChanged should have const QSize &size param
230 QObject::connect( mEngine, &QgsAbstract3DEngine::sizeChanged, mMarkerMaterial, [this] {
231 mMarkerMaterial->setViewportSize( mEngine->size() );
232 } );
233 mMarkerMaterial->setViewportSize( mEngine->size() );
234}
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
GeometryType
The geometry types are used to group Qgis::WkbType in a coarse way.
Definition qgis.h:337
@ Vertex
Clamp every vertex of feature.
static Qgs3DRenderContext fromMapSettings(const Qgs3DMapSettings *mapSettings)
Creates an initialized Qgs3DRenderContext instance from given Qgs3DMapSettings.
QgsVector3D origin() const
Returns coordinates in map CRS at which 3D scene has origin (0,0,0)
void sizeChanged()
Emitted after a call to setSize()
Abstract base class for marker symbol layers.
static QgsMarkerSymbol * createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
QSize size() const override
Returns size of the engine's rendering area in pixels.
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30