| 1 | /* ============================================================ |
| 2 | * |
| 3 | * This file is a part of digiKam project |
| 4 | * https://www.digikam.org |
| 5 | * |
| 6 | * Date : 2010-03-06 |
| 7 | * Description : sub class of QTreeWidget for drag-and-drop support |
| 8 | * |
| 9 | * SPDX-FileCopyrightText: 2010 by Michael G. Hansen <mike at mghansen dot de> |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 12 | * |
| 13 | * ============================================================ */ |
| 14 | |
| 15 | #include "mytreewidget.h" |
| 16 | |
| 17 | // Qt includes |
| 18 | |
| 19 | #include <QApplication> |
| 20 | #include <QMouseEvent> |
| 21 | #include <QDrag> |
| 22 | |
| 23 | // geoiface includes |
| 24 | |
| 25 | #include "geoifacetypes.h" |
| 26 | |
| 27 | // Local includes |
| 28 | |
| 29 | #include "mydragdrophandler.h" |
| 30 | |
| 31 | class Q_DECL_HIDDEN MyTreeWidget::Private |
| 32 | { |
| 33 | public: |
| 34 | |
| 35 | explicit Private() |
| 36 | : dragStartPos() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | QPoint dragStartPos; |
| 41 | }; |
| 42 | |
| 43 | MyTreeWidget::MyTreeWidget(QWidget* const parent) |
| 44 | : QTreeWidget(parent), |
| 45 | d (new Private) |
| 46 | { |
| 47 | setDragEnabled(true); |
| 48 | setUniformRowHeights(true); |
| 49 | setDragDropMode(QAbstractItemView::DragOnly); |
| 50 | } |
| 51 | |
| 52 | MyTreeWidget::~MyTreeWidget() |
| 53 | { |
| 54 | delete d; |
| 55 | } |
| 56 | |
| 57 | void MyTreeWidget::startDrag(Qt::DropActions /*supportedActions*/) |
| 58 | { |
| 59 | QMimeData* const dragMimeData = mimeData(selectionModel()->selectedIndexes()); |
| 60 | QDrag* const drag = new QDrag(this); |
| 61 | drag->setMimeData(dragMimeData); |
| 62 | drag->exec(Qt::CopyAction); |
| 63 | } |
| 64 | |
| 65 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 66 | |
| 67 | QMimeData* MyTreeWidget::mimeData(const QList<QTreeWidgetItem*>& items) const |
| 68 | |
| 69 | #else |
| 70 | |
| 71 | // cppcheck-suppress passedByValue |
| 72 | QMimeData* MyTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const // clazy:exclude=function-args-by-ref |
| 73 | |
| 74 | #endif |
| 75 | |
| 76 | { |
| 77 | return QTreeWidget::mimeData(items); |
| 78 | } |
| 79 | |
| 80 | // cppcheck-suppress passedByValue |
| 81 | QMimeData* MyTreeWidget::mimeData(const QModelIndexList itemsToDrag) const // clazy:exclude=function-args-by-ref |
| 82 | { |
| 83 | MyDragData* const mimeData = new MyDragData; |
| 84 | |
| 85 | // TODO: determine the indices of the items to drag! |
| 86 | |
| 87 | for (int i = 0 ; i < itemsToDrag.count() ; ++i) |
| 88 | { |
| 89 | mimeData->draggedIndices << QPersistentModelIndex(itemsToDrag.at(i)); |
| 90 | } |
| 91 | |
| 92 | return mimeData; |
| 93 | } |
| 94 | |
| 95 | #include "moc_mytreewidget.cpp" |