Get simple (text) drag and drop case working with drop source.

We ended up not needing the underlying QMimeData-based implementation, as the classes sitting on top of it
bypassed it anyway. It now treats the hierarchy as a pure data storage mechanism, generating the
QMimeData when drag-drop is initiated.
This commit is contained in:
Jay Nabonne
2019-01-28 15:30:25 +00:00
parent ea4c7120ff
commit 0dc1654ab6
4 changed files with 112 additions and 70 deletions

View File

@@ -11,6 +11,60 @@
#if wxUSE_DRAG_AND_DROP
#include "wx/dnd.h"
#include "wx/scopedarray.h"
#include "wx/qt/private/converter.h"
#include <QDrag>
#include <QWidget>
#include <QMimeData>
namespace
{
wxDragResult DropActionToDragResult(Qt::DropAction action)
{
switch ( action )
{
case Qt::IgnoreAction:
return wxDragCancel;
case Qt::CopyAction:
return wxDragCopy;
case Qt::MoveAction:
return wxDragMove;
case Qt::LinkAction:
return wxDragLink;
default:
return wxDragNone;
}
}
void AddDataFormat(wxDataObject* dataObject, QMimeData* mimeData, const wxDataFormat& format)
{
const size_t data_size = dataObject->GetDataSize(format);
QByteArray data(static_cast<int>(data_size), Qt::Initialization());
dataObject->GetDataHere(format, data.data());
mimeData->setData(wxQtConvertString(format.GetMimeType()), data);
}
QMimeData* CreateMimeData(wxDataObject* dataObject)
{
QMimeData* mimeData = new QMimeData();
const size_t count = dataObject->GetFormatCount();
wxScopedArray<wxDataFormat> array(dataObject->GetFormatCount());
dataObject->GetAllFormats(array.get());
for ( size_t i = 0; i < count; i++ )
{
AddDataFormat(dataObject, mimeData, array[i]);
}
return mimeData;
}
}
wxDropTarget::wxDropTarget(wxDataObject *WXUNUSED(dataObject))
{
@@ -39,25 +93,47 @@ wxDataFormat wxDropTarget::GetMatchingPair()
//##############################################################################
wxDropSource::wxDropSource( wxWindow *WXUNUSED(win),
wxDropSource::wxDropSource( wxWindow *win,
const wxIcon &WXUNUSED(copy),
const wxIcon &WXUNUSED(move),
const wxIcon &WXUNUSED(none))
: m_parentWindow(win)
{
}
wxDropSource::wxDropSource( wxDataObject& WXUNUSED(data),
wxWindow *WXUNUSED(win),
wxDropSource::wxDropSource( wxDataObject& data,
wxWindow *win,
const wxIcon &WXUNUSED(copy),
const wxIcon &WXUNUSED(move),
const wxIcon &WXUNUSED(none))
: m_parentWindow(win)
{
SetData(data);
}
wxDragResult wxDropSource::DoDragDrop(int WXUNUSED(flags))
wxDragResult wxDropSource::DoDragDrop(int flags /*=wxDrag_CopyOnly*/)
{
return wxDragResult();
wxCHECK_MSG(m_data != NULL, wxDragNone, wxT("No data in wxDropSource!"));
wxCHECK_MSG(m_parentWindow != NULL, wxDragNone, wxT("NULL parent window in wxDropSource!"));
QDrag drag(m_parentWindow->GetHandle());
drag.setMimeData(CreateMimeData(m_data));
Qt::DropActions actions = Qt::CopyAction | Qt::MoveAction;
Qt::DropAction defaultAction = Qt::CopyAction;
switch ( flags )
{
case wxDrag_CopyOnly:
actions = Qt::CopyAction;
break;
case wxDrag_DefaultMove:
defaultAction = Qt::MoveAction;
break;
default:
break;
}
return DropActionToDragResult(drag.exec(actions, defaultAction));
}
#endif // wxUSE_DRAG_AND_DROP