~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/core/qgsmaplayer.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgsmaplayer.h  -  description
 
3
                             -------------------
 
4
    begin                : Fri Jun 28 2002
 
5
    copyright            : (C) 2002 by Gary E.Sherman
 
6
    email                : sherman at mrcc.com
 
7
***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
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.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
/* $Id$ */
 
18
 
 
19
#ifndef QGSMAPLAYER_H
 
20
#define QGSMAPLAYER_H
 
21
 
 
22
 
 
23
#include <QObject>
 
24
#include <QUndoStack>
 
25
#include <QVariant>
 
26
#include <QImage>
 
27
 
 
28
#include "qgsrectangle.h"
 
29
 
 
30
class QgsRenderContext;
 
31
class QgsCoordinateReferenceSystem;
 
32
 
 
33
class QDomNode;
 
34
class QDomDocument;
 
35
class QKeyEvent;
 
36
class QPainter;
 
37
 
 
38
/** \ingroup core
 
39
 * Base class for all map layer types.
 
40
 * This is the base class for all map layer types (vector, raster).
 
41
 */
 
42
class CORE_EXPORT QgsMapLayer : public QObject
 
43
{
 
44
    Q_OBJECT
 
45
 
 
46
  public:
 
47
    /** Layers enum defining the types of layers that can be added to a map */
 
48
    enum LayerType
 
49
    {
 
50
      VectorLayer,
 
51
      RasterLayer
 
52
    };
 
53
 
 
54
    /** Constructor
 
55
     * @param type Type of layer as defined in QgsMapLayer::LayerType enum
 
56
     * @param lyrname Display Name of the layer
 
57
     */
 
58
    QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, QString lyrname = QString::null, QString source = QString::null );
 
59
 
 
60
    /** Destructor */
 
61
    virtual ~QgsMapLayer();
 
62
 
 
63
    /** Get the type of the layer
 
64
     * @return Integer matching a value in the QgsMapLayer::LayerType enum
 
65
     */
 
66
    QgsMapLayer::LayerType type() const;
 
67
 
 
68
    /** Get this layer's unique ID, this ID is used to access this layer from map layer registry */
 
69
    QString getLayerID() const;
 
70
 
 
71
    /** Set the display name of the layer
 
72
     * @param name New name for the layer
 
73
     */
 
74
    void setLayerName( const QString & name );
 
75
 
 
76
    /** Get the display name of the layer
 
77
     * @return the layer name
 
78
     */
 
79
    QString const & name() const;
 
80
 
 
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
 
85
     * layer.
 
86
     */
 
87
    virtual bool draw( QgsRenderContext& rendererContext );
 
88
 
 
89
    /** Draw labels
 
90
     * @TODO to be removed: used only in vector layers
 
91
     */
 
92
    virtual void drawLabels( QgsRenderContext& rendererContext );
 
93
 
 
94
    /** Return the extent of the layer as a QRect */
 
95
    QgsRectangle extent() const;
 
96
 
 
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
 
100
     */
 
101
    bool isValid();
 
102
 
 
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.
 
106
     */
 
107
    QString publicSource() const;
 
108
 
 
109
    /** Returns the source for the layer */
 
110
    QString const & source() const;
 
111
 
 
112
    /**
 
113
     * Returns the sublayers of this layer
 
114
     * (Useful for providers that manage their own layers, such as WMS)
 
115
     */
 
116
    virtual QStringList subLayers();
 
117
 
 
118
    /**
 
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)
 
121
     */
 
122
    virtual void setLayerOrder( QStringList layers );
 
123
 
 
124
    /** Set the visibility of the given sublayer name */
 
125
    virtual void setSubLayerVisibility( QString name, bool vis );
 
126
 
 
127
 
 
128
    /** True if the layer can be edited */
 
129
    virtual bool isEditable() const = 0;
 
130
 
 
131
    /** sets state from Dom document
 
132
       @param layer_node is Dom node corresponding to ``maplayer'' tag
 
133
       @note
 
134
 
 
135
       The Dom node corresponds to a Dom document project file XML element read
 
136
       by QgsProject.
 
137
 
 
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.
 
140
 
 
141
       Invoked by QgsProject::read().
 
142
 
 
143
       @returns true if successful
 
144
     */
 
145
    bool readXML( QDomNode & layer_node );
 
146
 
 
147
 
 
148
    /** stores state in Dom node
 
149
       @param layer_node is Dom node corresponding to ``projectlayers'' tag
 
150
       @note
 
151
 
 
152
       The Dom node corresponds to a Dom document project file XML element to be
 
153
       written by QgsProject.
 
154
 
 
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.
 
157
 
 
158
       Invoked by QgsProject::write().
 
159
 
 
160
       @returns true if successful
 
161
    */
 
162
    bool writeXML( QDomNode & layer_node, QDomDocument & document );
 
163
 
 
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 );
 
173
 
 
174
    /** Copies the symbology settings from another layer. Returns true in case of success */
 
175
    virtual bool copySymbologySettings( const QgsMapLayer& other ) = 0;
 
176
 
 
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;
 
179
 
 
180
    /** Accessor for transparency level. */
 
181
    unsigned int getTransparency();
 
182
 
 
183
    /** Mutator for transparency level. Should be between 0 and 255 */
 
184
    void setTransparency( unsigned int );
 
185
 
 
186
    /**
 
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.
 
191
     */
 
192
    virtual QString lastErrorTitle();
 
193
 
 
194
    /**
 
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.
 
199
     */
 
200
    virtual QString lastError();
 
201
 
 
202
    /** Returns layer's spatial reference system
 
203
    @note This was introduced in QGIS 1.4
 
204
    */
 
205
    const QgsCoordinateReferenceSystem& crs();
 
206
 
 
207
    /** Returns layer's spatial reference system
 
208
    @note This method is here for API compatibility
 
209
    and will be deprecited in 2.0
 
210
    @see crs()
 
211
    */
 
212
    const QgsCoordinateReferenceSystem& srs();
 
213
 
 
214
    /** Sets layer's spatial reference system
 
215
    @note emitSignal added in 1.4 */
 
216
    void setCrs( const QgsCoordinateReferenceSystem& srs, bool emitSignal = true );
 
217
 
 
218
 
 
219
    /** A convenience function to capitalise the layer name */
 
220
    static QString capitaliseLayerName( const QString name );
 
221
 
 
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 ();
 
229
     */
 
230
    virtual QString loadDefaultStyle( bool & theResultFlag );
 
231
 
 
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 ();
 
244
     */
 
245
    virtual QString loadNamedStyle( const QString theURI, bool & theResultFlag );
 
246
 
 
247
    virtual bool loadNamedStyleFromDb( const QString db, const QString theURI, QString &qml );
 
248
 
 
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()
 
256
     */
 
257
    virtual QString saveDefaultStyle( bool & theResultFlag );
 
258
 
 
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 ();
 
271
     */
 
272
    virtual QString saveNamedStyle( const QString theURI, bool & theResultFlag );
 
273
 
 
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.
 
278
    */
 
279
    virtual bool readSymbology( const QDomNode& node, QString& errorMessage ) = 0;
 
280
 
 
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.
 
286
     */
 
287
    virtual bool writeSymbology( QDomNode&, QDomDocument& doc, QString& errorMessage ) const = 0;
 
288
 
 
289
    /** Return pointer to layer's undo stack */
 
290
    QUndoStack* undoStack();
 
291
 
 
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 );
 
298
 
 
299
  public slots:
 
300
 
 
301
    /** Event handler for when a coordinate transform fails due to bad vertex error */
 
302
    virtual void invalidTransformInput();
 
303
 
 
304
    /** Accessor and mutator for the minimum scale member */
 
305
    void setMinimumScale( float theMinScale );
 
306
    float minimumScale();
 
307
 
 
308
    /** Accessor and mutator for the maximum scale member */
 
309
    void setMaximumScale( float theMaxScale );
 
310
    float maximumScale();
 
311
 
 
312
    /** Accessor and mutator for the scale based visilibility flag */
 
313
    void toggleScaleBasedVisibility( bool theVisibilityFlag );
 
314
    bool hasScaleBasedVisibility();
 
315
 
 
316
  signals:
 
317
 
 
318
    /** Emit a signal to notify of a progress event */
 
319
    void drawingProgress( int theProgress, int theTotalSteps );
 
320
 
 
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 );
 
323
 
 
324
    /** Emit a signal that the layer name has been changed */
 
325
    void layerNameChanged();
 
326
 
 
327
    /** Emit a signal that layer's CRS has been reset
 
328
     added in 1.4
 
329
     */
 
330
    void layerCrsChanged();
 
331
 
 
332
    /** This signal should be connected with the slot QgsMapCanvas::refresh()
 
333
     * @TODO: to be removed - GUI dependency
 
334
     */
 
335
    void repaintRequested();
 
336
 
 
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();
 
340
 
 
341
    /** This is used to send a request that any mapcanvas using this layer update its extents */
 
342
    void recalculateExtents();
 
343
 
 
344
  protected:
 
345
 
 
346
    /** called by readXML(), used by children to read state specific to them from
 
347
        project files.
 
348
    */
 
349
    virtual bool readXml( QDomNode & layer_node );
 
350
 
 
351
    /** called by writeXML(), used by children to write state specific to them to
 
352
        project files.
 
353
    */
 
354
    virtual bool writeXml( QDomNode & layer_node, QDomDocument & document );
 
355
 
 
356
 
 
357
    /** Read custom properties from project file. Added in v1.4 */
 
358
    void readCustomProperties( QDomNode & layerNode );
 
359
 
 
360
    /** Write custom properties to project file. Added in v1.4 */
 
361
    void writeCustomProperties( QDomNode & layerNode, QDomDocument & doc );
 
362
 
 
363
    /** debugging member - invoked when a connect() is made to this object */
 
364
    void connectNotify( const char * signal );
 
365
 
 
366
    /** Transparency level for this layer should be 0-255 (255 being opaque) */
 
367
    unsigned int mTransparencyLevel;
 
368
 
 
369
    /** Extent of the layer */
 
370
    QgsRectangle mLayerExtent;
 
371
 
 
372
    /** Indicates if the layer is valid and can be drawn */
 
373
    bool mValid;
 
374
 
 
375
    /** data source description string, varies by layer type */
 
376
    QString mDataSource;
 
377
 
 
378
    /** Name of the layer - used for display */
 
379
    QString mLayerName;
 
380
 
 
381
    /** layer's Spatial reference system */
 
382
    QgsCoordinateReferenceSystem* mCRS;
 
383
 
 
384
  private:
 
385
 
 
386
    /** private copy constructor - QgsMapLayer not copyable */
 
387
    QgsMapLayer( QgsMapLayer const & );
 
388
 
 
389
    /** private assign operator - QgsMapLayer not copyable */
 
390
    QgsMapLayer & operator=( QgsMapLayer const & );
 
391
 
 
392
    /** Unique ID of this layer - used to refer to this layer in map layer registry */
 
393
    QString mID;
 
394
 
 
395
    /** Type of the layer (eg. vector, raster) */
 
396
    QgsMapLayer::LayerType mLayerType;
 
397
 
 
398
    /** Tag for embedding additional information */
 
399
    QString mTag;
 
400
 
 
401
    /** Minimum scale at which this layer should be displayed */
 
402
    float mMinScale;
 
403
    /** Maximum scale at which this layer should be displayed */
 
404
    float mMaxScale;
 
405
    /** A flag that tells us whether to use the above vars to restrict layer visibility */
 
406
    bool mScaleBasedVisibility;
 
407
 
 
408
    /** Collection of undoable operations for this layer. **/
 
409
    QUndoStack mUndoStack;
 
410
 
 
411
    QMap<QString, QVariant> mCustomProperties;
 
412
 
 
413
    /**QImage for caching of rendering operations
 
414
     * @note This property was added in QGIS 1.4 **/
 
415
    QImage * mpCacheImage;
 
416
 
 
417
};
 
418
 
 
419
#endif