QGIS API Documentation  3.0.2-Girona (307d082)
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 
22  : QObject( layout )
23  , mLayout( layout )
24 {}
25 
27 {
28  mFeature = feature;
29  mGeometryCache.clear();
30  emit changed();
31 }
32 
34 {
35  if ( !crs.isValid() )
36  {
37  // no projection, return the native geometry
38  return mFeature.geometry();
39  }
40 
41  if ( !mLayer || !mFeature.isValid() || !mFeature.hasGeometry() )
42  {
43  return QgsGeometry();
44  }
45 
46  if ( mLayer->crs() == crs )
47  {
48  // no projection, return the native geometry
49  return mFeature.geometry();
50  }
51 
52  auto it = mGeometryCache.constFind( crs.srsid() );
53  if ( it != mGeometryCache.constEnd() )
54  {
55  // we have it in cache, return it
56  return it.value();
57  }
58 
59  QgsGeometry transformed = mFeature.geometry();
60  transformed.transform( QgsCoordinateTransform( mLayer->crs(), crs, mLayout->project() ) );
61  mGeometryCache[crs.srsid()] = transformed;
62  return transformed;
63 }
64 
66 {
67  return mLayer;
68 }
69 
71 {
72  mLayer = layer;
73  emit layerChanged( layer );
74  emit changed();
75 }
76 
77 void QgsLayoutReportContext::setPredefinedScales( const QVector<qreal> &scales )
78 {
79  mPredefinedScales = scales;
80  // make sure the list is sorted
81  std::sort( mPredefinedScales.begin(), mPredefinedScales.end() );
82 }
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:176
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:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
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.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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:128
QgsFeature feature() const
Returns the current feature for evaluating the layout.
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.