Refactor to do custom qt clipboard actions without dynamic cast
This commit is contained in:
@@ -427,6 +427,10 @@ public:
|
||||
#endif // different wxTextDataObject implementations
|
||||
|
||||
private:
|
||||
#if defined(__WXQT__)
|
||||
void DoSetDataFrom(const class QMimeData &mimeData, const wxDataFormat &format) wxOVERRIDE;
|
||||
#endif
|
||||
|
||||
wxString m_text;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxTextDataObject);
|
||||
|
@@ -12,6 +12,8 @@
|
||||
// wxDataObject is the same as wxDataObjectBase under wxQT
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class QMimeData;
|
||||
|
||||
class WXDLLIMPEXP_CORE wxDataObject : public wxDataObjectBase
|
||||
{
|
||||
public:
|
||||
@@ -19,6 +21,11 @@ public:
|
||||
virtual ~wxDataObject();
|
||||
|
||||
virtual bool IsSupportedFormat( const wxDataFormat& format, Direction dir = Get ) const;
|
||||
virtual void AddDataTo(QMimeData &mimeData) const;
|
||||
virtual bool SetDataFrom(const QMimeData &mimeData);
|
||||
|
||||
private:
|
||||
virtual void DoSetDataFrom(const QMimeData &mimeData, const wxDataFormat &format);
|
||||
};
|
||||
|
||||
#endif // _WX_QT_DATAOBJ_H_
|
||||
|
@@ -14,6 +14,9 @@ public:
|
||||
wxBitmapDataObject();
|
||||
wxBitmapDataObject(const wxBitmap& bitmap);
|
||||
|
||||
void AddDataTo(QMimeData &mimeData) const wxOVERRIDE;
|
||||
bool SetDataFrom(const QMimeData &mimeData) wxOVERRIDE;
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
@@ -15,7 +15,6 @@
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtGui/QClipboard>
|
||||
#include <QtCore/QMimeData>
|
||||
#include <QPixmap>
|
||||
|
||||
#include "wx/clipbrd.h"
|
||||
#include "wx/scopedarray.h"
|
||||
@@ -89,37 +88,8 @@ bool wxClipboard::IsOpened() const
|
||||
|
||||
bool wxClipboard::AddData( wxDataObject *data )
|
||||
{
|
||||
wxBitmapDataObjectBase* bitmap = dynamic_cast<wxBitmapDataObjectBase*>(data);
|
||||
if (bitmap != NULL)
|
||||
{
|
||||
QtClipboard->setPixmap(*bitmap->GetBitmap().GetHandle());
|
||||
delete data;
|
||||
return true;
|
||||
}
|
||||
|
||||
QMimeData *MimeData = new QMimeData;
|
||||
const size_t count = data->GetFormatCount();
|
||||
wxDataFormatArray formats(count);
|
||||
data->GetAllFormats(formats.get());
|
||||
|
||||
// how to add timestamp?
|
||||
|
||||
// Unfortunately I cannot find a way to use the qt clipboard with
|
||||
// a callback to select the data type, so I must copy it all here
|
||||
|
||||
for ( size_t i = 0; i < count; i++ )
|
||||
{
|
||||
const wxDataFormat format(formats[i]);
|
||||
|
||||
int size = data->GetDataSize( format );
|
||||
if ( !size )
|
||||
continue;
|
||||
|
||||
QByteArray bytearray(size, 0);
|
||||
data->GetDataHere(format, bytearray.data());
|
||||
MimeData->setData(wxQtConvertString(format.GetMimeType()), bytearray);
|
||||
}
|
||||
|
||||
QMimeData *MimeData = new QMimeData;
|
||||
data->AddDataTo(*MimeData);
|
||||
delete data;
|
||||
|
||||
QtClipboard->setMimeData(MimeData, (QClipboard::Mode)Mode());
|
||||
@@ -142,43 +112,8 @@ bool wxClipboard::GetData( wxDataObject& data )
|
||||
{
|
||||
wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
|
||||
|
||||
wxBitmapDataObjectBase* bitmap = dynamic_cast<wxBitmapDataObjectBase*>(&data);
|
||||
if (bitmap != NULL)
|
||||
{
|
||||
QPixmap pix = QtClipboard->pixmap();
|
||||
if (pix.isNull())
|
||||
return false;
|
||||
|
||||
bitmap->SetBitmap(wxBitmap(pix));
|
||||
return true;
|
||||
}
|
||||
|
||||
const QMimeData *MimeData = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
||||
const size_t count = data.GetFormatCount(wxDataObject::Set);
|
||||
wxDataFormatArray formats(count);
|
||||
data.GetAllFormats(formats.get(), wxDataObject::Set);
|
||||
|
||||
for ( size_t i = 0; i < count; i++ )
|
||||
{
|
||||
const wxDataFormat format(formats[i]);
|
||||
|
||||
// is this format supported by clipboard ?
|
||||
if( !MimeData->hasFormat(wxQtConvertString(format.GetMimeType())) )
|
||||
continue;
|
||||
|
||||
wxTextDataObject *textdata = dynamic_cast<wxTextDataObject*>(&data);
|
||||
if ( textdata )
|
||||
textdata->SetText(wxQtConvertString(MimeData->text()));
|
||||
else
|
||||
{
|
||||
QByteArray bytearray = MimeData->data( wxQtConvertString(format.GetMimeType()) );
|
||||
data.SetData(format, bytearray.size(), bytearray.constData());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return data.SetDataFrom(*MimeData);
|
||||
}
|
||||
|
||||
void wxClipboard::Clear()
|
||||
|
@@ -12,8 +12,14 @@
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#include <QtCore/QMimeData>
|
||||
#include <QtGui/QPixmap>
|
||||
|
||||
#include "wx/dataobj.h"
|
||||
#include "wx/scopedarray.h"
|
||||
#include "wx/qt/private/converter.h"
|
||||
|
||||
typedef wxScopedArray<wxDataFormat> wxDataFormatArray;
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -150,6 +156,58 @@ bool wxDataObject::IsSupportedFormat(const wxDataFormat& format,
|
||||
return false;
|
||||
}
|
||||
|
||||
void wxDataObject::AddDataTo(QMimeData &mimeData) const
|
||||
{
|
||||
const size_t count = GetFormatCount();
|
||||
wxDataFormatArray formats(count);
|
||||
GetAllFormats(formats.get());
|
||||
|
||||
// how to add timestamp?
|
||||
|
||||
// Unfortunately I cannot find a way to use the qt clipboard with
|
||||
// a callback to select the data type, so I must copy it all here
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
const wxDataFormat format(formats[i]);
|
||||
|
||||
int size = GetDataSize(format);
|
||||
if (!size)
|
||||
continue;
|
||||
|
||||
QByteArray bytearray(size, 0);
|
||||
GetDataHere(format, bytearray.data());
|
||||
mimeData.setData(wxQtConvertString(format.GetMimeType()), bytearray);
|
||||
}
|
||||
}
|
||||
|
||||
bool wxDataObject::SetDataFrom(const QMimeData &mimeData)
|
||||
{
|
||||
const size_t count = GetFormatCount(Set);
|
||||
wxDataFormatArray formats(count);
|
||||
GetAllFormats(formats.get(), Set);
|
||||
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
const wxDataFormat format(formats[i]);
|
||||
|
||||
// is this format supported by clipboard ?
|
||||
if (!mimeData.hasFormat(wxQtConvertString(format.GetMimeType())))
|
||||
continue;
|
||||
|
||||
DoSetDataFrom(mimeData, format);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void wxDataObject::DoSetDataFrom(const QMimeData &mimeData, const wxDataFormat &format)
|
||||
{
|
||||
QByteArray bytearray = mimeData.data(wxQtConvertString(format.GetMimeType()));
|
||||
SetData(format, bytearray.size(), bytearray.constData());
|
||||
}
|
||||
|
||||
//############################################################################
|
||||
|
||||
wxBitmapDataObject::wxBitmapDataObject()
|
||||
@@ -161,6 +219,20 @@ wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &bitmap )
|
||||
{
|
||||
}
|
||||
|
||||
void wxBitmapDataObject::AddDataTo(QMimeData &mimeData) const
|
||||
{
|
||||
mimeData.setImageData(GetBitmap().GetHandle()->toImage());
|
||||
}
|
||||
|
||||
bool wxBitmapDataObject::SetDataFrom(const QMimeData &mimeData)
|
||||
{
|
||||
if (!mimeData.hasImage())
|
||||
return false;
|
||||
|
||||
SetBitmap(wxBitmap(QPixmap::fromImage(qvariant_cast<QImage>(mimeData.imageData()))));
|
||||
return true;
|
||||
}
|
||||
|
||||
//#############################################################################
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxTextDataObject
|
||||
@@ -175,6 +247,11 @@ void wxTextDataObject::GetAllFormats(wxDataFormat *formats,
|
||||
}
|
||||
#endif
|
||||
|
||||
void wxTextDataObject::DoSetDataFrom(const QMimeData &mimeData, const wxDataFormat &WXUNUSED(format))
|
||||
{
|
||||
SetText(wxQtConvertString(mimeData.text()));
|
||||
}
|
||||
|
||||
//#############################################################################
|
||||
|
||||
wxFileDataObject::wxFileDataObject()
|
||||
|
Reference in New Issue
Block a user