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:
@@ -1,5 +1,5 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
// Name: src/qt/dataobj.cpp
|
// Name: src/qt/dataobj.h
|
||||||
// Author: Peter Most
|
// Author: Peter Most
|
||||||
// Copyright: (c) Peter Most
|
// Copyright: (c) Peter Most
|
||||||
// Licence: wxWindows licence
|
// Licence: wxWindows licence
|
||||||
@@ -8,24 +8,17 @@
|
|||||||
#ifndef _WX_QT_DATAOBJ_H_
|
#ifndef _WX_QT_DATAOBJ_H_
|
||||||
#define _WX_QT_DATAOBJ_H_
|
#define _WX_QT_DATAOBJ_H_
|
||||||
|
|
||||||
class QMimeData;
|
// ----------------------------------------------------------------------------
|
||||||
|
// wxDataObject is the same as wxDataObjectBase under wxQT
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
class WXDLLIMPEXP_CORE wxDataObject : public wxDataObjectBase
|
class WXDLLIMPEXP_CORE wxDataObject : public wxDataObjectBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxDataObject();
|
wxDataObject();
|
||||||
~wxDataObject();
|
virtual ~wxDataObject();
|
||||||
|
|
||||||
virtual bool IsSupportedFormat(const wxDataFormat& format, Direction dir) const;
|
virtual bool IsSupportedFormat( const wxDataFormat& format, Direction dir = Get ) const;
|
||||||
virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const;
|
|
||||||
virtual size_t GetFormatCount(Direction dir = Get) const;
|
|
||||||
virtual void GetAllFormats(wxDataFormat *formats, Direction dir = Get) const;
|
|
||||||
virtual size_t GetDataSize(const wxDataFormat& format) const;
|
|
||||||
virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
|
|
||||||
virtual bool SetData(const wxDataFormat& format, size_t len, const void * buf);
|
|
||||||
|
|
||||||
private:
|
|
||||||
QMimeData *m_qtMimeData; // to handle formats that have no helper classes
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _WX_QT_DATAOBJ_H_
|
#endif // _WX_QT_DATAOBJ_H_
|
||||||
|
@@ -42,5 +42,8 @@ public:
|
|||||||
const wxIcon &none = wxNullIcon);
|
const wxIcon &none = wxNullIcon);
|
||||||
|
|
||||||
virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly);
|
virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly);
|
||||||
|
|
||||||
|
private:
|
||||||
|
wxWindow* m_parentWindow;
|
||||||
};
|
};
|
||||||
#endif // _WX_QT_DND_H_
|
#endif // _WX_QT_DND_H_
|
||||||
|
@@ -12,11 +12,8 @@
|
|||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/qt/private/converter.h"
|
|
||||||
#include "wx/qt/private/utils.h"
|
|
||||||
#include "wx/dataobj.h"
|
#include "wx/dataobj.h"
|
||||||
|
#include "wx/scopedarray.h"
|
||||||
#include <QtCore/QMimeData>
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -118,64 +115,33 @@ bool wxDataFormat::operator!=(const wxDataFormat& format) const
|
|||||||
|
|
||||||
wxDataObject::wxDataObject()
|
wxDataObject::wxDataObject()
|
||||||
{
|
{
|
||||||
m_qtMimeData = new QMimeData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxDataObject::~wxDataObject()
|
wxDataObject::~wxDataObject()
|
||||||
{
|
{
|
||||||
delete m_qtMimeData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction) const
|
bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const
|
||||||
{
|
{
|
||||||
return wxDataFormat(format) != wxDF_INVALID;
|
const size_t formatCount = GetFormatCount(dir);
|
||||||
}
|
if ( formatCount == 1 )
|
||||||
wxDataFormat wxDataObject::GetPreferredFormat(Direction) const
|
|
||||||
{
|
|
||||||
/* formats are in order of preference */
|
|
||||||
if (m_qtMimeData->formats().count())
|
|
||||||
return m_qtMimeData->formats().first();
|
|
||||||
|
|
||||||
return wxDataFormat();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t wxDataObject::GetFormatCount(Direction) const
|
|
||||||
{
|
|
||||||
return m_qtMimeData->formats().count();
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxDataObject::GetAllFormats(wxDataFormat *formats, Direction) const
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
foreach (QString format, m_qtMimeData->formats())
|
|
||||||
{
|
{
|
||||||
formats[i] = format;
|
return format == GetPreferredFormat();
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
size_t wxDataObject::GetDataSize(const wxDataFormat& format) const
|
wxScopedArray<wxDataFormat> formats(formatCount);
|
||||||
{
|
GetAllFormats(formats.get(), dir);
|
||||||
return m_qtMimeData->data( wxQtConvertString(format.m_MimeType) ).count();
|
|
||||||
}
|
for ( size_t n = 0; n < formatCount; ++n )
|
||||||
|
{
|
||||||
|
if ( formats[n] == format )
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool wxDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
|
|
||||||
{
|
|
||||||
if (!m_qtMimeData->hasFormat(wxQtConvertString(format.m_MimeType)))
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QByteArray data = m_qtMimeData->data( wxQtConvertString(format.m_MimeType) ).data();
|
|
||||||
memcpy(buf, data.constData(), data.size());
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxDataObject::SetData(const wxDataFormat& format, size_t len, const void * buf)
|
//#############################################################################
|
||||||
{
|
|
||||||
QByteArray bytearray((const char*)buf, len);
|
|
||||||
m_qtMimeData->setData(wxQtConvertString(format.m_MimeType), bytearray);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
wxBitmapDataObject::wxBitmapDataObject()
|
wxBitmapDataObject::wxBitmapDataObject()
|
||||||
{
|
{
|
||||||
@@ -185,6 +151,8 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &WXUNUSED(bitmap) )
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#############################################################################
|
||||||
|
|
||||||
wxFileDataObject::wxFileDataObject()
|
wxFileDataObject::wxFileDataObject()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -193,3 +161,5 @@ void wxFileDataObject::AddFile( const wxString &WXUNUSED(filename) )
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -11,6 +11,60 @@
|
|||||||
#if wxUSE_DRAG_AND_DROP
|
#if wxUSE_DRAG_AND_DROP
|
||||||
|
|
||||||
#include "wx/dnd.h"
|
#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))
|
wxDropTarget::wxDropTarget(wxDataObject *WXUNUSED(dataObject))
|
||||||
{
|
{
|
||||||
@@ -39,25 +93,47 @@ wxDataFormat wxDropTarget::GetMatchingPair()
|
|||||||
|
|
||||||
//##############################################################################
|
//##############################################################################
|
||||||
|
|
||||||
|
wxDropSource::wxDropSource( wxWindow *win,
|
||||||
wxDropSource::wxDropSource( wxWindow *WXUNUSED(win),
|
|
||||||
const wxIcon &WXUNUSED(copy),
|
const wxIcon &WXUNUSED(copy),
|
||||||
const wxIcon &WXUNUSED(move),
|
const wxIcon &WXUNUSED(move),
|
||||||
const wxIcon &WXUNUSED(none))
|
const wxIcon &WXUNUSED(none))
|
||||||
|
: m_parentWindow(win)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
wxDropSource::wxDropSource( wxDataObject& WXUNUSED(data),
|
wxDropSource::wxDropSource( wxDataObject& data,
|
||||||
wxWindow *WXUNUSED(win),
|
wxWindow *win,
|
||||||
const wxIcon &WXUNUSED(copy),
|
const wxIcon &WXUNUSED(copy),
|
||||||
const wxIcon &WXUNUSED(move),
|
const wxIcon &WXUNUSED(move),
|
||||||
const wxIcon &WXUNUSED(none))
|
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
|
#endif // wxUSE_DRAG_AND_DROP
|
||||||
|
Reference in New Issue
Block a user