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
|
||||
|
||||
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;
|
||||
|
||||
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,15 @@ public:
|
||||
virtual ~wxDataObject();
|
||||
|
||||
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_
|
||||
|
@@ -14,6 +14,11 @@ public:
|
||||
wxBitmapDataObject();
|
||||
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:
|
||||
|
||||
private:
|
||||
|
@@ -14,14 +14,13 @@
|
||||
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtGui/QClipboard>
|
||||
#include <QtCore/QMimeData>
|
||||
|
||||
#include "wx/clipbrd.h"
|
||||
#include "wx/scopedarray.h"
|
||||
#include "wx/scopeguard.h"
|
||||
#include "wx/qt/private/converter.h"
|
||||
|
||||
#include <QtCore/QMimeData>
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxClipboard ctor/dtor
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -90,28 +89,7 @@ bool wxClipboard::IsOpened() const
|
||||
bool wxClipboard::AddData( wxDataObject *data )
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
data->QtAddDataTo(*MimeData);
|
||||
delete data;
|
||||
|
||||
QtClipboard->setMimeData(MimeData, (QClipboard::Mode)Mode());
|
||||
@@ -135,31 +113,7 @@ bool wxClipboard::GetData( wxDataObject& data )
|
||||
wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
|
||||
|
||||
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();
|
||||
data.SetData(format, bytearray.size(), bytearray.constData());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return data.QtSetDataFrom(*MimeData);
|
||||
}
|
||||
|
||||
void wxClipboard::Clear()
|
||||
@@ -170,6 +124,10 @@ void wxClipboard::Clear()
|
||||
bool wxClipboard::IsSupported( const wxDataFormat& format )
|
||||
{
|
||||
const QMimeData *data = QtClipboard->mimeData( (QClipboard::Mode)Mode() );
|
||||
if (format.GetType() == wxDF_BITMAP)
|
||||
{
|
||||
return data->hasImage();
|
||||
}
|
||||
return data->hasFormat(wxQtConvertString(format.GetMimeType()));
|
||||
}
|
||||
|
||||
|
@@ -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,16 +156,83 @@ bool wxDataObject::IsSupportedFormat(const wxDataFormat& format,
|
||||
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( 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
|
||||
@@ -174,6 +247,11 @@ void wxTextDataObject::GetAllFormats(wxDataFormat *formats,
|
||||
}
|
||||
#endif
|
||||
|
||||
void wxTextDataObject::QtSetDataSingleFormat(const QMimeData &mimeData, const wxDataFormat &WXUNUSED(format))
|
||||
{
|
||||
SetText(wxQtConvertString(mimeData.text()));
|
||||
}
|
||||
|
||||
//#############################################################################
|
||||
|
||||
wxFileDataObject::wxFileDataObject()
|
||||
|
Reference in New Issue
Block a user