Skip to content

Commit

Permalink
PyQGIS: few additions for more pythonic api
Browse files Browse the repository at this point in the history
- QgsVectorLayer and QgsVectorDataProvider support iterating
- QgsFeature allows direct access to attributes (get/set/del)


git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12878 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Feb 4, 2010
1 parent 66f9788 commit e1838c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/core/qgsfeature.sip
Expand Up @@ -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 = "" );

Expand Down
17 changes: 17 additions & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -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,
Expand Down

0 comments on commit e1838c1

Please sign in to comment.