Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsmimeconverter.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qt_windows.h>
7
8#include <QtGui/private/qguiapplication_p.h>
9#include <QtGui/qpa/qplatformintegration.h>
10
12
13/*!
14 \class QWindowsMimeConverter
15 \brief The QWindowsMimeConverter class maps open-standard MIME to Window Clipboard formats.
16 \inmodule QtGui
17
18 Qt's drag-and-drop and clipboard facilities use the MIME standard.
19 On X11, this maps trivially to the Xdnd protocol, but on Windows
20 although some applications use MIME types to describe clipboard
21 formats, others use arbitrary non-standardized naming conventions,
22 or unnamed built-in formats of Windows.
23
24 By instantiating subclasses of QWindowsMimeConverter that provide
25 conversions between Windows Clipboard and MIME formats, you can convert
26 proprietary clipboard formats to MIME formats.
27
28 Construct an instance of your converter implementation after instantiating
29 QGuiApplication:
30
31 \code
32 int main(int argc, char **argv)
33 {
34 QGuiApplication app(argc, argv);
35 JsonMimeConverter jsonConverter;
36 }
37 \endcode
38
39 Destroying the instance will unregister the converter and remove support
40 for the conversion. It is also valid to heap-allocate the converter
41 instance; Qt takes ownership and will delete the converter object during
42 QGuiApplication shut-down.
43
44 Qt has predefined support for the following Windows Clipboard formats:
45
46 \table
47 \header \li Windows Format \li Equivalent MIME type
48 \row \li \c CF_UNICODETEXT \li \c text/plain
49 \row \li \c CF_TEXT \li \c text/plain
50 \row \li \c CF_DIB \li \c{image/xyz}, where \c xyz is
51 a \l{QImageWriter::supportedImageFormats()}{Qt image format}
52 \row \li \c CF_HDROP \li \c text/uri-list
53 \row \li \c CF_INETURL \li \c text/uri-list
54 \row \li \c CF_HTML \li \c text/html
55 \endtable
56
57 An example use of this class would be to map the Windows Metafile
58 clipboard format (\c CF_METAFILEPICT) to and from the MIME type
59 \c{image/x-wmf}. This conversion might simply be adding or removing
60 a header, or even just passing on the data. See \l{Drag and Drop}
61 for more information on choosing and definition MIME types.
62
63 You can check if a MIME type is convertible using canConvertFromMime() and
64 can perform conversions with convertToMime() and convertFromMime().
65*/
66
67
68/*!
69 \fn bool QWindowsMimeConverter::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const
70
71 Returns \c true if the converter can convert from the \a mimeData to
72 the format specified in \a formatetc.
73
74 All subclasses must reimplement this pure virtual function.
75*/
76
77/*!
78 \fn bool QWindowsMimeConverter::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const
79
80 Returns \c true if the converter can convert to the \a mimeType from
81 the available formats in \a pDataObj.
82
83 All subclasses must reimplement this pure virtual function.
84*/
85
86/*!
87 \fn QString QWindowsMimeConverter::mimeForFormat(const FORMATETC &formatetc) const
88
89 Returns the mime type that will be created form the format specified
90 in \a formatetc, or an empty string if this converter does not support
91 \a formatetc.
92
93 All subclasses must reimplement this pure virtual function.
94*/
95
96/*!
97 \fn QList<FORMATETC> QWindowsMimeConverter::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const
98
99 Returns a QList of FORMATETC structures representing the different windows clipboard
100 formats that can be provided for the \a mimeType from the \a mimeData.
101
102 All subclasses must reimplement this pure virtual function.
103*/
104
105/*!
106 \fn QVariant QWindowsMimeConverter::convertToMime(const QString &mimeType, IDataObject *pDataObj,
107 QMetaType preferredType) const
108
109 Returns a QVariant containing the converted data for \a mimeType from \a pDataObj.
110 If possible the QVariant should be of the \a preferredType to avoid needless conversions.
111
112 All subclasses must reimplement this pure virtual function.
113*/
114
115/*!
116 \fn bool QWindowsMimeConverter::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const
117
118 Convert the \a mimeData to the format specified in \a formatetc.
119 The converted data should then be placed in \a pmedium structure.
120
121 Return true if the conversion was successful.
122
123 All subclasses must reimplement this pure virtual function.
124*/
125
126/*!
127 Constructs a QWindowsMimeConverter instance.
128
129 The instance is automatically registered, and will be called to convert data during
130 clipboard or drag'n'drop operations.
131
132 Call this constructor after QGuiApplication has been created.
133*/
134QWindowsMimeConverter::QWindowsMimeConverter()
135{
136 using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
137 auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration());
138 Q_ASSERT(nativeWindowsApp);
139 nativeWindowsApp->registerMime(this);
140}
141
142/*!
143 Constructs a QWindowsMimeConverter instance.
144
145 The instance is automatically unregistered.
146*/
147QWindowsMimeConverter::~QWindowsMimeConverter()
148{
149 using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
150 auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration());
151 Q_ASSERT(nativeWindowsApp);
152 nativeWindowsApp->unregisterMime(this);
153}
154
155/*!
156 Registers the MIME type \a mimeType, and returns an ID number
157 identifying the format on Windows.
158
159 A mime type \c {application/x-qt-windows-mime;value="WindowsType"} will be
160 registered as the clipboard format for \c WindowsType.
161*/
162int QWindowsMimeConverter::registerMimeType(const QString &mimeType)
163{
164 using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
165 auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration());
166 Q_ASSERT(nativeWindowsApp);
167 return nativeWindowsApp->registerMimeType(mimeType);
168}
169
170QT_END_NAMESPACE