Skip to content

Commit af532ec

Browse files
committed
Improved docs for QgsMapCanvas::setTheme, handle setLayers and
setLayerStyleOverrides when canvas is following a map theme
1 parent 7b7c3e1 commit af532ec

File tree

4 files changed

+118
-251
lines changed

4 files changed

+118
-251
lines changed

python/gui/qgsmapcanvas.sip

Lines changed: 25 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -18,298 +18,82 @@ class QgsMapCanvas : QGraphicsView
1818
public:
1919

2020
//! Constructor
21-
QgsMapCanvas( QWidget * parent /TransferThis/ = 0 );
22-
21+
QgsMapCanvas( QWidget *parent /TransferThis/ = 0 );
2322
~QgsMapCanvas();
24-
25-
//! Returns the magnification factor
26-
//! @note added in 2.16
2723
double magnificationFactor() const;
28-
29-
//! Set list of layers that should be shown in the canvas
30-
//! @note added in 3.0
31-
void setLayers( const QList<QgsMapLayer*>& layers );
32-
33-
void setCurrentLayer( QgsMapLayer* layer );
34-
35-
//! Get access to properties used for map rendering
36-
//! @note added in 2.4
37-
const QgsMapSettings& mapSettings() const /KeepReference/;
38-
39-
//! sets destination coordinate reference system
40-
//! @note added in 2.4
41-
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
42-
43-
//! Get access to the labeling results (may be null)
44-
//! @note added in 2.4
45-
const QgsLabelingResults* labelingResults() const;
46-
47-
//! Set whether to cache images of rendered layers
48-
//! @note added in 2.4
24+
void setLayers( const QList<QgsMapLayer *> &layers );
25+
void setCurrentLayer( QgsMapLayer *layer );
26+
const QgsMapSettings &mapSettings() const /KeepReference/;
27+
void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
28+
const QgsLabelingResults *labelingResults() const;
4929
void setCachingEnabled( bool enabled );
50-
51-
//! Check whether images of rendered layers are curerently being cached
52-
//! @note added in 2.4
5330
bool isCachingEnabled() const;
54-
55-
//! Make sure to remove any rendered images from cache (does nothing if cache is not enabled)
56-
//! @note added in 2.4
5731
void clearCache();
58-
59-
//! Reload all layers, clear the cache and refresh the canvas
60-
//! @note added in 2.9
6132
void refreshAllLayers();
62-
63-
//! Set whether the layers are rendered in parallel or sequentially
64-
//! @note added in 2.4
6533
void setParallelRenderingEnabled( bool enabled );
66-
67-
//! Check whether the layers are rendered in parallel or sequentially
68-
//! @note added in 2.4
6934
bool isParallelRenderingEnabled() const;
70-
71-
//! Set how often map preview should be updated while it is being rendered (in milliseconds)
72-
//! @note added in 2.4
7335
void setMapUpdateInterval( int timeMilliseconds );
74-
75-
//! Find out how often map preview should be updated while it is being rendered (in milliseconds)
76-
//! @note added in 2.4
7736
int mapUpdateInterval() const;
78-
79-
//! Get the last reported scale of the canvas
8037
double scale();
81-
82-
//! Returns the mapUnitsPerPixel (map units per pixel) for the canvas
8338
double mapUnitsPerPixel() const;
84-
85-
//! Returns the current zoom extent of the map canvas
8639
QgsRectangle extent() const;
87-
//! Returns the combined extent for all layers on the map canvas
8840
QgsRectangle fullExtent() const;
89-
90-
//! Set the extent of the map canvas
9141
void setExtent( const QgsRectangle &r, bool magnified = false );
92-
93-
//! Get the current map canvas rotation in clockwise degrees
94-
//! @note added in 2.8
9542
double rotation() const;
96-
97-
//! Set the rotation of the map canvas in clockwise degrees
98-
//! @note added in 2.8
9943
void setRotation( double degrees );
100-
101-
//! Set the center of the map canvas, in geographical coordinates
102-
//! @note added in 2.8
103-
void setCenter( const QgsPoint& center );
104-
105-
//! Get map center, in geographical coordinates
106-
//! @note added in 2.8
44+
void setCenter( const QgsPoint &center );
10745
QgsPoint center() const;
108-
109-
//! Zoom to the full extent of all layers
11046
void zoomToFullExtent();
111-
112-
//! Zoom to the previous extent (view)
11347
void zoomToPreviousExtent();
114-
115-
//! Zoom to the next extent (view)
11648
void zoomToNextExtent();
117-
118-
// ! Clears the list of extents and sets current extent as first item
11949
void clearExtentHistory();
120-
121-
/** Zoom to the extent of the selected features of current (vector) layer.
122-
* @param layer optionally specify different than current layer
123-
*/
124-
void zoomToSelected( QgsVectorLayer* layer = 0 );
125-
126-
/** Set canvas extent to the bounding box of a set of features
127-
@param layer the vector layer
128-
@param ids the feature ids*/
129-
void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
130-
131-
/** Centers canvas extent to feature ids
132-
@param layer the vector layer
133-
@param ids the feature ids*/
134-
void panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
135-
136-
//! Pan to the selected features of current (vector) layer keeping same extent.
137-
void panToSelected( QgsVectorLayer* layer = 0 );
138-
139-
//! \brief Sets the map tool currently being used on the canvas
140-
void setMapTool( QgsMapTool* mapTool );
141-
142-
/** \brief Unset the current map tool or last non zoom tool
143-
*
144-
* This is called from destructor of map tools to make sure
145-
* that this map tool won't be used any more.
146-
* You don't have to call it manualy, QgsMapTool takes care of it.
147-
*/
148-
void unsetMapTool( QgsMapTool* mapTool );
149-
150-
//! Returns the currently active tool
151-
QgsMapTool* mapTool();
152-
153-
//! Write property of QColor bgColor.
154-
void setCanvasColor( const QColor & _newVal );
155-
//! Read property of QColor bgColor.
50+
void zoomToSelected( QgsVectorLayer *layer = 0 );
51+
void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
52+
void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
53+
void panToSelected( QgsVectorLayer *layer = 0 );
54+
void setMapTool( QgsMapTool *mapTool );
55+
void unsetMapTool( QgsMapTool *mapTool );
56+
QgsMapTool *mapTool();
57+
void setCanvasColor( const QColor &_newVal );
15658
QColor canvasColor() const;
157-
158-
//! Set color of selected vector features
159-
//! @note added in 2.4
160-
void setSelectionColor( const QColor& color );
161-
162-
//! Emits signal scaleChanged to update scale in main window
59+
void setSelectionColor( const QColor &color );
16360
void updateScale();
164-
165-
//! return the map layer at position index in the layer stack
16661
QgsMapLayer *layer( int index );
167-
168-
//! return number of layers on the map
16962
int layerCount() const;
170-
171-
//! return list of layers within map canvas.
172-
QList<QgsMapLayer*> layers() const;
63+
QList<QgsMapLayer *> layers() const;
17364
void freeze( bool frozen = true );
17465
bool isFrozen() const;
17566
bool renderFlag() const;
176-
177-
//! Get the current canvas map units
17867
QgsUnitTypes::DistanceUnit mapUnits() const;
179-
180-
//! Getter for stored overrides of styles for layers.
181-
//! @note added in 2.12
18268
QMap<QString, QString> layerStyleOverrides() const;
183-
184-
//! Setter for stored overrides of styles for layers.
185-
//! @note added in 2.12
186-
void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
187-
69+
void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
18870
void setTheme( const QString &theme );
18971
QString theme() const;
19072
const QgsMapToPixel *getCoordinateTransform();
19173
bool isDrawing();
19274
QgsMapLayer *currentLayer();
19375
void setWheelFactor( double factor );
194-
195-
//! Zoom to a specific scale
19676
void zoomScale( double scale );
197-
198-
//! Zoom with the factor supplied. Factor > 1 zooms out, interval (0,1) zooms in
199-
//! If point is given, re-center on it
20077
void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
201-
202-
//! Zooms in/out with a given center
20378
void zoomWithCenter( int x, int y, bool zoomIn );
204-
205-
//! Zooms to feature extent. Adds a small margin around the extent
206-
//! and does a pan if rect is empty (point extent)
207-
void zoomToFeatureExtent( QgsRectangle& rect );
208-
209-
//! Returns whether the scale is locked, so zooming can be performed using magnication.
210-
//! @note added in 2.16
211-
//! @see setScaleLocked()
79+
void zoomToFeatureExtent( QgsRectangle &rect );
21280
bool scaleLocked() const;
213-
214-
//! used to determine if anti-aliasing is enabled or not
21581
void enableAntiAliasing( bool flag );
216-
217-
//! true if antialising is enabled
21882
bool antiAliasingEnabled() const;
219-
220-
//! sets map tile rendering flag
22183
void enableMapTileRendering( bool flag );
222-
223-
// following 2 methods should be moved elsewhere or changed to private
224-
// currently used by pan map tool
225-
//! Ends pan action and redraws the canvas.
22684
void panActionEnd( QPoint releasePoint );
227-
228-
//! Called when mouse is moving and pan is activated
229-
void panAction( QMouseEvent * event );
230-
231-
//! returns last position of mouse cursor
85+
void panAction( QMouseEvent *event );
23286
QPoint mouseLastXY();
233-
234-
/** Enables a preview mode for the map canvas
235-
* @param previewEnabled set to true to enable a preview mode
236-
* @see setPreviewMode
237-
* @note added in 2.3 */
23887
void setPreviewModeEnabled( bool previewEnabled );
239-
240-
/** Returns whether a preview mode is enabled for the map canvas
241-
* @returns true if a preview mode is currently enabled
242-
* @see setPreviewModeEnabled
243-
* @see previewMode
244-
* @note added in 2.3 */
24588
bool previewModeEnabled() const;
246-
247-
/** Sets a preview mode for the map canvas. This setting only has an effect if
248-
* previewModeEnabled is true.
249-
* @param mode preview mode for the canvas
250-
* @see previewMode
251-
* @see setPreviewModeEnabled
252-
* @see previewModeEnabled
253-
* @note added in 2.3 */
25489
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
255-
256-
/** Returns the current preview mode for the map canvas. This setting only has an effect if
257-
* previewModeEnabled is true.
258-
* @returns preview mode for map canvas
259-
* @see setPreviewMode
260-
* @see previewModeEnabled
261-
* @note added in 2.3 */
26290
QgsPreviewEffect::PreviewMode previewMode() const;
263-
264-
/** Return snapping utility class that is associated with map canvas.
265-
* If no snapping utils instance has been associated previously, an internal will be created for convenience
266-
* (so map tools do not need to test for existence of the instance).
267-
*
268-
* Main canvas in QGIS returns an instance which is always up-to-date with the project's snapping configuration.
269-
* @note added in 2.8
270-
*/
271-
QgsSnappingUtils* snappingUtils() const;
272-
273-
/** Assign an instance of snapping utils to the map canvas.
274-
* The instance is not owned by the canvas, so it is possible to use one instance in multiple canvases.
275-
*
276-
* For main canvas in QGIS, do not associate a different instance from the existing one (it is updated from
277-
* the project's snapping configuration).
278-
* @note added in 2.8
279-
*/
280-
void setSnappingUtils( QgsSnappingUtils* utils );
281-
282-
/** Sets an expression context scope for the map canvas. This scope is injected into the expression
283-
* context used for rendering the map, and can be used to apply specific variable overrides for
284-
* expression evaluation for the map canvas render. This method will overwrite the existing expression
285-
* context scope for the canvas.
286-
* @param scope new expression context scope
287-
* @note added in QGIS 2.12
288-
* @see expressionContextScope()
289-
*/
290-
void setExpressionContextScope( const QgsExpressionContextScope& scope );
291-
292-
/** Returns a reference to the expression context scope for the map canvas. This scope is injected
293-
* into the expression context used for rendering the map, and can be used to apply specific variable
294-
* overrides for expression evaluation for the map canvas render.
295-
* @note added in QGIS 2.12
296-
* @see setExpressionContextScope()
297-
*/
298-
QgsExpressionContextScope& expressionContextScope();
299-
300-
/** Returns a const reference to the expression context scope for the map canvas.
301-
* @note added in QGIS 2.12
302-
* @see setExpressionContextScope()
303-
* @note not available in python bindings
304-
*/
305-
// const QgsExpressionContextScope& expressionContextScope() const;
306-
307-
/** Sets the segmentation tolerance applied when rendering curved geometries
308-
@param tolerance the segmentation tolerance*/
91+
QgsSnappingUtils *snappingUtils() const;
92+
void setSnappingUtils( QgsSnappingUtils *utils );
93+
void setExpressionContextScope( const QgsExpressionContextScope &scope );
94+
QgsExpressionContextScope &expressionContextScope();
95+
// const QgsExpressionContextScope &expressionContextScope() const;
30996
void setSegmentationTolerance( double tolerance );
310-
311-
/** Sets segmentation tolerance type (maximum angle or maximum difference between curve and approximation)
312-
@param type the segmentation tolerance typename*/
31397
void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
31498

31599
public slots:

src/gui/qgsmapcanvas.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ const QgsMapToPixel *QgsMapCanvas::getCoordinateTransform()
291291
}
292292

293293
void QgsMapCanvas::setLayers( const QList<QgsMapLayer *> &layers )
294+
{
295+
// following a theme => request denied!
296+
if ( !mTheme.isEmpty() )
297+
return;
298+
299+
setLayersPrivate( layers );
300+
}
301+
302+
void QgsMapCanvas::setLayersPrivate( const QList<QgsMapLayer *> &layers )
294303
{
295304
QList<QgsMapLayer *> oldLayers = mSettings.layers();
296305

@@ -487,6 +496,12 @@ void QgsMapCanvas::refreshMap()
487496

488497
if ( !mTheme.isEmpty() )
489498
{
499+
// IMPORTANT: we MUST set the layer style overrides here! (At the time of writing this
500+
// comment) retrieving layer styles from the theme collection gives an XML snapshot of the
501+
// current state of the style. If we had stored the style overrides earlier (such as in
502+
// mapThemeChanged slot) then this xml could be out of date...
503+
// TODO: if in future QgsMapThemeCollection::mapThemeStyleOverrides is changed to
504+
// just return the style name, we can instead set the overrides in mapThemeChanged and not here
490505
mSettings.setLayerStyleOverrides( QgsProject::instance()->mapThemeCollection()->mapThemeStyleOverrides( mTheme ) );
491506
}
492507

@@ -530,7 +545,17 @@ void QgsMapCanvas::mapThemeChanged( const QString &theme )
530545
{
531546
if ( theme == mTheme )
532547
{
533-
setLayers( QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( mTheme ) );
548+
// set the canvas layers to match the new layers contained in the map theme
549+
// NOTE: we do this when the theme layers change and not when we are refreshing the map
550+
// as setLayers() sets up necessary connections to handle changes to the layers
551+
setLayersPrivate( QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( mTheme ) );
552+
// IMPORTANT: we don't set the layer style overrides here! (At the time of writing this
553+
// comment) retrieving layer styles from the theme collection gives an XML snapshot of the
554+
// current state of the style. If changes were made to the style then this xml
555+
// snapshot goes out of sync...
556+
// TODO: if in future QgsMapThemeCollection::mapThemeStyleOverrides is changed to
557+
// just return the style name, we can instead set the overrides here and not in refreshMap()
558+
534559
clearCache();
535560
refresh();
536561
}
@@ -1628,7 +1653,7 @@ void QgsMapCanvas::setTheme( const QString &theme )
16281653
else
16291654
{
16301655
mTheme = theme;
1631-
setLayers( QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( mTheme ) );
1656+
setLayersPrivate( QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( mTheme ) );
16321657
emit themeChanged( theme );
16331658
}
16341659
}

0 commit comments

Comments
 (0)