1
/***************************************************************************
2
qgsmaplayer.h - description
4
begin : Fri Jun 28 2002
5
copyright : (C) 2002 by Gary E.Sherman
6
email : sherman at mrcc.com
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
28
#include "qgsrectangle.h"
30
class QgsRenderContext;
31
class QgsCoordinateReferenceSystem;
39
* Base class for all map layer types.
40
* This is the base class for all map layer types (vector, raster).
42
class CORE_EXPORT QgsMapLayer : public QObject
47
/** Layers enum defining the types of layers that can be added to a map */
55
* @param type Type of layer as defined in QgsMapLayer::LayerType enum
56
* @param lyrname Display Name of the layer
58
QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, QString lyrname = QString::null, QString source = QString::null );
61
virtual ~QgsMapLayer();
63
/** Get the type of the layer
64
* @return Integer matching a value in the QgsMapLayer::LayerType enum
66
QgsMapLayer::LayerType type() const;
68
/** Get this layer's unique ID, this ID is used to access this layer from map layer registry */
69
QString getLayerID() const;
71
/** Set the display name of the layer
72
* @param name New name for the layer
74
void setLayerName( const QString & name );
76
/** Get the display name of the layer
77
* @return the layer name
79
QString const & name() const;
81
/** This is the method that does the actual work of
82
* drawing the layer onto a paint device.
83
* @param QgsRenderContext - describes the extents,
84
* resolution etc. that should be used when rendering the
87
virtual bool draw( QgsRenderContext& rendererContext );
90
* @TODO to be removed: used only in vector layers
92
virtual void drawLabels( QgsRenderContext& rendererContext );
94
/** Return the extent of the layer as a QRect */
95
QgsRectangle extent() const;
97
/*! Return the status of the layer. An invalid layer is one which has a bad datasource
98
* or other problem. Child classes set this flag when intialized
99
* @return True if the layer is valid and can be accessed
103
/*! Gets a version of the internal layer definition that has sensitive
104
* bits removed (for example, the password). This function should
105
* be used when displaying the source name for general viewing.
107
QString publicSource() const;
109
/** Returns the source for the layer */
110
QString const & source() const;
113
* Returns the sublayers of this layer
114
* (Useful for providers that manage their own layers, such as WMS)
116
virtual QStringList subLayers();
119
* Reorders the *previously selected* sublayers of this layer from bottom to top
120
* (Useful for providers that manage their own layers, such as WMS)
122
virtual void setLayerOrder( QStringList layers );
124
/** Set the visibility of the given sublayer name */
125
virtual void setSubLayerVisibility( QString name, bool vis );
128
/** True if the layer can be edited */
129
virtual bool isEditable() const = 0;
131
/** sets state from Dom document
132
@param layer_node is Dom node corresponding to ``maplayer'' tag
135
The Dom node corresponds to a Dom document project file XML element read
138
This, in turn, calls readXml(), which is over-rideable by sub-classes so
139
that they can read their own specific state from the given Dom node.
141
Invoked by QgsProject::read().
143
@returns true if successful
145
bool readXML( QDomNode & layer_node );
148
/** stores state in Dom node
149
@param layer_node is Dom node corresponding to ``projectlayers'' tag
152
The Dom node corresponds to a Dom document project file XML element to be
153
written by QgsProject.
155
This, in turn, calls writeXml(), which is over-rideable by sub-classes so
156
that they can write their own specific state to the given Dom node.
158
Invoked by QgsProject::write().
160
@returns true if successful
162
bool writeXML( QDomNode & layer_node, QDomDocument & document );
164
/** Set a custom property for layer. Properties are stored in a map and saved in project file.
165
* @note Added in v1.4 */
166
void setCustomProperty( const QString& key, const QVariant& value );
167
/** Read a custom property from layer. Properties are stored in a map and saved in project file.
168
* @note Added in v1.4 */
169
QVariant customProperty( const QString& value, const QVariant& defaultValue = QVariant() ) const;
170
/** Remove a custom property from layer. Properties are stored in a map and saved in project file.
171
* @note Added in v1.4 */
172
void removeCustomProperty( const QString& key );
174
/** Copies the symbology settings from another layer. Returns true in case of success */
175
virtual bool copySymbologySettings( const QgsMapLayer& other ) = 0;
177
/** Returns true if this layer can be in the same symbology group with another layer */
178
virtual bool hasCompatibleSymbology( const QgsMapLayer& other ) const = 0;
180
/** Accessor for transparency level. */
181
unsigned int getTransparency();
183
/** Mutator for transparency level. Should be between 0 and 255 */
184
void setTransparency( unsigned int );
187
* If an operation returns 0 (e.g. draw()), this function
188
* returns the text of the error associated with the failure.
189
* Interactive users of this provider can then, for example,
190
* call a QMessageBox to display the contents.
192
virtual QString lastErrorTitle();
195
* If an operation returns 0 (e.g. draw()), this function
196
* returns the text of the error associated with the failure.
197
* Interactive users of this provider can then, for example,
198
* call a QMessageBox to display the contents.
200
virtual QString lastError();
202
/** Returns layer's spatial reference system
203
@note This was introduced in QGIS 1.4
205
const QgsCoordinateReferenceSystem& crs();
207
/** Returns layer's spatial reference system
208
@note This method is here for API compatibility
209
and will be deprecited in 2.0
212
const QgsCoordinateReferenceSystem& srs();
214
/** Sets layer's spatial reference system
215
@note emitSignal added in 1.4 */
216
void setCrs( const QgsCoordinateReferenceSystem& srs, bool emitSignal = true );
219
/** A convenience function to capitalise the layer name */
220
static QString capitaliseLayerName( const QString name );
222
/** Retrieve the default style for this layer if one
223
* exists (either as a .qml file on disk or as a
224
* record in the users style table in their personal qgis.db)
225
* @param a reference to a flag that will be set to false if
226
* we did not manage to load the default style.
227
* @return a QString with any status messages
228
* @see also loadNamedStyle ();
230
virtual QString loadDefaultStyle( bool & theResultFlag );
232
/** Retrieve a named style for this layer if one
233
* exists (either as a .qml file on disk or as a
234
* record in the users style table in their personal qgis.db)
235
* @param QString theURI - the file name or other URI for the
236
* style file. First an attempt will be made to see if this
237
* is a file and load that, if that fails the qgis.db styles
238
* table will be consulted to see if there is a style who's
239
* key matches the URI.
240
* @param a reference to a flag that will be set to false if
241
* we did not manage to load the default style.
242
* @return a QString with any status messages
243
* @see also loadDefaultStyle ();
245
virtual QString loadNamedStyle( const QString theURI, bool & theResultFlag );
247
virtual bool loadNamedStyleFromDb( const QString db, const QString theURI, QString &qml );
249
/** Save the properties of this layer as the default style
250
* (either as a .qml file on disk or as a
251
* record in the users style table in their personal qgis.db)
252
* @param a reference to a flag that will be set to false if
253
* we did not manage to save the default style.
254
* @return a QString with any status messages
255
* @see also loadNamedStyle () and saveNamedStyle()
257
virtual QString saveDefaultStyle( bool & theResultFlag );
259
/** Save the properties of this layer as a named style
260
* (either as a .qml file on disk or as a
261
* record in the users style table in their personal qgis.db)
262
* @param QString theURI - the file name or other URI for the
263
* style file. First an attempt will be made to see if this
264
* is a file and save to that, if that fails the qgis.db styles
265
* table will be used to create a style entry who's
266
* key matches the URI.
267
* @param a reference to a flag that will be set to false if
268
* we did not manage to save the default style.
269
* @return a QString with any status messages
270
* @see also saveDefaultStyle ();
272
virtual QString saveNamedStyle( const QString theURI, bool & theResultFlag );
274
/** Read the symbology for the current layer from the Dom node supplied.
275
* @param QDomNode node that will contain the symbology definition for this layer.
276
* @param errorMessage reference to string that will be updated with any error messages
277
* @return true in case of success.
279
virtual bool readSymbology( const QDomNode& node, QString& errorMessage ) = 0;
281
/** Write the symbology for the layer into the docment provided.
282
* @param QDomNode the node that will have the style element added to it.
283
* @param QDomDocument the document that will have the QDomNode added.
284
* @param errorMessage reference to string that will be updated with any error messages
285
* @return true in case of success.
287
virtual bool writeSymbology( QDomNode&, QDomDocument& doc, QString& errorMessage ) const = 0;
289
/** Return pointer to layer's undo stack */
290
QUndoStack* undoStack();
292
/** Get the QImage used for caching render operations
293
* @note This method was added in QGIS 1.4 **/
294
QImage * cacheImage() { return mpCacheImage; }
295
/** Set the QImage used for caching render operations
296
* @note This method was added in QGIS 1.4 **/
297
void setCacheImage( QImage * thepImage );
301
/** Event handler for when a coordinate transform fails due to bad vertex error */
302
virtual void invalidTransformInput();
304
/** Accessor and mutator for the minimum scale member */
305
void setMinimumScale( float theMinScale );
306
float minimumScale();
308
/** Accessor and mutator for the maximum scale member */
309
void setMaximumScale( float theMaxScale );
310
float maximumScale();
312
/** Accessor and mutator for the scale based visilibility flag */
313
void toggleScaleBasedVisibility( bool theVisibilityFlag );
314
bool hasScaleBasedVisibility();
318
/** Emit a signal to notify of a progress event */
319
void drawingProgress( int theProgress, int theTotalSteps );
321
/** Emit a signal with status (e.g. to be caught by QgisApp and display a msg on status bar) */
322
void statusChanged( QString theStatus );
324
/** Emit a signal that the layer name has been changed */
325
void layerNameChanged();
327
/** Emit a signal that layer's CRS has been reset
330
void layerCrsChanged();
332
/** This signal should be connected with the slot QgsMapCanvas::refresh()
333
* @TODO: to be removed - GUI dependency
335
void repaintRequested();
337
/**The layer emits this signal when a screen update is requested.
338
This signal should be connected with the slot QgsMapCanvas::updateMap()*/
339
void screenUpdateRequested();
341
/** This is used to send a request that any mapcanvas using this layer update its extents */
342
void recalculateExtents();
346
/** called by readXML(), used by children to read state specific to them from
349
virtual bool readXml( QDomNode & layer_node );
351
/** called by writeXML(), used by children to write state specific to them to
354
virtual bool writeXml( QDomNode & layer_node, QDomDocument & document );
357
/** Read custom properties from project file. Added in v1.4 */
358
void readCustomProperties( QDomNode & layerNode );
360
/** Write custom properties to project file. Added in v1.4 */
361
void writeCustomProperties( QDomNode & layerNode, QDomDocument & doc );
363
/** debugging member - invoked when a connect() is made to this object */
364
void connectNotify( const char * signal );
366
/** Transparency level for this layer should be 0-255 (255 being opaque) */
367
unsigned int mTransparencyLevel;
369
/** Extent of the layer */
370
QgsRectangle mLayerExtent;
372
/** Indicates if the layer is valid and can be drawn */
375
/** data source description string, varies by layer type */
378
/** Name of the layer - used for display */
381
/** layer's Spatial reference system */
382
QgsCoordinateReferenceSystem* mCRS;
386
/** private copy constructor - QgsMapLayer not copyable */
387
QgsMapLayer( QgsMapLayer const & );
389
/** private assign operator - QgsMapLayer not copyable */
390
QgsMapLayer & operator=( QgsMapLayer const & );
392
/** Unique ID of this layer - used to refer to this layer in map layer registry */
395
/** Type of the layer (eg. vector, raster) */
396
QgsMapLayer::LayerType mLayerType;
398
/** Tag for embedding additional information */
401
/** Minimum scale at which this layer should be displayed */
403
/** Maximum scale at which this layer should be displayed */
405
/** A flag that tells us whether to use the above vars to restrict layer visibility */
406
bool mScaleBasedVisibility;
408
/** Collection of undoable operations for this layer. **/
409
QUndoStack mUndoStack;
411
QMap<QString, QVariant> mCustomProperties;
413
/**QImage for caching of rendering operations
414
* @note This property was added in QGIS 1.4 **/
415
QImage * mpCacheImage;