summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2014-11-21 10:46:22 +0100
committerDavid Faure <david.faure@kdab.com>2014-11-24 08:05:51 +0100
commitb13aa15e1007a1b5ed61049bbd9ef8f95b6d12a5 (patch)
treee202817bf80517ce9efd178d6642acf12cac05ee /src
parent736ac191565196514e14f875c774771026f95d7e (diff)
QAbstractItemView: call canDropMimeData, as one would expect.
The virtual method was added for 5.0 but never called. The old code (only checking mimetypes) is now the default implementation for canDropMimeData. Model subclasses can now refine this by having index-specific logic instead, or in order to inspect the dropped data (e.g. to accept files and refuse directories, which are all text/uri-list). [ChangeLog][QtWidgets][QAbstractItemView] now calls canDropMimeData in order to decide whether or not to accept the drop. Task-number: QTBUG-30534 Change-Id: Ied3aa964b4025bae6a1a26df89a681bfe61c3faa Reviewed-by: Stephen Kelly <steveire@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.cpp17
-rw-r--r--src/widgets/doc/snippets/qlistview-dnd/main.cpp2
-rw-r--r--src/widgets/doc/snippets/qlistview-dnd/mainwindow.cpp2
-rw-r--r--src/widgets/doc/snippets/qlistview-dnd/model.cpp21
-rw-r--r--src/widgets/doc/snippets/qlistview-dnd/model.h2
-rw-r--r--src/widgets/doc/snippets/qlistview-dnd/qlistview-dnd.pro1
-rw-r--r--src/widgets/doc/src/model-view-programming.qdoc3
-rw-r--r--src/widgets/itemviews/qabstractitemview_p.h16
8 files changed, 47 insertions, 17 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp
index b15d255e66..a9cfa3e7ca 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.cpp
+++ b/src/corelib/itemmodels/qabstractitemmodel.cpp
@@ -1842,7 +1842,9 @@ QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
/*!
Returns \c{true} if a model can accept a drop of the \a data. This
- default implementation always returns \c{true}.
+ default implementation only checks if \a data has at least one format
+ in the list of mimeTypes() and if \a action is among the
+ model's supportedDropActions().
Reimplement this function in your custom model, if you want to
test whether the \a data can be dropped at \a row, \a column,
@@ -1855,12 +1857,19 @@ bool QAbstractItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction a
int row, int column,
const QModelIndex &parent) const
{
- Q_UNUSED(data)
- Q_UNUSED(action)
Q_UNUSED(row)
Q_UNUSED(column)
Q_UNUSED(parent)
- return true;
+
+ if (!(action & supportedDropActions()))
+ return false;
+
+ const QStringList modelTypes = mimeTypes();
+ for (int i = 0; i < modelTypes.count(); ++i) {
+ if (data->hasFormat(modelTypes.at(i)))
+ return true;
+ }
+ return false;
}
/*!
diff --git a/src/widgets/doc/snippets/qlistview-dnd/main.cpp b/src/widgets/doc/snippets/qlistview-dnd/main.cpp
index c4a8aebb01..422ce35c6b 100644
--- a/src/widgets/doc/snippets/qlistview-dnd/main.cpp
+++ b/src/widgets/doc/snippets/qlistview-dnd/main.cpp
@@ -38,7 +38,7 @@
**
****************************************************************************/
-#include <QtGui>
+#include <QtWidgets>
#include "mainwindow.h"
diff --git a/src/widgets/doc/snippets/qlistview-dnd/mainwindow.cpp b/src/widgets/doc/snippets/qlistview-dnd/mainwindow.cpp
index ed06819dd9..3b19295676 100644
--- a/src/widgets/doc/snippets/qlistview-dnd/mainwindow.cpp
+++ b/src/widgets/doc/snippets/qlistview-dnd/mainwindow.cpp
@@ -38,7 +38,7 @@
**
****************************************************************************/
-#include <QtGui>
+#include <QtWidgets>
#include "mainwindow.h"
#include "model.h"
diff --git a/src/widgets/doc/snippets/qlistview-dnd/model.cpp b/src/widgets/doc/snippets/qlistview-dnd/model.cpp
index 6ebeb2eb56..aa12cef3a2 100644
--- a/src/widgets/doc/snippets/qlistview-dnd/model.cpp
+++ b/src/widgets/doc/snippets/qlistview-dnd/model.cpp
@@ -65,18 +65,31 @@ DragDropListModel::DragDropListModel(const QStringList &strings,
}
//! [0]
-bool DragDropListModel::dropMimeData(const QMimeData *data,
+bool DragDropListModel::canDropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
- if (action == Qt::IgnoreAction)
- return true;
+ Q_UNUSED(action);
+ Q_UNUSED(row);
+ Q_UNUSED(parent);
if (!data->hasFormat("application/vnd.text.list"))
return false;
if (column > 0)
-//! [0] //! [1]
return false;
+
+ return true;
+}
+//! [0]
+//! [1]
+bool DragDropListModel::dropMimeData(const QMimeData *data,
+ Qt::DropAction action, int row, int column, const QModelIndex &parent)
+{
+ if (!canDropMimeData(data, action, row, column, parent))
+ return false;
+
+ if (action == Qt::IgnoreAction)
+ return true;
//! [1]
//! [2]
diff --git a/src/widgets/doc/snippets/qlistview-dnd/model.h b/src/widgets/doc/snippets/qlistview-dnd/model.h
index 9b667ad4a3..9217052971 100644
--- a/src/widgets/doc/snippets/qlistview-dnd/model.h
+++ b/src/widgets/doc/snippets/qlistview-dnd/model.h
@@ -63,6 +63,8 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const;
+ bool canDropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int column, const QModelIndex &parent);
bool dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent);
QMimeData *mimeData(const QModelIndexList &indexes) const;
diff --git a/src/widgets/doc/snippets/qlistview-dnd/qlistview-dnd.pro b/src/widgets/doc/snippets/qlistview-dnd/qlistview-dnd.pro
index 71fa273a69..bc2a1f0dce 100644
--- a/src/widgets/doc/snippets/qlistview-dnd/qlistview-dnd.pro
+++ b/src/widgets/doc/snippets/qlistview-dnd/qlistview-dnd.pro
@@ -3,3 +3,4 @@ SOURCES = main.cpp \
model.cpp
HEADERS = mainwindow.h \
model.h
+QT += widgets
diff --git a/src/widgets/doc/src/model-view-programming.qdoc b/src/widgets/doc/src/model-view-programming.qdoc
index 8978efa1e3..ada0460689 100644
--- a/src/widgets/doc/src/model-view-programming.qdoc
+++ b/src/widgets/doc/src/model-view-programming.qdoc
@@ -1769,6 +1769,9 @@
dropped onto existing items separately to data dropped into the top level
of the model (i.e., onto an invalid item).
+ Models can forbid dropping on certain items, or depending on the dropped data,
+ by reimplementing QAbstractItemModel::canDropMimeData().
+
The model first has to make sure that the operation should be acted on,
the data supplied is in a format that can be used, and that its destination
within the model is valid:
diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h
index 047533b22c..e04f08da36 100644
--- a/src/widgets/itemviews/qabstractitemview_p.h
+++ b/src/widgets/itemviews/qabstractitemview_p.h
@@ -164,13 +164,15 @@ public:
#ifndef QT_NO_DRAGANDDROP
virtual QAbstractItemView::DropIndicatorPosition position(const QPoint &pos, const QRect &rect, const QModelIndex &idx) const;
- inline bool canDecode(QDropEvent *e) const {
- QStringList modelTypes = model->mimeTypes();
- const QMimeData *mime = e->mimeData();
- for (int i = 0; i < modelTypes.count(); ++i)
- if (mime->hasFormat(modelTypes.at(i))
- && (e->dropAction() & model->supportedDropActions()))
- return true;
+ inline bool canDecode(QDropEvent *event) {
+ QModelIndex index;
+ int col = -1;
+ int row = -1;
+ if (dropOn(event, &row, &col, &index)) {
+ return model->canDropMimeData(event->mimeData(),
+ dragDropMode == QAbstractItemView::InternalMove ? Qt::MoveAction : event->dropAction(),
+ row, col, index);
+ }
return false;
}