UI list template: committing patch [#26629].

This adds the ability (esp. for py scripts) to add some controls for each list element. See http://wiki.blender.org/index.php/User:Mont29/UI_Template_List_Enhancement for details.
This commit is contained in:
Bastien Montagne 2011-10-28 13:09:43 +00:00
parent 2ed7a66653
commit 7627a742ab
3 changed files with 53 additions and 4 deletions

@ -749,7 +749,7 @@ void uiTemplateTextureImage(uiLayout *layout, struct bContext *C, struct Tex *te
void uiTemplateReportsBanner(uiLayout *layout, struct bContext *C);
void uiTemplateKeymapItemProperties(uiLayout *layout, struct PointerRNA *ptr);
void uiTemplateList(uiLayout *layout, struct bContext *C, struct PointerRNA *ptr, const char *propname, struct PointerRNA *activeptr, const char *activeprop, int rows, int maxrows, int type);
void uiTemplateList(uiLayout *layout, struct bContext *C, struct PointerRNA *ptr, const char *propname, struct PointerRNA *activeptr, const char *activeprop, const char *prop_list, int rows, int maxrows, int type);
/* items */
void uiItemO(uiLayout *layout, const char *name, int icon, const char *opname);

@ -2059,7 +2059,7 @@ static int list_item_icon_get(bContext *C, PointerRNA *itemptr, int rnaicon, int
return rnaicon;
}
static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *itemptr, int i, int rnaicon, PointerRNA *activeptr, PropertyRNA *activeprop)
static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, PointerRNA *itemptr, int i, int rnaicon, PointerRNA *activeptr, PropertyRNA *activeprop, const char *prop_list)
{
uiBlock *block= uiLayoutGetBlock(layout);
uiBut *but;
@ -2164,6 +2164,52 @@ static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, Pointe
/* nothing else special to do... */
uiItemL(sub, name, icon); /* fails, backdrop LISTROW... */
}
/* There is a last chance to display custom controls (in addition to the name/label):
* If the given item property group features a string property named as prop_list,
* this tries to add controls for all properties of the item listed in that string property.
* (colon-separated names).
*
* This is especially useful for python. E.g., if you list a collection of this property
* group:
*
* class TestPropertyGroup(bpy.types.PropertyGroup):
* bool = BoolProperty(default=False)
* integer = IntProperty()
* string = StringProperty()
*
* # A string of all identifiers (colon-separated) which propertys controls should be
* # displayed in a template_list.
* template_list_controls = StringProperty(default="integer:bool:string", options={"HIDDEN"})
*
* youll get a numfield for the integer prop, a check box for the bool prop, and a textfield
* for the string prop, after the name of each item of the collection.
*/
else if (prop_list) {
PropertyRNA *prop_ctrls;
row = uiLayoutRow(sub, 1);
uiItemL(row, name, icon);
/* XXX: Check, as sometimes we get an itemptr looking like
* {id = {data = 0x0}, type = 0x0, data = 0x0}
* which would obviously produce a sigsev */
if (itemptr->type) {
/* If the special property is set for the item, and it is a collection… */
prop_ctrls = RNA_struct_find_property(itemptr, prop_list);
if(prop_ctrls) {
if(RNA_property_type(prop_ctrls) == PROP_STRING) {
char *prop_names = RNA_property_string_get_alloc(itemptr, prop_ctrls, NULL, 0, NULL);
char *id = NULL;
char *ctx = NULL;
for(id = BLI_strtok_r(prop_names, ":", &ctx); id; id = BLI_strtok_r(NULL, ":", &ctx)) {
uiItemR(row, itemptr, id, 0, NULL, 0);
MEM_freeN(id);
}
MEM_freeN(prop_names);
}
}
}
}
else
uiItemL(sub, name, icon); /* fails, backdrop LISTROW... */
@ -2172,7 +2218,7 @@ static void list_item_row(bContext *C, uiLayout *layout, PointerRNA *ptr, Pointe
MEM_freeN(namebuf);
}
void uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *activeptr, const char *activepropname, int rows, int maxrows, int listtype)
void uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *propname, PointerRNA *activeptr, const char *activepropname, const char *prop_list, int rows, int maxrows, int listtype)
{
//Scene *scene= CTX_data_scene(C);
PropertyRNA *prop= NULL, *activeprop;
@ -2326,7 +2372,7 @@ void uiTemplateList(uiLayout *layout, bContext *C, PointerRNA *ptr, const char *
/* create list items */
RNA_PROP_BEGIN(ptr, itemptr, prop) {
if(i >= pa->list_scroll && i<pa->list_scroll+items)
list_item_row(C, col, ptr, &itemptr, i, rnaicon, activeptr, activeprop);
list_item_row(C, col, ptr, &itemptr, i, rnaicon, activeptr, activeprop, prop_list);
i++;
}

@ -408,6 +408,9 @@ void RNA_api_ui_layout(StructRNA *srna)
RNA_def_property_flag(parm, PROP_REQUIRED|PROP_RNAPTR|PROP_NEVER_NULL);
parm= RNA_def_string(func, "active_property", "", 0, "", "Identifier of property in data, for the active element");
RNA_def_property_flag(parm, PROP_REQUIRED);
parm= RNA_def_string(func, "prop_list", "", 0, "",
"Identifier of a string property in each data member, specifying which "
"of its properties should have a widget displayed in its row.");
RNA_def_int(func, "rows", 5, 0, INT_MAX, "", "Number of rows to display", 0, INT_MAX);
RNA_def_int(func, "maxrows", 5, 0, INT_MAX, "", "Maximum number of rows to display", 0, INT_MAX);
RNA_def_enum(func, "type", list_type_items, 0, "Type", "Type of list to use");