Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
re-add vector attribute method removed in r10863
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10982 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jun 26, 2009
1 parent 80fd596 commit ccea976
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
12 changes: 10 additions & 2 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -168,11 +168,19 @@ class QgsVectorDataProvider : QgsDataProvider

/**
* Adds new attributes
* @param attributes map with attribute name as key and type as value
* @param attributes list of new attribute fields
* @return true in case of success and false in case of failure
* @note changed in 1.2
*/
virtual bool addAttributes(const QList<QgsField> & attributes);
virtual bool addAttributes(const QList<QgsField> &attributes);

/**
* Adds new attributes
* @param attributes map with attribute name as key and type as value
* @return true in case of success and false in case of failure
* @note deprecated
*/
virtual bool addAttributes(const QMap<QString, QString> &attributes);

/**
* Deletes existing attributes
Expand Down
10 changes: 9 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -324,9 +324,17 @@ public:
bool changeAttributeValue(int fid, int field, QVariant value, bool emitSignal = true);

/** add an attribute field (but does not commit it)
returns the field index or -1 in case of failure */
returns true in case of success
@note added in 1.2
*/
bool addAttribute( const QgsField &field );

/** add an attribute field (but does not commit it)
returns true in case of success
@note deprecated
*/
bool addAttribute( QString name, QString type );

/** delete an attribute field (but does not commit it) */
bool deleteAttribute(int attr);

Expand Down
32 changes: 32 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -83,6 +83,22 @@ bool QgsVectorDataProvider::addAttributes( const QList<QgsField> & attributes )
return false;
}

bool QgsVectorDataProvider::addAttributes( const QMap<QString, QString> &attributes )
{
const QMap<QString, QVariant::Type> &map = supportedNativeTypes();
QList< QgsField > list;

for ( QMap<QString, QString>::const_iterator it = attributes.constBegin(); it != attributes.constEnd(); it++ )
{
if ( !map.contains( it.value() ) )
return false;

list << QgsField( it.key(), map[ it.value()], it.value() );
}

return addAttributes( list );
}

bool QgsVectorDataProvider::deleteAttributes( const QgsAttributeIds& attributes )
{
return false;
Expand Down Expand Up @@ -267,6 +283,22 @@ const QList< QgsVectorDataProvider::NativeType > &QgsVectorDataProvider::nativeT
return mNativeTypes;
}

const QMap<QString, QVariant::Type> &QgsVectorDataProvider::supportedNativeTypes() const
{
if ( mOldTypeList.size() > 0 )
return mOldTypeList;

QgsVectorDataProvider *p = ( QgsVectorDataProvider * )this;

const QList< QgsVectorDataProvider::NativeType > &types = nativeTypes();

for ( QList< QgsVectorDataProvider::NativeType >::const_iterator it = types.constBegin(); it != types.constEnd(); it++ )
{
p->mOldTypeList.insert( it->mTypeName, it->mType );
}

return p->mOldTypeList;
}

bool QgsVectorDataProvider::supportedType( const QgsField &field ) const
{
Expand Down
25 changes: 22 additions & 3 deletions src/core/qgsvectordataprovider.h
Expand Up @@ -209,12 +209,20 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider

/**
* Adds new attributes
* @param attributes map with attribute name as key and type as value
* @param attributes list of new attributes
* @return true in case of success and false in case of failure
* @note changed in 1.2
* @note added in 1.2
*/
virtual bool addAttributes( const QList<QgsField> &attributes );

/**
* Add new attributes
* @param attributes map of attributes name as key and type as value
* @return true in case of success and false in case of failure
* @note deprecated
*/
virtual bool addAttributes( const QMap<QString, QString> &attributes );

/**
* Deletes existing attributes
* @param attributes a set containing names of attributes
Expand Down Expand Up @@ -310,11 +318,18 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider


/**
* Returns the names of the numerical types
* Returns the names of the supported types
* @note added in 1.2
*/
const QList< NativeType > &nativeTypes() const;


/**
* Returns the names of the supported types
* @note deprecated
*/
const QMap<QString, QVariant::Type> &supportedNativeTypes() const;

protected:
QVariant convertValue( QVariant::Type type, QString value );

Expand All @@ -337,6 +352,10 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider

/**The names of the providers native types*/
QList< NativeType > mNativeTypes;

private:
/** old notation **/
QMap<QString, QVariant::Type> mOldTypeList;
};

#endif
20 changes: 13 additions & 7 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -752,23 +752,19 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
// TODO: create a mechanism to let layer know whether it's current layer or not [MD]
bool sel = mSelectedFeatureIds.contains( fet.id() );

mCurrentVertexMarkerType = QgsVectorLayer::NoMarker;

if ( mEditable )
{
// Cache this for the use of (e.g.) modifying the feature's uncommitted geometry.
mCachedGeometries[fet.id()] = *fet.geometry();

if ( mVertexMarkerOnlyForSelection && !sel )
{
mCurrentVertexMarkerType = QgsVectorLayer::NoMarker;
}
else
if ( !mVertexMarkerOnlyForSelection || sel )
{
mCurrentVertexMarkerType = vertexMarker;
}
}



//QgsDebugMsg(QString("markerScale before renderFeature(): %1").arg(markerScaleFactor));
// markerScalerFactore reflects the wanted scaling of the marker
mRenderer->renderFeature(
Expand Down Expand Up @@ -2673,6 +2669,16 @@ bool QgsVectorLayer::addAttribute( const QgsField &field )
return true;
}

bool QgsVectorLayer::addAttribute( QString name, QString type )
{
const QMap<QString, QVariant::Type> &map = mDataProvider->supportedNativeTypes();

if ( !map.contains( type ) )
return false;

return addAttribute( QgsField( name, map[ type ], type ) );
}

bool QgsVectorLayer::deleteAttribute( int index )
{
if ( !isEditable() )
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -396,6 +396,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
@note added in version 1.2 */
bool addAttribute( const QgsField &field );

/** add an attribute field (but does not commit it)
returns true if the field was added
@note deprecated */
bool addAttribute( QString name, QString type );

/** delete an attribute field (but does not commit it) */
bool deleteAttribute( int attr );

Expand Down

0 comments on commit ccea976

Please sign in to comment.