Python/RNA: added collection.move(from, to) for python defined

collection properties.
This commit is contained in:
Brecht Van Lommel 2010-03-10 20:54:14 +00:00
parent 0ef0caaedf
commit 969b4673c7
3 changed files with 51 additions and 0 deletions

@ -742,6 +742,7 @@ void RNA_property_pointer_remove(PointerRNA *ptr, PropertyRNA *prop);
void RNA_property_collection_add(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *r_ptr);
int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key);
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop);
int RNA_property_collection_move(PointerRNA *ptr, PropertyRNA *prop, int key, int pos);
/* copy/reset */
int RNA_property_copy(PointerRNA *ptr, PointerRNA *fromptr, PropertyRNA *prop, int index);

@ -2172,6 +2172,34 @@ int RNA_property_collection_remove(PointerRNA *ptr, PropertyRNA *prop, int key)
return 0;
}
int RNA_property_collection_move(PointerRNA *ptr, PropertyRNA *prop, int key, int pos)
{
IDProperty *idprop;
if((idprop=rna_idproperty_check(&prop, ptr))) {
IDProperty tmp, *array;
int len;
len= idprop->len;
array= IDP_IDPArray(idprop);
if(key >= 0 && key < len && pos >= 0 && pos < len && key != pos) {
memcpy(&tmp, &array[key], sizeof(IDProperty));
if(pos < key)
memmove(array+pos+1, array+pos, sizeof(IDProperty)*(key - pos));
else
memmove(array+key, array+key+1, sizeof(IDProperty)*(pos - key));
memcpy(&array[pos], &tmp, sizeof(IDProperty));
}
return 1;
}
else if(prop->flag & PROP_IDPROPERTY)
return 1;
return 0;
}
void RNA_property_collection_clear(PointerRNA *ptr, PropertyRNA *prop)
{
IDProperty *idprop;

@ -2220,6 +2220,27 @@ static PyObject *pyrna_prop_remove(BPy_PropertyRNA *self, PyObject *value)
return ret;
}
static PyObject *pyrna_prop_move(BPy_PropertyRNA *self, PyObject *args)
{
PyObject *ret;
int key=0, pos=0;
if (!PyArg_ParseTuple(args, "ii", &key, &pos)) {
PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move(): expected two ints as arguments");
return NULL;
}
if(!RNA_property_collection_move(&self->ptr, self->prop, key, pos)) {
PyErr_SetString( PyExc_TypeError, "bpy_prop_collection.move() not supported for this collection");
return NULL;
}
ret = Py_None;
Py_INCREF(ret);
return ret;
}
static PyObject *pyrna_struct_get_id_data(BPy_StructRNA *self)
{
if(self->ptr.id.data) {
@ -2714,6 +2735,7 @@ static struct PyMethodDef pyrna_prop_collection_methods[] = {
/* moved into a getset */
{"add", (PyCFunction)pyrna_prop_add, METH_NOARGS, NULL},
{"remove", (PyCFunction)pyrna_prop_remove, METH_O, NULL},
{"move", (PyCFunction)pyrna_prop_move, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};