QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsvectorlayer.h
Go to the documentation of this file.
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 
18 #ifndef QGSVECTORLAYER_H
19 #define QGSVECTORLAYER_H
20 
21 #include <QMap>
22 #include <QSet>
23 #include <QList>
24 #include <QStringList>
25 
26 #include "qgis.h"
27 #include "qgsmaplayer.h"
28 #include "qgsfeature.h"
29 #include "qgsfeatureiterator.h"
30 #include "qgsfield.h"
31 #include "qgssnapper.h"
32 #include "qgsfield.h"
33 
34 class QPainter;
35 class QImage;
36 
37 class QgsAttributeAction;
39 class QgsFeatureRequest;
40 class QgsGeometry;
41 class QgsGeometryVertexIndex;
42 class QgsMapToPixel;
43 class QgsLabel;
44 class QgsRectangle;
47 class QgsRectangle;
52 class QgsGeometryCache;
55 
56 typedef QList<int> QgsAttributeList;
57 typedef QSet<int> QgsAttributeIds;
58 
60 class CORE_EXPORT QgsAttributeEditorElement : public QObject
61 {
62  Q_OBJECT
63  public:
64 
66  {
69  AeTypeInvalid
70  };
71 
72 
73  QgsAttributeEditorElement( AttributeEditorType type, QString name, QObject *parent = NULL )
74  : QObject( parent ), mType( type ), mName( name ) {}
75 
77 
78  QString name() const { return mName; }
79  AttributeEditorType type() const { return mType; }
80 
81  virtual QDomElement toDomElement( QDomDocument& doc ) const = 0;
82 
83  protected:
85  QString mName;
86 };
87 
90 {
91  public:
92  QgsAttributeEditorContainer( QString name, QObject *parent )
93  : QgsAttributeEditorElement( AeTypeContainer, name, parent ) {}
94 
96 
97  virtual QDomElement toDomElement( QDomDocument& doc ) const;
98  virtual void addChildElement( QgsAttributeEditorElement *widget );
99  QList<QgsAttributeEditorElement*> children() const { return mChildren; }
100 
101  private:
102  QList<QgsAttributeEditorElement*> mChildren;
103 };
104 
107 {
108  public:
109  QgsAttributeEditorField( QString name , int idx, QObject *parent )
110  : QgsAttributeEditorElement( AeTypeField, name, parent ), mIdx( idx ) {}
111 
113 
114  virtual QDomElement toDomElement( QDomDocument& doc ) const;
115  int idx() const { return mIdx; }
116 
117  private:
118  int mIdx;
119 };
120 
122 struct CORE_EXPORT QgsVectorJoinInfo
123 {
127  QString joinLayerId;
129  QString joinFieldName;
135  QHash< QString, QgsAttributes> cachedAttributes;
136 
141 };
142 
143 
412 class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
413 {
414  Q_OBJECT
415 
416  public:
419  {
420  GeneratedLayout = 0,
421  TabLayout = 1,
422  UiFileLayout = 2
423  };
424 
425  enum EditType
426  {
447  };
448 
449  struct RangeData
450  {
452  RangeData( QVariant theMin, QVariant theMax, QVariant theStep )
453  : mMin( theMin ), mMax( theMax ), mStep( theStep ) {}
454 
455  QVariant mMin;
456  QVariant mMax;
457  QVariant mStep;
458  };
459 
461  {
463  ValueRelationData( QString layer, QString key, QString value, bool allowNull, bool orderByValue,
464  bool allowMulti = false,
465  QString filterExpression = QString::null )
466  : mLayer( layer )
467  , mKey( key )
468  , mValue( value )
469  , mFilterExpression( filterExpression )
470  , mAllowNull( allowNull )
471  , mOrderByValue( orderByValue )
472  , mAllowMulti( allowMulti )
473  {}
474 
475  QString mLayer;
476  QString mKey;
477  QString mValue;
481  bool mAllowMulti; /* allow selection of multiple keys @added in 1.9 */
482  };
483 
484  struct GroupData
485  {
487  GroupData( QString name , QList<QString> fields )
488  : mName( name ), mFields( fields ) {}
489  QString mName;
490  QList<QString> mFields;
491  };
492 
493  struct TabData
494  {
495  TabData() {}
496  TabData( QString name , QList<QString> fields , QList<GroupData> groups )
497  : mName( name ), mFields( fields ), mGroups( groups ) {}
498  QString mName;
499  QList<QString> mFields;
500  QList<GroupData> mGroups;
501  };
502 
516  QgsVectorLayer( QString path = QString::null, QString baseName = QString::null,
517  QString providerLib = QString::null, bool loadDefaultStyleFlag = true );
518 
520  virtual ~QgsVectorLayer();
521 
523  QString storageType() const;
524 
526  QString capabilitiesString() const;
527 
529  QString dataComment() const;
530 
532  void setDisplayField( QString fldName = "" );
533 
535  const QString displayField() const;
536 
544  void setDisplayExpression( const QString displayExpression );
545 
554  const QString displayExpression();
555 
557  QgsVectorDataProvider* dataProvider();
558 
562  const QgsVectorDataProvider* dataProvider() const;
563 
565  void setProviderEncoding( const QString& encoding );
566 
568  void setCoordinateSystem();
569 
573  void addJoin( const QgsVectorJoinInfo& joinInfo );
574 
577  void removeJoin( const QString& joinLayerId );
578 
580  const QList< QgsVectorJoinInfo >& vectorJoins() const;
581 
583  QgsLabel *label();
584 
585  const QgsLabel *label() const;
586 
587  QgsAttributeAction *actions() { return mActions; }
588 
594  int selectedFeatureCount();
595 
604  void select( QgsRectangle & rect, bool addToSelection );
605 
617  void modifySelection( QgsFeatureIds selectIds, QgsFeatureIds deselectIds );
618 
620  void invertSelection();
621 
623  void selectAll();
624 
632  void invertSelectionInRectangle( QgsRectangle & rect );
633 
641  QgsFeatureList selectedFeatures();
642 
649  const QgsFeatureIds &selectedFeaturesIds() const;
650 
658  void setSelectedFeatures( const QgsFeatureIds &ids );
659 
661  QgsRectangle boundingBoxOfSelected();
662 
664  void setDiagramRenderer( QgsDiagramRendererV2* r );
665  const QgsDiagramRendererV2* diagramRenderer() const { return mDiagramRenderer; }
666 
667  void setDiagramLayerSettings( const QgsDiagramLayerSettings& s );
668  const QgsDiagramLayerSettings *diagramLayerSettings() const { return mDiagramLayerSettings; }
669 
672  QgsFeatureRendererV2* rendererV2();
676  void setRendererV2( QgsFeatureRendererV2* r );
677 
681  void drawRendererV2( QgsFeatureIterator &fit, QgsRenderContext& rendererContext, bool labeling );
682 
686  void drawRendererV2Levels( QgsFeatureIterator &fit, QgsRenderContext& rendererContext, bool labeling );
687 
689  QGis::GeometryType geometryType() const;
690 
694  bool hasGeometryType() const;
695 
697  QGis::WkbType wkbType() const;
698 
700  QString providerType() const;
701 
705  virtual bool readXml( const QDomNode& layer_node );
706 
710  virtual bool writeXml( QDomNode & layer_node, QDomDocument & doc );
711 
720  virtual void saveStyleToDatabase( QString name, QString description,
721  bool useAsDefault, QString uiFileContent,
722  QString &msgError );
723 
732  virtual int listStylesInDatabase( QStringList &ids, QStringList &names,
733  QStringList &descriptions, QString &msgError );
734 
738  virtual QString getStyleFromDatabase( QString styleId, QString &msgError );
739 
746  virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag, bool loadFromLocalDb );
747 
752  virtual QString loadNamedStyle( const QString theURI, bool &theResultFlag );
753 
754  virtual bool applyNamedStyle( QString namedStyle , QString errorMsg );
755 
760  QgsAttributeEditorElement* attributeEditorElementFromDomElement( QDomElement &elem, QObject* parent );
761 
767  bool readSymbology( const QDomNode& node, QString& errorMessage );
768 
775  bool writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
776 
777  bool writeSld( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
778  bool readSld( const QDomNode& node, QString& errorMessage );
779 
787  virtual long featureCount() const;
788 
795  long featureCount( QgsSymbolV2* symbol );
796 
802  bool countSymbolFeatures( bool showProgress = true );
803 
811  virtual bool setSubsetString( QString subset );
812 
817  virtual QString subsetString();
818 
822  QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() );
823 
829  bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );
830 
836  bool updateFeature( QgsFeature &f );
837 
842  bool insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex );
843 
848  bool moveVertex( double x, double y, QgsFeatureId atFeatureId, int atVertex );
849 
852  bool deleteVertex( QgsFeatureId atFeatureId, int atVertex );
853 
857  bool deleteSelectedFeatures();
858 
868  int addRing( const QList<QgsPoint>& ring );
869 
880  int addPart( const QList<QgsPoint>& ring );
881 
887  int translateFeature( QgsFeatureId featureId, double dx, double dy );
888 
896  int splitFeatures( const QList<QgsPoint>& splitLine, bool topologicalEditing = false );
897 
904  int removePolygonIntersections( QgsGeometry* geom, QgsFeatureIds ignoreFeatures = QgsFeatureIds() );
905 
911  int addTopologicalPoints( QgsGeometry* geom );
912 
920  int addTopologicalPoints( const QgsPoint& p );
921 
927  int insertSegmentVerticesForSnap( const QList<QgsSnappingResult>& snapResults );
928 
930  void enableLabels( bool on );
931 
933  bool hasLabelsEnabled() const;
934 
936  virtual bool isEditable() const;
937 
941  virtual bool isReadOnly() const;
942 
944  virtual bool isModified() const;
945 
952  bool snapPoint( QgsPoint& point, double tolerance );
953 
961  int snapWithContext( const QgsPoint& startPoint,
962  double snappingTolerance,
963  QMultiMap < double, QgsSnappingResult > &snappingResults,
964  QgsSnapper::SnappingType snap_to );
965 
968  virtual void reload();
969 
973  bool draw( QgsRenderContext& rendererContext );
974 
976  void drawLabels( QgsRenderContext& rendererContext );
977 
980 
982  const QgsFields &pendingFields() const;
983 
985  QgsAttributeList pendingAllAttributesList();
986 
990  QgsAttributeList pendingPkAttributesList();
991 
993  int pendingFeatureCount();
994 
999  bool setReadOnly( bool readonly = true );
1000 
1002  bool startEditing();
1003 
1006  bool changeGeometry( QgsFeatureId fid, QgsGeometry* geom );
1007 
1009  bool changeAttributeValue( QgsFeatureId fid, int field, QVariant value, bool emitSignal = true );
1010 
1014  bool addAttribute( const QgsField &field );
1015 
1018  void addAttributeAlias( int attIndex, QString aliasString );
1019 
1022  void addAttributeEditorWidget( QgsAttributeEditorElement* data );
1023 
1026  QList< QgsAttributeEditorElement* > &attributeEditorElements();
1029  void clearAttributeEditorWidgets();
1030 
1033  QString attributeAlias( int attributeIndex ) const;
1034 
1037  QString attributeDisplayName( int attributeIndex ) const;
1038 
1039  const QMap< QString, QString >& attributeAliases() const { return mAttributeAliasMap; }
1040 
1041  const QSet<QString>& excludeAttributesWMS() const { return mExcludeAttributesWMS; }
1042  void setExcludeAttributesWMS( const QSet<QString>& att ) { mExcludeAttributesWMS = att; }
1043 
1044  const QSet<QString>& excludeAttributesWFS() const { return mExcludeAttributesWFS; }
1045  void setExcludeAttributesWFS( const QSet<QString>& att ) { mExcludeAttributesWFS = att; }
1046 
1048  bool deleteAttribute( int attr );
1049 
1057  bool deleteAttributes( QList<int> attrs );
1058 
1060  bool addFeatures( QgsFeatureList features, bool makeSelected = true );
1061 
1063  bool deleteFeature( QgsFeatureId fid );
1064 
1080  bool commitChanges();
1081  const QStringList &commitErrors();
1082 
1086  bool rollBack( bool deleteBuffer = true );
1087 
1089  EditType editType( int idx );
1090 
1092  void setEditType( int idx, EditType edit );
1093 
1095  EditorLayout editorLayout();
1096 
1098  void setEditorLayout( EditorLayout editorLayout );
1099 
1101  void setCheckedState( int idx, QString checked, QString notChecked );
1102 
1107  QPair<QString, QString> checkedState( int idx );
1108 
1110  QString editForm();
1111 
1113  void setEditForm( QString ui );
1114 
1116  QString annotationForm() const { return mAnnotationForm; }
1117 
1119  void setAnnotationForm( const QString& ui );
1120 
1122  QString editFormInit();
1123 
1125  void setEditFormInit( QString function );
1126 
1128  QMap<QString, QVariant> &valueMap( int idx );
1129 
1131  RangeData &range( int idx );
1132 
1136  ValueRelationData &valueRelation( int idx );
1137 
1141  QString &dateFormat( int idx );
1142 
1146  QSize &widgetSize( int idx );
1147 
1151  bool fieldEditable( int idx );
1152 
1156  bool labelOnTop( int idx );
1157 
1161  void setFieldEditable( int idx, bool editable );
1162 
1166  void setLabelOnTop( int idx, bool onTop );
1167 
1169  QgsVectorLayerEditBuffer* editBuffer() { return mEditBuffer; }
1170 
1175  void beginEditCommand( QString text );
1176 
1178  void endEditCommand();
1179 
1181  void destroyEditCommand();
1182 
1186  int fieldNameIndex( const QString& fieldName ) const;
1187 
1191  {
1194  NoMarker /* added in version 1.1 */
1195  };
1196 
1199  static void drawVertexMarker( double x, double y, QPainter& p, QgsVectorLayer::VertexMarkerType type, int vertexSize );
1200 
1203  void updateFields();
1204 
1207  void createJoinCaches();
1208 
1214  void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
1215 
1218  QVariant minimumValue( int index );
1219 
1222  QVariant maximumValue( int index );
1223 
1224  /* Set the blending mode used for rendering each feature
1225  * @note added in 2.0
1226  */
1227  void setFeatureBlendMode( const QPainter::CompositionMode blendMode );
1228  /* Returns the current blending mode for features
1229  * @note added in 2.0
1230  */
1231  QPainter::CompositionMode featureBlendMode() const;
1232 
1233  /* Set the transparency for the vector layer
1234  * @note added in 2.0
1235  */
1236  void setLayerTransparency( int layerTransparency );
1237  /* Returns the current transparency for the vector layer
1238  * @note added in 2.0
1239  */
1240  int layerTransparency() const;
1241 
1242  public slots:
1250  void select( const QgsFeatureId &featureId );
1251 
1259  void select( const QgsFeatureIds& featureIds );
1260 
1268  void deselect( const QgsFeatureId featureId );
1269 
1277  void deselect( const QgsFeatureIds& featureIds );
1278 
1284  void removeSelection();
1285 
1286  void triggerRepaint();
1287 
1291  virtual void updateExtents();
1292 
1295  void checkJoinLayerRemove( QString theLayerId );
1296 
1297  QString metadata();
1298 
1300  inline QgsGeometryCache* cache() { return mCache; }
1301 
1306  virtual void onCacheImageDelete();
1307 
1308  protected slots:
1309  void invalidateSymbolCountedFlag();
1310 
1311  signals:
1312 
1320  void selectionChanged( const QgsFeatureIds selected, const QgsFeatureIds deselected, const bool clearAndSelect );
1321 
1323  void selectionChanged();
1324 
1326  void layerModified();
1327 
1329  void editingStarted();
1330 
1332  void editingStopped();
1333 
1335  void beforeCommitChanges();
1336 
1338  void beforeRollBack();
1339 
1348  void attributeAdded( int idx );
1357  void attributeDeleted( int idx );
1358  void featureAdded( QgsFeatureId fid ); // added in 1.7
1359  void featureDeleted( QgsFeatureId fid );
1366  void updatedFields();
1367  void layerDeleted();
1368 
1369  void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant & );
1370  void geometryChanged( QgsFeatureId fid, QgsGeometry &geom ); // added in 1.9
1371 
1374  void committedAttributesDeleted( const QString& layerId, const QgsAttributeList& deletedAttributes );
1375  void committedAttributesAdded( const QString& layerId, const QList<QgsField>& addedAttributes );
1376  void committedFeaturesAdded( const QString& layerId, const QgsFeatureList& addedFeatures );
1377  void committedFeaturesRemoved( const QString& layerId, const QgsFeatureIds& deletedFeatureIds );
1378  void committedAttributeValuesChanges( const QString& layerId, const QgsChangedAttributesMap& changedAttributesValues );
1379  void committedGeometriesChanges( const QString& layerId, const QgsGeometryMap& changedGeometries );
1380 
1384  void labelingFontNotFound( QgsVectorLayer* layer, const QString& fontfamily );
1385 
1386  protected:
1388  void setExtent( const QgsRectangle &rect );
1389 
1390  private: // Private methods
1391 
1393  QgsVectorLayer( const QgsVectorLayer & rhs );
1394 
1396  QgsVectorLayer & operator=( QgsVectorLayer const & rhs );
1397 
1402  bool setDataProvider( QString const & provider );
1403 
1405  QgsFeatureId findFreeId();
1406 
1415  void snapToGeometry( const QgsPoint& startPoint,
1416  QgsFeatureId featureId,
1417  QgsGeometry* geom,
1418  double sqrSnappingTolerance,
1419  QMultiMap<double, QgsSnappingResult>& snappingResults,
1420  QgsSnapper::SnappingType snap_to ) const;
1421 
1423  static QgsVectorLayer::VertexMarkerType currentVertexMarkerType();
1424 
1426  static int currentVertexMarkerSize();
1427 
1429  //void addJoinedAttributes( QgsFeature& f, bool all = false );
1430 
1432  void stopRendererV2( QgsRenderContext& rendererContext, QgsSingleSymbolRendererV2* selRenderer );
1433 
1438  void prepareLabelingAndDiagrams( QgsRenderContext& rendererContext, QgsAttributeList& attributes, bool& labeling );
1439 
1440  private: // Private attributes
1441 
1446 
1452 
1455 
1457 
1459  QString mDisplayField;
1460 
1463 
1465  QString mProviderKey;
1466 
1469 
1472 
1478 
1481 
1483  QMap< QString, QString > mAttributeAliasMap;
1484 
1486  QList< QgsAttributeEditorElement* > mAttributeEditorElements;
1487 
1489  QSet<QString> mExcludeAttributesWMS;
1491  QSet<QString> mExcludeAttributesWFS;
1492 
1494  QList< TabData > mTabs;
1495 
1498 
1501 
1504 
1506  bool mLabelOn;
1507 
1510 
1512  QPainter::CompositionMode mFeatureBlendMode;
1513 
1516 
1519 
1522 
1525 
1526  QStringList mCommitErrors;
1527 
1528  QMap< QString, EditType > mEditTypes;
1529  QMap< QString, bool> mFieldEditables;
1530  QMap< QString, bool> mLabelOnTop;
1531  QMap< QString, QMap<QString, QVariant> > mValueMaps;
1532  QMap< QString, RangeData > mRanges;
1533  QMap< QString, QPair<QString, QString> > mCheckedStates;
1534  QMap< QString, ValueRelationData > mValueRelations;
1535  QMap< QString, QString> mDateFormats;
1536  QMap< QString, QSize> mWidgetSize;
1537 
1540 
1541  QString mEditForm, mEditFormInit;
1542  //annotation form for this layer
1544 
1547 
1551 
1552  //stores information about joined layers
1554 
1555  //diagram rendering object. 0 if diagram drawing is disabled
1557 
1558  //stores infos about diagram placement (placement type, priority, position distance)
1560 
1562 
1563  // Features in renderer classes counted
1565 
1566  // Feature counts for each renderer symbol
1567  QMap<QgsSymbolV2*, long> mSymbolFeatureCountMap;
1568 
1570 
1572 };
1573 
1574 #endif