Skip to content
Permalink
Browse files

PyQGIS: few additions for more pythonic api

- QgsVectorLayer and QgsVectorDataProvider support iterating
- QgsFeature allows direct access to attributes (get/set/del)


git-svn-id: http://svn.osgeo.org/qgis/trunk@12878 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Feb 4, 2010
1 parent 54b079e commit 2034de2bc9e2c2c7a4114ef52680a2963adc7e2f
Showing with 62 additions and 0 deletions.
  1. +26 −0 python/core/qgsfeature.sip
  2. +17 −0 python/core/qgsvectordataprovider.sip
  3. +19 −0 python/core/qgsvectorlayer.sip
@@ -8,6 +8,32 @@ class QgsFeature

public:

SIP_PYOBJECT __getitem__(int key);
%MethodCode
const QgsAttributeMap& attrMap = sipCpp->attributeMap();
QgsAttributeMap::const_iterator it = attrMap.find(a0);
if (it == attrMap.end())
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
else
{
QVariant* v = new QVariant(it.value());
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
}
%End

void __setitem__(int key, QVariant value);
%MethodCode
sipCpp->addAttribute(a0, *a1);
%End

void __delitem__(int key);
%MethodCode
if (sipCpp->attributeMap().contains(a0))
sipCpp->deleteAttribute(a0);
else
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
%End

//! Constructor
QgsFeature(int id = 0, QString typeName = "" );

@@ -7,6 +7,23 @@ class QgsVectorDataProvider : QgsDataProvider

public:

QgsVectorDataProvider* __iter__();
%MethodCode
sipRes = sipCpp;
%End

SIP_PYOBJECT __next__();
%MethodCode
QgsFeature* f = new QgsFeature;
if (sipCpp->nextFeature(*f))
sipRes = sipConvertFromInstance(f, sipClass_QgsFeature, Py_None);
else
{
delete f;
PyErr_SetString(PyExc_StopIteration,"");
}
%End

// If you add to this, please also add to capabilitiesString()
/**
* enumeration with capabilities that providers might implement
@@ -5,6 +5,25 @@ class QgsVectorLayer : QgsMapLayer
%End

public:

QgsVectorLayer* __iter__();
%MethodCode
sipRes = sipCpp;
%End

SIP_PYOBJECT __next__();
%MethodCode
QgsFeature* f = new QgsFeature;
if (sipCpp->nextFeature(*f))
sipRes = sipConvertFromInstance(f, sipClass_QgsFeature, Py_None);
else
{
delete f;
PyErr_SetString(PyExc_StopIteration,"");
}
%End


enum EditType {
LineEdit,
UniqueValues,

0 comments on commit 2034de2

Please sign in to comment.
You can’t perform that action at this time.