diff options
author | Morten Johan Sørvig <morten.sorvig@digia.com> | 2013-01-02 13:48:33 +0100 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@digia.com> | 2013-01-23 14:57:36 +0100 |
commit | 014f00c11f69a60fa33509bf11b9ebda986f6dc0 (patch) | |
tree | dbfc2672b00300599a94e6717705f568116ebc2e | |
parent | 69edc40ed1c5af56e082d746379c31f001d1a5da (diff) |
Add QMacPasteboardMime.
Equivalent to QMacPasteboardMime from Qt 4. Requires
exported functions from QPlatformNativeInterface.
The API from Qt 4 is in large parts kept, with the
exception of the static functions used internally
by Qt (?)
The implementation is based on having two equal classes
and keeping them in sync: One internal to the Cocoa
platform plugin and one public in QtMacExtras.
Change-Id: I334fc982355fbb6315d7a44eeca8551071ba60be
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
-rw-r--r-- | examples/macpasteboardmime/macpasteboardmime.pro | 6 | ||||
-rw-r--r-- | examples/macpasteboardmime/main.cpp | 152 | ||||
-rw-r--r-- | src/qmacpasteboardmime.h | 78 | ||||
-rw-r--r-- | src/qmacpasteboardmime.mm | 213 | ||||
-rw-r--r-- | src/qtmacextras.pri | 6 |
5 files changed, 454 insertions, 1 deletions
diff --git a/examples/macpasteboardmime/macpasteboardmime.pro b/examples/macpasteboardmime/macpasteboardmime.pro new file mode 100644 index 0000000..f2bcd2e --- /dev/null +++ b/examples/macpasteboardmime/macpasteboardmime.pro @@ -0,0 +1,6 @@ +include (../../src/qtmacextras.pri) + +SOURCES += main.cpp + + + diff --git a/examples/macpasteboardmime/main.cpp b/examples/macpasteboardmime/main.cpp new file mode 100644 index 0000000..718626a --- /dev/null +++ b/examples/macpasteboardmime/main.cpp @@ -0,0 +1,152 @@ +/**************************************************************************** + ** + ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). + ** Contact: http://www.qt-project.org/legal + ** + ** This file is part of QtMacExtras in the Qt Toolkit. + ** + ** $QT_BEGIN_LICENSE:LGPL$ + ** Commercial License Usage + ** Licensees holding valid commercial Qt licenses may use this file in + ** accordance with the commercial license agreement provided with the + ** Software or, alternatively, in accordance with the terms contained in + ** a written agreement between you and Digia. For licensing terms and + ** conditions see http://qt.digia.com/licensing. For further information + ** use the contact form at http://qt.digia.com/contact-us. + ** + ** GNU Lesser General Public License Usage + ** Alternatively, 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, Digia gives you certain additional + ** rights. These rights are described in the Digia 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. + ** + ** + ** $QT_END_LICENSE$ + ** + ****************************************************************************/ + +#include <QApplication> +#include <QtWidgets/QtWidgets> + +#include <qmacpasteboardmime.h> + +class VCardMime : public QMacPasteboardMime +{ +public: + VCardMime() : QMacPasteboardMime(MIME_ALL) + { } + + QString convertorName() + { + return QString("VCardMime"); + } + + bool canConvert(const QString &mime, QString flav) + { + return mimeFor(flav) == mime; + } + + QString mimeFor(QString flav) + { + if (flav == QString("public.vcard")) + return QString("application/x-mycompany-VCard"); + return QString(); + } + + QString flavorFor(const QString &mime) + { + if (mime == QString("application/x-mycompany-VCard")) + return QString("public.vcard"); + return QString(); + } + + QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav) + { + Q_UNUSED(mime); + Q_UNUSED(flav); + + QByteArray all; + foreach (QByteArray i, data) { + all += i; + } + return QVariant(all); + } + + QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav) + { + Q_UNUSED(mime); + Q_UNUSED(data); + Q_UNUSED(flav); + // Todo: implement! + return QList<QByteArray>(); + } + +}; + +class TestWidget : public QWidget +{ +public: + TestWidget() : QWidget(0) + { + vcardMime = new VCardMime(); + setAcceptDrops(true); + } + + ~TestWidget() + { + delete vcardMime; + } + + void dragEnterEvent(QDragEnterEvent *e) + { + e->accept(); + + } + + virtual void dropEvent(QDropEvent *e) + { + e->accept(); + contentsDropEvent(e); + } + + void contentsDropEvent(QDropEvent* e) + { + if ( e->mimeData()->hasFormat( "application/x-mycompany-VCard" ) ) + { + QString s = QString( e->mimeData()->data( "application/x-mycompany-VCard" ) ); + + // s now contains text of vcard + qDebug() << "got vcard" << s.count(); + + e->acceptProposedAction(); + } + } +private: + VCardMime *vcardMime; +}; + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + qRegisterDraggedTypes(QStringList() << QStringLiteral("public.vcard")); + + TestWidget wid1; + wid1.show(); + + return app.exec(); +} diff --git a/src/qmacpasteboardmime.h b/src/qmacpasteboardmime.h new file mode 100644 index 0000000..33a141e --- /dev/null +++ b/src/qmacpasteboardmime.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtMacExtras of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMACMIME_H +#define QMACMIME_H + +#include <QtCore> + +#include <CoreFoundation/CoreFoundation.h> + +QT_BEGIN_NAMESPACE + +void qRegisterDraggedTypes(const QStringList &types); + +// Duplicate of QMacPasteboardMime in the Cocoa Platform Plugin. Keep in sync! +class QMacPasteboardMime { + char type; +public: + enum QMacPasteboardMimeType { MIME_DND=0x01, + MIME_CLIP=0x02, + MIME_QT_CONVERTOR=0x04, + MIME_QT3_CONVERTOR=0x08, + MIME_ALL=MIME_DND|MIME_CLIP + }; + explicit QMacPasteboardMime(char); + virtual ~QMacPasteboardMime(); + + virtual QString convertorName() = 0; + virtual bool canConvert(const QString &mime, QString flav) = 0; + virtual QString mimeFor(QString flav) = 0; + virtual QString flavorFor(const QString &mime) = 0; + virtual QVariant convertToMime(const QString &mime, QList<QByteArray> data, QString flav) = 0; + virtual QList<QByteArray> convertFromMime(const QString &mime, QVariant data, QString flav) = 0; + virtual int count(QMimeData *mimeData); +}; + +QT_END_NAMESPACE + +#endif + diff --git a/src/qmacpasteboardmime.mm b/src/qmacpasteboardmime.mm new file mode 100644 index 0000000..cc1222c --- /dev/null +++ b/src/qmacpasteboardmime.mm @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtMacExtras of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, 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, Digia gives you certain additional +** rights. These rights are described in the Digia 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. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmacpasteboardmime.h" +#include <qpa/qplatformnativeinterface.h> +#include <qapplication.h> +#include <qlogging.h> + +#include <Cocoa/Cocoa.h> + +QT_BEGIN_NAMESPACE + +static QPlatformNativeInterface::NativeResourceForIntegrationFunction resolvePlatformFunction(const QByteArray &functionName) +{ + QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface(); + QPlatformNativeInterface::NativeResourceForIntegrationFunction fn = nativeInterface->nativeResourceFunctionForIntegration(functionName); + if (!fn) + qWarning() << "Qt could not resolve function" << functionName << "from QGuiApplication::platformNativeInterface()->nativeResourceFunctionForIntegration()"; + return fn; +} + +/*! + \fn void qRegisterDraggedTypes(const QStringList &types) + \relates QtMacPasteboardMime + + Registers the given \a types as custom pasteboard types. + + This function should be called to enable the Drag and Drop events + for custom pasteboard types on Cocoa implementations. This is required + in addition to a QtMacPasteboardMime subclass implementation. By default + drag and drop is enabled for all standard pasteboard types. + + \sa QMacPasteboardMime +*/ +void qRegisterDraggedTypes(const QStringList &types) +{ + QPlatformNativeInterface::NativeResourceForIntegrationFunction function = resolvePlatformFunction("registerdraggedtypes"); + typedef void (*RegisterDraggedTypesFunction)(const QStringList &types); + reinterpret_cast<RegisterDraggedTypesFunction>(function)(types); +} + +/*! + \class QMacPasteboardMime + \brief The QMacPasteboardMime class converts between a MIME type and a + \l{http://developer.apple.com/macosx/uniformtypeidentifiers.html}{Uniform + Type Identifier (UTI)} format. + + Qt's drag and drop and clipboard facilities use the MIME + standard. On X11, this maps trivially to the Xdnd protocol. On + Mac, although some applications use MIME to describe clipboard + contents, it is more common to use Apple's UTI format. + + QMacPasteboardMime's role is to bridge the gap between MIME and UTI; + By subclasses this class, one can extend Qt's drag and drop + and clipboard handling to convert to and from unsupported, or proprietary, UTI formats. + + A subclass of QMacPasteboardMime will automatically be registered, and active, upon instantiation. + + Qt has predefined support for the following UTIs: + \list + \i public.utf8-plain-text - converts to "text/plain" + \i public.utf16-plain-text - converts to "text/plain" + \i public.html - converts to "text/html" + \i public.url - converts to "text/uri-list" + \i public.file-url - converts to "text/uri-list" + \i public.tiff - converts to "application/x-qt-image" + \i public.vcard - converts to "text/plain" + \i com.apple.traditional-mac-plain-text - converts to "text/plain" + \i com.apple.pict - converts to "application/x-qt-image" + \endlist + + When working with MIME data, Qt will interate through all instances of QMacPasteboardMime to + find an instance that can convert to, or from, a specific MIME type. It will do this by calling + canConvert() on each instance, starting with (and choosing) the last created instance first. + The actual conversions will be done by using convertToMime() and convertFromMime(). + + \note The API uses the term "flavor" in some cases. This is for backwards + compatibility reasons, and should now be understood as UTIs. +*/ + +/*! \enum QMacPasteboardMime::QMacPasteboardMimeType + \internal +*/ + +/*! + Constructs a new conversion object of type \a t, adding it to the + globally accessed list of available converters. +*/ +QMacPasteboardMime::QMacPasteboardMime(char t) : type(t) +{ + QPlatformNativeInterface::NativeResourceForIntegrationFunction function = resolvePlatformFunction("addToMimeList"); + typedef void (*AddToGlobalMimeListFunction)(QMacPasteboardMime *); + reinterpret_cast<AddToGlobalMimeListFunction>(function)(this); +} + +/*! + Destroys a conversion object, removing it from the global + list of available converters. +*/ +QMacPasteboardMime::~QMacPasteboardMime() +{ + QPlatformNativeInterface::NativeResourceForIntegrationFunction function = resolvePlatformFunction("removeFromMimeList"); + typedef void (*RemoveFromGlobalMimeListFunction)(QMacPasteboardMime *); + reinterpret_cast<RemoveFromGlobalMimeListFunction>(function)(this); +} + +/*! + Returns the item count for the given \a mimeData +*/ +int QMacPasteboardMime::count(QMimeData *mimeData) +{ + Q_UNUSED(mimeData); + return 1; +} + +/*! + \fn QString QMacPasteboardMime::convertorName() + + Returns a name for the converter. + + All subclasses must reimplement this pure virtual function. +*/ + +/*! + \fn bool QMacPasteboardMime::canConvert(const QString &mime, QString flav) + + Returns true if the converter can convert (both ways) between + \a mime and \a flav; otherwise returns false. + + All subclasses must reimplement this pure virtual function. +*/ + +/*! + \fn QString QMacPasteboardMime::mimeFor(QString flav) + + Returns the MIME UTI used for Mac flavor \a flav, or 0 if this + converter does not support \a flav. + + All subclasses must reimplement this pure virtual function. +*/ + +/*! + \fn QString QMacPasteboardMime::flavorFor(const QString &mime) + + Returns the Mac UTI used for MIME type \a mime, or 0 if this + converter does not support \a mime. + + All subclasses must reimplement this pure virtual function. +*/ + +/*! + \fn QVariant QMacPasteboardMime::convertToMime(const QString &mime, QList<QByteArray> data, QString flav) + + Returns \a data converted from Mac UTI \a flav to MIME type \a + mime. + + Note that Mac flavors must all be self-terminating. The input \a + data may contain trailing data. + + All subclasses must reimplement this pure virtual function. +*/ + +/*! + \fn QList<QByteArray> QMacPasteboardMime::convertFromMime(const QString &mime, QVariant data, QString flav) + + Returns \a data converted from MIME type \a mime + to Mac UTI \a flav. + + Note that Mac flavors must all be self-terminating. The return + value may contain trailing data. + + All subclasses must reimplement this pure virtual function. +*/ + +QT_END_NAMESPACE diff --git a/src/qtmacextras.pri b/src/qtmacextras.pri index 47d7721..abc22db 100644 --- a/src/qtmacextras.pri +++ b/src/qtmacextras.pri @@ -2,7 +2,7 @@ QT += widgets gui-private INCLUDEPATH += $$PWD DEPENDPATH += $$PWD -# qtmacunifiedtoolbar +# QtMacUnifiedToolBar HEADERS += $$PWD/qtmacunifiedtoolbar.h \ $$PWD/qtmactoolbardelegate.h \ $$PWD/qtmactoolbutton.h \ @@ -18,3 +18,7 @@ mac { } else { SOURCES += $$PWD/qtmacunifiedtoolbar.cpp } + +# QtMacPasteboardMime +HEADERS += $$PWD/qmacpasteboardmime.h +OBJECTIVE_SOURCES += $$PWD/qmacpasteboardmime.mm |