Merge branch 'qt_clipboard_bitmap' of https://github.com/GeoTeric/wxWidgets
Implement copying bitmap to clipboard for wxQt. See https://github.com/wxWidgets/wxWidgets/pull/1368
This commit is contained in:
@@ -427,6 +427,11 @@ public:
|
|||||||
#endif // different wxTextDataObject implementations
|
#endif // different wxTextDataObject implementations
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#if defined(__WXQT__)
|
||||||
|
// Overridden to set text directly instead of extracting byte array
|
||||||
|
void QtSetDataSingleFormat(const class QMimeData &mimeData, const wxDataFormat &format) wxOVERRIDE;
|
||||||
|
#endif
|
||||||
|
|
||||||
wxString m_text;
|
wxString m_text;
|
||||||
|
|
||||||
wxDECLARE_NO_COPY_CLASS(wxTextDataObject);
|
wxDECLARE_NO_COPY_CLASS(wxTextDataObject);
|
||||||
|
@@ -12,6 +12,8 @@
|
|||||||
// wxDataObject is the same as wxDataObjectBase under wxQT
|
// wxDataObject is the same as wxDataObjectBase under wxQT
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class QMimeData;
|
||||||
|
|
||||||
class WXDLLIMPEXP_CORE wxDataObject : public wxDataObjectBase
|
class WXDLLIMPEXP_CORE wxDataObject : public wxDataObjectBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -19,6 +21,15 @@ public:
|
|||||||
virtual ~wxDataObject();
|
virtual ~wxDataObject();
|
||||||
|
|
||||||
virtual bool IsSupportedFormat( const wxDataFormat& format, Direction dir = Get ) const;
|
virtual bool IsSupportedFormat( const wxDataFormat& format, Direction dir = Get ) const;
|
||||||
|
|
||||||
|
// Adds object's data to Qt mime data appropriately for type
|
||||||
|
virtual void QtAddDataTo(QMimeData &mimeData) const;
|
||||||
|
// Sets object's data from Qt mime data appropriately for type
|
||||||
|
virtual bool QtSetDataFrom(const QMimeData &mimeData);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Sets object's data from Qt mime data in specific format
|
||||||
|
virtual void QtSetDataSingleFormat(const QMimeData &mimeData, const wxDataFormat &format);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _WX_QT_DATAOBJ_H_
|
#endif // _WX_QT_DATAOBJ_H_
|
||||||
|
@@ -14,6 +14,11 @@ public:
|
|||||||
wxBitmapDataObject();
|
wxBitmapDataObject();
|
||||||
wxBitmapDataObject(const wxBitmap& bitmap);
|
wxBitmapDataObject(const wxBitmap& bitmap);
|
||||||
|
|
||||||
|
// Overridden to set image data directly, which Qt will write to clipboard in many formats
|
||||||
|
void QtAddDataTo(QMimeData &mimeData) const wxOVERRIDE;
|
||||||
|
// Overridden to retrieve image data from any format that Qt can read from clipboard
|
||||||
|
bool QtSetDataFrom(const QMimeData &mimeData) wxOVERRIDE;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@@ -14,14 +14,13 @@
|
|||||||
|
|
||||||
#include <QtWidgets/QApplication>
|
#include <QtWidgets/QApplication>
|
||||||
#include <QtGui/QClipboard>
|
#include <QtGui/QClipboard>
|
||||||
|
#include <QtCore/QMimeData>
|
||||||
|
|
||||||
#include "wx/clipbrd.h"
|
#include "wx/clipbrd.h"
|
||||||
#include "wx/scopedarray.h"
|
#include "wx/scopedarray.h"
|
||||||
#include "wx/scopeguard.h"
|
#include "wx/scopeguard.h"
|
||||||
#include "wx/qt/private/converter.h"
|
#include "wx/qt/private/converter.h"
|
||||||
|
|
||||||
#include <QtCore/QMimeData>
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// wxClipboard ctor/dtor
|
// wxClipboard ctor/dtor
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -90,28 +89,7 @@ bool wxClipboard::IsOpened() const
|
|||||||
bool wxClipboard::AddData( wxDataObject *data )
|
bool wxClipboard::AddData( wxDataObject *data )
|
||||||
{
|
{
|
||||||
QMimeData *MimeData = new QMimeData;
|
QMimeData *MimeData = new QMimeData;
|
||||||
const size_t count = data->GetFormatCount();
|
data->QtAddDataTo(*MimeData);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete data;
|
delete data;
|
||||||
|
|
||||||
QtClipboard->setMimeData(MimeData, (QClipboard::Mode)Mode());
|
QtClipboard->setMimeData(MimeData, (QClipboard::Mode)Mode());
|
||||||
@@ -135,31 +113,7 @@ bool wxClipboard::GetData( wxDataObject& data )
|
|||||||
wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
|
wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
|
||||||
|
|
||||||
const QMimeData *MimeData = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
const QMimeData *MimeData = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
||||||
const size_t count = data.GetFormatCount(wxDataObject::Set);
|
return data.QtSetDataFrom(*MimeData);
|
||||||
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();
|
|
||||||
data.SetData(format, bytearray.size(), bytearray.constData());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxClipboard::Clear()
|
void wxClipboard::Clear()
|
||||||
@@ -170,6 +124,10 @@ void wxClipboard::Clear()
|
|||||||
bool wxClipboard::IsSupported( const wxDataFormat& format )
|
bool wxClipboard::IsSupported( const wxDataFormat& format )
|
||||||
{
|
{
|
||||||
const QMimeData *data = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
const QMimeData *data = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
||||||
|
if (format.GetType() == wxDF_BITMAP)
|
||||||
|
{
|
||||||
|
return data->hasImage();
|
||||||
|
}
|
||||||
return data->hasFormat(wxQtConvertString(format.GetMimeType()));
|
return data->hasFormat(wxQtConvertString(format.GetMimeType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,8 +12,14 @@
|
|||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <QtCore/QMimeData>
|
||||||
|
#include <QtGui/QPixmap>
|
||||||
|
|
||||||
#include "wx/dataobj.h"
|
#include "wx/dataobj.h"
|
||||||
#include "wx/scopedarray.h"
|
#include "wx/scopedarray.h"
|
||||||
|
#include "wx/qt/private/converter.h"
|
||||||
|
|
||||||
|
typedef wxScopedArray<wxDataFormat> wxDataFormatArray;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -150,16 +156,83 @@ bool wxDataObject::IsSupportedFormat(const wxDataFormat& format,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxDataObject::QtAddDataTo(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::QtSetDataFrom(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;
|
||||||
|
|
||||||
|
QtSetDataSingleFormat(mimeData, format);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxDataObject::QtSetDataSingleFormat(const QMimeData &mimeData, const wxDataFormat &format)
|
||||||
|
{
|
||||||
|
QByteArray bytearray = mimeData.data(wxQtConvertString(format.GetMimeType()));
|
||||||
|
SetData(format, bytearray.size(), bytearray.constData());
|
||||||
|
}
|
||||||
|
|
||||||
//############################################################################
|
//############################################################################
|
||||||
|
|
||||||
wxBitmapDataObject::wxBitmapDataObject()
|
wxBitmapDataObject::wxBitmapDataObject()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &WXUNUSED(bitmap) )
|
wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &bitmap )
|
||||||
|
: wxBitmapDataObjectBase( bitmap )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxBitmapDataObject::QtAddDataTo(QMimeData &mimeData) const
|
||||||
|
{
|
||||||
|
mimeData.setImageData(GetBitmap().GetHandle()->toImage());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxBitmapDataObject::QtSetDataFrom(const QMimeData &mimeData)
|
||||||
|
{
|
||||||
|
if (!mimeData.hasImage())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
SetBitmap(wxBitmap(QPixmap::fromImage(qvariant_cast<QImage>(mimeData.imageData()))));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
//#############################################################################
|
//#############################################################################
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// wxTextDataObject
|
// wxTextDataObject
|
||||||
@@ -174,6 +247,11 @@ void wxTextDataObject::GetAllFormats(wxDataFormat *formats,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void wxTextDataObject::QtSetDataSingleFormat(const QMimeData &mimeData, const wxDataFormat &WXUNUSED(format))
|
||||||
|
{
|
||||||
|
SetText(wxQtConvertString(mimeData.text()));
|
||||||
|
}
|
||||||
|
|
||||||
//#############################################################################
|
//#############################################################################
|
||||||
|
|
||||||
wxFileDataObject::wxFileDataObject()
|
wxFileDataObject::wxFileDataObject()
|
||||||
|
Reference in New Issue
Block a user