Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Montagne <montagne29@wanadoo.fr>2013-08-29 16:55:31 +0400
committerBastien Montagne <montagne29@wanadoo.fr>2013-08-29 16:55:31 +0400
commit113997a03c9b1ecc7ec6460f24781d54fd9efb57 (patch)
tree27febdc8ef32e37b0519d4a9bd8426c1ef73257d /release/scripts/startup/bl_ui/__init__.py
parent034d5a9578714db01cd05629804d1dbef48c1734 (diff)
Last uiList patch (for now!): filtering and reordering of shown elements.
Thanks to Brecht for the reviews. :) This commit adds a show/hide extension below each uiList, containing by default an option to filter and/or reorder items by name (and to reverse those filtering and reordering). Each derived uiList class in Python can define more specific filtering by implementing callbacks: the draw_filter() function to draw options in UI, and the filter_items() function to effectively filter/reorder items. Note: the advanced options for vgroups shown as "proof od concept" in patches do not go in trunk for now, we have to find a better way to get those vgroups info for UI code, we can't afford to loop over each vertex here! And doc (release notes and uiList example) is still to be updated, will do this in next days.
Diffstat (limited to 'release/scripts/startup/bl_ui/__init__.py')
-rw-r--r--release/scripts/startup/bl_ui/__init__.py57
1 files changed, 56 insertions, 1 deletions
diff --git a/release/scripts/startup/bl_ui/__init__.py b/release/scripts/startup/bl_ui/__init__.py
index df6247f65f5..ba256bdb6c8 100644
--- a/release/scripts/startup/bl_ui/__init__.py
+++ b/release/scripts/startup/bl_ui/__init__.py
@@ -142,6 +142,61 @@ def unregister():
# Define a default UIList, when a list does not need any custom drawing...
# Keep in sync with its #defined name in UI_interface.h
class UI_UL_list(bpy.types.UIList):
- pass
+ # These are common filtering or ordering operations (same as the default C ones!).
+ @staticmethod
+ def filter_items_by_name(pattern, bitflag, items, propname="name", flags=None, reverse=False):
+ """
+ Set FILTER_ITEM for items which name matches filter_name one (case-insensitive).
+ pattern is the filtering pattern.
+ propname is the name of the string property to use for filtering.
+ flags must be a list of integers the same length as items, or None!
+ return a list of flags (based on given flags if not None),
+ or an empty list if no flags were given and no filtering has been done.
+ """
+ import fnmatch
+
+ if not pattern: # Empty pattern = no filtering!
+ return flags or []
+
+ if flags is None:
+ flags = [0] * len(items)
+ for idx, it in enumerate(items):
+ name = getattr(it, propname, None)
+ # Implicitly add heading/trailing wildcards if needed.
+ if pattern[0] != "*":
+ pattern = "*" + pattern
+ if pattern[-1] != "*":
+ pattern = pattern + "*"
+ # This is similar to a logical xor
+ if bool(name and fnmatch.fnmatch(name.lower(), pattern.lower())) is not bool(reverse):
+ flags[idx] |= bitflag
+ return flags
+
+ @staticmethod
+ def sort_items_helper(sort_data, key, reverse=False):
+ """
+ Common sorting utility. Returns a neworder list mapping org_idx -> new_idx.
+ sort_data must be an (unordered) list of tuples [(org_idx, ...), (org_idx, ...), ...].
+ key must be the same kind of callable you would use for sorted() builtin function.
+ reverse will reverse the sorting!
+ """
+ sort_data.sort(key=key, reverse=reverse)
+ neworder = [None] * len(sort_data)
+ for newidx, (orgidx, *_) in enumerate(sort_data):
+ neworder[orgidx] = newidx
+ return neworder
+
+ @classmethod
+ def sort_items_by_name(cls, items, propname="name"):
+ """
+ Re-order items using their names (case-insensitive).
+ propname is the name of the string property to use for sorting.
+ return a list mapping org_idx -> new_idx,
+ or an empty list if no sorting has been done.
+ """
+ neworder = [None] * len(items)
+ _sort = [(idx, getattr(it, propname, "")) for idx, it in enumerate(items)]
+ return cls.sort_items_helper(_sort, lambda e: e[1].lower())
+
bpy.utils.register_class(UI_UL_list)