diff --git a/sources/ElementsCollection/elementscollectionwidget.cpp b/sources/ElementsCollection/elementscollectionwidget.cpp index f8d073b77..198e6ddb1 100644 --- a/sources/ElementsCollection/elementscollectionwidget.cpp +++ b/sources/ElementsCollection/elementscollectionwidget.cpp @@ -55,6 +55,32 @@ #include #include +namespace { +/** + @brief The SearchResultsView class + The flat list of search results. Rows are not backed by the collection + model, so the drag is built here from the path each row carries, with + the same content and pixmap as a drag from the tree. +*/ +class SearchResultsView : public QListView +{ + public: + using QListView::QListView; + + protected: + void startDrag(Qt::DropActions supportedActions) override + { + const QString path = + currentIndex().data(Qt::UserRole + 2).toString(); + if (path.isEmpty()) { + QListView::startDrag(supportedActions); + return; + } + ElementsTreeView::execElementDrag(this, ElementsLocation(path)); + } +}; +} + /** @brief ElementsCollectionWidget::ElementsCollectionWidget Default constructor. @@ -230,8 +256,9 @@ void ElementsCollectionWidget::setUpWidget() //ranked list instead, and takes the tab widget's place while a search //is active. m_search_model = new QStandardItemModel(this); - m_search_results = new QListView(this); + m_search_results = new SearchResultsView(this); m_search_results->setModel(m_search_model); + m_search_results->setDragDropMode(QAbstractItemView::DragOnly); m_search_results->setIconSize(QSize(50, 50)); m_search_results->setUniformItemSizes(false); m_search_results->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); diff --git a/sources/ElementsCollection/elementstreeview.cpp b/sources/ElementsCollection/elementstreeview.cpp index 581bc1b64..b04c2726f 100644 --- a/sources/ElementsCollection/elementstreeview.cpp +++ b/sources/ElementsCollection/elementstreeview.cpp @@ -82,13 +82,27 @@ void ElementsTreeView::startDrag(Qt::DropActions supportedActions) @param location : location to use for create the content of the QDrag */ void ElementsTreeView::startElementDrag(const ElementsLocation &location) +{ + execElementDrag(this, location); +} + +/** + @brief ElementsTreeView::execElementDrag + Build and run the QDrag for @a location, from @a source. + Static so that a view which is not an ElementsTreeView -- the flat list + of search results -- starts exactly the same drag as the tree. + @param source : the widget the drag starts from + @param location : location to use for create the content of the QDrag +*/ +void ElementsTreeView::execElementDrag(QWidget *source, + const ElementsLocation &location) { if (! location.exist()) return; #if QT_VERSION < QT_VERSION_CHECK(6, 2, 0) - QDrag* drag = new QDrag(this); + QDrag* drag = new QDrag(source); #else - QScopedPointer drag(new QDrag(this)); + QScopedPointer drag(new QDrag(source)); #endif QString location_str = location.toString(); @@ -209,7 +223,7 @@ void ElementsTreeView::startElementDrag(const ElementsLocation &location) &elmt_creation_state)); if (elmt_creation_state) { return; } - QPixmap elmt_pixmap(QET::Palette::forPalette(temp_elmt->pixmap(), palette())); + QPixmap elmt_pixmap(QET::Palette::forPalette(temp_elmt->pixmap(), source->palette())); QPoint elmt_hotspot(temp_elmt->hotspot()); //Adjust the size of the pixmap if he is too big diff --git a/sources/ElementsCollection/elementstreeview.h b/sources/ElementsCollection/elementstreeview.h index 58c6a3426..8ee65656b 100644 --- a/sources/ElementsCollection/elementstreeview.h +++ b/sources/ElementsCollection/elementstreeview.h @@ -32,6 +32,8 @@ class ElementsTreeView : public QTreeView { public: ElementsTreeView(QWidget *parent = nullptr); + static void execElementDrag(QWidget *source, + const ElementsLocation &location); protected: void startDrag(Qt::DropActions supportedActions) override;