Qgis属性显示

1、部分有关类介绍

————————————-class:QgsDualView—————————————-
/初始化属性对话框
void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsFeatureRequest &request,
const QgsAttributeEditorContext &context )

//初始化图层的属性缓存对象
void QgsDualView::initLayerCache( QgsVectorLayer* layer, bool cacheGeometry )

{
初始化mLayerCache 对象
}

//初始化属性表
void QgsDualView::initModels( QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request )

{

  1. 初始化mMasterModel对象,并设置属性表多少列
  2. 调用mMasterModel对象的函数::loadAttributes() 加载属性字段
  3. 调用mMasterModel对象的函数::loadLayer() 加载图层数据

}

————————————-class:QgsAttributeTableModel—————————————-
//加载图层数据

void QgsAttributeTableModel::loadLayer()
{ 
// make sure attributes are properly updated before caching the data 
// (emit of progress() signal may enter event loop and thus attribute
// table view may be updated with inconsistent model which may assume
// wrong number of attributes)
loadAttributes();  //加载属性字段
beginResetModel();  
if ( rowCount() != 0 )
  {
    removeRows( 0, rowCount() );
  }
 // mLayerCache对象获取要素
QgsFeatureIterator features = mLayerCache->getFeatures( mFeatureRequest ); 
int i = 0;
QTime t;
t.start();
while ( features.nextFeature( mFeat ) )  
{
    ++i;  
     if ( t.elapsed() > 1000 )
     {
      bool cancel = false;   
            emit progress( i, cancel );
            if ( cancel )
        break;
            t.restart();
      }
     featureAdded( mFeat.id() ); 
} 
emit finished(); 
connect( mLayerCache, SIGNAL( invalidated() ), this, SLOT( loadLayer() ), Qt::UniqueConnection );
endResetModel();

}

————————————-class:QgsVectorLayerCache(缓存要素)—————————————-
//成员函数部分解释

class CORE_EXPORT QgsVectorLayerCache : public QObject
{
    Q_OBJECT
  private:
    /**
     * This is a wrapper class around a cached @link QgsFeature @endlink, which will inform the cache, when it has been deleted, so indexes can be updated that the wrapped feature needs to be fetched again if needed.
     */
    class QgsCachedFeature
    {
      public:
        /**
         * Will create a new cached feature.
         * @param feat     The feature to cache. A copy will be made.
         * @param vlCache  The cache to inform when the feature has been removed from the cache.
         */
        QgsCachedFeature( const QgsFeature& feat, QgsVectorLayerCache* vlCache )
            : mCache( vlCache )
        {
          mFeature = new QgsFeature( feat );
        }

        ~QgsCachedFeature()
        {
          // That's the reason we need this wrapper:
          // Inform the cache that this feature has been removed
          mCache->featureRemoved( mFeature->id() );
          delete mFeature;
        }

        inline const QgsFeature* feature() { return mFeature; }

      private:
        QgsFeature* mFeature;
        QgsVectorLayerCache* mCache;

        friend class QgsVectorLayerCache;
        Q_DISABLE_COPY( QgsCachedFeature )
    };

  public:
    QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize, QObject* parent = nullptr );
    ~QgsVectorLayerCache();

    /**
     * 设置最大缓存要素的数量
     */
    void setCacheSize( int cacheSize );

    /**
     * @brief
     * Returns the maximum number of features this cache will hold.
     * In case full caching is enabled, this number can change, as new features get added.
     *
     * @return int
     */
    int cacheSize();

    /**
     * 启用或禁用缓存的几何图形
     * @param cacheGeometry    Enable or disable the caching of geometries
     */
    void setCacheGeometry( bool cacheGeometry );

    /**
     *  设置缓存的属性子集
     * @param attributes   The attributes to be cached
     */
    void setCacheSubsetOfAttributes( const QgsAttributeList& attributes );

    /**
     *  缓存属性的子集将自动扩展到包括新添加的属性
     * @param cacheAddedAttributes   Automatically cache new attributes
     */
    void setCacheAddedAttributes( bool cacheAddedAttributes );

    /**
     * @brief
     *  如果启用,将缓存中的所有功能。缓存大小将逐步增加提供的所有功能空间
     * 所有功能被启用时,将读入缓存。这个特性将最有可能被用于缓慢的数据源,请注意,调用这个方法可能需要很长时间。
     * @param fullCache   True: enable full caching, False: disable full caching
     */
    void setFullCache( bool fullCache );

    /**
     * @brief
     * Adds a {@link QgsAbstractCacheIndex} to this cache. Cache indices know about features present in this cache and decide, 
     * if enough information is present in the cache to respond to a {@link QgsFeatureRequest}.
     * The layer cache will take ownership of the index.
     * @param cacheIndex  The cache index to add.
     */
    void addCacheIndex( QgsAbstractCacheIndex *cacheIndex );

    /**
     * 查询图层缓存的要素
     * 如果VectorLayerCache(而且它的任何指数)能够满足要求,返回的{ @link QgsFeatureIterator }将遍历所有缓存要素。
     * 如果从缓存的要素不能得到满足要求,部分或者所有的要素将会通过数据调度获得。
     * @param featureRequest  请求指定过滤器和必需的数据
     * @return 返回满足请求的数据容器
     */
    QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest = QgsFeatureRequest() );

    /**
     * 判断缓存要素容器中包含当前要素
     * @param  fid The feature id to look for
     * @return True if this id is in the cache
     */
    bool isFidCached( const QgsFeatureId fid );

    /**
     * 根据featureId获得要素,要考虑盗更改的添加的删除的和原要素
     * @param featureId The id of the feature to query
     * @param feature   The result of the operation will be written to this feature
     * @param skipCache Will query the layer regardless if the feature is in the cache already
     * @return true in case of success
     */
    bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );

    /**
     * 从要素缓存容器中删除缓存的要素
     * @param fid The id of the feature to delete
     * @return true if the feature was removed, false if the feature id was not found in the cache
     */
    bool removeCachedFeature( QgsFeatureId fid );

    /**
     * Returns the layer to which this cache belongs
     */
    QgsVectorLayer* layer();

  protected:
    /**
     * @brief
     * Gets called, whenever the full list of feature ids for a certain request is known.
     * Broadcasts this information to indices, so they can update their tables.
     * @param featureRequest  The feature request that was answered
     * @param fids            The feature ids that have been returned
     */
    void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids );

    /**
     * @brief
     * Gets called, whenever a feature has been removed.
     * Broadcasts this information to indices, so they can invalidate their cache if required.
     * @param fid             The feature id of the removed feature.
     */
    void featureRemoved( QgsFeatureId fid );

    /**
     * @brief
     * Checks if the information required to complete the request is cached.
     * i.e. If all attributes required and the geometry is held in the cache.
     * Please note, that this does not check, if the requested features are cached.
     *
     *
     * @param featureRequest  The {@link QgsFeatureRequest} to be answered
     * @return                True if the information is being cached, false if not
     */
    bool checkInformationCovered( const QgsFeatureRequest& featureRequest );


  signals:

    /**
    * 缓存要素时,这个信号不断通知工程和取消当前操作
     * @param i       The number of already fetched features
     * @param cancel 引用bool型变量,设置true此操作将被取消
     *
     * @note not available in python bindings
     */
    void progress( int i, bool& cancel );

    /**
     * When filling the cache, this signal gets emitted once the cache is fully initialized.
     */
    void finished();

    /**
     * @brief Is emitted when the cached layer is deleted. Is emitted when the cached layers layerDelete()
     * signal is being emitted, but before the local reference to it has been set to NULL. So call to
     * @link layer() @endlink will still return a valid pointer for cleanup purpose.
     */
    void cachedLayerDeleted();

    /**
     * @brief Is emitted when an attribute is changed. Is re-emitted after the layer itself emits this signal.
     *        You should connect to this signal, to be sure, to not get a cached value if querying the cache.
     */
    void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );

    /**
     * Is emitted, when a new feature has been added to the layer and this cache.
     * You should connect to this signal instead of the layers', if you want to be sure
     * that this cache has updated information for the new feature
     *
     * @param fid The featureid of the changed feature
     */
    void featureAdded( QgsFeatureId fid );

    /**
     * The cache has been invalidated and cleared.
     */
    void invalidated();

  private slots:
    void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant& value );
    void featureDeleted( QgsFeatureId fid );
    void onFeatureAdded( QgsFeatureId fid );
    void attributeAdded( int field );
    void attributeDeleted( int field );
    void geometryChanged( QgsFeatureId fid, QgsGeometry& geom );
    void layerDeleted();
    void invalidate();

  private:
    inline void cacheFeature( QgsFeature& feat )
    {
      QgsCachedFeature* cachedFeature = new QgsCachedFeature( feat, this );
      mCache.insert( feat.id(), cachedFeature );
    }

    QgsVectorLayer* mLayer;
    QCache< QgsFeatureId, QgsCachedFeature > mCache;
    bool mCacheGeometry;
    bool mFullCache;
    QList mCacheIndices;
    QgsAttributeList mCachedAttributes;
};

2、技术流程图

Created with Raphaël 2.1.0QgsDualView::initQgsDualView::initLayerCacheQgsDualView::initModelsQgsAttributeTableModel::loadLayer()QgsAttributeTableModel::loadAttributes()QgsAttributeTableModel::loadFeatureAtIdQgsVectorLayerCache::featureAtIdQgsVectorLayerCache::getFeaturesQgsVectorLayerCache::checkInformationCovered

3后续补充:

从图层中获取要素,并要素存入mCache存储要素容器,执行顺序
void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )

bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )

bool QgsCachedFeatureWriterIterator::fetchFeature( QgsFeature& f )

bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )

bool QgsVectorLayerFeatureIterator::fetchFeature( QgsFeature& f )

bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )

QgsVectorLayerCache::cacheFeature( QgsFeature& feat )

从mCache存储要素容器获取要素,将要素属性贴到属性表中
//将mFeat对象中的属性字段值存储到属性表中
QgsAttributeTableModel::data()

//获取第fid个要素对象,并将对象存储到mFeat对象中
QgsAttributeTableModel::loadFeatureAtId( QgsFeatureId fid )

//从mCache存储要素容器中获取要素存储到feature
QgsVectorLayerCache::featureAtId( QgsFeatureId featureId, QgsFeature& feature, bool skipCache )

//将mFeat对象中的属性字段值存储到属性表中
QgsAttributeTableModel::data()

//从数据获取要素
QgsCachedFeatureWriterIterator::fetchFeature( QgsFeature& f )

你可能感兴趣的:(Qgis属性显示)