QGIS API Documentation  2.12.0-Lyon
qgsmaphittest.cpp
Go to the documentation of this file.
1 #include "qgsmaphittest.h"
2 
3 #include "qgsmaplayerregistry.h"
4 #include "qgsrendercontext.h"
6 #include "qgsrendererv2.h"
8 #include "qgsvectorlayer.h"
10 
12  : mSettings( settings )
13 {
14 }
15 
16 
18 {
19  // TODO: do we need this temp image?
21  tmpImage.setDotsPerMeterX( mSettings.outputDpi() * 25.4 );
22  tmpImage.setDotsPerMeterY( mSettings.outputDpi() * 25.4 );
23  QPainter painter( &tmpImage );
24 
26  context.setPainter( &painter ); // we are not going to draw anything, but we still need a working painter
27 
28  Q_FOREACH ( const QString& layerID, mSettings.layers() )
29  {
30  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerID ) );
31  if ( !vl || !vl->rendererV2() )
32  continue;
33 
34  if ( vl->hasScaleBasedVisibility() && ( mSettings.scale() < vl->minimumScale() || mSettings.scale() > vl->maximumScale() ) )
35  {
36  mHitTest[vl] = SymbolV2Set(); // no symbols -> will not be shown
37  continue;
38  }
39 
41  {
44  }
45 
47  SymbolV2Set& usedSymbols = mHitTest[vl];
48  runHitTestLayer( vl, usedSymbols, context );
49  }
50 
51  painter.end();
52 }
53 
55 {
56  if ( !symbol || !layer || !mHitTest.contains( layer ) )
57  return false;
58 
59  return mHitTest.value( layer ).contains( QgsSymbolLayerV2Utils::symbolProperties( symbol ) );
60 }
61 
63 {
64  bool hasStyleOverride = mSettings.layerStyleOverrides().contains( vl->id() );
65  if ( hasStyleOverride )
67 
69  bool moreSymbolsPerFeature = r->capabilities() & QgsFeatureRendererV2::MoreSymbolsPerFeature;
70  r->startRender( context, vl->fields() );
71  QgsFeature f;
72  QgsFeatureRequest request( context.extent() );
74  QgsFeatureIterator fi = vl->getFeatures( request );
75  while ( fi.nextFeature( f ) )
76  {
77  context.expressionContext().setFeature( f );
78 
79  //make sure we store string representation of symbol, not pointer
80  //otherwise layer style override changes will delete original symbols and leave hanging pointers
81  if ( moreSymbolsPerFeature )
82  {
83  Q_FOREACH ( QgsSymbolV2* s, r->originalSymbolsForFeature( f, context ) )
84  {
85  if ( s )
87  }
88  }
89  else
90  {
91  QgsSymbolV2* s = r->originalSymbolForFeature( f, context );
92  if ( s )
94  }
95  }
96  r->stopRender( context );
97 
98  if ( hasStyleOverride )
100 }
101 
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
HitTest mHitTest
Definition: qgsmaphittest.h:46
Wrapper for iterator of features from vector data provider or vector layer.
void setDotsPerMeterX(int x)
bool end()
bool contains(const Key &key) const
double scale() const
Return the calculated scale of the map.
Use exact geometry intersection (slower) instead of bounding boxes.
const QgsCoordinateTransform * layerTransform(QgsMapLayer *layer) const
Return coordinate transform from layer's CRS to destination CRS.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsFields fields() const
Returns the list of fields of this layer.
virtual Q_DECL_DEPRECATED QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature)
Return symbol for feature.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer's style manager.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
bool symbolVisible(QgsSymbolV2 *symbol, QgsVectorLayer *layer) const
Tests whether a symbol is visible for a specified layer.
float minimumScale() const
Returns the minimum scale denominator at which the layer is visible.
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
QMap< QString, QString > layerStyleOverrides() const
Get map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
const QgsRectangle & extent() const
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
void setExtent(const QgsRectangle &extent)
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)=0
Needs to be called when a new render cycle is started.
The QgsMapSettings class contains configuration for rendering of the map.
virtual void stopRender(QgsRenderContext &context)=0
void setCoordinateTransform(const QgsCoordinateTransform *t)
Sets coordinate transformation.
virtual Q_DECL_DEPRECATED QgsSymbolV2List originalSymbolsForFeature(QgsFeature &feat)
Equivalent of originalSymbolsForFeature() call extended to support renderers that may use more symbol...
QgsFeatureRendererV2 * rendererV2()
Return renderer V2.
int outputDpi() const
Return DPI used for conversion between real world units (e.g.
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
QSize outputSize() const
Return the size of the resulting map image.
float maximumScale() const
Returns the maximum scale denominator at which the layer is visible.
QSet< QString > SymbolV2Set
Definition: qgsmaphittest.h:34
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void setPainter(QPainter *p)
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
void runHitTestLayer(QgsVectorLayer *vl, SymbolV2Set &usedSymbols, QgsRenderContext &context)
Runs test for visible symbols within a layer.
QgsFeatureRequest & setFlags(const QgsFeatureRequest::Flags &flags)
Set flags that affect how features will be fetched.
QgsMapHitTest(const QgsMapSettings &settings)
QgsExpressionContext & expressionContext()
Gets the expression context.
Contains information about the context of a rendering operation.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
static QString symbolProperties(QgsSymbolV2 *symbol)
Returns a string representing the symbol.
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
virtual int capabilities()
returns bitwise OR-ed capabilities of the renderer
bool nextFeature(QgsFeature &f)
QgsRectangle outputExtentToLayerExtent(QgsMapLayer *theLayer, QgsRectangle extent) const
transform bounding box from output CRS to layer's CRS
Represents a vector layer which manages a vector based data sets.
QgsMapSettings mSettings
Definition: qgsmaphittest.h:45
const T value(const Key &key) const