From abdebb189510cd9cf2b27ce5c0baa89a8690cb4c Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 10:42:01 +0000 Subject: [PATCH] Do not allow drag and drop process on targets that do not support the formats dragged. Privatize some Impl members. --- src/qt/dnd.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 17c81fdfad..58d20d53c5 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -173,13 +173,19 @@ public: void OnEnter(QEvent* event) { - event->accept(); - QDragEnterEvent *e = static_cast(event); - const QPoint where = e->pos(); PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + if ( !CanDropHere() ) + { + e->setDropAction(Qt::IgnoreAction); + return; + } + + event->accept(); + + const QPoint where = e->pos(); wxDragResult result = m_dropTarget->OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); e->setDropAction(DragResultToDropAction(result)); @@ -196,10 +202,10 @@ public: event->accept(); QDragMoveEvent *e = static_cast(event); - const QPoint where = e->pos(); PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + const QPoint where = e->pos(); wxDragResult result = m_dropTarget->OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); e->setDropAction(DragResultToDropAction(result)); @@ -210,15 +216,27 @@ public: event->accept(); const QDropEvent *e = static_cast(event); - const QPoint where = e->pos(); PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + const QPoint where = e->pos(); if ( m_dropTarget->OnDrop(where.x(), where.y()) ) { m_dropTarget->OnData(where.x(), where.y(), DropActionToDragResult(e->dropAction())); } } + + const QMimeData* GetMimeData() const + { + return m_pendingMimeData; + } + +private: + + bool CanDropHere() const + { + return !m_dropTarget->GetMatchingPair().GetMimeType().empty(); + } const QMimeData* m_pendingMimeData; wxDropTarget* m_dropTarget; @@ -238,7 +256,7 @@ wxDropTarget::~wxDropTarget() bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) { - return !GetMatchingPair().GetMimeType().empty(); + return true; } wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) @@ -255,17 +273,18 @@ bool wxDropTarget::GetData() if ( mimeType.empty() ) return false; - const QByteArray data = m_pImpl->m_pendingMimeData->data(wxQtConvertString(mimeType)); + const QByteArray data = m_pImpl->GetMimeData()->data(wxQtConvertString(mimeType)); return m_dataObject->SetData(droppedFormat, data.size(), data.data()); } wxDataFormat wxDropTarget::GetMatchingPair() { - if ( m_pImpl->m_pendingMimeData == NULL || m_dataObject == NULL ) + const QMimeData* mimeData = m_pImpl->GetMimeData(); + if ( mimeData == NULL || m_dataObject == NULL ) return wxFormatInvalid; - const QStringList formats = m_pImpl->m_pendingMimeData->formats(); + const QStringList formats = mimeData->formats(); for ( int i = 0; i < formats.count(); ++i ) { const wxDataFormat format(wxQtConvertString(formats[i]));