Skip to content

Commit

Permalink
add python binding to QgsVectorDataProvider::fieldNameMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Jan 16, 2012
1 parent 6f2ade7 commit d8bdcb2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
103 changes: 103 additions & 0 deletions python/core/conversions.sip
Expand Up @@ -820,6 +820,109 @@ template<TYPE>
%End
};

%MappedType QMap<QString, int>
{
%TypeHeaderCode
#include <QMap>
#if (SIP_VERSION >= 0x040900)
#define sipClass_QString ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QString))
#endif
%End

%ConvertFromTypeCode
// Create the dictionary.
PyObject *d = PyDict_New();

if (!d)
return NULL;

// Set the dictionary elements.
QMap<QString, int>::const_iterator i = sipCpp->constBegin();

while (i != sipCpp->constEnd())
{
QString *t1 = new QString(i.key());

PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_QString, sipTransferObj);
PyObject *t2obj = PyInt_FromLong( (long) i.value() );

if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
{
Py_DECREF(d);

if (t1obj) {
Py_DECREF(t1obj);
} else {
delete t1;
}

if (t2obj) {
Py_DECREF(t2obj);
}

return NULL;
}

Py_DECREF(t1obj);
Py_DECREF(t2obj);

++i;
}

return d;
%End

%ConvertToTypeCode
PyObject *t1obj, *t2obj;
#if PY_VERSION_HEX >= 0x02050000
Py_ssize_t i = 0;
#else
int i = 0;
#endif


// Check the type if that is all that is required.
if (sipIsErr == NULL)
{
if (!PyDict_Check(sipPy))
return 0;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
if (!sipCanConvertToInstance(t1obj, sipClass_QString, SIP_NOT_NONE))
return 0;
}

return 1;
}

QMap<QString, int> *qm = new QMap<QString, int>;

while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
{
int state;

QString *t1 = reinterpret_cast<QString *>(sipConvertToInstance(t1obj, sipClass_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
int t2 = PyInt_AsLong(t1obj);

if (*sipIsErr)
{
sipReleaseInstance(t1, sipClass_QString, state);
delete qm;
return 0;
}

qm->insert(*t1, t2);

sipReleaseInstance(t1, sipClass_QString, state);
}

*sipCppPtr = qm;

return sipGetState(sipTransferObj);
%End
};

template<TYPE1, TYPE2>
%MappedType QMap<TYPE1, TYPE2*>
{
Expand Down
3 changes: 3 additions & 0 deletions python/core/qgsvectordataprovider.sip
Expand Up @@ -261,6 +261,9 @@ class QgsVectorDataProvider : QgsDataProvider
* Returns the index of a field name or -1 if the field does not exist
*/
int fieldNameIndex(const QString& fieldName) const;

/**Return a map where the key is the name of the field and the value is its index*/
QMap<QString, int> fieldNameMap() const;

/**
* Return list of indexes to fetch all attributes in nextFeature()
Expand Down

0 comments on commit d8bdcb2

Please sign in to comment.