QGIS API Documentation  3.6.0-Noosa (5873452)
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  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 }
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:183
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.
QgsGeometry currentGeometry(const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem()) const
Returns the current feature() geometry in the given crs.
void changed()
Emitted certain settings in the context is changed, e.g.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
void setPredefinedScales(const QVector< qreal > &scales)
Sets the list of predefined scales to use with the layout.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout&#39;s context.
void setLayer(QgsVectorLayer *layer)
Sets the vector layer associated with the layout&#39;s context.
QgsLayoutReportContext(QgsLayout *layout)
Constructor for QgsLayoutReportContext.
void layerChanged(QgsVectorLayer *layer)
Emitted when the context&#39;s layer is changed.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:130
QgsFeature feature() const
Returns the current feature for evaluating the layout.
QgsGeometry geometry
Definition: qgsfeature.h:67
long srsid() const
Returns the internal CRS ID, if available.
Represents a vector layer which manages a vector based data sets.
void setFeature(const QgsFeature &feature)
Sets the current feature for evaluating the layout.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.