Skip to content
Permalink
Browse files

Add __getattr__ and __setattr__ to QgsFeature

  • Loading branch information
NathanW2 committed Jun 9, 2013
1 parent d004791 commit d539739089cce84c2f1d6b94c0c435d3f8cf7b35
Showing with 54 additions and 0 deletions.
  1. +54 −0 python/core/qgsfeature.sip
@@ -30,6 +30,60 @@ class QgsFeature

public:

SIP_PYOBJECT __getattr__(const QString& name);
%MethodCode
int fieldIdx = sipCpp->fieldNameIndex(*a0);
QString altname = QString(*a0).replace("_"," ");
int altfieldIdx = sipCpp->fieldNameIndex(altname);

if (fieldIdx >= 0)
{
QVariant* v = new QVariant( sipCpp->attribute(fieldIdx) );
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
}
else if( altfieldIdx >= 0 )
{
QVariant* v = new QVariant( sipCpp->attribute(altfieldIdx) );
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
}
else
{
PyObject* key = PyString_FromString(a0->toStdString().c_str());
sipRes = PyObject_GenericGetAttr(sipSelf, key );
}
%End

void __setattr__(const QString& key, QVariant value);
%MethodCode
int fieldIdx = sipCpp->fieldNameIndex(*a0);
QString altname = QString(*a0).replace("_"," ");
int altfieldIdx = sipCpp->fieldNameIndex(altname);

if (fieldIdx >= 0)
{
sipCpp->setAttribute(fieldIdx, *a1);
}
else if( altfieldIdx >= 0 )
{
sipCpp->setAttribute(altfieldIdx, *a1);
}
else
{
PyObject* key = PyString_FromString(a0->toStdString().c_str());
PyObject* value = sipConvertFromType( a1, sipType_QVariant, sipSelf);
PyObject_GenericSetAttr(sipSelf, key, value);
}
%End

void __delattr__(const QString& key);
%MethodCode
int fieldIdx = sipCpp->fieldNameIndex(*a0);
if (fieldIdx == -1)
PyErr_SetString(PyExc_KeyError, a0->toAscii());
else
sipCpp->deleteAttribute(fieldIdx);
%End

SIP_PYOBJECT __getitem__(int key);
%MethodCode
const QgsAttributes& attrs = sipCpp->attributes();

0 comments on commit d539739

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