File indexing completed on 2025-01-19 03:57:44

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-03-06
0007  * Description : sub class of QTreeWidget for drag-and-drop support
0008  *
0009  * SPDX-FileCopyrightText: 2010 by Michael G. Hansen <mike at mghansen dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "mytreewidget.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QMouseEvent>
0021 #include <QDrag>
0022 
0023 // geoiface includes
0024 
0025 #include "geoifacetypes.h"
0026 
0027 // local includes
0028 
0029 #include "mydragdrophandler.h"
0030 
0031 class Q_DECL_HIDDEN MyTreeWidget::Private
0032 {
0033 public:
0034 
0035     explicit Private()
0036         : dragStartPos()
0037     {
0038     }
0039 
0040     QPoint dragStartPos;
0041 };
0042 
0043 MyTreeWidget::MyTreeWidget(QWidget* const parent)
0044     : QTreeWidget(parent),
0045       d          (new Private())
0046 {
0047     setDragEnabled(true);
0048     setUniformRowHeights(true);
0049     setDragDropMode(QAbstractItemView::DragOnly);
0050 }
0051 
0052 MyTreeWidget::~MyTreeWidget()
0053 {
0054     delete d;
0055 }
0056 
0057 void MyTreeWidget::startDrag(Qt::DropActions /*supportedActions*/)
0058 {
0059     QMimeData* const dragMimeData = mimeData(selectionModel()->selectedIndexes());
0060     QDrag* const drag             = new QDrag(this);
0061     drag->setMimeData(dragMimeData);
0062     drag->exec(Qt::CopyAction);
0063 }
0064 
0065 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0066 
0067 QMimeData* MyTreeWidget::mimeData(const QList<QTreeWidgetItem*>& items) const
0068 
0069 #else
0070 
0071 // cppcheck-suppress passedByValue
0072 QMimeData* MyTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const    // clazy:exclude=function-args-by-ref
0073 
0074 #endif
0075 
0076 {
0077     return QTreeWidget::mimeData(items);
0078 }
0079 
0080 // cppcheck-suppress passedByValue
0081 QMimeData* MyTreeWidget::mimeData(const QModelIndexList itemsToDrag) const          // clazy:exclude=function-args-by-ref
0082 {
0083     MyDragData* const mimeData = new MyDragData;
0084 
0085     // TODO: determine the indices of the items to drag!
0086 
0087     for (int i = 0 ; i < itemsToDrag.count() ; ++i)
0088     {
0089         mimeData->draggedIndices << QPersistentModelIndex(itemsToDrag.at(i));
0090     }
0091 
0092     return mimeData;
0093 }
0094 
0095 #include "moc_mytreewidget.cpp"