QGIS API Documentation 3.27.0-Master (95e00c50d2)
qgslayoutreportcontext.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutreportcontext.cpp
3 --------------------
4 begin : July 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18#include "qgsfeature.h"
19#include "qgslayout.h"
20#include "qgsvectorlayer.h"
21
23 : QObject( layout )
24 , mLayout( layout )
25{}
26
28{
29 mFeature = feature;
30 mGeometryCache.clear();
31 emit changed();
32}
33
35{
36 if ( !crs.isValid() )
37 {
38 // no projection, return the native geometry
39 return mFeature.geometry();
40 }
41
42 if ( !mLayer || !mFeature.isValid() || !mFeature.hasGeometry() )
43 {
44 return QgsGeometry();
45 }
46
47 if ( mLayer->crs() == crs )
48 {
49 // no projection, return the native geometry
50 return mFeature.geometry();
51 }
52
53 const auto it = mGeometryCache.constFind( crs.srsid() );
54 if ( it != mGeometryCache.constEnd() )
55 {
56 // we have it in cache, return it
57 return it.value();
58 }
59
60 QgsGeometry transformed = mFeature.geometry();
61 transformed.transform( QgsCoordinateTransform( mLayer->crs(), crs, mLayout->project() ) );
62 mGeometryCache[crs.srsid()] = transformed;
63 return transformed;
64}
65
67{
68 return mLayer;
69}
70
72{
73 mLayer = layer;
74 emit layerChanged( layer );
75 emit changed();
76}
77
78void QgsLayoutReportContext::setPredefinedScales( const QVector<qreal> &scales )
79{
80 mPredefinedScales = scales;
81 // make sure the list is sorted
82 std::sort( mPredefinedScales.begin(), mPredefinedScales.end() ); // clazy:exclude=detaching-member
83}
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
long srsid() const
Returns the internal CRS ID, if available.
Class for doing transforms between two map coordinate systems.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:216
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
void setLayer(QgsVectorLayer *layer)
Sets the vector layer associated with the layout's context.
void setFeature(const QgsFeature &feature)
Sets the current feature for evaluating the layout.
QgsFeature feature() const
Returns the current feature for evaluating the layout.
Q_DECL_DEPRECATED void setPredefinedScales(const QVector< qreal > &scales)
Sets the list of predefined scales to use with the layout.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
QgsLayoutReportContext(QgsLayout *layout)
Constructor for QgsLayoutReportContext.
void changed()
Emitted certain settings in the context is changed, e.g.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
QgsGeometry currentGeometry(const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Returns the current feature() geometry in the given crs.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
Represents a vector layer which manages a vector based data sets.
const QgsCoordinateReferenceSystem & crs