From f0ae353c9ef526e91faeed9376528bd6250066a0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 13 Feb 2020 21:49:14 +0100 Subject: [PATCH] LibGUI: Allow dropping drags on AbstractView You can now drop things on an AbstractView, which will ask its model if the drag is acceptable to drop at the index where it's dropped. If it's accepted by the model, the view will fire the on_drop hook. --- Libraries/LibGUI/AbstractView.cpp | 15 +++++++++++++++ Libraries/LibGUI/AbstractView.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/Libraries/LibGUI/AbstractView.cpp b/Libraries/LibGUI/AbstractView.cpp index 5929f16762..ba770efaa3 100644 --- a/Libraries/LibGUI/AbstractView.cpp +++ b/Libraries/LibGUI/AbstractView.cpp @@ -326,4 +326,19 @@ void AbstractView::context_menu_event(ContextMenuEvent& event) on_context_menu_request(index, event); } +void AbstractView::drop_event(DropEvent& event) +{ + event.accept(); + + if (!model()) + return; + + auto index = index_at_event_position(event.position()); + if (!index.is_valid()) + return; + + if (on_drop) + on_drop(index, event); +} + } diff --git a/Libraries/LibGUI/AbstractView.h b/Libraries/LibGUI/AbstractView.h index c098e661f6..8e9b52d71e 100644 --- a/Libraries/LibGUI/AbstractView.h +++ b/Libraries/LibGUI/AbstractView.h @@ -67,6 +67,7 @@ public: Function on_activation; Function on_selection; Function on_context_menu_request; + Function on_drop; Function(const ModelIndex&)> aid_create_editing_delegate; @@ -83,6 +84,7 @@ protected: virtual void mouseup_event(MouseEvent&) override; virtual void doubleclick_event(MouseEvent&) override; virtual void context_menu_event(ContextMenuEvent&) override; + virtual void drop_event(DropEvent&) override; virtual void did_scroll() override; void activate(const ModelIndex&);