QGIS API Documentation 4.3.0-Master (621ced71bd7)
Loading...
Searching...
No Matches
qgsrubberband_impl.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrubberband_impl.cpp
3 --------------------------------------
4 Date : July 2026
5 Copyright : (C) 2026 by Nyall Dawson
6 Email : nyall.dawson@gmail.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 "qgsrubberband_impl.h"
17
19#include "qgsmapcanvas.h"
20#include "qgstextrenderer.h"
21#include "qgsvectorlayer.h"
22
25 , mLayer( layer )
26{
27 QgsFeature f;
28 QgsFeatureIterator it = mLayer->getFeatures( qgis::listToSet( fids ) );
29 mFeatures.resize( fids.size() );
30 int index = 0;
31 while ( it.nextFeature( f ) )
32 {
33 mFeatures[index] = f;
34 index++;
35 }
36}
37
39{
41 if ( !rubberBand || !mLayer || !mLayer->labelsEnabled() || !mLayer->labeling() || mFeatures.empty() )
42 return;
43
44 QgsMapCanvas *canvas = rubberBand->canvas();
45 if ( !canvas )
46 return;
47
48 // fetch current rubber band geometry
49 QgsGeometry previewGeom = rubberBand->asGeometry();
50 if ( previewGeom.isEmpty() )
51 return;
52
54
56
57 QgsExpressionContextScopePopper scopePopper( context.expressionContext(), featureScope );
59
60 // set up a super minimal, fast labeling engine. Why? Well...
61 // we can't just use QgsTextRenderer here, as that won't fully reflect the actual appearance
62 // of the labels actually rendered for the features. Eg it won't respect settings like text repeat distances,
63 // anchor points, overrun, curved placement modes, etc.
64 // So to get a useful preview we need to use the actual labeling engine to render the label.
65 // But we also don't need a lot of the weight of pal labeling, eg we only need to render the
66 // single least-cost candidate for each feature, we won't consider obstacles or overlaps, we don't
67 // need the label search tree, etc.
68 // Disabling all this gives us a very lean label engine, which is quite cheap to use and gives
69 // perfect 1:1 previews of the actual label to be rendered.
70 QgsMapSettings mapSettings = canvas->mapSettings();
72 QgsLabelingEngineSettings labelSettings = mapSettings.labelingEngineSettings();
74 labelSettings.setFlag( Qgis::LabelingFlag::IgnoreObstacles, true );
76 labelSettings.setFlag( Qgis::LabelingFlag::IgnoreOverlaps, true );
77 mapSettings.setLabelingEngineSettings( labelSettings );
78 QgsDefaultLabelingEngine engine( mapSettings );
79
80 QgsPalLayerSettings settings = mLayer->labeling()->settings();
81 auto provider = new QgsVectorLayerLabelProvider( mLayer, QString(), false, &settings );
82 engine.addProvider( provider );
83
84 QSet<QString> attributeNames;
85 if ( !provider->prepare( context, attributeNames ) )
86 {
87 return;
88 }
89
90 const QVector< QgsGeometry> previewGeomParts = previewGeom.asGeometryCollection();
91 int index = 0;
92 for ( const QgsFeature &feature : std::as_const( mFeatures ) )
93 {
94 if ( index >= previewGeomParts.size() )
95 break;
96
97 // each individual part of the rubber band's geometry corresponds to an individual feature,
98 // so we re-associate them here:
99 QgsFeature tempFeature = feature;
100 tempFeature.setGeometry( previewGeomParts.at( index ) );
101
102 featureScope->setFeature( tempFeature );
103 featureScope->setFields( tempFeature.fields() );
104 provider->registerFeature( tempFeature, context );
105 index++;
106 }
107
108 QgsScopedQPainterState painterState( context.painter() );
109 context.painter()->translate( -rubberBand->pos() );
110
111 // running the engine calculates the placement and does the actual rendering:
112 engine.run( context );
113}
@ DisableSearchTree
Disable the creation of the label search tree.
Definition qgis.h:3053
@ IgnoreObstacles
Disable obstacle handling.
Definition qgis.h:3050
@ SingleCandidateOnly
Generate only the single least-cost candidate for each feature. Useful for fast labeling,...
Definition qgis.h:3051
@ IgnoreOverlaps
Disable overlap detection and search solver, immediately returning lowest cost candidate per feature.
Definition qgis.h:3052
@ RecordProfile
Enable run-time profiling while rendering.
Definition qgis.h:2963
@ Reverse
Reverse/inverse transform (from destination to source).
Definition qgis.h:2864
@ RecordProfile
Enable run-time profiling while rendering.
Definition qgis.h:2928
Default QgsLabelingEngine implementation, which completes the whole labeling operation (including lab...
void run(QgsRenderContext &context) override
Runs the labeling job.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFields fields
Definition qgsfeature.h:65
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
QVector< QgsGeometry > asGeometryCollection() const
Returns contents of the geometry as a list of geometries.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Stores global configuration for labeling engine.
void setFlag(Qgis::LabelingFlag f, bool enabled=true)
Sets whether a particual flag is enabled.
QString addProvider(QgsAbstractLabelProvider *provider)
Adds a provider of label features.
Map canvas is a class for displaying all GIS data types on a canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Contains configuration for rendering maps.
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns the global configuration of the labeling engine.
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Returns the coordinate transform from layer's CRS to destination CRS.
void setLabelingEngineSettings(const QgsLabelingEngineSettings &settings)
Sets the global configuration of the labeling engine.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
Contains settings for how a map layer will be labeled.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
QgsRubberBandPreviewItem(QgsRubberBand *rubberBand)
Constructor for a QgsRubberBandPreviewItem, attached to the specified rubberBand.
QgsRubberBand * rubberBand()
Returns the rubber band associated with the item.
Responsible for drawing transient features (e.g.
Scoped object for saving and restoring a QPainter object's state.
Implements a label provider for vector layers.
void render(QgsRenderContext &context) final
Renders the custom preview overlay using the specified render context.
QgsVectorLayerLabelRubberBandPreview(QgsRubberBand *rubberBand, const QList< QgsFeatureId > &fids, QgsVectorLayer *layer)
Constructor for QgsVectorLayerLabelRubberBandPreview.
Represents a vector layer which manages a vector based dataset.