Skip to content

Commit c6ff2f6

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

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/3d/lineentity.cpp

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

9-
#include <Qt3DExtras/QPhongMaterial>
10-
#include <Qt3DRender/QGeometryRenderer>
11-
129
#include "qgsvectorlayer.h"
1310
#include "qgsmultipolygon.h"
1411
#include "qgsgeos.h"
@@ -17,14 +14,70 @@
1714
LineEntity::LineEntity( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol, Qt3DCore::QNode *parent )
1815
: Qt3DCore::QEntity( parent )
1916
{
20-
QgsPointXY origin( map.originX, map.originY );
17+
addEntityForSelectedLines( map, layer, symbol );
18+
addEntityForNotSelectedLines( map, layer, symbol );
19+
}
2120

21+
Qt3DExtras::QPhongMaterial *LineEntity::material( const Line3DSymbol &symbol ) const
22+
{
2223
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial;
24+
2325
material->setAmbient( symbol.material.ambient() );
2426
material->setDiffuse( symbol.material.diffuse() );
2527
material->setSpecular( symbol.material.specular() );
2628
material->setShininess( symbol.material.shininess() );
27-
addComponent( material );
29+
30+
return material;
31+
}
32+
33+
void LineEntity::addEntityForSelectedLines( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol )
34+
{
35+
// build the default material
36+
Qt3DExtras::QPhongMaterial *mat = material( symbol );
37+
38+
// update the material with selection colors
39+
mat->setDiffuse( map.selectionColor() );
40+
mat->setAmbient( map.selectionColor().darker() );
41+
42+
// build the feature request to select features
43+
QgsFeatureRequest req;
44+
req.setDestinationCrs( map.crs );
45+
req.setFilterFids( layer->selectedFeatureIds() );
46+
47+
// build the entity
48+
LineEntityNode *entity = new LineEntityNode( map, layer, symbol, req );
49+
entity->addComponent( mat );
50+
entity->setParent( this );
51+
}
52+
53+
void LineEntity::addEntityForNotSelectedLines( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol )
54+
{
55+
// build the default material
56+
Qt3DExtras::QPhongMaterial *mat = material( symbol );
57+
58+
// build the feature request to select features
59+
QgsFeatureRequest req;
60+
req.setDestinationCrs( map.crs );
61+
62+
QgsFeatureIds notSelected = layer->allFeatureIds();
63+
notSelected.subtract( layer->selectedFeatureIds() );
64+
req.setFilterFids( notSelected );
65+
66+
// build the entity
67+
LineEntityNode *entity = new LineEntityNode( map, layer, symbol, req );
68+
entity->addComponent( mat );
69+
entity->setParent( this );
70+
}
71+
72+
LineEntityNode::LineEntityNode( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent )
73+
: Qt3DCore::QEntity( parent )
74+
{
75+
addComponent( renderer( map, symbol, layer, req ) );
76+
}
77+
78+
Qt3DRender::QGeometryRenderer *LineEntityNode::renderer( const Map3D &map, const Line3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &request )
79+
{
80+
QgsPointXY origin( map.originX, map.originY );
2881

2982
// TODO: configurable
3083
int nSegments = 4;
@@ -34,8 +87,6 @@ LineEntity::LineEntity( const Map3D &map, QgsVectorLayer *layer, const Line3DSym
3487

3588
QList<QgsPolygonV2 *> polygons;
3689
QgsFeature f;
37-
QgsFeatureRequest request;
38-
request.setDestinationCrs( map.crs );
3990
QgsFeatureIterator fi = layer->getFeatures( request );
4091
while ( fi.nextFeature( f ) )
4192
{
@@ -68,10 +119,11 @@ LineEntity::LineEntity( const Map3D &map, QgsVectorLayer *layer, const Line3DSym
68119
}
69120
}
70121

71-
geometry = new PolygonGeometry;
72-
geometry->setPolygons( polygons, origin, /*symbol.height,*/ symbol.extrusionHeight );
122+
mGeometry = new PolygonGeometry;
123+
mGeometry->setPolygons( polygons, origin, /*symbol.height,*/ symbol.extrusionHeight );
73124

74125
Qt3DRender::QGeometryRenderer *renderer = new Qt3DRender::QGeometryRenderer;
75-
renderer->setGeometry( geometry );
76-
addComponent( renderer );
126+
renderer->setGeometry( mGeometry );
127+
128+
return renderer;
77129
}

src/3d/lineentity.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#define LINEENTITY_H
33

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

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

1012
class QgsVectorLayer;
13+
class QgsFeatureRequest;
1114

1215

1316
//! Entity that handles rendering of linestrings
@@ -16,7 +19,22 @@ class LineEntity : public Qt3DCore::QEntity
1619
public:
1720
LineEntity( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol, Qt3DCore::QNode *parent = nullptr );
1821

19-
PolygonGeometry *geometry;
22+
private:
23+
void addEntityForSelectedLines( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol );
24+
void addEntityForNotSelectedLines( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol );
25+
26+
Qt3DExtras::QPhongMaterial *material( const Line3DSymbol &symbol ) const;
27+
};
28+
29+
class LineEntityNode : public Qt3DCore::QEntity
30+
{
31+
public:
32+
LineEntityNode( const Map3D &map, QgsVectorLayer *layer, const Line3DSymbol &symbol, const QgsFeatureRequest &req, Qt3DCore::QNode *parent = nullptr );
33+
34+
private:
35+
Qt3DRender::QGeometryRenderer *renderer( const Map3D &map, const Line3DSymbol &symbol, const QgsVectorLayer *layer, const QgsFeatureRequest &req );
36+
37+
PolygonGeometry *mGeometry;
2038
};
2139

2240
#endif // LINEENTITY_H

0 commit comments

Comments
 (0)