From ea4c7120ff50969e24f7d5f28d64d65b0a028c78 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Mon, 28 Jan 2019 15:22:01 +0000 Subject: [PATCH 01/21] Fixed up and cleaned up Qt variant of wxDataFormat. Kept "mime type" and "id" conceptually separate in the interface in case they need to diverge later. Got rid of the odd "wxChar *" variants and the QString one. Implemented unimplemented "type" functions. Implemented "!=" in terms of "==", to keep from having two places to keep in sync. --- include/wx/qt/dataform.h | 36 +++++++------- src/qt/clipbrd.cpp | 8 +-- src/qt/dataobj.cpp | 102 +++++++++++++++++++-------------------- 3 files changed, 72 insertions(+), 74 deletions(-) diff --git a/include/wx/qt/dataform.h b/include/wx/qt/dataform.h index b6f5c307bf..55932a5815 100644 --- a/include/wx/qt/dataform.h +++ b/include/wx/qt/dataform.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: wx/qt/toolbar.h +// Name: wx/qt/dataform.h // Author: Sean D'Epagnier // Copyright: (c) Sean D'Epagnier 2014 // Licence: wxWindows licence @@ -8,34 +8,34 @@ #ifndef _WX_QT_DATAFORM_H_ #define _WX_QT_DATAFORM_H_ -class QString; - class WXDLLIMPEXP_CORE wxDataFormat { public: - wxDataFormat(); - wxDataFormat( wxDataFormatId formatId ); + wxDataFormat(wxDataFormatId formatId = wxDF_INVALID); wxDataFormat(const wxString &id); - wxDataFormat(const QString &id); - wxDataFormat(const wxChar *id); - void SetId( const wxChar *id ); - + // Standard methods + const wxString& GetId() const; + void SetId(const wxString& id); + + wxDataFormatId GetType() const; + void SetType(wxDataFormatId type); + bool operator==(wxDataFormatId format) const; bool operator!=(wxDataFormatId format) const; bool operator==(const wxDataFormat& format) const; bool operator!=(const wxDataFormat& format) const; - // string ids are used for custom types - this SetId() must be used for - // application-specific formats - wxString GetId() const; - void SetId( const wxString& id ); + // Direct access to the underlying mime type. + // Equivalent to "id", except "id" is supposed to be + // invalid for standard types, whereas this should + // always be valid (if meaningful). + const wxString& GetMimeType() const; + void SetMimeType(const wxString& mimeType); - // implementation - wxDataFormatId GetType() const; - void SetType( wxDataFormatId type ); - - wxString m_MimeType; +private: + wxString m_mimeType; + wxDataFormatId m_formatId; }; #endif // _WX_QT_DATAFORM_H_ diff --git a/src/qt/clipbrd.cpp b/src/qt/clipbrd.cpp index 5479fe3109..8f92c947ed 100644 --- a/src/qt/clipbrd.cpp +++ b/src/qt/clipbrd.cpp @@ -109,7 +109,7 @@ bool wxClipboard::AddData( wxDataObject *data ) QByteArray bytearray(size, 0); data->GetDataHere(format, bytearray.data()); - MimeData->setData(wxQtConvertString(format.m_MimeType), bytearray); + MimeData->setData(wxQtConvertString(format.GetMimeType()), bytearray); } delete data; @@ -144,7 +144,7 @@ bool wxClipboard::GetData( wxDataObject& data ) const wxDataFormat format(formats[i]); // is this format supported by clipboard ? - if( !MimeData->hasFormat(wxQtConvertString(format.m_MimeType)) ) + if( !MimeData->hasFormat(wxQtConvertString(format.GetMimeType())) ) continue; wxTextDataObject *textdata = dynamic_cast(&data); @@ -152,7 +152,7 @@ bool wxClipboard::GetData( wxDataObject& data ) textdata->SetText(wxQtConvertString(MimeData->text())); else { - QByteArray bytearray = MimeData->data( wxQtConvertString(format.m_MimeType) ).data(); + QByteArray bytearray = MimeData->data( wxQtConvertString(format.GetMimeType()) ).data(); data.SetData(format, bytearray.size(), bytearray.constData()); } @@ -170,7 +170,7 @@ void wxClipboard::Clear() bool wxClipboard::IsSupported( const wxDataFormat& format ) { const QMimeData *data = QtClipboard->mimeData( (QClipboard::Mode)Mode() ); - return data->hasFormat(wxQtConvertString(format.m_MimeType)); + return data->hasFormat(wxQtConvertString(format.GetMimeType())); } bool wxClipboard::IsSupportedAsync(wxEvtHandler *sink) diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index 27108b1a46..2840e735a8 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -18,102 +18,100 @@ #include -wxDataFormat::wxDataFormat() +namespace { -} - -static wxString DataFormatIdToMimeType( wxDataFormatId formatId ) -{ - switch(formatId) { - case wxDF_TEXT: return "text/plain"; - case wxDF_BITMAP: return "image/bmp"; - case wxDF_TIFF: return "image/tiff"; - case wxDF_WAVE: return "audio/x-wav"; - case wxDF_UNICODETEXT: return "text/plain"; - case wxDF_HTML: return "text/html"; - case wxDF_METAFILE: - case wxDF_SYLK: - case wxDF_DIF: - case wxDF_OEMTEXT: - case wxDF_DIB: - case wxDF_PALETTE: - case wxDF_PENDATA: - case wxDF_RIFF: - case wxDF_ENHMETAFILE: - case wxDF_FILENAME: - case wxDF_LOCALE: - case wxDF_PRIVATE: - case wxDF_INVALID: - case wxDF_MAX: - break; + wxString DataFormatIdToMimeType(wxDataFormatId formatId) + { + switch ( formatId ) + { + case wxDF_TEXT: return "text/plain"; + case wxDF_BITMAP: return "image/bmp"; + case wxDF_TIFF: return "image/tiff"; + case wxDF_WAVE: return "audio/x-wav"; + case wxDF_UNICODETEXT: return "text/plain"; + case wxDF_HTML: return "text/html"; + case wxDF_METAFILE: + case wxDF_SYLK: + case wxDF_DIF: + case wxDF_OEMTEXT: + case wxDF_DIB: + case wxDF_PALETTE: + case wxDF_PENDATA: + case wxDF_RIFF: + case wxDF_ENHMETAFILE: + case wxDF_FILENAME: + case wxDF_LOCALE: + case wxDF_PRIVATE: + case wxDF_INVALID: + case wxDF_MAX: + default: + return ""; + } } - return ""; } -wxDataFormat::wxDataFormat( wxDataFormatId formatId ) +wxDataFormat::wxDataFormat(wxDataFormatId formatId) { - m_MimeType = DataFormatIdToMimeType(formatId); + SetType(formatId); } wxDataFormat::wxDataFormat(const wxString &id) { - m_MimeType = id; + SetId(id); } -wxDataFormat::wxDataFormat(const wxChar *id) +const wxString& wxDataFormat::GetMimeType() const { - m_MimeType = id; + return m_mimeType; } -wxDataFormat::wxDataFormat(const QString &id) +void wxDataFormat::SetMimeType(const wxString& mimeType) { - m_MimeType = wxQtConvertString(id); + m_mimeType = mimeType; + m_formatId = wxDF_INVALID; } -void wxDataFormat::SetId( const wxChar *id ) +void wxDataFormat::SetId(const wxString& id) { - m_MimeType = id; + SetMimeType(id); } -void wxDataFormat::SetId( const wxString& id ) +const wxString& wxDataFormat::GetId() const { - m_MimeType = id; -} - -wxString wxDataFormat::GetId() const -{ - return m_MimeType; + return m_mimeType; } wxDataFormatId wxDataFormat::GetType() const { - wxMISSING_IMPLEMENTATION( "wxDataFormat GetType" ); - return wxDataFormatId(); + return m_formatId; } -void wxDataFormat::SetType( wxDataFormatId WXUNUSED(type) ) +void wxDataFormat::SetType(wxDataFormatId formatId) { - wxMISSING_IMPLEMENTATION( "wxDataFormat SetType" ); + m_mimeType = DataFormatIdToMimeType(formatId); + m_formatId = formatId; } bool wxDataFormat::operator==(wxDataFormatId format) const { - return m_MimeType == DataFormatIdToMimeType(format); + return m_mimeType == DataFormatIdToMimeType(format) + && m_formatId == format; } bool wxDataFormat::operator!=(wxDataFormatId format) const { - return m_MimeType != DataFormatIdToMimeType(format); + return !operator==(format); } bool wxDataFormat::operator==(const wxDataFormat& format) const { - return m_MimeType == format.m_MimeType; + return m_mimeType == format.m_mimeType + && m_formatId == format.m_formatId; } bool wxDataFormat::operator!=(const wxDataFormat& format) const { - return m_MimeType != format.m_MimeType; + return !operator==(format); } //############################################################################# From 0dc1654ab67da3af71210b7adc47cb042ff09d00 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Mon, 28 Jan 2019 15:30:25 +0000 Subject: [PATCH 02/21] 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. --- include/wx/qt/dataobj.h | 19 +++------ include/wx/qt/dnd.h | 3 ++ src/qt/dataobj.cpp | 72 ++++++++++----------------------- src/qt/dnd.cpp | 88 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 112 insertions(+), 70 deletions(-) diff --git a/include/wx/qt/dataobj.h b/include/wx/qt/dataobj.h index 002de9c2a0..0002c13d8a 100644 --- a/include/wx/qt/dataobj.h +++ b/include/wx/qt/dataobj.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/qt/dataobj.cpp +// Name: src/qt/dataobj.h // Author: Peter Most // Copyright: (c) Peter Most // Licence: wxWindows licence @@ -8,24 +8,17 @@ #ifndef _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 { public: wxDataObject(); - ~wxDataObject(); + virtual ~wxDataObject(); - virtual bool IsSupportedFormat(const wxDataFormat& format, Direction dir) 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 + virtual bool IsSupportedFormat( const wxDataFormat& format, Direction dir = Get ) const; }; #endif // _WX_QT_DATAOBJ_H_ diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index d06a35a570..6edd330f6e 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -42,5 +42,8 @@ public: const wxIcon &none = wxNullIcon); virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly); + +private: + wxWindow* m_parentWindow; }; #endif // _WX_QT_DND_H_ diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index 2840e735a8..b1cb3b7fd2 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -12,11 +12,8 @@ #pragma hdrstop #endif -#include "wx/qt/private/converter.h" -#include "wx/qt/private/utils.h" #include "wx/dataobj.h" - -#include +#include "wx/scopedarray.h" namespace { @@ -118,64 +115,33 @@ bool wxDataFormat::operator!=(const wxDataFormat& format) const wxDataObject::wxDataObject() { - m_qtMimeData = new QMimeData; } 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; -} -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()) + const size_t formatCount = GetFormatCount(dir); + if ( formatCount == 1 ) { - formats[i] = format; - i++; + return format == GetPreferredFormat(); } + + wxScopedArray formats(formatCount); + GetAllFormats(formats.get(), dir); + + for ( size_t n = 0; n < formatCount; ++n ) + { + if ( formats[n] == format ) + return true; + } + + return false; } -size_t wxDataObject::GetDataSize(const wxDataFormat& format) const -{ - return m_qtMimeData->data( wxQtConvertString(format.m_MimeType) ).count(); -} - -bool wxDataObject::GetDataHere(const wxDataFormat& format, void *buf) const -{ - if (!m_qtMimeData->hasFormat(wxQtConvertString(format.m_MimeType))) - 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() { @@ -185,6 +151,8 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &WXUNUSED(bitmap) ) { } +//############################################################################# + wxFileDataObject::wxFileDataObject() { } @@ -193,3 +161,5 @@ void wxFileDataObject::AddFile( const wxString &WXUNUSED(filename) ) { } + + diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 7e99e94ec2..68922e2f8b 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -11,6 +11,60 @@ #if wxUSE_DRAG_AND_DROP #include "wx/dnd.h" +#include "wx/scopedarray.h" + +#include "wx/qt/private/converter.h" + +#include +#include +#include + +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(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 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 From 323cbdabdb2eb1fcda2156134b2df192590c6573 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 16:13:41 +0000 Subject: [PATCH 03/21] Implement initial try of wxDropTarget. The drop target needs to use an event filter to capture drag-and-drop events from the window. --- include/wx/qt/dnd.h | 20 +++++-- include/wx/qt/window.h | 15 +++++ src/qt/dataobj.cpp | 6 ++ src/qt/dnd.cpp | 121 ++++++++++++++++++++++++++++++++++++++--- src/qt/window.cpp | 68 +++++++++++++++++++++-- 5 files changed, 211 insertions(+), 19 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index 6edd330f6e..ec8bf6feb3 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -10,22 +10,30 @@ #define wxDROP_ICON(name) wxICON(name) +class QMimeData; + class WXDLLIMPEXP_CORE wxDropTarget : public wxDropTargetBase { public: - wxDropTarget(wxDataObject *dataObject = NULL ); + wxDropTarget(wxDataObject *dataObject = NULL); - virtual bool OnDrop(wxCoord x, wxCoord y); - virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def); - virtual bool GetData(); + virtual bool OnDrop(wxCoord x, wxCoord y) wxOVERRIDE; + virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) wxOVERRIDE; + virtual bool GetData() wxOVERRIDE; wxDataFormat GetMatchingPair(); -protected: + void OnQtEnter(QEvent* event); + void OnQtLeave(QEvent* event); + void OnQtMove(QEvent* event); + void OnQtDrop(QEvent* event); private: -}; + class PendingMimeDataSetter; + friend class PendingMimeDataSetter; + const QMimeData* m_pendingMimeData; +}; class WXDLLIMPEXP_CORE wxDropSource: public wxDropSourceBase { diff --git a/include/wx/qt/window.h b/include/wx/qt/window.h index a2f1542a27..130267f75e 100644 --- a/include/wx/qt/window.h +++ b/include/wx/qt/window.h @@ -10,6 +10,7 @@ #define _WX_QT_WINDOW_H_ #include +#include class QShortcut; template < class T > class QList; @@ -216,6 +217,16 @@ protected: QWidget *m_qtWindow; private: + class DnDEventAdapter : public QObject + { + public: + DnDEventAdapter(); + virtual bool eventFilter(QObject* watched, QEvent* event); + void SetDropTarget(wxDropTarget* dropTarget, QWidget* window); + private: + wxDropTarget *m_dropTarget; + }; + void Init(); QScrollArea *m_qtContainer; @@ -238,6 +249,10 @@ private: bool m_processingShortcut; #endif // wxUSE_ACCEL +#if wxUSE_DRAG_AND_DROP + DnDEventAdapter dnd_event_adapter; +#endif + wxDECLARE_DYNAMIC_CLASS_NO_COPY( wxWindowQt ); }; diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index b1cb3b7fd2..e6f8f6e1be 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -102,6 +102,12 @@ bool wxDataFormat::operator!=(wxDataFormatId format) const bool wxDataFormat::operator==(const wxDataFormat& format) const { + // If mime types match, then that's good enough. + // (Could be comparing a standard constructed format to a + // custom constructed one, where both are actually the same.) + if (!m_mimeType.empty() && m_mimeType == format.m_mimeType) + return true; + return m_mimeType == format.m_mimeType && m_formatId == format.m_formatId; } diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 68922e2f8b..750b19524d 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -18,6 +18,7 @@ #include #include #include +#include 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(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(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(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())); + } } //############################################################################## diff --git a/src/qt/window.cpp b/src/qt/window.cpp index 88e1b4b1e0..d638daa64b 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -31,12 +31,12 @@ #endif // WX_PRECOMP #include "wx/window.h" +#include "wx/dnd.h" #include "wx/tooltip.h" #include "wx/qt/private/utils.h" #include "wx/qt/private/converter.h" #include "wx/qt/private/winevent.h" - #define VERT_SCROLLBAR_POSITION 0, 1 #define HORZ_SCROLLBAR_POSITION 1, 0 @@ -193,9 +193,6 @@ static const char WINDOW_POINTER_PROPERTY_NAME[] = "wxWindowPointer"; return const_cast< wxWindowQt * >( ( variant.value< const wxWindow * >() )); } - - - static wxWindowQt *s_capturedWindow = NULL; /* static */ wxWindowQt *wxWindowBase::DoFindFocus() @@ -225,6 +222,8 @@ void wxWindowQt::Init() #endif m_qtWindow = NULL; m_qtContainer = NULL; + + m_dropTarget = NULL; } wxWindowQt::wxWindowQt() @@ -688,9 +687,9 @@ void wxWindowQt::ScrollWindow( int dx, int dy, const wxRect *rect ) #if wxUSE_DRAG_AND_DROP -void wxWindowQt::SetDropTarget( wxDropTarget * WXUNUSED( dropTarget ) ) +void wxWindowQt::SetDropTarget( wxDropTarget *dropTarget ) { - wxMISSING_IMPLEMENTATION( __FUNCTION__ ); + dnd_event_adapter.SetDropTarget(dropTarget, m_qtWindow); } #endif @@ -1543,3 +1542,60 @@ QPainter *wxWindowQt::QtGetPainter() { return m_qtPainter; } + +//############################################################################## +// DnDEventAdapter +//############################################################################## + +wxWindow::DnDEventAdapter::DnDEventAdapter() : m_dropTarget(NULL) +{ +} + +bool wxWindow::DnDEventAdapter::eventFilter(QObject* watched, QEvent* event) +{ + if ( m_dropTarget != NULL ) + { + switch ( event->type() ) + { + case QEvent::Drop: + m_dropTarget->OnQtDrop(event); + return true; + + case QEvent::DragEnter: + m_dropTarget->OnQtEnter(event); + return true; + + case QEvent::DragMove: + m_dropTarget->OnQtMove(event); + return true; + + case QEvent::DragLeave: + m_dropTarget->OnQtLeave(event); + return true; + + default: + break; + } + } + + return QObject::eventFilter(watched, event); +} + +void wxWindow::DnDEventAdapter::SetDropTarget(wxDropTarget* dropTarget, QWidget* window) +{ + if ( m_dropTarget == dropTarget ) + return; + + m_dropTarget = dropTarget; + + if ( m_dropTarget == NULL ) + { + window->removeEventFilter(this); + window->setAcceptDrops(false); + } + else + { + window->installEventFilter(this); + window->setAcceptDrops(true); + } +} From 2bceaaa572a84c61730ad4af87e00d71beb2c6f2 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 16:29:58 +0000 Subject: [PATCH 04/21] Clean up wxDropTarget (first stage) by moving Qt specific stuff (e.g. QMimeData) into an Impl. --- include/wx/qt/dnd.h | 7 +++-- src/qt/dnd.cpp | 62 +++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index ec8bf6feb3..c76ba7eeac 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -16,6 +16,7 @@ class WXDLLIMPEXP_CORE wxDropTarget : public wxDropTargetBase { public: wxDropTarget(wxDataObject *dataObject = NULL); + virtual ~wxDropTarget(); virtual bool OnDrop(wxCoord x, wxCoord y) wxOVERRIDE; virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) wxOVERRIDE; @@ -29,10 +30,8 @@ public: void OnQtDrop(QEvent* event); private: - class PendingMimeDataSetter; - friend class PendingMimeDataSetter; - - const QMimeData* m_pendingMimeData; + class Impl; + Impl* m_pImpl; }; class WXDLLIMPEXP_CORE wxDropSource: public wxDropSourceBase diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 750b19524d..575b1ffae2 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -82,40 +82,54 @@ namespace } } -class wxDropTarget::PendingMimeDataSetter + +class wxDropTarget::Impl { 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; - + const QMimeData* m_pendingMimeData; }; +namespace +{ + class PendingMimeDataSetter + { + public: + PendingMimeDataSetter(const QMimeData*& targetMimeData, const QMimeData* mimeData) + : m_targetMimeData(targetMimeData) + { + m_targetMimeData = mimeData; + } + + ~PendingMimeDataSetter() + { + m_targetMimeData = NULL; + } + + private: + const QMimeData*& m_targetMimeData; + }; +} + wxDropTarget::wxDropTarget(wxDataObject *dataObject) : wxDropTargetBase(dataObject), - m_pendingMimeData(NULL) + m_pImpl(new Impl) { } +wxDropTarget::~wxDropTarget() +{ + delete m_pImpl; +} + bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) { return !GetMatchingPair().GetMimeType().empty(); } -wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult default_drag_result) +wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) { GetData(); - return default_drag_result; + return def; } bool wxDropTarget::GetData() @@ -126,17 +140,17 @@ bool wxDropTarget::GetData() if ( mimeType.empty() ) return false; - const QByteArray data = m_pendingMimeData->data(wxQtConvertString(mimeType)); + const QByteArray data = m_pImpl->m_pendingMimeData->data(wxQtConvertString(mimeType)); return m_dataObject->SetData(droppedFormat, data.size(), data.data()); } wxDataFormat wxDropTarget::GetMatchingPair() { - if ( m_pendingMimeData == NULL || m_dataObject == NULL ) + if ( m_pImpl->m_pendingMimeData == NULL || m_dataObject == NULL ) return wxFormatInvalid; - const QStringList formats = m_pendingMimeData->formats(); + const QStringList formats = m_pImpl->m_pendingMimeData->formats(); for ( int i = 0; i < formats.count(); ++i ) { const wxDataFormat format(wxQtConvertString(formats[i])); @@ -156,7 +170,7 @@ void wxDropTarget::OnQtEnter(QEvent* event) QDragEnterEvent *e = static_cast(event); const QPoint where = e->pos(); - PendingMimeDataSetter setter(this, e->mimeData()); + PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, e->mimeData()); wxDragResult result = OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); @@ -176,7 +190,7 @@ void wxDropTarget::OnQtMove(QEvent* event) QDragMoveEvent *e = static_cast(event); const QPoint where = e->pos(); - PendingMimeDataSetter setter(this, e->mimeData()); + PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, e->mimeData()); wxDragResult result = OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); @@ -190,7 +204,7 @@ void wxDropTarget::OnQtDrop(QEvent* event) const QDropEvent *e = static_cast(event); const QPoint where = e->pos(); - PendingMimeDataSetter setter(this, e->mimeData()); + PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, e->mimeData()); if ( OnDrop(where.x(), where.y()) ) { From 8c63c40953314de6d63b5c55824b396d260a90e9 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 16:55:31 +0000 Subject: [PATCH 05/21] Clean up wxDropTarget and wxWindow a bit more by moving DnD stuff fully into drop target (besides connecting and disconnecting). Window doesn't have to know more than how to hook itself up. Impl now performs function of event filter/adapter. --- include/wx/qt/dnd.h | 8 +-- include/wx/qt/window.h | 14 ---- src/qt/dnd.cpp | 148 +++++++++++++++++++++++++++-------------- src/qt/window.cpp | 72 ++++---------------- 4 files changed, 113 insertions(+), 129 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index c76ba7eeac..d7e108cbdd 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -10,8 +10,6 @@ #define wxDROP_ICON(name) wxICON(name) -class QMimeData; - class WXDLLIMPEXP_CORE wxDropTarget : public wxDropTargetBase { public: @@ -24,10 +22,8 @@ public: wxDataFormat GetMatchingPair(); - void OnQtEnter(QEvent* event); - void OnQtLeave(QEvent* event); - void OnQtMove(QEvent* event); - void OnQtDrop(QEvent* event); + void ConnectToQWidget(QWidget* widget); + void DisconnectFromQWidget(QWidget* widget); private: class Impl; diff --git a/include/wx/qt/window.h b/include/wx/qt/window.h index 130267f75e..3978615ae6 100644 --- a/include/wx/qt/window.h +++ b/include/wx/qt/window.h @@ -217,16 +217,6 @@ protected: QWidget *m_qtWindow; private: - class DnDEventAdapter : public QObject - { - public: - DnDEventAdapter(); - virtual bool eventFilter(QObject* watched, QEvent* event); - void SetDropTarget(wxDropTarget* dropTarget, QWidget* window); - private: - wxDropTarget *m_dropTarget; - }; - void Init(); QScrollArea *m_qtContainer; @@ -249,10 +239,6 @@ private: bool m_processingShortcut; #endif // wxUSE_ACCEL -#if wxUSE_DRAG_AND_DROP - DnDEventAdapter dnd_event_adapter; -#endif - wxDECLARE_DYNAMIC_CLASS_NO_COPY( wxWindowQt ); }; diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 575b1ffae2..9398d2d9f7 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -82,13 +82,6 @@ namespace } } - -class wxDropTarget::Impl -{ -public: - const QMimeData* m_pendingMimeData; -}; - namespace { class PendingMimeDataSetter @@ -110,9 +103,99 @@ namespace }; } +class wxDropTarget::Impl : public QObject +{ +public: + explicit Impl(wxDropTarget* dropTarget) : m_dropTarget(dropTarget) + { + } + + virtual bool eventFilter(QObject* watched, QEvent* event) wxOVERRIDE + { + if ( m_dropTarget != NULL ) + { + switch ( event->type() ) + { + case QEvent::Drop: + OnDrop(event); + return true; + + case QEvent::DragEnter: + OnEnter(event); + return true; + + case QEvent::DragMove: + OnMove(event); + return true; + + case QEvent::DragLeave: + OnLeave(event); + return true; + + default: + break; + } + } + + return QObject::eventFilter(watched, event); + } + + void OnEnter(QEvent* event) + { + event->accept(); + + QDragEnterEvent *e = static_cast(event); + const QPoint where = e->pos(); + + PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + + wxDragResult result = m_dropTarget->OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + + e->setDropAction(DragResultToDropAction(result)); + } + + void OnLeave(QEvent* event) + { + event->accept(); + m_dropTarget->OnLeave(); + } + + void OnMove(QEvent* event) + { + event->accept(); + + QDragMoveEvent *e = static_cast(event); + const QPoint where = e->pos(); + + PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + + wxDragResult result = m_dropTarget->OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + + e->setDropAction(DragResultToDropAction(result)); + } + + void OnDrop(QEvent* event) + { + event->accept(); + + const QDropEvent *e = static_cast(event); + const QPoint where = e->pos(); + + PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + + if ( m_dropTarget->OnDrop(where.x(), where.y()) ) + { + m_dropTarget->OnData(where.x(), where.y(), DropActionToDragResult(e->dropAction())); + } + } + + const QMimeData* m_pendingMimeData; + wxDropTarget* m_dropTarget; +}; + wxDropTarget::wxDropTarget(wxDataObject *dataObject) : wxDropTargetBase(dataObject), - m_pImpl(new Impl) + m_pImpl(new Impl(this)) { } @@ -163,53 +246,16 @@ wxDataFormat wxDropTarget::GetMatchingPair() return wxFormatInvalid; } -void wxDropTarget::OnQtEnter(QEvent* event) +void wxDropTarget::ConnectToQWidget(QWidget* widget) { - event->accept(); - - QDragEnterEvent *e = static_cast(event); - const QPoint where = e->pos(); - - PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, e->mimeData()); - - wxDragResult result = OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); - - e->setDropAction(DragResultToDropAction(result)); + widget->setAcceptDrops(true); + widget->installEventFilter(m_pImpl); } -void wxDropTarget::OnQtLeave(QEvent* event) +void wxDropTarget::DisconnectFromQWidget(QWidget* widget) { - event->accept(); - OnLeave(); -} - -void wxDropTarget::OnQtMove(QEvent* event) -{ - event->accept(); - - QDragMoveEvent *e = static_cast(event); - const QPoint where = e->pos(); - - PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, 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(event); - const QPoint where = e->pos(); - - PendingMimeDataSetter setter(m_pImpl->m_pendingMimeData, e->mimeData()); - - if ( OnDrop(where.x(), where.y()) ) - { - OnData(where.x(), where.y(), DropActionToDragResult(e->dropAction())); - } + widget->setAcceptDrops(false); + widget->removeEventFilter(m_pImpl); } //############################################################################## diff --git a/src/qt/window.cpp b/src/qt/window.cpp index d638daa64b..dc22ca3dd7 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -689,7 +689,20 @@ void wxWindowQt::ScrollWindow( int dx, int dy, const wxRect *rect ) #if wxUSE_DRAG_AND_DROP void wxWindowQt::SetDropTarget( wxDropTarget *dropTarget ) { - dnd_event_adapter.SetDropTarget(dropTarget, m_qtWindow); + if ( m_dropTarget == dropTarget ) + return; + + if ( m_dropTarget != NULL ) + { + m_dropTarget->DisconnectFromQWidget(m_qtWindow); + } + + m_dropTarget = dropTarget; + + if (m_dropTarget != NULL) + { + m_dropTarget->ConnectToQWidget(m_qtWindow); + } } #endif @@ -1542,60 +1555,3 @@ QPainter *wxWindowQt::QtGetPainter() { return m_qtPainter; } - -//############################################################################## -// DnDEventAdapter -//############################################################################## - -wxWindow::DnDEventAdapter::DnDEventAdapter() : m_dropTarget(NULL) -{ -} - -bool wxWindow::DnDEventAdapter::eventFilter(QObject* watched, QEvent* event) -{ - if ( m_dropTarget != NULL ) - { - switch ( event->type() ) - { - case QEvent::Drop: - m_dropTarget->OnQtDrop(event); - return true; - - case QEvent::DragEnter: - m_dropTarget->OnQtEnter(event); - return true; - - case QEvent::DragMove: - m_dropTarget->OnQtMove(event); - return true; - - case QEvent::DragLeave: - m_dropTarget->OnQtLeave(event); - return true; - - default: - break; - } - } - - return QObject::eventFilter(watched, event); -} - -void wxWindow::DnDEventAdapter::SetDropTarget(wxDropTarget* dropTarget, QWidget* window) -{ - if ( m_dropTarget == dropTarget ) - return; - - m_dropTarget = dropTarget; - - if ( m_dropTarget == NULL ) - { - window->removeEventFilter(this); - window->setAcceptDrops(false); - } - else - { - window->installEventFilter(this); - window->setAcceptDrops(true); - } -} From 87f6707123cc3c83d79fd9ee7960ac7bbaf43946 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 16:58:52 +0000 Subject: [PATCH 06/21] Fix some spacing transgressions. --- src/qt/dnd.cpp | 4 ++-- src/qt/window.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 9398d2d9f7..4debdf400a 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -260,7 +260,7 @@ void wxDropTarget::DisconnectFromQWidget(QWidget* widget) //############################################################################## -wxDropSource::wxDropSource( wxWindow *win, +wxDropSource::wxDropSource(wxWindow *win, const wxIcon &WXUNUSED(copy), const wxIcon &WXUNUSED(move), const wxIcon &WXUNUSED(none)) @@ -268,7 +268,7 @@ wxDropSource::wxDropSource( wxWindow *win, { } -wxDropSource::wxDropSource( wxDataObject& data, +wxDropSource::wxDropSource(wxDataObject& data, wxWindow *win, const wxIcon &WXUNUSED(copy), const wxIcon &WXUNUSED(move), diff --git a/src/qt/window.cpp b/src/qt/window.cpp index dc22ca3dd7..26df142c7b 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -699,7 +699,7 @@ void wxWindowQt::SetDropTarget( wxDropTarget *dropTarget ) m_dropTarget = dropTarget; - if (m_dropTarget != NULL) + if ( m_dropTarget != NULL ) { m_dropTarget->ConnectToQWidget(m_qtWindow); } From e995618a3a0ef4db69eba04ed6c0040a8af2b2e6 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 17:07:14 +0000 Subject: [PATCH 07/21] Be sure drop target is disconnected before destructing window. --- src/qt/window.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/qt/window.cpp b/src/qt/window.cpp index 26df142c7b..1788afd646 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -259,6 +259,10 @@ wxWindowQt::~wxWindowQt() delete m_qtShortcuts; #endif +#if wxUSE_DRAG_AND_DROP + SetDropTarget(NULL); +#endif + // Delete only if the qt widget was created or assigned to this base class if (m_qtWindow) { From 6ba6d9967a436c427f7de709eae5e97d3dfa5da6 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Tue, 29 Jan 2019 17:11:09 +0000 Subject: [PATCH 08/21] Change Qt wxDropTarget to remember widget and disconnect self when necessary. Clean up naming a bit. --- include/wx/qt/dnd.h | 4 ++-- src/qt/dnd.cpp | 43 ++++++++++++++++++++++++++++++++++++------- src/qt/window.cpp | 4 ++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index d7e108cbdd..0c0b954692 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -22,8 +22,8 @@ public: wxDataFormat GetMatchingPair(); - void ConnectToQWidget(QWidget* widget); - void DisconnectFromQWidget(QWidget* widget); + void ConnectTo(QWidget* widget); + void Disconnect(); private: class Impl; diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 4debdf400a..2f3d7d9d03 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -106,10 +106,40 @@ namespace class wxDropTarget::Impl : public QObject { public: - explicit Impl(wxDropTarget* dropTarget) : m_dropTarget(dropTarget) + explicit Impl(wxDropTarget* dropTarget) + : m_dropTarget(dropTarget), + m_widget(NULL) { } + ~Impl() + { + Disconnect(); + } + + void ConnectTo(QWidget* widget) + { + Disconnect(); + + m_widget = widget; + + if ( m_widget != NULL ) + { + m_widget->setAcceptDrops(true); + m_widget->installEventFilter(this); + } + } + + void Disconnect() + { + if ( m_widget != NULL ) + { + m_widget->setAcceptDrops(false); + m_widget->removeEventFilter(this); + m_widget = NULL; + } + } + virtual bool eventFilter(QObject* watched, QEvent* event) wxOVERRIDE { if ( m_dropTarget != NULL ) @@ -191,6 +221,7 @@ public: const QMimeData* m_pendingMimeData; wxDropTarget* m_dropTarget; + QWidget* m_widget; }; wxDropTarget::wxDropTarget(wxDataObject *dataObject) @@ -246,16 +277,14 @@ wxDataFormat wxDropTarget::GetMatchingPair() return wxFormatInvalid; } -void wxDropTarget::ConnectToQWidget(QWidget* widget) +void wxDropTarget::ConnectTo(QWidget* widget) { - widget->setAcceptDrops(true); - widget->installEventFilter(m_pImpl); + m_pImpl->ConnectTo(widget); } -void wxDropTarget::DisconnectFromQWidget(QWidget* widget) +void wxDropTarget::Disconnect() { - widget->setAcceptDrops(false); - widget->removeEventFilter(m_pImpl); + m_pImpl->Disconnect(); } //############################################################################## diff --git a/src/qt/window.cpp b/src/qt/window.cpp index 1788afd646..dba441cb39 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -698,14 +698,14 @@ void wxWindowQt::SetDropTarget( wxDropTarget *dropTarget ) if ( m_dropTarget != NULL ) { - m_dropTarget->DisconnectFromQWidget(m_qtWindow); + m_dropTarget->Disconnect(); } m_dropTarget = dropTarget; if ( m_dropTarget != NULL ) { - m_dropTarget->ConnectToQWidget(m_qtWindow); + m_dropTarget->ConnectTo(m_qtWindow); } } #endif From fbc39bac546b8b33cd9e3331dfae356e8eb7d609 Mon Sep 17 00:00:00 2001 From: Graham Dawes Date: Wed, 30 Jan 2019 09:12:08 +0000 Subject: [PATCH 09/21] Fix Linux build --- src/qt/dnd.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 2f3d7d9d03..17c81fdfad 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -12,6 +12,7 @@ #include "wx/dnd.h" #include "wx/scopedarray.h" +#include "wx/window.h" #include "wx/qt/private/converter.h" From c3b8b81da572558e4feef04d0c19568165f87786 Mon Sep 17 00:00:00 2001 From: Graham Dawes Date: Wed, 30 Jan 2019 09:27:35 +0000 Subject: [PATCH 10/21] Remove unused includes from qt/window.h --- include/wx/qt/window.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/wx/qt/window.h b/include/wx/qt/window.h index 3978615ae6..6f5a2a87e1 100644 --- a/include/wx/qt/window.h +++ b/include/wx/qt/window.h @@ -9,9 +9,6 @@ #ifndef _WX_QT_WINDOW_H_ #define _WX_QT_WINDOW_H_ -#include -#include - class QShortcut; template < class T > class QList; From abdebb189510cd9cf2b27ce5c0baa89a8690cb4c Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 10:42:01 +0000 Subject: [PATCH 11/21] 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])); From 6fa65900f954fe3e8ae828178a51ae5905262dca Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 14:22:01 +0000 Subject: [PATCH 12/21] Implement cursor overriding for DnD on wxQt. This uses the cursor variant (as opposed to the icon one) since these actually are cursors, the base class looks like cursors are meant to be used, and the icon thing looks like a hack. --- include/wx/qt/dnd.h | 14 +++++++------- src/qt/dnd.cpp | 28 ++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index 0c0b954692..4f5c2a4cbd 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -8,7 +8,7 @@ #ifndef _WX_QT_DND_H_ #define _WX_QT_DND_H_ -#define wxDROP_ICON(name) wxICON(name) +#define wxDROP_ICON(name) wxCursor(name##_xpm) class WXDLLIMPEXP_CORE wxDropTarget : public wxDropTargetBase { @@ -34,15 +34,15 @@ class WXDLLIMPEXP_CORE wxDropSource: public wxDropSourceBase { public: wxDropSource( wxWindow *win = NULL, - const wxIcon © = wxNullIcon, - const wxIcon &move = wxNullIcon, - const wxIcon &none = wxNullIcon); + const wxCursor © = wxNullCursor, + const wxCursor &move = wxNullCursor, + const wxCursor &none = wxNullCursor); wxDropSource( wxDataObject& data, wxWindow *win, - const wxIcon © = wxNullIcon, - const wxIcon &move = wxNullIcon, - const wxIcon &none = wxNullIcon); + const wxCursor © = wxNullCursor, + const wxCursor &move = wxNullCursor, + const wxCursor &none = wxNullCursor); virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly); diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 58d20d53c5..7d42adeea4 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -81,6 +81,12 @@ namespace return mimeData; } + + void SetDragCursor(QDrag& drag, const wxCursor& cursor, Qt::DropAction action) + { + if ( cursor.IsOk() ) + drag.setDragCursor(cursor.GetHandle().pixmap(), action); + } } namespace @@ -310,19 +316,21 @@ void wxDropTarget::Disconnect() //############################################################################## wxDropSource::wxDropSource(wxWindow *win, - const wxIcon &WXUNUSED(copy), - const wxIcon &WXUNUSED(move), - const wxIcon &WXUNUSED(none)) - : m_parentWindow(win) + const wxCursor ©, + const wxCursor &move, + const wxCursor &none) + : wxDropSourceBase(copy, move, none), + m_parentWindow(win) { } wxDropSource::wxDropSource(wxDataObject& data, wxWindow *win, - const wxIcon &WXUNUSED(copy), - const wxIcon &WXUNUSED(move), - const wxIcon &WXUNUSED(none)) - : m_parentWindow(win) + const wxCursor ©, + const wxCursor &move, + const wxCursor &none) + : wxDropSourceBase(copy, move, none), + m_parentWindow(win) { SetData(data); } @@ -335,6 +343,10 @@ wxDragResult wxDropSource::DoDragDrop(int flags /*=wxDrag_CopyOnly*/) QDrag drag(m_parentWindow->GetHandle()); drag.setMimeData(CreateMimeData(m_data)); + SetDragCursor(drag, m_cursorCopy, Qt::CopyAction); + SetDragCursor(drag, m_cursorMove, Qt::MoveAction); + SetDragCursor(drag, m_cursorStop, Qt::IgnoreAction); + Qt::DropActions actions = Qt::CopyAction | Qt::MoveAction; Qt::DropAction defaultAction = Qt::CopyAction; switch ( flags ) From df38836862c28c9912dc41ec3cf6f8cc70f3b0f3 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 14:36:06 +0000 Subject: [PATCH 13/21] Fixed some spacing. --- include/wx/qt/dnd.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index 4f5c2a4cbd..e1ef68473b 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -33,16 +33,16 @@ private: class WXDLLIMPEXP_CORE wxDropSource: public wxDropSourceBase { public: - wxDropSource( wxWindow *win = NULL, - const wxCursor © = wxNullCursor, - const wxCursor &move = wxNullCursor, - const wxCursor &none = wxNullCursor); + wxDropSource(wxWindow *win = NULL, + const wxCursor © = wxNullCursor, + const wxCursor &move = wxNullCursor, + const wxCursor &none = wxNullCursor); - wxDropSource( wxDataObject& data, - wxWindow *win, - const wxCursor © = wxNullCursor, - const wxCursor &move = wxNullCursor, - const wxCursor &none = wxNullCursor); + wxDropSource(wxDataObject& data, + wxWindow *win, + const wxCursor © = wxNullCursor, + const wxCursor &move = wxNullCursor, + const wxCursor &none = wxNullCursor); virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly); From ece6626a59a491350fe6f38fbbde349eae5b42be Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:03:05 +0000 Subject: [PATCH 14/21] Switch wxQt to use UTF-8 for text data transfer via wxDataObject. The result otherwise was less than useful. --- include/wx/dataobj.h | 2 +- src/qt/dataobj.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/wx/dataobj.h b/include/wx/dataobj.h index 348ad20286..885a75d5a9 100644 --- a/include/wx/dataobj.h +++ b/include/wx/dataobj.h @@ -322,7 +322,7 @@ private: // ---------------------------------------------------------------------------- #if wxUSE_UNICODE - #if defined(__WXGTK20__) || defined(__WXX11__) + #if defined(__WXGTK20__) || defined(__WXX11__) || defined(__WXQT__) #define wxNEEDS_UTF8_FOR_TEXT_DATAOBJ #elif defined(__WXMAC__) #define wxNEEDS_UTF16_FOR_TEXT_DATAOBJ diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index e6f8f6e1be..463677d497 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -157,6 +157,20 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &WXUNUSED(bitmap) ) { } +//############################################################################# +// ---------------------------------------------------------------------------- +// wxTextDataObject +// ---------------------------------------------------------------------------- + +#if wxUSE_UNICODE +void wxTextDataObject::GetAllFormats(wxDataFormat *formats, + wxDataObjectBase::Direction WXUNUSED(dir)) const +{ + formats[0] = wxDataFormat(wxDF_UNICODETEXT); + formats[1] = wxDataFormat(wxDF_TEXT); +} +#endif + //############################################################################# wxFileDataObject::wxFileDataObject() From 32780f00f2e62ff92ebdbc0be93a174b194fd4a5 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:11:46 +0000 Subject: [PATCH 15/21] Combine two separate anonymous namespaces into one. --- src/qt/dnd.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 7d42adeea4..2552b99074 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -87,10 +87,7 @@ namespace if ( cursor.IsOk() ) drag.setDragCursor(cursor.GetHandle().pixmap(), action); } -} -namespace -{ class PendingMimeDataSetter { public: From 4844c80e1831a82cee5c105955677deb4c5073fa Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:19:39 +0000 Subject: [PATCH 16/21] Add some consts to const things. --- src/qt/dnd.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 2552b99074..9b799324b8 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -178,7 +178,7 @@ public: { QDragEnterEvent *e = static_cast(event); - PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + const PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); if ( !CanDropHere() ) { @@ -189,7 +189,7 @@ public: event->accept(); const QPoint where = e->pos(); - wxDragResult result = m_dropTarget->OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + const wxDragResult result = m_dropTarget->OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); e->setDropAction(DragResultToDropAction(result)); } @@ -206,10 +206,10 @@ public: QDragMoveEvent *e = static_cast(event); - PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + const PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); const QPoint where = e->pos(); - wxDragResult result = m_dropTarget->OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + const wxDragResult result = m_dropTarget->OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); e->setDropAction(DragResultToDropAction(result)); } @@ -220,7 +220,7 @@ public: const QDropEvent *e = static_cast(event); - PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); + const PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); const QPoint where = e->pos(); if ( m_dropTarget->OnDrop(where.x(), where.y()) ) From 76eee80738397fdf8c7baaf9a609e49174fa9b5d Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:21:43 +0000 Subject: [PATCH 17/21] Initialize m_pendingMimeData to NULL! --- src/qt/dnd.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 9b799324b8..eddc667d52 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -112,7 +112,8 @@ class wxDropTarget::Impl : public QObject public: explicit Impl(wxDropTarget* dropTarget) : m_dropTarget(dropTarget), - m_widget(NULL) + m_widget(NULL), + m_pendingMimeData(NULL) { } @@ -241,9 +242,9 @@ private: return !m_dropTarget->GetMatchingPair().GetMimeType().empty(); } - const QMimeData* m_pendingMimeData; wxDropTarget* m_dropTarget; QWidget* m_widget; + const QMimeData* m_pendingMimeData; }; wxDropTarget::wxDropTarget(wxDataObject *dataObject) From 58639481c606f95fda8031714ec80b4982460167 Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:25:51 +0000 Subject: [PATCH 18/21] Make OnData behave like other implementations in terms of paying attention to what GetData returns. --- src/qt/dnd.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index eddc667d52..83893db06c 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -265,8 +265,7 @@ bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) { - GetData(); - return def; + return GetData() ? def : wxDragNone; } bool wxDropTarget::GetData() From 653979936bebf36d8faebc01d01a1ce5473be99e Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 15:59:37 +0000 Subject: [PATCH 19/21] Clean up the code a bit to follow the guidelines, including handling of default in switches and line length. --- include/wx/qt/dnd.h | 4 +++- src/qt/dataobj.cpp | 9 +++---- src/qt/dnd.cpp | 58 +++++++++++++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/include/wx/qt/dnd.h b/include/wx/qt/dnd.h index e1ef68473b..6644d9325b 100644 --- a/include/wx/qt/dnd.h +++ b/include/wx/qt/dnd.h @@ -17,7 +17,9 @@ public: virtual ~wxDropTarget(); virtual bool OnDrop(wxCoord x, wxCoord y) wxOVERRIDE; - virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) wxOVERRIDE; + virtual wxDragResult OnData(wxCoord x, + wxCoord y, + wxDragResult def) wxOVERRIDE; virtual bool GetData() wxOVERRIDE; wxDataFormat GetMatchingPair(); diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index 463677d497..b0b3b09ade 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -117,7 +117,7 @@ bool wxDataFormat::operator!=(const wxDataFormat& format) const return !operator==(format); } -//############################################################################# +//############################################################################ wxDataObject::wxDataObject() { @@ -127,7 +127,8 @@ wxDataObject::~wxDataObject() { } -bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) const +bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, + Direction dir) const { const size_t formatCount = GetFormatCount(dir); if ( formatCount == 1 ) @@ -147,7 +148,7 @@ bool wxDataObject::IsSupportedFormat(const wxDataFormat& format, Direction dir) return false; } -//############################################################################# +//############################################################################ wxBitmapDataObject::wxBitmapDataObject() { @@ -160,7 +161,7 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &WXUNUSED(bitmap) ) //############################################################################# // ---------------------------------------------------------------------------- // wxTextDataObject -// ---------------------------------------------------------------------------- +// --------------------------------------------------------------------------- #if wxUSE_UNICODE void wxTextDataObject::GetAllFormats(wxDataFormat *formats, diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 83893db06c..2b4794e2e9 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -32,12 +32,14 @@ namespace case Qt::CopyAction: return wxDragCopy; case Qt::MoveAction: + case Qt::TargetMoveAction: return wxDragMove; case Qt::LinkAction: return wxDragLink; - default: - return wxDragNone; } + + wxFAIL_MSG("Illegal drop action"); + return wxDragNone; } Qt::DropAction DragResultToDropAction(wxDragResult result) @@ -50,12 +52,19 @@ namespace return Qt::MoveAction; case wxDragLink: return Qt::LinkAction; - default: + case wxDragError: + case wxDragNone: + case wxDragCancel: return Qt::IgnoreAction; } + + wxFAIL_MSG("Illegal drag result"); + return Qt::IgnoreAction; } - void AddDataFormat(wxDataObject* dataObject, QMimeData* mimeData, const wxDataFormat& format) + void AddDataFormat(wxDataObject* dataObject, + QMimeData* mimeData, + const wxDataFormat& format) { const size_t data_size = dataObject->GetDataSize(format); @@ -82,7 +91,9 @@ namespace return mimeData; } - void SetDragCursor(QDrag& drag, const wxCursor& cursor, Qt::DropAction action) + void SetDragCursor(QDrag& drag, + const wxCursor& cursor, + Qt::DropAction action) { if ( cursor.IsOk() ) drag.setDragCursor(cursor.GetHandle().pixmap(), action); @@ -91,7 +102,8 @@ namespace class PendingMimeDataSetter { public: - PendingMimeDataSetter(const QMimeData*& targetMimeData, const QMimeData* mimeData) + PendingMimeDataSetter(const QMimeData*& targetMimeData, + const QMimeData* mimeData) : m_targetMimeData(targetMimeData) { m_targetMimeData = mimeData; @@ -190,7 +202,11 @@ public: event->accept(); const QPoint where = e->pos(); - const wxDragResult result = m_dropTarget->OnEnter(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + const wxDragResult proposedResult = + DropActionToDragResult(e->proposedAction()); + const wxDragResult result = m_dropTarget->OnEnter(where.x(), + where.y(), + proposedResult); e->setDropAction(DragResultToDropAction(result)); } @@ -210,7 +226,11 @@ public: const PendingMimeDataSetter setter(m_pendingMimeData, e->mimeData()); const QPoint where = e->pos(); - const wxDragResult result = m_dropTarget->OnDragOver(where.x(), where.y(), DropActionToDragResult(e->proposedAction())); + const wxDragResult proposedResult = + DropActionToDragResult(e->proposedAction()); + const wxDragResult result = m_dropTarget->OnDragOver(where.x(), + where.y(), + proposedResult); e->setDropAction(DragResultToDropAction(result)); } @@ -226,7 +246,9 @@ public: const QPoint where = e->pos(); if ( m_dropTarget->OnDrop(where.x(), where.y()) ) { - m_dropTarget->OnData(where.x(), where.y(), DropActionToDragResult(e->dropAction())); + m_dropTarget->OnData(where.x(), + where.y(), + DropActionToDragResult(e->dropAction())); } } @@ -263,7 +285,9 @@ bool wxDropTarget::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y)) return true; } -wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def) +wxDragResult wxDropTarget::OnData(wxCoord WXUNUSED(x), + wxCoord WXUNUSED(y), + wxDragResult def) { return GetData() ? def : wxDragNone; } @@ -272,11 +296,12 @@ bool wxDropTarget::GetData() { const wxDataFormat droppedFormat = GetMatchingPair(); - const wxString mimeType = droppedFormat.GetMimeType(); + const wxString& mimeType = droppedFormat.GetMimeType(); if ( mimeType.empty() ) return false; - const QByteArray data = m_pImpl->GetMimeData()->data(wxQtConvertString(mimeType)); + const QString qMimeType = wxQtConvertString(mimeType); + const QByteArray data = m_pImpl->GetMimeData()->data(qMimeType); return m_dataObject->SetData(droppedFormat, data.size(), data.data()); } @@ -310,7 +335,7 @@ void wxDropTarget::Disconnect() m_pImpl->Disconnect(); } -//############################################################################## +//########################################################################### wxDropSource::wxDropSource(wxWindow *win, const wxCursor ©, @@ -334,8 +359,11 @@ wxDropSource::wxDropSource(wxDataObject& data, wxDragResult wxDropSource::DoDragDrop(int flags /*=wxDrag_CopyOnly*/) { - wxCHECK_MSG(m_data != NULL, wxDragNone, wxT("No data in wxDropSource!")); - wxCHECK_MSG(m_parentWindow != NULL, wxDragNone, wxT("NULL parent window in wxDropSource!")); + 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)); From 111a37fd737560c0894fabdc89bd83cf37ab455c Mon Sep 17 00:00:00 2001 From: Jay Nabonne Date: Wed, 30 Jan 2019 16:09:18 +0000 Subject: [PATCH 20/21] Get rid of another switch/default issue by avoiding switch altogther. Makes the code more readable (I think) actually. --- src/qt/dnd.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 2b4794e2e9..8186a3d951 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -372,19 +372,15 @@ wxDragResult wxDropSource::DoDragDrop(int flags /*=wxDrag_CopyOnly*/) SetDragCursor(drag, m_cursorMove, Qt::MoveAction); SetDragCursor(drag, m_cursorStop, Qt::IgnoreAction); - 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; - } + const Qt::DropActions actions = + flags == wxDrag_CopyOnly + ? Qt::CopyAction + : Qt::CopyAction | Qt::MoveAction; + + const Qt::DropAction defaultAction = + flags == wxDrag_DefaultMove + ? Qt::MoveAction + : Qt::CopyAction; return DropActionToDragResult(drag.exec(actions, defaultAction)); } From 0145916fb6a60f36ae6f8a66edacee4970c938d8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 2 Feb 2019 15:50:13 +0100 Subject: [PATCH 21/21] Unindent contents of anonymous namespaces No real changes, only formatting. --- src/qt/dataobj.cpp | 54 ++++++------- src/qt/dnd.cpp | 184 +++++++++++++++++++++++---------------------- 2 files changed, 121 insertions(+), 117 deletions(-) diff --git a/src/qt/dataobj.cpp b/src/qt/dataobj.cpp index b0b3b09ade..82d8cb88e9 100644 --- a/src/qt/dataobj.cpp +++ b/src/qt/dataobj.cpp @@ -17,36 +17,38 @@ namespace { - wxString DataFormatIdToMimeType(wxDataFormatId formatId) + +wxString DataFormatIdToMimeType(wxDataFormatId formatId) +{ + switch ( formatId ) { - switch ( formatId ) - { - case wxDF_TEXT: return "text/plain"; - case wxDF_BITMAP: return "image/bmp"; - case wxDF_TIFF: return "image/tiff"; - case wxDF_WAVE: return "audio/x-wav"; - case wxDF_UNICODETEXT: return "text/plain"; - case wxDF_HTML: return "text/html"; - case wxDF_METAFILE: - case wxDF_SYLK: - case wxDF_DIF: - case wxDF_OEMTEXT: - case wxDF_DIB: - case wxDF_PALETTE: - case wxDF_PENDATA: - case wxDF_RIFF: - case wxDF_ENHMETAFILE: - case wxDF_FILENAME: - case wxDF_LOCALE: - case wxDF_PRIVATE: - case wxDF_INVALID: - case wxDF_MAX: - default: - return ""; - } + case wxDF_TEXT: return "text/plain"; + case wxDF_BITMAP: return "image/bmp"; + case wxDF_TIFF: return "image/tiff"; + case wxDF_WAVE: return "audio/x-wav"; + case wxDF_UNICODETEXT: return "text/plain"; + case wxDF_HTML: return "text/html"; + case wxDF_METAFILE: + case wxDF_SYLK: + case wxDF_DIF: + case wxDF_OEMTEXT: + case wxDF_DIB: + case wxDF_PALETTE: + case wxDF_PENDATA: + case wxDF_RIFF: + case wxDF_ENHMETAFILE: + case wxDF_FILENAME: + case wxDF_LOCALE: + case wxDF_PRIVATE: + case wxDF_INVALID: + case wxDF_MAX: + default: + return ""; } } +} // anonymous namespace + wxDataFormat::wxDataFormat(wxDataFormatId formatId) { SetType(formatId); diff --git a/src/qt/dnd.cpp b/src/qt/dnd.cpp index 8186a3d951..a5d3f5d962 100644 --- a/src/qt/dnd.cpp +++ b/src/qt/dnd.cpp @@ -23,102 +23,104 @@ namespace { - wxDragResult DropActionToDragResult(Qt::DropAction action) - { - switch ( action ) - { - case Qt::IgnoreAction: - return wxDragCancel; - case Qt::CopyAction: - return wxDragCopy; - case Qt::MoveAction: - case Qt::TargetMoveAction: - return wxDragMove; - case Qt::LinkAction: - return wxDragLink; - } - wxFAIL_MSG("Illegal drop action"); - return wxDragNone; +wxDragResult DropActionToDragResult(Qt::DropAction action) +{ + switch ( action ) + { + case Qt::IgnoreAction: + return wxDragCancel; + case Qt::CopyAction: + return wxDragCopy; + case Qt::MoveAction: + case Qt::TargetMoveAction: + return wxDragMove; + case Qt::LinkAction: + return wxDragLink; } - Qt::DropAction DragResultToDropAction(wxDragResult result) - { - switch ( result ) - { - case wxDragCopy: - return Qt::CopyAction; - case wxDragMove: - return Qt::MoveAction; - case wxDragLink: - return Qt::LinkAction; - case wxDragError: - case wxDragNone: - case wxDragCancel: - return Qt::IgnoreAction; - } - - wxFAIL_MSG("Illegal drag result"); - return Qt::IgnoreAction; - } - - void AddDataFormat(wxDataObject* dataObject, - QMimeData* mimeData, - const wxDataFormat& format) - { - const size_t data_size = dataObject->GetDataSize(format); - - QByteArray data(static_cast(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 array(dataObject->GetFormatCount()); - dataObject->GetAllFormats(array.get()); - - for ( size_t i = 0; i < count; i++ ) - { - AddDataFormat(dataObject, mimeData, array[i]); - } - - return mimeData; - } - - void SetDragCursor(QDrag& drag, - const wxCursor& cursor, - Qt::DropAction action) - { - if ( cursor.IsOk() ) - drag.setDragCursor(cursor.GetHandle().pixmap(), action); - } - - class PendingMimeDataSetter - { - public: - PendingMimeDataSetter(const QMimeData*& targetMimeData, - const QMimeData* mimeData) - : m_targetMimeData(targetMimeData) - { - m_targetMimeData = mimeData; - } - - ~PendingMimeDataSetter() - { - m_targetMimeData = NULL; - } - - private: - const QMimeData*& m_targetMimeData; - }; + wxFAIL_MSG("Illegal drop action"); + return wxDragNone; } +Qt::DropAction DragResultToDropAction(wxDragResult result) +{ + switch ( result ) + { + case wxDragCopy: + return Qt::CopyAction; + case wxDragMove: + return Qt::MoveAction; + case wxDragLink: + return Qt::LinkAction; + case wxDragError: + case wxDragNone: + case wxDragCancel: + return Qt::IgnoreAction; + } + + wxFAIL_MSG("Illegal drag result"); + return Qt::IgnoreAction; +} + +void AddDataFormat(wxDataObject* dataObject, + QMimeData* mimeData, + const wxDataFormat& format) +{ + const size_t data_size = dataObject->GetDataSize(format); + + QByteArray data(static_cast(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 array(dataObject->GetFormatCount()); + dataObject->GetAllFormats(array.get()); + + for ( size_t i = 0; i < count; i++ ) + { + AddDataFormat(dataObject, mimeData, array[i]); + } + + return mimeData; +} + +void SetDragCursor(QDrag& drag, + const wxCursor& cursor, + Qt::DropAction action) +{ + if ( cursor.IsOk() ) + drag.setDragCursor(cursor.GetHandle().pixmap(), action); +} + +class PendingMimeDataSetter +{ +public: + PendingMimeDataSetter(const QMimeData*& targetMimeData, + const QMimeData* mimeData) + : m_targetMimeData(targetMimeData) + { + m_targetMimeData = mimeData; + } + + ~PendingMimeDataSetter() + { + m_targetMimeData = NULL; + } + +private: + const QMimeData*& m_targetMimeData; +}; + +} // anonymous namespace + class wxDropTarget::Impl : public QObject { public: