From be1867b6c4743da937d269f04c2e108a18d3f400 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Tue, 24 Jan 2012 17:29:20 +0100 Subject: [PATCH] Add the QAbstractItemModel::canDropMimeData method. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Can be used by views to indicate whether a drop is allowed (eg adequete permissions in a filesystem model). Change-Id: Iefedb5399e44c8edc5f5df1403c8d5c0da618612 Reviewed-by: Peter Penz Reviewed-by: Thorbjørn Lund Martsum Reviewed-by: Stephen Kelly Reviewed-by: Olivier Goffart --- src/corelib/itemmodels/qabstractitemmodel.cpp | 19 ++++++++++++- src/corelib/itemmodels/qabstractitemmodel.h | 2 ++ .../qabstractitemmodel/tst_qabstractitemmodel.cpp | 32 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index d9fff33..cb5c62c 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -1753,6 +1753,23 @@ QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const } /*! + Returns whether a model can accept a drop of data. + + This can be used to indicate whether a drop of certain data is allowed, for example + by using a 'forbidden' emblem on a mouse cursor during a drag operation. + + This method returns true by default. + + \sa dropMimeData(), {Using drag and drop with item views} + */ +bool QAbstractItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, + const QModelIndex &parent) const +{ + return true; +} + +/*! Handles the \a data supplied by a drag and drop operation that ended with the given \a action. @@ -1773,7 +1790,7 @@ QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const greater than or equal zero, it means that the drop occurred just before the specified \a row and \a column in the specified \a parent. - \sa supportedDropActions(), {Using drag and drop with item views} + \sa supportedDropActions(), canDropMimeData(), {Using drag and drop with item views} */ bool QAbstractItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index 06b7e48..4ea912c 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -193,6 +193,8 @@ public: virtual QStringList mimeTypes() const; virtual QMimeData *mimeData(const QModelIndexList &indexes) const; + virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) const; virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); virtual Qt::DropActions supportedDropActions() const; diff --git a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp index 5d7902a..2ca5df4 100644 --- a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -72,6 +72,7 @@ private slots: void match(); void dropMimeData_data(); void dropMimeData(); + void canDropMimeData(); void changePersistentIndex(); void movePersistentIndex(); @@ -150,6 +151,9 @@ public: const QModelIndex &destinationParent, int destinationChild); void reset(); + bool canDropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) const; + int cCount, rCount; mutable bool wrongIndex; QVector > table; @@ -315,6 +319,25 @@ void QtTestModel::reset() QAbstractItemModel::reset(); } +bool QtTestModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) const +{ + Q_UNUSED(data); + Q_UNUSED(action); + + // For testing purposes, we impose some arbitrary rules on what may be dropped. + if (!parent.isValid() && row < 0 && column < 0) { + // a drop in emtpy space in the view is allowed. + // For example, in a filesystem view, a file may be dropped into empty space + // if it represents a writable directory. + return true; + } + + // We then arbitrarily decide to only allow drops on odd rows. + // A filesystem view/model might be able to drop onto (writable) directories. + return row % 2 == 0; +} + /** * The source Model *must* be initialized before the _data function, since the _data function uses QModelIndexes to reference the items in the tables. * Therefore, we must initialize it globally. @@ -755,6 +778,15 @@ void tst_QAbstractItemModel::dropMimeData() } } +void tst_QAbstractItemModel::canDropMimeData() +{ + QtTestModel model(3, 3); + + QVERIFY(model.canDropMimeData(0, Qt::CopyAction, -1, -1, QModelIndex())); + QVERIFY(model.canDropMimeData(0, Qt::CopyAction, 0, 0, QModelIndex())); + QVERIFY(!model.canDropMimeData(0, Qt::CopyAction, 1, 0, QModelIndex())); +} + void tst_QAbstractItemModel::changePersistentIndex() { QtTestModel model(3, 3); -- 2.7.4