Skip to content

Commit e83ce54

Browse files
pblottierewonder-sk
authored andcommitted
Feature selection is also displayed in 3D map view for polygons
1 parent c6ff2f6 commit e83ce54

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

src/3d/polygonentity.cpp

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include "terraingenerator.h"
77
#include "utils.h"
88

9-
#include <Qt3DExtras/QPhongMaterial>
10-
#include <Qt3DRender/QGeometryRenderer>
119
#include <Qt3DCore/QTransform>
1210

1311
#include "qgsvectorlayer.h"
@@ -18,19 +16,80 @@
1816
PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, Qt3DCore::QNode *parent )
1917
: Qt3DCore::QEntity( parent )
2018
{
21-
QgsPointXY origin( map.originX, map.originY );
19+
addEntityForSelectedPolygons( map, layer, symbol );
20+
addEntityForNotSelectedPolygons( map, layer, symbol );
21+
}
22+
23+
void PolygonEntity::addEntityForSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol )
24+
{
25+
// build the default material
26+
Qt3DExtras::QPhongMaterial *mat = material( symbol );
27+
28+
// update the material with selection colors
29+
mat->setDiffuse( map.selectionColor() );
30+
mat->setAmbient( map.selectionColor().darker() );
31+
32+
// build a transform function
33+
Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
34+
tform->setTranslation( QVector3D( 0, 0, 0 ) );
35+
36+
// build the feature request to select features
37+
QgsFeatureRequest req;
38+
req.setDestinationCrs( map.crs );
39+
req.setFilterFids( layer->selectedFeatureIds() );
40+
41+
// build the entity
42+
PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req );
43+
entity->addComponent( mat );
44+
entity->addComponent( tform );
45+
entity->setParent( this );
46+
}
47+
48+
void PolygonEntity::addEntityForNotSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol )
49+
{
50+
// build the default material
51+
Qt3DExtras::QPhongMaterial *mat = material( symbol );
52+
53+
// build a transform function
54+
Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
55+
tform->setTranslation( QVector3D( 0, 0, 0 ) );
56+
57+
// build the feature request to select features
58+
QgsFeatureRequest req;
59+
req.setDestinationCrs( map.crs );
60+
61+
QgsFeatureIds notSelected = layer->allFeatureIds();
62+
notSelected.subtract( layer->selectedFeatureIds() );
63+
req.setFilterFids( notSelected );
64+
65+
// build the entity
66+
PolygonEntityNode *entity = new PolygonEntityNode( map, layer, symbol, req );
67+
entity->addComponent( mat );
68+
entity->addComponent( tform );
69+
entity->setParent( this );
70+
}
2271

72+
Qt3DExtras::QPhongMaterial *PolygonEntity::material( const Polygon3DSymbol &symbol ) const
73+
{
2374
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
2475
material->setAmbient( symbol.material.ambient() );
2576
material->setDiffuse( symbol.material.diffuse() );
2677
material->setSpecular( symbol.material.specular() );
2778
material->setShininess( symbol.material.shininess() );
28-
addComponent( material );
79+
return material;
80+
}
2981

82+
PolygonEntityNode::PolygonEntityNode( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
83+
: Qt3DCore::QEntity( parent )
84+
{
85+
addComponent( renderer( map, symbol, layer, req ) );
86+
}
87+
88+
Qt3DRender::QGeometryRenderer *PolygonEntityNode::renderer( const Map3D &map, const Polygon3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request )
89+
{
90+
QgsPointXY origin( map.originX, map.originY );
3091
QList<QgsPolygonV2 *> polygons;
3192
QgsFeature f;
32-
QgsFeatureRequest request;
33-
request.setDestinationCrs( map.crs );
3493
QgsFeatureIterator fi = layer->getFeatures( request );
3594
while ( fi.nextFeature( f ) )
3695
{
@@ -62,14 +121,11 @@ PolygonEntity::PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Pol
62121
qDebug() << "not a polygon";
63122
}
64123

65-
geometry = new PolygonGeometry;
66-
geometry->setPolygons( polygons, origin, symbol.extrusionHeight );
124+
mGeometry = new PolygonGeometry;
125+
mGeometry->setPolygons( polygons, origin, symbol.extrusionHeight );
67126

68127
Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
69-
renderer->setGeometry( geometry );
70-
addComponent( renderer );
128+
renderer->setGeometry( mGeometry );
71129

72-
Qt3DCore::QTransform *tform = new Qt3DCore::QTransform;
73-
tform->setTranslation( QVector3D( 0, 0, 0 ) );
74-
addComponent( tform );
130+
return renderer;
75131
}

src/3d/polygonentity.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
#define POLYGONENTITY_H
33

44
#include <Qt3DCore/QEntity>
5+
#include <Qt3DExtras/QPhongMaterial>
6+
#include <Qt3DRender/QGeometryRenderer>
57

68
class Map3D;
79
class PolygonGeometry;
810
class Polygon3DSymbol;
911

1012
class QgsPointXY;
1113
class QgsVectorLayer;
14+
class QgsFeatureRequest;
1215

1316

1417
//! Entity that handles rendering of polygons
@@ -17,7 +20,22 @@ class PolygonEntity : public Qt3DCore::QEntity
1720
public:
1821
PolygonEntity( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, Qt3DCore::QNode *parent = nullptr );
1922

20-
PolygonGeometry *geometry;
23+
private:
24+
void addEntityForSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol );
25+
void addEntityForNotSelectedPolygons( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol );
26+
27+
Qt3DExtras::QPhongMaterial *material( const Polygon3DSymbol &symbol ) const;
28+
};
29+
30+
class PolygonEntityNode : public Qt3DCore::QEntity
31+
{
32+
public:
33+
PolygonEntityNode( const Map3D &map, QgsVectorLayer *layer, const Polygon3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent = nullptr );
34+
35+
private:
36+
Qt3DRender::QGeometryRenderer *renderer( const Map3D &map, const Polygon3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request );
37+
38+
PolygonGeometry *mGeometry;
2139
};
2240

2341
#endif // POLYGONENTITY_H

0 commit comments

Comments
 (0)