QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
17 #include "qgslayoutreportcontext.h"
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 
78 void 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 }
QgsLayoutReportContext::setLayer
void setLayer(QgsVectorLayer *layer)
Sets the vector layer associated with the layout's context.
Definition: qgslayoutreportcontext.cpp:71
QgsGeometry::transform
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.
Definition: qgsgeometry.cpp:3128
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgsfeature.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsCoordinateReferenceSystem::srsid
long srsid() const
Returns the internal CRS ID, if available.
Definition: qgscoordinatereferencesystem.cpp:1185
QgsLayoutReportContext::setFeature
void setFeature(const QgsFeature &feature)
Sets the current feature for evaluating the layout.
Definition: qgslayoutreportcontext.cpp:27
QgsFeature::isValid
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:216
QgsLayoutReportContext::feature
QgsFeature feature() const
Returns the current feature for evaluating the layout.
Definition: qgslayoutreportcontext.h:62
QgsCoordinateReferenceSystem::isValid
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Definition: qgscoordinatereferencesystem.cpp:977
QgsLayoutReportContext::changed
void changed()
Emitted certain settings in the context is changed, e.g.
qgslayout.h
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
qgsvectorlayer.h
QgsLayoutReportContext::currentGeometry
QgsGeometry currentGeometry(const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Returns the current feature() geometry in the given crs.
Definition: qgslayoutreportcontext.cpp:34
QgsLayoutReportContext::setPredefinedScales
Q_DECL_DEPRECATED void setPredefinedScales(const QVector< qreal > &scales)
Sets the list of predefined scales to use with the layout.
Definition: qgslayoutreportcontext.cpp:78
qgslayoutreportcontext.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsLayoutReportContext::QgsLayoutReportContext
QgsLayoutReportContext(QgsLayout *layout)
Constructor for QgsLayoutReportContext.
Definition: qgslayoutreportcontext.cpp:22
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsLayout::project
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
QgsLayoutReportContext::layerChanged
void layerChanged(QgsVectorLayer *layer)
Emitted when the context's layer is changed.
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsLayoutReportContext::layer
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
Definition: qgslayoutreportcontext.cpp:66