Support QImage and others in Wayland clipboard.

The wayland plugin's clipboard implementation lacked support for data
set via QMimeData::setImageData, QClipboard::setPixmap, etc. because
everything was treated as a QByteArray. A variant having an image
cannot however be converted to a byte array. Therefore, to make these
convenience functions to work, the image is now encoded via
QImageWriter before sending it. On the receiving side QMimeData is
capable of decoding images from byte arrays, so no problem there.
The patch also renames an internal QWaylandMimeData class to a more
suitable clipboard-specific name as the functionality is clipboard
specific.

Change-Id: I77e7df903b8dbe9731613fdbb9693b2a37b05d18
Reviewed-on: http://codereview.qt.nokia.com/2616
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Jørgen Lind <jorgen.lind@nokia.com>
This commit is contained in:
Laszlo Agocs 2011-08-04 13:01:04 +03:00 committed by Qt by Nokia
parent 9158f5a979
commit beb3dfed07
5 changed files with 153 additions and 12 deletions

View File

@ -42,6 +42,7 @@
#include "qwaylandclipboard.h"
#include "qwaylanddisplay.h"
#include "qwaylandinputdevice.h"
#include "qwaylandmime.h"
#include <QtGui/QPlatformNativeInterface>
#include <QtGui/QApplication>
#include <QtCore/QMimeData>
@ -52,7 +53,7 @@
static QWaylandClipboard *clipboard = 0;
class QWaylandMimeData : public QInternalMimeData
class QWaylandClipboardMimeData : public QInternalMimeData
{
public:
void clearAll();
@ -64,28 +65,28 @@ private:
QStringList mFormatList;
};
void QWaylandMimeData::clearAll()
void QWaylandClipboardMimeData::clearAll()
{
clear();
mFormatList.clear();
}
void QWaylandMimeData::setFormats(const QStringList &formatList)
void QWaylandClipboardMimeData::setFormats(const QStringList &formatList)
{
mFormatList = formatList;
}
bool QWaylandMimeData::hasFormat_sys(const QString &mimeType) const
bool QWaylandClipboardMimeData::hasFormat_sys(const QString &mimeType) const
{
return formats().contains(mimeType);
}
QStringList QWaylandMimeData::formats_sys() const
QStringList QWaylandClipboardMimeData::formats_sys() const
{
return mFormatList;
}
QVariant QWaylandMimeData::retrieveData_sys(const QString &mimeType, QVariant::Type type) const
QVariant QWaylandClipboardMimeData::retrieveData_sys(const QString &mimeType, QVariant::Type type) const
{
return clipboard->retrieveData(mimeType, type);
}
@ -147,7 +148,7 @@ void QWaylandSelection::send(void *data,
Q_UNUSED(selection);
QWaylandSelection *self = static_cast<QWaylandSelection *>(data);
QString mimeType = QString::fromLatin1(mime_type);
QByteArray content = self->mMimeData->data(mimeType);
QByteArray content = QWaylandMimeHelper::getByteArray(self->mMimeData, mimeType);
if (!content.isEmpty()) {
QFile f;
if (f.open(fd, QIODevice::WriteOnly))
@ -230,7 +231,7 @@ QMimeData *QWaylandClipboard::mimeData(QClipboard::Mode mode)
if (!mSelections.isEmpty())
return mSelections.last()->mMimeData;
if (!mMimeDataIn)
mMimeDataIn = new QWaylandMimeData;
mMimeDataIn = new QWaylandClipboardMimeData;
mMimeDataIn->clearAll();
if (!mOfferedMimeTypes.isEmpty() && mOffer)
mMimeDataIn->setFormats(mOfferedMimeTypes);

View File

@ -48,7 +48,7 @@
class QWaylandDisplay;
class QWaylandSelection;
class QWaylandMimeData;
class QWaylandClipboardMimeData;
struct wl_selection_offer;
class QWaylandClipboardSignalEmitter : public QObject
@ -90,7 +90,7 @@ private:
static void forceRoundtrip(struct wl_display *display);
QWaylandDisplay *mDisplay;
QWaylandMimeData *mMimeDataIn;
QWaylandClipboardMimeData *mMimeDataIn;
QList<QWaylandSelection *> mSelections;
QStringList mOfferedMimeTypes;
struct wl_selection_offer *mOffer;

View File

@ -0,0 +1,83 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qwaylandmime.h"
#include <QImage>
#include <QColor>
#include <QUrl>
#include <QBuffer>
#include <QImageWriter>
QByteArray QWaylandMimeHelper::getByteArray(QMimeData *mimeData, const QString &mimeType)
{
QByteArray content;
if (mimeType == QLatin1String("text/plain")) {
content = mimeData->text().toUtf8();
} else if (mimeData->hasImage()
&& (mimeType == QLatin1String("application/x-qt-image")
|| mimeType.startsWith("image/"))) {
QImage image = qvariant_cast<QImage>(mimeData->imageData());
if (!image.isNull()) {
QBuffer buf;
buf.open(QIODevice::ReadWrite);
QByteArray fmt = "BMP";
if (mimeType.startsWith("image/")) {
QByteArray imgFmt = mimeType.mid(6).toUpper().toAscii();
if (QImageWriter::supportedImageFormats().contains(imgFmt))
fmt = imgFmt;
}
QImageWriter wr(&buf, fmt);
wr.write(image);
content = buf.buffer();
}
} else if (mimeType == QLatin1String("application/x-color")) {
content = qvariant_cast<QColor>(mimeData->colorData()).name().toAscii();
} else if (mimeType == QLatin1String("text/uri-list")) {
QList<QUrl> urls = mimeData->urls();
for (int i = 0; i < urls.count(); ++i) {
content.append(urls.at(i).toEncoded());
content.append('\n');
}
} else {
content = mimeData->data(mimeType);
}
return content;
}

View File

@ -0,0 +1,55 @@
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef QWAYLANDMIME_H
#define QWAYLANDMIME_H
#include <QString>
#include <QByteArray>
#include <QMimeData>
class QWaylandMimeHelper
{
public:
static QByteArray getByteArray(QMimeData *mimeData, const QString &mimeType);
};
#endif

View File

@ -20,7 +20,8 @@ SOURCES = main.cpp \
qwaylandwindow.cpp \
qwaylandscreen.cpp \
qwaylandshmwindow.cpp \
qwaylandclipboard.cpp
qwaylandclipboard.cpp \
qwaylandmime.cpp
HEADERS = qwaylandintegration.h \
qwaylandnativeinterface.h \
@ -31,7 +32,8 @@ HEADERS = qwaylandintegration.h \
qwaylandshmsurface.h \
qwaylandbuffer.h \
qwaylandshmwindow.h \
qwaylandclipboard.h
qwaylandclipboard.h \
qwaylandmime.h
INCLUDEPATH += $$QMAKE_INCDIR_WAYLAND
LIBS += $$QMAKE_LIBS_WAYLAND