Use QtClipboard image functions instead of setting mime type

This commit is contained in:
Matthew Griffin 2019-06-25 11:21:37 +01:00
parent 73c51b1250
commit aa786e813b
3 changed files with 27 additions and 62 deletions

View File

@ -8,28 +8,15 @@
#ifndef _WX_QT_DATAOBJ2_H_
#define _WX_QT_DATAOBJ2_H_
#include <wx/scopedptr.h>
class QByteArray;
class WXDLLIMPEXP_CORE wxBitmapDataObject : public wxBitmapDataObjectBase
{
public:
wxBitmapDataObject();
wxBitmapDataObject(const wxBitmap& bitmap);
~wxBitmapDataObject();
void SetBitmap(const wxBitmap& bitmap) wxOVERRIDE;
size_t GetDataSize() const wxOVERRIDE;
bool GetDataHere(void *buf) const wxOVERRIDE;
bool SetData(const wxDataFormat& format, size_t len, const void *buf) wxOVERRIDE;
protected:
void DoConvertToPng();
private:
wxScopedPtr<QByteArray> m_imageBytes;
};

View File

@ -14,14 +14,14 @@
#include <QtWidgets/QApplication>
#include <QtGui/QClipboard>
#include <QtCore/QMimeData>
#include <QPixmap>
#include "wx/clipbrd.h"
#include "wx/scopedarray.h"
#include "wx/scopeguard.h"
#include "wx/qt/private/converter.h"
#include <QtCore/QMimeData>
// ----------------------------------------------------------------------------
// wxClipboard ctor/dtor
// ----------------------------------------------------------------------------
@ -89,6 +89,14 @@ 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);
@ -134,6 +142,17 @@ 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);
@ -170,6 +189,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()));
}

View File

@ -12,9 +12,6 @@
#pragma hdrstop
#endif
#include <QPixmap>
#include <QBuffer>
#include "wx/dataobj.h"
#include "wx/scopedarray.h"
@ -26,7 +23,7 @@ wxString DataFormatIdToMimeType(wxDataFormatId formatId)
switch ( formatId )
{
case wxDF_TEXT: return "text/plain";
case wxDF_BITMAP: return "PNG";
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";
@ -156,54 +153,12 @@ bool wxDataObject::IsSupportedFormat(const wxDataFormat& format,
//############################################################################
wxBitmapDataObject::wxBitmapDataObject()
: m_imageBytes(new QByteArray())
{
}
wxBitmapDataObject::wxBitmapDataObject( const wxBitmap &bitmap )
: wxBitmapDataObjectBase( bitmap ),
m_imageBytes(new QByteArray())
: wxBitmapDataObjectBase( bitmap )
{
DoConvertToPng();
}
wxBitmapDataObject::~wxBitmapDataObject()
{
}
void wxBitmapDataObject::SetBitmap(const wxBitmap &bitmap)
{
wxBitmapDataObjectBase::SetBitmap(bitmap);
DoConvertToPng();
}
size_t wxBitmapDataObject::GetDataSize() const
{
return m_imageBytes->size();
}
bool wxBitmapDataObject::GetDataHere(void *buf) const
{
memcpy(buf, m_imageBytes->constData(), GetDataSize());
return true;
}
bool wxBitmapDataObject::SetData(const wxDataFormat &WXUNUSED(format), size_t len, const void *buf)
{
m_imageBytes->resize(len);
memcpy(m_imageBytes->data(), buf, len);
QPixmap pix;
pix.loadFromData(*m_imageBytes);
m_bitmap = wxBitmap(pix);
return true;
}
void wxBitmapDataObject::DoConvertToPng()
{
QBuffer buffer(m_imageBytes.get());
buffer.open(QIODevice::WriteOnly);
m_bitmap.GetHandle()->save(&buffer, "PNG");
}
//#############################################################################