~ubuntu-branches/ubuntu/hardy/qgis/hardy

« back to all changes in this revision

Viewing changes to src/qgsvectorlayer.h

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                          qgsvectorlayer.h  -  description
3
 
                             -------------------
4
 
    begin                : Oct 29, 2003
5
 
    copyright            : (C) 2003 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: qgsvectorlayer.h,v 1.69.2.2 2005/10/05 08:54:43 g_j_m Exp $ */
18
 
 
19
 
#ifndef QGSVECTORLAYER_H
20
 
#define QGSVECTORLAYER_H
21
 
 
22
 
class QPainter;
23
 
class QgsRect;
24
 
class QLibrary;
25
 
class QgsMapToPixel;
26
 
class OGRLayer;
27
 
class OGRDataSource;
28
 
class QgsData;
29
 
class QgsRenderer;
30
 
class QgsLegendItem;
31
 
class QgsDlgVectorLayerProperties;
32
 
class QgisApp;
33
 
class QgsIdentifyResults;
34
 
class QgsLabel;
35
 
 
36
 
#include <map>
37
 
#include <vector>
38
 
 
39
 
#include "qgsmaplayer.h"
40
 
#include "qvaluevector.h"
41
 
#include "qgsattributetabledisplay.h"
42
 
#include "qgsvectordataprovider.h"
43
 
#include "qgsattributeaction.h"
44
 
 
45
 
/*! \class QgsVectorLayer
46
 
 * \brief Vector layer backed by a data source provider
47
 
 */
48
 
 
49
 
class QgsVectorLayer : public QgsMapLayer
50
 
{
51
 
  Q_OBJECT;
52
 
 
53
 
public:
54
 
 
55
 
  //! Constructor
56
 
  QgsVectorLayer(QString baseName = 0, QString path = 0, QString providerLib = 0);
57
 
 
58
 
  //! Destructor
59
 
  virtual ~QgsVectorLayer();
60
 
 
61
 
  //! Identify feature found within the search rectangle
62
 
  void identify(QgsRect *);
63
 
 
64
 
  //! Select features found within the search rectangle
65
 
  void select(QgsRect * rect, bool lock);
66
 
 
67
 
  //!Select not selected features and deselect selected ones
68
 
  void invertSelection();
69
 
 
70
 
  //! Display the attribute table
71
 
  void table();
72
 
 
73
 
  //! Resize and fill the attribute table
74
 
  void fillTable(QgsAttributeTable* t);
75
 
 
76
 
  //! Set the primary display field to be used in the identify results dialog
77
 
  void setDisplayField(QString fldName=0);
78
 
 
79
 
  //! Returns the primary display field name used in the identify results dialog
80
 
const QString displayField() const { return fieldIndex; }
81
 
 
82
 
  //! Initialize the context menu
83
 
  void initContextMenu(QgisApp * app);
84
 
 
85
 
  enum SHAPETYPE
86
 
  {
87
 
    Point,
88
 
    Line,
89
 
    Polygon
90
 
  };
91
 
 
92
 
  /** bind layer to a specific data provider
93
 
 
94
 
     @param provider should be "postgres", "ogr", or ??
95
 
 
96
 
     @todo XXX should this return bool?  Throw exceptions?
97
 
  */
98
 
  bool setDataProvider( QString const & provider );
99
 
  
100
 
  //! Setup the coordinate system tranformation for the layer
101
 
  void setCoordinateSystem();
102
 
 
103
 
  QgsVectorDataProvider * getDataProvider();
104
 
 
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();
107
 
  /*!
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
110
 
   */
111
 
  int getProjectionSrid();
112
 
  QgsLabel *label();
113
 
 
114
 
  QgsAttributeAction* actions() { return &mActions; }
115
 
 
116
 
 signals:
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);
123
 
 
124
 
public slots:
125
 
 
126
 
  void inOverview( bool );
127
 
 
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()
157
 
  {
158
 
    return valid;
159
 
  }
160
 
 
161
 
  /** reads vector layer specific state from project file DOM node.
162
 
 
163
 
      @note
164
 
 
165
 
      Called by QgsMapLayer::readXML().
166
 
 
167
 
  */
168
 
  /* virtual */ bool readXML_( QDomNode & layer_node );
169
 
 
170
 
 
171
 
  /** write vector layer specific state to project file DOM node.
172
 
 
173
 
      @note
174
 
 
175
 
      Called by QgsMapLayer::writeXML().
176
 
 
177
 
  */
178
 
  /* virtual */ bool writeXML_( QDomNode & layer_node, QDomDocument & doc );
179
 
 
180
 
 
181
 
  /**
182
 
  * Get the first feature resulting from a select operation
183
 
  * @return QgsFeature
184
 
  */
185
 
  virtual QgsFeature * getFirstFeature(bool fetchAttributes=false) const;
186
 
 
187
 
  /**
188
 
  * Get the next feature resulting from a select operation
189
 
  * @return QgsFeature
190
 
  */
191
 
  virtual QgsFeature * getNextFeature(bool fetchAttributes=false) const;
192
 
 
193
 
  /**
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
197
 
   */
198
 
  virtual bool getNextFeature(QgsFeature &feature, bool fetchAttributes=false) const;
199
 
 
200
 
  /**
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
204
 
   * can be returned.
205
 
   * @return long containing number of features
206
 
   */
207
 
  virtual long featureCount() const;
208
 
 
209
 
  /**
210
 
   * Update the feature count 
211
 
   * @return long containing the number of features in the datasource
212
 
   */
213
 
  virtual long updateFeatureCount() const;
214
 
 
215
 
  /**
216
 
   * Update the extents for the layer. This is necessary if features are
217
 
   * added/deleted or the layer has been subsetted.
218
 
   */
219
 
  virtual void updateExtents();
220
 
 
221
 
  /**
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
225
 
   * store.
226
 
   */
227
 
  virtual void setSubsetString(QString subset);
228
 
 
229
 
  /**
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
232
 
   */
233
 
  virtual QString subsetString();
234
 
  /**
235
 
   * Number of attribute fields for a feature in the layer
236
 
   */
237
 
  virtual int fieldCount() const;
238
 
 
239
 
 
240
 
  /**
241
 
    Return a list of field names for this layer
242
 
   @return vector of field names
243
 
  */
244
 
  virtual std::vector<QgsField> const & fields() const;
245
 
 
246
 
  /**Adds a feature
247
 
   @return true in case of success and false in case of error*/
248
 
  bool addFeature(QgsFeature* f);
249
 
 
250
 
  /**Deletes the selected features
251
 
     @return true in case of success and false otherwise*/
252
 
  bool deleteSelectedFeatures();
253
 
 
254
 
  /**Returns the default value for the attribute @c attr for the feature
255
 
     @c f. */
256
 
  QString getDefaultValue(const QString& attr, QgsFeature* f);
257
 
 
258
 
  /**Set labels on */
259
 
  void setLabelOn( bool on );
260
 
 
261
 
  /**Label is on */
262
 
  bool labelOn( void );
263
 
 
264
 
  /**
265
 
   * Minimum scale at which the layer is rendered
266
 
   * @return Scale as integer 
267
 
   */
268
 
  int minimumScale();
269
 
  /**
270
 
   * Maximum scale at which the layer is rendered
271
 
   * @return Scale as integer 
272
 
   */
273
 
  int maximumScale();
274
 
  /**
275
 
   * Is scale dependent rendering in effect?
276
 
   * @return true if so
277
 
   */
278
 
  bool scaleDependentRender();
279
 
 
280
 
  /**Returns true if the provider is in editing mode*/
281
 
  virtual bool isEditable() const {return (mEditable&&dataProvider);}
282
 
 
283
 
  /**Returns true if the provider has been modified since the last commit*/
284
 
  virtual bool isModified() const {return mModified;}
285
 
 
286
 
  //! Save as shapefile
287
 
  virtual void saveAsShapefile();
288
 
 
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);
295
 
 
296
 
  /**Commits edited attributes. Depending on the feature id,
297
 
     the changes are written to not commited features or redirected to
298
 
     the data provider*/
299
 
  bool commitAttributeChanges(const std::set<QString>& deleted,
300
 
            const std::map<QString,QString>& added,
301
 
            std::map<int,std::map<QString,QString> >& changed);
302
 
 
303
 
  /** \brief Draws the layer using coordinate transformation
304
 
   *  \param widthScale line width scale
305
 
   *  \param symbolScale symbol scale
306
 
   */
307
 
  void draw(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf,  QPaintDevice * dst, double widthScale, double symbolScale, int oversampling = 1);
308
 
 
309
 
  /** \brief Draws the layer labels using coordinate transformation
310
 
   *  \param scale size scale, applied to all values in pixels
311
 
   */
312
 
  void drawLabels(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf,  QPaintDevice * dst, double scale);
313
 
 
314
 
  
315
 
 
316
 
protected:
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;
328
 
  /**Label */
329
 
  QgsLabel *mLabel;
330
 
  /**Display labels */
331
 
  bool mLabelOn;
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)*/
337
 
  int findFreeId();
338
 
  /**Writes the changes to disk*/
339
 
  bool commitChanges();
340
 
  /**Discards the edits*/
341
 
  bool rollBack();
342
 
 
343
 
protected slots:
344
 
  void startEditing();
345
 
  void stopEditing();
346
 
 
347
 
 
348
 
private:                       // Private attributes
349
 
 
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 );
353
 
 
354
 
  //! Draws the layer labels using coordinate transformation
355
 
  void drawLabels(QPainter * p, QgsRect * viewExtent, QgsMapToPixel * cXf,  QPaintDevice * dst);
356
 
 
357
 
  // Convenience function to transform the given point
358
 
  void transformPoint(double& x, double& y, 
359
 
      QgsMapToPixel* mtp, bool projectionsEnabledFlag);
360
 
  
361
 
  void transformPoints(std::vector<double>& x, std::vector<double>& y,
362
 
      std::vector<double>& z,
363
 
      QgsMapToPixel* mtp, bool projectionsEnabledFlag);
364
 
 
365
 
  // Inverse projects the given rectangle if coordinate transforms are
366
 
  // in effect. Alters the given rectangle
367
 
  QgsRect inverseProjectRect(const QgsRect& r) const;
368
 
 
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
371
 
  // (WKB).
372
 
  unsigned char* drawLineString(unsigned char* WKBlinestring, QPainter* p,
373
 
      QgsMapToPixel* mtp, 
374
 
      bool projectionsEnabledFlag);
375
 
 
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);
380
 
 
381
 
  /** tailor the right-click context menu with vector layer only stuff
382
 
 
383
 
    @note called by QgsMapLayer::initContextMenu();
384
 
   */
385
 
  void initContextMenu_(QgisApp *);
386
 
 
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
392
 
  QString fieldIndex;
393
 
  //! Data provider key
394
 
  QString providerKey;
395
 
  //! Flag to indicate if this is a valid layer
396
 
  bool valid;
397
 
  bool registered;
398
 
 
399
 
  /** constants for endian-ness
400
 
    XDR is network, or big-endian, byte order
401
 
    NDR is little-endian byte order
402
 
  */
403
 
  typedef enum ENDIAN
404
 
  {
405
 
    XDR = 0,
406
 
    NDR = 1
407
 
  }
408
 
  endian_t;
409
 
 
410
 
  enum WKBTYPE
411
 
  {
412
 
    WKBPoint = 1,
413
 
    WKBLineString,
414
 
    WKBPolygon,
415
 
    WKBMultiPoint,
416
 
    WKBMultiLineString,
417
 
    WKBMultiPolygon
418
 
  };
419
 
private:                       // Private methods
420
 
  endian_t endian();
421
 
  // pointer for loading the provider library
422
 
  QLibrary *myLib;
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
427
 
  int updateThreshold;
428
 
  //! Minimum scale factor at which the layer is displayed
429
 
  int mMinimumScale;
430
 
  //! Maximum scale factor at which the layer is displayed
431
 
  int mMaximumScale;
432
 
  //! Flag to indicate if scale dependent rendering is in effect
433
 
  bool mScaleDependentRender;
434
 
 
435
 
  /// vector layers are not copyable
436
 
  QgsVectorLayer( QgsVectorLayer const & rhs );
437
 
 
438
 
  /// vector layers are not copyable
439
 
  QgsVectorLayer & operator=( QgsVectorLayer const & rhs );
440
 
 
441
 
  // The user-defined actions that are accessed from the
442
 
  // Identify Results dialog box
443
 
  QgsAttributeAction mActions;
444
 
 
445
 
  /**Flag indicating wheter the layer is in editing mode or not*/
446
 
  bool mEditable;
447
 
  /**Flag indicating wheter the layer has been modified since the last commit*/
448
 
  bool mModified;
449
 
 
450
 
};
451
 
 
452
 
 
453
 
// Convenience function to transform the given point
454
 
inline void QgsVectorLayer::transformPoint(double& x, 
455
 
             double& y, 
456
 
             QgsMapToPixel* mtp,
457
 
             bool projectionsEnabledFlag)
458
 
{
459
 
  // transform the point
460
 
  if (projectionsEnabledFlag)
461
 
  {
462
 
    double z = 0;
463
 
    mCoordinateTransform->transformInPlace(x, y, z);
464
 
  }
465
 
 
466
 
  // transform from projected coordinate system to pixel 
467
 
  // position on map canvas
468
 
  mtp->transformInPlace(x, y);
469
 
}
470
 
 
471
 
inline void QgsVectorLayer::transformPoints(
472
 
    std::vector<double>& x, std::vector<double>& y, std::vector<double>& z,
473
 
    QgsMapToPixel* mtp, bool projectionsEnabledFlag)
474
 
{
475
 
  // transform the point
476
 
  if (projectionsEnabledFlag)
477
 
    mCoordinateTransform->transformInPlace(x, y, z);
478
 
 
479
 
  // transform from projected coordinate system to pixel 
480
 
  // position on map canvas
481
 
  mtp->transformInPlace(x, y);
482
 
}
483
 
#endif