QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:88
QgsQuickFeatureHighlight::widthChanged
void widthChanged()
Pen width of the highlighted geometry (feature).
QgsQuickMapTransform::setMapSettings
void setMapSettings(QgsQuickMapSettings *mapSettings)
Associated map settings.
Definition: qgsquickmaptransform.cpp:30
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsQuickFeatureHighlight::QgsQuickFeatureHighlight
QgsQuickFeatureHighlight(QQuickItem *parent=nullptr)
Creates a new feature highlight.
Definition: qgsquickfeaturehighlight.cpp:25
QgsQuickFeatureLayerPair::layer
QgsVectorLayer layer
Vector layer to which the feature belongs.
Definition: qgsquickfeaturelayerpair.h:49
QgsQuickMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs
CRS of destination coordinate reference system.
Definition: qgsquickmapsettings.h:105
QgsCsException
Definition: qgsexception.h:65
QgsQuickHighlightSGNode
Definition: qgsquickhighlightsgnode.h:38
QgsQuickFeatureHighlight::featureLayerPairChanged
void featureLayerPairChanged()
Feature to highlight.
QgsQuickFeatureHighlight::colorChanged
void colorChanged()
Color of the highlighted geometry (feature).
qgsquickfeaturehighlight.h
QgsQuickFeatureLayerPair::feature
QgsFeature feature
Feature that belongs to layer.
Definition: qgsquickfeaturelayerpair.h:56
qgsvectorlayer.h
QgsQuickMapSettings::transformContext
Q_INVOKABLE QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsquickmapsettings.cpp:70
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
qgsquickhighlightsgnode.h
QgsFeature
Definition: qgsfeature.h:55
QgsQuickFeatureHighlight::mapSettingsChanged
void mapSettingsChanged()
Associated map settings.
QgsQuickFeatureLayerPair::isValid
bool isValid() const
Whether.
Definition: qgsquickfeaturelayerpair.cpp:44
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
qgsquickmapsettings.h