diff options
author | Julian Eisel <julian@blender.org> | 2021-10-24 19:51:12 +0300 |
---|---|---|
committer | Julian Eisel <julian@blender.org> | 2021-10-25 11:46:52 +0300 |
commit | d16e7326386d055fc5cdfa9f60bcd3d75bcbbed5 (patch) | |
tree | db71ab36e0c6c1772e925389ff33e413aec2591a /source/blender/editors/include | |
parent | c4fa17c67a7c28d34abe0db2da8783f4c5ab2a8f (diff) |
UI: Refactor dropping support for the tree-view API
Introduces a dropping-controller API for the tree-view items,
`AbstractTreeViewItemDropController`. This reduces responsibilities of the main
tree-view item classes, which are already getting quite big. As I expect even
more functionality to be needed for it (e.g. drag support), it's better to
start introducing such controller types already.
Diffstat (limited to 'source/blender/editors/include')
-rw-r--r-- | source/blender/editors/include/UI_interface.h | 5 | ||||
-rw-r--r-- | source/blender/editors/include/UI_tree_view.hh | 66 |
2 files changed, 56 insertions, 15 deletions
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h index 67d034f4ab6..de6b975a910 100644 --- a/source/blender/editors/include/UI_interface.h +++ b/source/blender/editors/include/UI_interface.h @@ -2781,11 +2781,8 @@ void UI_interface_tag_script_reload(void); bool UI_tree_view_item_is_active(const uiTreeViewItemHandle *item); bool UI_tree_view_item_matches(const uiTreeViewItemHandle *a, const uiTreeViewItemHandle *b); bool UI_tree_view_item_can_drop(const uiTreeViewItemHandle *item_, const struct wmDrag *drag); +char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item, const struct wmDrag *drag); bool UI_tree_view_item_drop_handle(uiTreeViewItemHandle *item_, const struct ListBase *drags); -char *UI_tree_view_item_drop_tooltip(const uiTreeViewItemHandle *item, - const struct bContext *C, - const struct wmDrag *drag, - const struct wmEvent *event); bool UI_tree_view_item_can_rename(const uiTreeViewItemHandle *item_handle); void UI_tree_view_item_begin_rename(uiTreeViewItemHandle *item_handle); diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh index b1ec22c57a6..f565c80193f 100644 --- a/source/blender/editors/include/UI_tree_view.hh +++ b/source/blender/editors/include/UI_tree_view.hh @@ -48,6 +48,7 @@ namespace blender::ui { class AbstractTreeView; class AbstractTreeViewItem; +class AbstractTreeViewItemDropController; /* ---------------------------------------------------------------------- */ /** \name Tree-View Item Container @@ -242,17 +243,7 @@ class AbstractTreeViewItem : public TreeViewItemContainer { * arguments for checking if the item is currently in an active state. */ virtual void is_active(IsActiveFn is_active_fn); - virtual bool on_drop(const wmDrag &drag); - virtual bool can_drop(const wmDrag &drag) const; - /** - * Custom text to display when dragging over a tree item. Should explain what happens when - * dropping the data onto this item. Will only be used if #AbstractTreeViewItem::can_drop() - * returns true, so the implementing override doesn't have to check that again. - * The returned value must be a translated string. - */ - virtual std::string drop_tooltip(const bContext &C, - const wmDrag &drag, - const wmEvent &event) const; + /** * Queries if the tree-view item supports renaming in principle. Renaming may still fail, e.g. if * another item is already being renamed. @@ -282,6 +273,15 @@ class AbstractTreeViewItem : public TreeViewItemContainer { */ virtual bool matches(const AbstractTreeViewItem &other) const; + /** + * If an item wants to support dropping data into it, it has to return a drop controller here. + * That is an object implementing #AbstractTreeViewItemDropController. + * + * \note This drop controller may be requested for each event. The tree-view doesn't keep a drop + * controller around currently. So it can not contain persistent state. + */ + virtual std::unique_ptr<AbstractTreeViewItemDropController> create_drop_controller() const; + void begin_renaming(); void end_renaming(); @@ -344,6 +344,45 @@ class AbstractTreeViewItem : public TreeViewItemContainer { /** \} */ /* ---------------------------------------------------------------------- */ +/** \name Drag 'n Drop + * \{ */ + +/** + * Class to customize the drop behavior of a tree-item, plus the behavior when dragging over this + * item. An item can return a drop controller for itself via a custom implementation of + * #AbstractTreeViewItem::create_drop_controller(). + */ +class AbstractTreeViewItemDropController { + protected: + AbstractTreeView &tree_view_; + + public: + AbstractTreeViewItemDropController(AbstractTreeView &tree_view); + virtual ~AbstractTreeViewItemDropController() = default; + + /** + * Check if the data dragged with \a drag can be dropped on the item this controller is for. + */ + virtual bool can_drop(const wmDrag &drag) const = 0; + /** + * Custom text to display when dragging over a tree item. Should explain what happens when + * dropping the data onto this item. Will only be used if #AbstractTreeViewItem::can_drop() + * returns true, so the implementing override doesn't have to check that again. + * The returned value must be a translated string. + */ + virtual std::string drop_tooltip(const wmDrag &drag) const = 0; + /** + * Execute the logic to apply a drop of the data dragged with \a drag onto/into the item this + * controller is for. + */ + virtual bool on_drop(const wmDrag &drag) = 0; + + template<class TreeViewType> inline TreeViewType &tree_view() const; +}; + +/** \} */ + +/* ---------------------------------------------------------------------- */ /** \name Predefined Tree-View Item Types * * Common, Basic Tree-View Item Types. @@ -390,4 +429,9 @@ inline ItemT &TreeViewItemContainer::add_tree_item(Args &&...args) add_tree_item(std::make_unique<ItemT>(std::forward<Args>(args)...))); } +template<class TreeViewType> TreeViewType &AbstractTreeViewItemDropController::tree_view() const +{ + return static_cast<TreeViewType &>(tree_view_); +} + } // namespace blender::ui |