1
/***************************************************************************
2
qgsvectorlayer.h - description
5
copyright : (C) 2003 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
***************************************************************************/
17
/* $Id: qgsvectorlayer.h,v 1.69.2.2 2005/10/05 08:54:43 g_j_m Exp $ */
19
#ifndef QGSVECTORLAYER_H
20
#define QGSVECTORLAYER_H
31
class QgsDlgVectorLayerProperties;
33
class QgsIdentifyResults;
39
#include "qgsmaplayer.h"
40
#include "qvaluevector.h"
41
#include "qgsattributetabledisplay.h"
42
#include "qgsvectordataprovider.h"
43
#include "qgsattributeaction.h"
45
/*! \class QgsVectorLayer
46
* \brief Vector layer backed by a data source provider
49
class QgsVectorLayer : public QgsMapLayer
56
QgsVectorLayer(QString baseName = 0, QString path = 0, QString providerLib = 0);
59
virtual ~QgsVectorLayer();
61
//! Identify feature found within the search rectangle
62
void identify(QgsRect *);
64
//! Select features found within the search rectangle
65
void select(QgsRect * rect, bool lock);
67
//!Select not selected features and deselect selected ones
68
void invertSelection();
70
//! Display the attribute table
73
//! Resize and fill the attribute table
74
void fillTable(QgsAttributeTable* t);
76
//! Set the primary display field to be used in the identify results dialog
77
void setDisplayField(QString fldName=0);
79
//! Returns the primary display field name used in the identify results dialog
80
const QString displayField() const { return fieldIndex; }
82
//! Initialize the context menu
83
void initContextMenu(QgisApp * app);
92
/** bind layer to a specific data provider
94
@param provider should be "postgres", "ogr", or ??
96
@todo XXX should this return bool? Throw exceptions?
98
bool setDataProvider( QString const & provider );
100
//! Setup the coordinate system tranformation for the layer
101
void setCoordinateSystem();
103
QgsVectorDataProvider * getDataProvider();
105
/** \brief Query data provider to find out the WKT projection string for this layer. This implements the virtual method of the same name defined in QgsMapLayer*/
106
QString getProjectionWKT();
108
* Gets the SRID of the layer by querying the underlying data provider
109
* @return The SRID if the provider is able to provide it, otherwise 0
111
int getProjectionSrid();
114
QgsAttributeAction* actions() { return &mActions; }
117
/**This signal is emitted when the layer leaves editing mode.
118
The purpose is to tell QgsMapCanvas to remove the lines of
119
(unfinished) features
120
@param norepaint true: there is no repaint at all. False: QgsMapCanvas
121
decides, if a repaint is necessary or not*/
122
void editingStopped(bool norepaint);
126
void inOverview( bool );
128
/**Sets the 'tabledisplay' to 0 again*/
129
void invalidateTableDisplay();
130
void select(int number);
131
void removeSelection();
132
void triggerRepaint();
133
/**Shows the properties dialog*/
134
virtual void showLayerProperties();
135
/**Returns a pointer to the renderer*/
136
QgsRenderer *renderer();
137
/**Returns a pointer to the renderer dialog*/
138
QDialog *rendererDialog();
139
/**Sets the renderer. If a renderer is already present, it is deleted*/
140
void setRenderer(QgsRenderer * r);
141
/**Sets the renderer dialog. If a renderer dialog is already present, it is deleted*/
142
void setRendererDialog(QDialog * dialog);
143
/**Sets m_propertiesDialog*/
144
void setLayerProperties(QgsDlgVectorLayerProperties * properties);
145
/**Returns point, line or polygon*/
146
QGis::VectorType vectorType();
147
/**Returns a pointer to the properties dialog*/
148
QgsDlgVectorLayerProperties *propertiesDialog();
149
/** Return the context menu for the layer */
150
QPopupMenu *contextMenu();
151
/**Returns the bounding box of the selected features. If there is no selection, QgsRect(0,0,0,0) is returned*/
152
virtual QgsRect bBoxOfSelected();
153
//! Return the provider type for this layer
154
QString providerType();
155
//! Return the validity of the layer
156
inline bool isValid()
161
/** reads vector layer specific state from project file DOM node.
165
Called by QgsMapLayer::readXML().
168
/* virtual */ bool readXML_( QDomNode & layer_node );
171
/** write vector layer specific state to project file DOM node.
175
Called by QgsMapLayer::writeXML().
178
/* virtual */ bool writeXML_( QDomNode & layer_node, QDomDocument & doc );
182
* Get the first feature resulting from a select operation
185
virtual QgsFeature * getFirstFeature(bool fetchAttributes=false) const;
188
* Get the next feature resulting from a select operation
191
virtual QgsFeature * getNextFeature(bool fetchAttributes=false) const;
194
* Get the next feature using new method
195
* TODO - make this pure virtual once it works and change existing providers
196
* to use this method of fetching features
198
virtual bool getNextFeature(QgsFeature &feature, bool fetchAttributes=false) const;
201
* Number of features in the layer. This is necessary if features are
202
* added/deleted or the layer has been subsetted. If the data provider
203
* chooses not to support this feature, the total number of features
205
* @return long containing number of features
207
virtual long featureCount() const;
210
* Update the feature count
211
* @return long containing the number of features in the datasource
213
virtual long updateFeatureCount() const;
216
* Update the extents for the layer. This is necessary if features are
217
* added/deleted or the layer has been subsetted.
219
virtual void updateExtents();
222
* Set the string (typically sql) used to define a subset of the layer
223
* @param subset The subset string. This may be the where clause of a sql statement
224
* or other defintion string specific to the underlying dataprovider and data
227
virtual void setSubsetString(QString subset);
230
* Get the string (typically sql) used to define a subset of the layer
231
* @return The subset string or QString::null if not implemented by the provider
233
virtual QString subsetString();
235
* Number of attribute fields for a feature in the layer
237
virtual int fieldCount() const;
241
Return a list of field names for this layer
242
@return vector of field names
244
virtual std::vector<QgsField> const & fields() const;
247
@return true in case of success and false in case of error*/
248
bool addFeature(QgsFeature* f);
250
/**Deletes the selected features
251
@return true in case of success and false otherwise*/
252
bool deleteSelectedFeatures();
254
/**Returns the default value for the attribute @c attr for the feature
256
QString getDefaultValue(const QString& attr, QgsFeature* f);
259
void setLabelOn( bool on );
262
bool labelOn( void );
265
* Minimum scale at which the layer is rendered
266
* @return Scale as integer
270
* Maximum scale at which the layer is rendered
271
* @return Scale as integer
275
* Is scale dependent rendering in effect?
278
bool scaleDependentRender();
280
/**Returns true if the provider is in editing mode*/
281
virtual bool isEditable() const {return (mEditable&&dataProvider);}
283
/**Returns true if the provider has been modified since the last commit*/
284
virtual bool isModified() const {return mModified;}
286
//! Save as shapefile
287
virtual void saveAsShapefile();
289
/**Snaps a point to the closest vertex if there is one within the snapping tolerance (mSnappingTolerance)
290
@param point the point which is set to the position of a vertex if there is one within the snapping tolerance.
291
If there is no point within this tolerance, point is left unchanged.
292
@param tolerance the snapping tolerance
293
@return true if the position of point has been changed and false else*/
294
bool snapPoint(QgsPoint& point, double tolerance);
296
/**Commits edited attributes. Depending on the feature id,
297
the changes are written to not commited features or redirected to
299
bool commitAttributeChanges(const std::set<QString>& deleted,
300
const std::map<QString,QString>& added,
301
std::map<int,std::map<QString,QString> >& changed);
303
/** \brief Draws the layer using coordinate transformation
304
* \param widthScale line width scale
305
* \param symbolScale symbol scale
307
void draw(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf, QPaintDevice * dst, double widthScale, double symbolScale, int oversampling = 1);
309
/** \brief Draws the layer labels using coordinate transformation
310
* \param scale size scale, applied to all values in pixels
312
void drawLabels(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf, QPaintDevice * dst, double scale);
317
/**Pointer to the table display object if there is one, else a pointer to 0*/
318
QgsAttributeTableDisplay * tabledisplay;
319
/**Vector holding the information which features are activated*/
320
std::set<int> mSelected;
321
std::set<int> mDeleted;
322
/**Features which are not commited*/
323
std::list<QgsFeature*> mAddedFeatures;
324
/**Changed attributes which are not commited */
325
std::map<int,std::map<QString,QString> > mChangedAttributes;
326
/**Renderer object which holds the information about how to display the features*/
327
QgsRenderer *m_renderer;
332
/**Dialog to set the properties*/
333
QgsDlgVectorLayerProperties *m_propertiesDialog;
334
/**Widget to set the symbology properties*/
335
QDialog *m_rendererDialog;
336
/**Goes through all features and finds a free id (e.g. to give it temporarily to a not-commited feature)*/
338
/**Writes the changes to disk*/
339
bool commitChanges();
340
/**Discards the edits*/
348
private: // Private attributes
350
//! Draws features. May cause projections exceptions to be generated
351
// (i.e., code that calls this function needs to catch them
352
void drawFeature(QPainter* p, QgsFeature* fet, QgsMapToPixel * cXf, QPicture* marker, double markerScaleFactor, bool projectionsEnabledFlag );
354
//! Draws the layer labels using coordinate transformation
355
void drawLabels(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf, QPaintDevice * dst);
357
// Convenience function to transform the given point
358
void transformPoint(double& x, double& y,
359
QgsMapToPixel* mtp, bool projectionsEnabledFlag);
361
void transformPoints(std::vector<double>& x, std::vector<double>& y,
362
std::vector<double>& z,
363
QgsMapToPixel* mtp, bool projectionsEnabledFlag);
365
// Inverse projects the given rectangle if coordinate transforms are
366
// in effect. Alters the given rectangle
367
QgsRect inverseProjectRect(const QgsRect& r) const;
369
// Draw the linestring as given in the WKB format. Returns a pointer
370
// to the byte after the end of the line string binary data stream
372
unsigned char* drawLineString(unsigned char* WKBlinestring, QPainter* p,
374
bool projectionsEnabledFlag);
376
// Draw the polygon as given in the WKB format. Returns a pointer to
377
// the byte after the end of the polygon binary data stream (WKB).
378
unsigned char* drawPolygon(unsigned char* WKBpolygon, QPainter* p,
379
QgsMapToPixel* mtp, bool projectionsEnabledFlag);
381
/** tailor the right-click context menu with vector layer only stuff
383
@note called by QgsMapLayer::initContextMenu();
385
void initContextMenu_(QgisApp *);
387
//! Draws the layer using coordinate transformation
388
void draw(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf, QPaintDevice * dst);
389
//! Pointer to data provider derived from the abastract base class QgsDataProvider
390
QgsVectorDataProvider *dataProvider;
391
//! index of the primary label field
393
//! Data provider key
395
//! Flag to indicate if this is a valid layer
399
/** constants for endian-ness
400
XDR is network, or big-endian, byte order
401
NDR is little-endian byte order
419
private: // Private methods
421
// pointer for loading the provider library
423
//! Pointer to the identify results dialog
424
QgsIdentifyResults *ir;
425
//! Update threshold for drawing features as they are read. A value of zero indicates
426
// that no features will be drawn until all have been read
428
//! Minimum scale factor at which the layer is displayed
430
//! Maximum scale factor at which the layer is displayed
432
//! Flag to indicate if scale dependent rendering is in effect
433
bool mScaleDependentRender;
435
/// vector layers are not copyable
436
QgsVectorLayer( QgsVectorLayer const & rhs );
438
/// vector layers are not copyable
439
QgsVectorLayer & operator=( QgsVectorLayer const & rhs );
441
// The user-defined actions that are accessed from the
442
// Identify Results dialog box
443
QgsAttributeAction mActions;
445
/**Flag indicating wheter the layer is in editing mode or not*/
447
/**Flag indicating wheter the layer has been modified since the last commit*/
453
// Convenience function to transform the given point
454
inline void QgsVectorLayer::transformPoint(double& x,
457
bool projectionsEnabledFlag)
459
// transform the point
460
if (projectionsEnabledFlag)
463
mCoordinateTransform->transformInPlace(x, y, z);
466
// transform from projected coordinate system to pixel
467
// position on map canvas
468
mtp->transformInPlace(x, y);
471
inline void QgsVectorLayer::transformPoints(
472
std::vector<double>& x, std::vector<double>& y, std::vector<double>& z,
473
QgsMapToPixel* mtp, bool projectionsEnabledFlag)
475
// transform the point
476
if (projectionsEnabledFlag)
477
mCoordinateTransform->transformInPlace(x, y, z);
479
// transform from projected coordinate system to pixel
480
// position on map canvas
481
mtp->transformInPlace(x, y);