Quantum GIS API Documentation  1.7.4
src/core/qgsvectorlayer.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           qgsvectorlayer.h  -  description
00003                              -------------------
00004     begin                : Oct 29, 2003
00005     copyright            : (C) 2003 by Gary E.Sherman
00006     email                : sherman at mrcc.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 /* $Id$ */
00018 
00019 #ifndef QGSVECTORLAYER_H
00020 #define QGSVECTORLAYER_H
00021 
00022 #include <QMap>
00023 #include <QSet>
00024 #include <QList>
00025 #include <QStringList>
00026 
00027 #include "qgis.h"
00028 #include "qgsmaplayer.h"
00029 #include "qgsfeature.h"
00030 #include "qgssnapper.h"
00031 #include "qgsfield.h"
00032 
00033 class QPainter;
00034 class QImage;
00035 
00036 class QgsAttributeAction;
00037 class QgsCoordinateTransform;
00038 class QgsGeometry;
00039 class QgsGeometryVertexIndex;
00040 class QgsMapToPixel;
00041 class QgsLabel;
00042 class QgsRectangle;
00043 class QgsRenderer;
00044 class QgsUndoCommand;
00045 class QgsVectorDataProvider;
00046 class QgsVectorOverlay;
00047 class QgsSingleSymbolRendererV2;
00048 class QgsRectangle;
00049 class QgsVectorLayerJoinBuffer;
00050 class QgsFeatureRendererV2;
00051 class QgsDiagramRendererV2;
00052 struct QgsDiagramLayerSettings;
00053 
00054 typedef QList<int> QgsAttributeList;
00055 typedef QSet<int> QgsFeatureIds;
00056 typedef QSet<int> QgsAttributeIds;
00057 
00059 struct CORE_EXPORT QgsVectorJoinInfo
00060 {
00062   int targetField;
00064   QString joinLayerId;
00066   int joinField;
00068   bool memoryCache;
00070   QHash< QString, QgsAttributeMap> cachedAttributes;
00071 };
00072 
00077 struct CORE_EXPORT QgsFetchJoinInfo
00078 {
00079   const QgsVectorJoinInfo* joinInfo;
00080   QgsAttributeList attributes; //attributes to fetch
00081   int indexOffset; //index offset between this layer and join layer
00082 };
00083 
00087 class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
00088 {
00089     Q_OBJECT
00090 
00091   public:
00092     enum EditType
00093     {
00094       LineEdit,
00095       UniqueValues,
00096       UniqueValuesEditable,
00097       ValueMap,
00098       Classification,
00099       EditRange,
00100       SliderRange,
00101       CheckBox,    /* added in 1.4 */
00102       FileName,
00103       Enumeration,
00104       Immutable,   /* The attribute value should not be changed in the attribute form*/
00105       Hidden,      /* The attribute value should not be shown in the attribute form @added in 1.4 */
00106       TextEdit,    /* multiline edit @added in 1.4*/
00107       Calendar,    /* calendar widget @added in 1.5 */
00108       DialRange,   /* dial range @added in 1.5 */
00109     };
00110 
00111     struct RangeData
00112     {
00113       RangeData() {}
00114       RangeData( QVariant theMin, QVariant theMax, QVariant theStep )
00115           : mMin( theMin ), mMax( theMax ), mStep( theStep ) {}
00116 
00117       QVariant mMin;
00118       QVariant mMax;
00119       QVariant mStep;
00120     };
00121 
00123     QgsVectorLayer( QString path = QString::null, QString baseName = QString::null,
00124                     QString providerLib = QString::null, bool loadDefaultStyleFlag = true );
00125 
00127     virtual ~QgsVectorLayer();
00128 
00130     QString storageType() const;
00131 
00133     QString capabilitiesString() const;
00134 
00136     QString dataComment() const;
00137 
00139     void setDisplayField( QString fldName = "" );
00140 
00142     const QString displayField() const;
00143 
00145     QgsVectorDataProvider* dataProvider();
00146 
00148     const QgsVectorDataProvider* dataProvider() const;
00149 
00151     void setProviderEncoding( const QString& encoding );
00152 
00154     void setCoordinateSystem();
00155 
00159     void addJoin( QgsVectorJoinInfo joinInfo );
00160 
00163     void removeJoin( const QString& joinLayerId );
00164 
00166     const QList< QgsVectorJoinInfo >& vectorJoins() const;
00167 
00169     QgsLabel *label();
00170 
00171     const QgsLabel *label() const;
00172 
00173     QgsAttributeAction* actions() { return mActions; }
00174 
00176     int selectedFeatureCount();
00177 
00179     void select( QgsRectangle & rect, bool lock );
00180 
00182     void invertSelection();
00183 
00185     void invertSelectionInRectangle( QgsRectangle & rect );
00186 
00188     QgsFeatureList selectedFeatures();
00189 
00191     const QgsFeatureIds& selectedFeaturesIds() const;
00192 
00194     void setSelectedFeatures( const QgsFeatureIds& ids );
00195 
00197     QgsRectangle boundingBoxOfSelected();
00198 
00200     bool copySymbologySettings( const QgsMapLayer& other );
00201 
00203     bool hasCompatibleSymbology( const QgsMapLayer& other ) const;
00204 
00206     const QgsRenderer* renderer() const;
00207 
00209     void setRenderer( QgsRenderer * r );
00210 
00212     void setDiagramRenderer( QgsDiagramRendererV2* r );
00213     const QgsDiagramRendererV2* diagramRenderer() const { return mDiagramRenderer; }
00214 
00215     void setDiagramLayerSettings( const QgsDiagramLayerSettings& s );
00216     const QgsDiagramLayerSettings *diagramLayerSettings() const { return mDiagramLayerSettings; }
00217 
00220     QgsFeatureRendererV2* rendererV2();
00224     void setRendererV2( QgsFeatureRendererV2* r );
00228     bool isUsingRendererV2();
00232     void setUsingRendererV2( bool usingRendererV2 );
00233 
00237     void drawRendererV2( QgsRenderContext& rendererContext, bool labeling );
00238 
00242     void drawRendererV2Levels( QgsRenderContext& rendererContext, bool labeling );
00243 
00245     QGis::GeometryType geometryType() const;
00246 
00249     bool hasGeometryType() const;
00250 
00252     QGis::WkbType wkbType() const;
00253 
00255     QString providerType() const;
00256 
00260     virtual bool readXml( QDomNode & layer_node );
00261 
00265     virtual bool writeXml( QDomNode & layer_node, QDomDocument & doc );
00266 
00272     bool readSymbology( const QDomNode& node, QString& errorMessage );
00273 
00280     bool writeSymbology( QDomNode& node, QDomDocument& doc, QString& errorMessage ) const;
00281 
00282 
00290     virtual long featureCount() const;
00291 
00295     virtual long updateFeatureCount() const;
00296 
00304     virtual bool setSubsetString( QString subset );
00305 
00310     virtual QString subsetString();
00311 
00319     void select( QgsAttributeList fetchAttributes,
00320                  QgsRectangle rect = QgsRectangle(),
00321                  bool fetchGeometry = true,
00322                  bool useIntersect = false );
00323 
00329     bool nextFeature( QgsFeature& feature );
00330 
00333     bool featureAtId( int featureId, QgsFeature &f, bool fetchGeometries = true, bool fetchAttributes = true );
00334 
00340     bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );
00341 
00342 
00347     bool insertVertex( double x, double y, int atFeatureId, int beforeVertex );
00348 
00353     bool moveVertex( double x, double y, int atFeatureId, int atVertex );
00354 
00357     bool deleteVertex( int atFeatureId, int atVertex );
00358 
00362     bool deleteSelectedFeatures();
00363 
00372     int addRing( const QList<QgsPoint>& ring );
00373 
00383     int addIsland( const QList<QgsPoint>& ring );
00384 
00390     int translateFeature( int featureId, double dx, double dy );
00391 
00396     int splitFeatures( const QList<QgsPoint>& splitLine, bool topologicalEditing = false );
00397 
00402     int removePolygonIntersections( QgsGeometry* geom );
00403 
00410     int addTopologicalPoints( QgsGeometry* geom );
00411 
00418     int addTopologicalPoints( const QgsPoint& p );
00419 
00424     int insertSegmentVerticesForSnap( const QList<QgsSnappingResult>& snapResults );
00425 
00427     void enableLabels( bool on );
00428 
00430     bool hasLabelsEnabled( void ) const;
00431 
00433     virtual bool isEditable() const;
00434 
00437     virtual bool isReadOnly() const;
00438 
00440     virtual bool isModified() const;
00441 
00447     bool snapPoint( QgsPoint& point, double tolerance );
00448 
00456     int snapWithContext( const QgsPoint& startPoint,
00457                          double snappingTolerance,
00458                          QMultiMap < double,
00459                          QgsSnappingResult > & snappingResults,
00460                          QgsSnapper::SnappingType snap_to );
00461 
00464     virtual void reload();
00465 
00469     bool draw( QgsRenderContext& rendererContext );
00470 
00472     void drawLabels( QgsRenderContext& rendererContext );
00473 
00475     const QgsFieldMap &pendingFields() const;
00476 
00478     QgsAttributeList pendingAllAttributesList();
00479 
00481     int pendingFeatureCount();
00482 
00487     bool setReadOnly( bool readonly = true );
00488 
00490     void setModified( bool modified = true, bool onlyGeometryWasModified = false );
00491 
00493     bool startEditing();
00494 
00497     bool changeGeometry( int fid, QgsGeometry* geom );
00498 
00500     bool changeAttributeValue( int fid, int field, QVariant value, bool emitSignal = true );
00501 
00505     bool addAttribute( const QgsField &field );
00506 
00510     Q_DECL_DEPRECATED bool addAttribute( QString name, QString type );
00511 
00514     void addAttributeAlias( int attIndex, QString aliasString );
00515 
00518     QString attributeAlias( int attributeIndex ) const;
00519 
00522     QString attributeDisplayName( int attributeIndex ) const;
00523 
00525     bool deleteAttribute( int attr );
00526 
00528     bool addFeatures( QgsFeatureList features, bool makeSelected = true );
00529 
00531     bool deleteFeature( int fid );
00532 
00548     bool commitChanges();
00549     const QStringList &commitErrors();
00550 
00552     bool rollBack();
00553 
00555     EditType editType( int idx );
00556 
00558     void setEditType( int idx, EditType edit );
00559 
00561     void setCheckedState( int idx, QString checked, QString notChecked );
00562 
00564     QPair<QString, QString> checkedState( int idx );
00565 
00567     QString editForm();
00568 
00570     void setEditForm( QString ui );
00571 
00573     QString annotationForm() const { return mAnnotationForm; }
00574 
00576     void setAnnotationForm( const QString& ui );
00577 
00579     QString editFormInit();
00580 
00582     void setEditFormInit( QString function );
00583 
00585     QMap<QString, QVariant> &valueMap( int idx );
00586 
00588     RangeData &range( int idx );
00589 
00593     void addOverlay( QgsVectorOverlay* overlay );
00594 
00598     void removeOverlay( const QString& typeName );
00599 
00603     void vectorOverlays( QList<QgsVectorOverlay*>& overlayList );
00604 
00608     QgsVectorOverlay* findOverlayByType( const QString& typeName );
00609 
00610 
00615     void beginEditCommand( QString text );
00616 
00618     void endEditCommand();
00619 
00621     void destroyEditCommand();
00622 
00624     void undoEditCommand( QgsUndoCommand* cmd );
00625 
00627     void redoEditCommand( QgsUndoCommand* cmd );
00628 
00632     int fieldNameIndex( const QString& fieldName ) const;
00633 
00636     enum VertexMarkerType
00637     {
00638       SemiTransparentCircle,
00639       Cross,
00640       NoMarker  /* added in version 1.1 */
00641     };
00642 
00645     static void drawVertexMarker( double x, double y, QPainter& p, QgsVectorLayer::VertexMarkerType type, int vertexSize );
00646 
00649     void updateFieldMap();
00650 
00653     void createJoinCaches();
00654 
00660     void uniqueValues( int index, QList<QVariant> &uniqueValues, int limit = -1 );
00661 
00664     QVariant minimumValue( int index );
00665 
00668     QVariant maximumValue( int index );
00669 
00670   public slots:
00672     void select( int featureId, bool emitSignal = true );
00673 
00675     void deselect( int featureId, bool emitSignal = true );
00676 
00678     void removeSelection( bool emitSignal = true );
00679 
00680     void triggerRepaint();
00681 
00685     virtual void updateExtents();
00686 
00689     void checkJoinLayerRemove( QString theLayerId );
00690 
00691   signals:
00692 
00694     void selectionChanged();
00695 
00697     void layerModified( bool onlyGeometry );
00698 
00699     void editingStarted();
00700     void editingStopped();
00701     void attributeAdded( int idx );
00702     void attributeDeleted( int idx );
00703     void featureAdded( int fid );  // added in 1.7
00704     void featureDeleted( int fid );
00705     void layerDeleted();
00706 
00707     void attributeValueChanged( int fid, int idx, const QVariant & );
00708 
00711     void committedAttributesDeleted( const QString& layerId, const QgsAttributeIds& deletedAttributeIds );
00712     void committedAttributesAdded( const QString& layerId, const QList<QgsField>& addedAttributes );
00713     void committedFeaturesAdded( const QString& layerId, const QgsFeatureList& addedFeatures );
00714     void committedFeaturesRemoved( const QString& layerId, const QgsFeatureIds& deletedFeatureIds );
00715     void committedAttributeValuesChanges( const QString& layerId, const QgsChangedAttributesMap& changedAttributesValues );
00716     void committedGeometriesChanges( const QString& layerId, const QgsGeometryMap& changedGeometries );
00717 
00718   private:                       // Private methods
00719 
00721     QgsVectorLayer( QgsVectorLayer const & rhs );
00722 
00724     QgsVectorLayer & operator=( QgsVectorLayer const & rhs );
00725 
00730     bool setDataProvider( QString const & provider );
00731 
00734     void drawFeature( QgsRenderContext &renderContext,
00735                       QgsFeature& fet,
00736                       QImage* marker );
00737 
00739     void transformPoint( double& x, double& y,
00740                          const QgsMapToPixel* mtp, const QgsCoordinateTransform* ct );
00741 
00742     void transformPoints( std::vector<double>& x, std::vector<double>& y, std::vector<double>& z, QgsRenderContext &renderContext );
00743 
00747     unsigned char *drawLineString( unsigned char *WKBlinestring, QgsRenderContext &renderContext );
00748 
00752     unsigned char *drawPolygon( unsigned char *WKBpolygon, QgsRenderContext &renderContext );
00753 
00755     int findFreeId();
00756 
00758     void deleteCachedGeometries();
00759 
00768     void snapToGeometry( const QgsPoint& startPoint, int featureId, QgsGeometry* geom, double sqrSnappingTolerance,
00769                          QMultiMap<double, QgsSnappingResult>& snappingResults, QgsSnapper::SnappingType snap_to ) const;
00770 
00773     int boundingBoxFromPointList( const QList<QgsPoint>& list, double& xmin, double& ymin, double& xmax, double& ymax ) const;
00774 
00776     static QgsVectorLayer::VertexMarkerType currentVertexMarkerType();
00777 
00779     static int currentVertexMarkerSize();
00780 
00782     void updateFeatureAttributes( QgsFeature &f, bool all = false );
00783 
00791     void addJoinedFeatureAttributes( QgsFeature& f, const QgsVectorJoinInfo& joinInfo, const QString& joinFieldName, const QVariant& joinValue,
00792                                      const QgsAttributeList& attributes, int attributeIndexOffset );
00793 
00795     void updateFeatureGeometry( QgsFeature &f );
00796 
00798     void editGeometryChange( int featureId, QgsGeometry& geometry );
00799 
00801     void editFeatureAdd( QgsFeature& feature );
00802 
00804     void editFeatureDelete( int featureId );
00805 
00807     void editAttributeChange( int featureId, int field, QVariant value );
00808 
00810     void stopRendererV2( QgsRenderContext& rendererContext, QgsSingleSymbolRendererV2* selRenderer );
00811 
00813     void updateAttributeMapIndex( QgsAttributeMap& map, int oldIndex, int newIndex ) const;
00814 
00819     void prepareLabelingAndDiagrams( QgsRenderContext& rendererContext, QgsAttributeList& attributes, bool& labeling );
00820 
00821   private:                       // Private attributes
00822 
00826     int mUpdateThreshold;
00827 
00829     QgsVectorDataProvider *mDataProvider;
00830 
00832     QString mDisplayField;
00833 
00835     QString mProviderKey;
00836 
00838     QgsAttributeAction* mActions;
00839 
00841     bool mEditable;
00842 
00844     bool mReadOnly;
00845 
00847     bool mModified;
00848 
00850     QgsGeometryMap mCachedGeometries;
00851 
00853     QgsRectangle mCachedGeometriesRect;
00854 
00859     QgsFeatureIds mSelectedFeatureIds;
00860 
00865     QgsFeatureIds mDeletedFeatureIds;
00866 
00870     QgsFeatureList mAddedFeatures;
00871 
00873     QgsChangedAttributesMap mChangedAttributeValues;
00874 
00876     QgsAttributeIds mDeletedAttributeIds;
00877 
00879     QgsAttributeIds mAddedAttributeIds;
00880 
00882     QgsGeometryMap mChangedGeometries;
00883 
00885     QgsFieldMap mUpdatedFields;
00886 
00888     QMap< QString, QString > mAttributeAliasMap;
00889 
00891     int mMaxUpdatedIndex;
00892 
00894     int mWkbType;
00895 
00896     QgsUndoCommand * mActiveCommand;
00897 
00899     QgsRenderer *mRenderer;
00900 
00902     QgsFeatureRendererV2 *mRendererV2;
00903 
00905     bool mUsingRendererV2;
00906 
00908     QgsLabel *mLabel;
00909 
00911     bool mLabelOn;
00912 
00914     QgsVectorLayer::VertexMarkerType mCurrentVertexMarkerType;
00915 
00917     int mCurrentVertexMarkerSize;
00918 
00920     bool mVertexMarkerOnlyForSelection;
00921 
00923     QList<QgsVectorOverlay*> mOverlays;
00924 
00925     QStringList mCommitErrors;
00926 
00927     QMap< QString, EditType > mEditTypes;
00928     QMap< QString, QMap<QString, QVariant> > mValueMaps;
00929     QMap< QString, RangeData > mRanges;
00930     QMap< QString, QPair<QString, QString> > mCheckedStates;
00931 
00932     QString mEditForm, mEditFormInit;
00933     //annotation form for this layer
00934     QString mAnnotationForm;
00935 
00936     bool mFetching;
00937     QgsRectangle mFetchRect;
00938     QgsAttributeList mFetchAttributes;
00939     QgsAttributeList mFetchProvAttributes;
00940     bool mFetchGeometry;
00941 
00942     QSet<int> mFetchConsidered;
00943     QgsGeometryMap::iterator mFetchChangedGeomIt;
00944     QgsFeatureList::iterator mFetchAddedFeaturesIt;
00945 
00946     //stores information about joined layers
00947     QgsVectorLayerJoinBuffer* mJoinBuffer;
00948 
00949     //diagram rendering object. 0 if diagram drawing is disabled
00950     QgsDiagramRendererV2* mDiagramRenderer;
00951 
00952     //stores infos about diagram placement (placement type, priority, position distance)
00953     QgsDiagramLayerSettings *mDiagramLayerSettings;
00954 };
00955 
00956 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines