Implement initial try of wxDropTarget.

The drop target needs to use an event filter to capture drag-and-drop events from the window.
This commit is contained in:
Jay Nabonne
2019-01-29 16:13:41 +00:00
parent 0dc1654ab6
commit 323cbdabdb
5 changed files with 211 additions and 19 deletions

View File

@@ -18,6 +18,7 @@
#include <QDrag>
#include <QWidget>
#include <QMimeData>
#include <QCloseEvent>
namespace
{
@@ -38,6 +39,21 @@ namespace
}
}
Qt::DropAction DragResultToDropAction(wxDragResult result)
{
switch ( result )
{
case wxDragCopy:
return Qt::CopyAction;
case wxDragMove:
return Qt::MoveAction;
case wxDragLink:
return Qt::LinkAction;
default:
return Qt::IgnoreAction;
}
}
void AddDataFormat(wxDataObject* dataObject, QMimeData* mimeData, const wxDataFormat& format)
{
const size_t data_size = dataObject->GetDataSize(format);
@@ -66,29 +82,120 @@ namespace
}
}
wxDropTarget::wxDropTarget(wxDataObject *WXUNUSED(dataObject))
class wxDropTarget::PendingMimeDataSetter
{
public:
PendingMimeDataSetter(wxDropTarget* dropTarget, const QMimeData* mimeData)
: m_dropTarget(dropTarget)
{
m_dropTarget->m_pendingMimeData = mimeData;
}
~PendingMimeDataSetter()
{
m_dropTarget->m_pendingMimeData = NULL;
}
private:
wxDropTarget* m_dropTarget;
};
wxDropTarget::wxDropTarget(wxDataObject *dataObject)
: wxDropTargetBase(dataObject),
m_pendingMimeData(NULL)
{
}
bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y))
{
return false;
return !GetMatchingPair().GetMimeType().empty();
}
wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult WXUNUSED(def))
wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult default_drag_result)
{
return wxDragResult();
GetData();
return default_drag_result;
}
bool wxDropTarget::GetData()
{
return false;
const wxDataFormat droppedFormat = GetMatchingPair();
const wxString mimeType = droppedFormat.GetMimeType();
if ( mimeType.empty() )
return false;
const QByteArray data = m_pendingMimeData->data(wxQtConvertString(mimeType));
return m_dataObject->SetData(droppedFormat, data.size(), data.data());
}
wxDataFormat wxDropTarget::GetMatchingPair()
{
wxFAIL_MSG("wxDropTarget::GetMatchingPair() not implemented in src/qt/dnd.cpp");
return wxDF_INVALID;
if ( m_pendingMimeData == NULL || m_dataObject == NULL )
return wxFormatInvalid;
const QStringList formats = m_pendingMimeData->formats();
for ( int i = 0; i < formats.count(); ++i )
{
const wxDataFormat format(wxQtConvertString(formats[i]));
if ( m_dataObject->IsSupportedFormat(format) )
{
return format;
}
}
return wxFormatInvalid;
}
void wxDropTarget::OnQtEnter(QEvent* event)
{
event->accept();
QDragEnterEvent *e = static_cast<QDragEnterEvent*>(event);
const QPoint where = e->pos();
PendingMimeDataSetter setter(this, e->mimeData());
wxDragResult result = OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction()));
e->setDropAction(DragResultToDropAction(result));
}
void wxDropTarget::OnQtLeave(QEvent* event)
{
event->accept();
OnLeave();
}
void wxDropTarget::OnQtMove(QEvent* event)
{
event->accept();
QDragMoveEvent *e = static_cast<QDragMoveEvent*>(event);
const QPoint where = e->pos();
PendingMimeDataSetter setter(this, e->mimeData());
wxDragResult result = OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction()));
e->setDropAction(DragResultToDropAction(result));
}
void wxDropTarget::OnQtDrop(QEvent* event)
{
event->accept();
const QDropEvent *e = static_cast<QDropEvent*>(event);
const QPoint where = e->pos();
PendingMimeDataSetter setter(this, e->mimeData());
if ( OnDrop(where.x(), where.y()) )
{
OnData(where.x(), where.y(), DropActionToDragResult(e->dropAction()));
}
}
//##############################################################################