QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsquickfeaturehighlight.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsqguickfeaturehighlight.cpp
3  --------------------------------------
4  Date : May 2018
5  Copyright : (C) 2018 by Peter Petrik
6  Email : zilolv 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 <memory>
17 
18 #include "qgsvectorlayer.h"
19 
21 #include "qgsquickmapsettings.h"
23 
24 
26  : QQuickItem( parent )
27 {
28  setFlags( QQuickItem::ItemHasContents );
29  setAntialiasing( true );
30 
31  // transform to device coords
32  mTransform.appendToItem( this );
33 
34  connect( this, &QgsQuickFeatureHighlight::mapSettingsChanged, this, &QgsQuickFeatureHighlight::onMapSettingsChanged );
35  connect( this, &QgsQuickFeatureHighlight::featureLayerPairChanged, this, &QgsQuickFeatureHighlight::markDirty );
36  connect( this, &QgsQuickFeatureHighlight::colorChanged, this, &QgsQuickFeatureHighlight::markDirty );
37  connect( this, &QgsQuickFeatureHighlight::widthChanged, this, &QgsQuickFeatureHighlight::markDirty );
38 }
39 
40 void QgsQuickFeatureHighlight::markDirty()
41 {
42  mDirty = true;
43  update();
44 }
45 
46 void QgsQuickFeatureHighlight::onMapSettingsChanged()
47 {
48  mTransform.setMapSettings( mMapSettings );
49  markDirty();
50 }
51 
52 QSGNode *QgsQuickFeatureHighlight::updatePaintNode( QSGNode *n, QQuickItem::UpdatePaintNodeData * )
53 {
54  if ( !mDirty || !mMapSettings || !mFeatureLayerPair.isValid() )
55  return n;
56 
57  delete n;
58  n = new QSGNode;
59 
60  QgsVectorLayer *layer = mFeatureLayerPair.layer();
61  Q_ASSERT( layer ); // we checked the validity of feature-layer pair
62  QgsCoordinateTransform transf( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );
63 
64  QgsFeature feature = mFeatureLayerPair.feature();
65  if ( feature.hasGeometry() )
66  {
67  QgsGeometry geom( feature.geometry() );
68  try
69  {
70  geom.transform( transf );
71  std::unique_ptr<QgsQuickHighlightSGNode> rb( new QgsQuickHighlightSGNode( geom, mColor, mWidth ) );
72  rb->setFlag( QSGNode::OwnedByParent );
73  n->appendChildNode( rb.release() );
74  }
75  catch ( QgsCsException &e )
76  {
77  Q_UNUSED( e )
78  // Caught an error in transform
79  }
80  }
81  mDirty = false;
82 
83  return n;
84 }
QgsQuickFeatureHighlight(QQuickItem *parent=nullptr)
Creates a new feature highlight.
This is used to transform (render) QgsGeometry to node for QtQuick scene graph.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
void colorChanged()
Color of the highlighted geometry (feature).
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void featureLayerPairChanged()
Feature to highlight.
void mapSettingsChanged()
Associated map settings.
void widthChanged()
Pen width of the highlighted geometry (feature).
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
QgsFeature feature
Feature that belongs to layer.
void setMapSettings(QgsQuickMapSettings *mapSettings)
Associated map settings.
QgsVectorLayer layer
Vector layer to which the feature belongs.
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:85