summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@gmail.com>2020-01-22 21:15:46 +0300
committerAndreas Kling <kling@serenityos.org>2020-01-22 21:22:23 +0100
commitdec95cb8b39003d85a9aae46a69f0fbcf450830d (patch)
tree7cd88165160820239687dbcff0fca01686e8439f /Libraries
parentd3ce7ae0e305a4eb6056076eb34577a13e5f8ed5 (diff)
downloadserenity-dec95cb8b39003d85a9aae46a69f0fbcf450830d.zip
LibGUI: Let GModel specify the drag data type
GModel subclasses can now override drag_data_type() to specify which type GAbstractView should set for drag data. The default implementation returns a null string, which disables dragging from this widget.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibGUI/GAbstractView.cpp7
-rw-r--r--Libraries/LibGUI/GFileSystemModel.h1
-rw-r--r--Libraries/LibGUI/GModel.h2
-rw-r--r--Libraries/LibGUI/GSortingProxyModel.cpp5
-rw-r--r--Libraries/LibGUI/GSortingProxyModel.h1
5 files changed, 14 insertions, 2 deletions
diff --git a/Libraries/LibGUI/GAbstractView.cpp b/Libraries/LibGUI/GAbstractView.cpp
index b8dd15c0fd..b979d6d491 100644
--- a/Libraries/LibGUI/GAbstractView.cpp
+++ b/Libraries/LibGUI/GAbstractView.cpp
@@ -191,7 +191,7 @@ void GAbstractView::mousedown_event(GMouseEvent& event)
m_selection.clear();
} else if (event.modifiers() & Mod_Ctrl) {
m_selection.toggle(index);
- } else if (event.button() == GMouseButton::Left) {
+ } else if (event.button() == GMouseButton::Left && !m_model->drag_data_type().is_null()) {
// We might be starting a drag, so don't throw away other selected items yet.
m_might_drag = true;
m_selection.add(index);
@@ -219,6 +219,9 @@ void GAbstractView::mousemove_event(GMouseEvent& event)
if (distance_travelled_squared <= drag_distance_threshold)
return GScrollableWidget::mousemove_event(event);
+ auto data_type = m_model->drag_data_type();
+ ASSERT(!data_type.is_null());
+
dbg() << "Initiate drag!";
auto drag_operation = GDragOperation::construct();
@@ -248,7 +251,7 @@ void GAbstractView::mousemove_event(GMouseEvent& event)
drag_operation->set_text(text_builder.to_string());
drag_operation->set_bitmap(bitmap);
- drag_operation->set_data("url-list", data_builder.to_string());
+ drag_operation->set_data(data_type, data_builder.to_string());
auto outcome = drag_operation->exec();
diff --git a/Libraries/LibGUI/GFileSystemModel.h b/Libraries/LibGUI/GFileSystemModel.h
index ac746dc118..977b35e2bd 100644
--- a/Libraries/LibGUI/GFileSystemModel.h
+++ b/Libraries/LibGUI/GFileSystemModel.h
@@ -117,6 +117,7 @@ public:
virtual void update() override;
virtual GModelIndex parent_index(const GModelIndex&) const override;
virtual GModelIndex index(int row, int column = 0, const GModelIndex& parent = GModelIndex()) const override;
+ virtual StringView drag_data_type() const override { return "url-list"; }
static String timestamp_string(time_t timestamp)
{
diff --git a/Libraries/LibGUI/GModel.h b/Libraries/LibGUI/GModel.h
index bf3efa0ba4..12f16fdeb8 100644
--- a/Libraries/LibGUI/GModel.h
+++ b/Libraries/LibGUI/GModel.h
@@ -94,6 +94,8 @@ public:
virtual GSortOrder sort_order() const { return GSortOrder::None; }
virtual void set_key_column_and_sort_order(int, GSortOrder) {}
+ virtual StringView drag_data_type() const { return {}; }
+
void register_view(Badge<GAbstractView>, GAbstractView&);
void unregister_view(Badge<GAbstractView>, GAbstractView&);
diff --git a/Libraries/LibGUI/GSortingProxyModel.cpp b/Libraries/LibGUI/GSortingProxyModel.cpp
index 6ca7934927..eeb88ca571 100644
--- a/Libraries/LibGUI/GSortingProxyModel.cpp
+++ b/Libraries/LibGUI/GSortingProxyModel.cpp
@@ -87,6 +87,11 @@ void GSortingProxyModel::update()
target().update();
}
+StringView GSortingProxyModel::drag_data_type() const
+{
+ return target().drag_data_type();
+}
+
void GSortingProxyModel::set_key_column_and_sort_order(int column, GSortOrder sort_order)
{
if (column == m_key_column && sort_order == m_sort_order)
diff --git a/Libraries/LibGUI/GSortingProxyModel.h b/Libraries/LibGUI/GSortingProxyModel.h
index f5882419b1..c084205216 100644
--- a/Libraries/LibGUI/GSortingProxyModel.h
+++ b/Libraries/LibGUI/GSortingProxyModel.h
@@ -40,6 +40,7 @@ public:
virtual ColumnMetadata column_metadata(int) const override;
virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
virtual void update() override;
+ virtual StringView drag_data_type() const override;
virtual int key_column() const override { return m_key_column; }
virtual GSortOrder sort_order() const override { return m_sort_order; }