Prechádzať zdrojové kódy

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.
Andreas Kling 5 rokov pred
rodič
commit
f0ae353c9e

+ 15 - 0
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);
+}
+
 }

+ 2 - 0
Libraries/LibGUI/AbstractView.h

@@ -67,6 +67,7 @@ public:
     Function<void(const ModelIndex&)> on_activation;
     Function<void(const ModelIndex&)> on_selection;
     Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
+    Function<void(const ModelIndex&, const DropEvent&)> on_drop;
 
     Function<OwnPtr<ModelEditingDelegate>(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&);