author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 8 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qmime.h" |
|
43 |
||
44 |
#include "qimagereader.h" |
|
45 |
#include "qimagewriter.h" |
|
46 |
#include "qdatastream.h" |
|
47 |
#include "qbuffer.h" |
|
48 |
#include "qt_windows.h" |
|
49 |
#include "qapplication_p.h" |
|
50 |
#include "qtextcodec.h" |
|
51 |
#include "qregexp.h" |
|
52 |
#include "qalgorithms.h" |
|
53 |
#include "qmap.h" |
|
54 |
#include "qdnd_p.h" |
|
55 |
#include <shlobj.h> |
|
56 |
#include "qurl.h" |
|
57 |
#include "qvariant.h" |
|
58 |
#include "qtextdocument.h" |
|
59 |
#include "qdir.h" |
|
60 |
||
61 |
#if defined(Q_OS_WINCE) |
|
62 |
#include "qguifunctions_wince.h" |
|
63 |
#endif |
|
64 |
||
65 |
QT_BEGIN_NAMESPACE |
|
66 |
||
67 |
#ifndef QT_NO_IMAGEFORMAT_BMP |
|
68 |
#ifndef CF_DIBV5 |
|
69 |
#define CF_DIBV5 17 |
|
70 |
#endif |
|
71 |
/* The MSVC compilers allows multi-byte characters, that has the behavior of |
|
72 |
* that each character gets shifted into position. 0x73524742 below is for MSVC |
|
73 |
* equivalent to doing 'sRGB', but this does of course not work |
|
74 |
* on conformant C++ compilers. */ |
|
75 |
#define BMP_LCS_sRGB 0x73524742 |
|
76 |
#define BMP_LCS_GM_IMAGES 0x00000004L |
|
77 |
||
78 |
struct _CIEXYZ { |
|
79 |
long ciexyzX, ciexyzY, ciexyzZ; |
|
80 |
}; |
|
81 |
||
82 |
struct _CIEXYZTRIPLE { |
|
83 |
_CIEXYZ ciexyzRed, ciexyzGreen, ciexyzBlue; |
|
84 |
}; |
|
85 |
||
86 |
struct BMP_BITMAPV5HEADER { |
|
87 |
DWORD bV5Size; |
|
88 |
LONG bV5Width; |
|
89 |
LONG bV5Height; |
|
90 |
WORD bV5Planes; |
|
91 |
WORD bV5BitCount; |
|
92 |
DWORD bV5Compression; |
|
93 |
DWORD bV5SizeImage; |
|
94 |
LONG bV5XPelsPerMeter; |
|
95 |
LONG bV5YPelsPerMeter; |
|
96 |
DWORD bV5ClrUsed; |
|
97 |
DWORD bV5ClrImportant; |
|
98 |
DWORD bV5RedMask; |
|
99 |
DWORD bV5GreenMask; |
|
100 |
DWORD bV5BlueMask; |
|
101 |
DWORD bV5AlphaMask; |
|
102 |
DWORD bV5CSType; |
|
103 |
_CIEXYZTRIPLE bV5Endpoints; |
|
104 |
DWORD bV5GammaRed; |
|
105 |
DWORD bV5GammaGreen; |
|
106 |
DWORD bV5GammaBlue; |
|
107 |
DWORD bV5Intent; |
|
108 |
DWORD bV5ProfileData; |
|
109 |
DWORD bV5ProfileSize; |
|
110 |
DWORD bV5Reserved; |
|
111 |
}; |
|
112 |
static const int BMP_BITFIELDS = 3; |
|
113 |
||
114 |
extern bool qt_read_dib(QDataStream&, QImage&); // qimage.cpp |
|
115 |
extern bool qt_write_dib(QDataStream&, QImage); // qimage.cpp |
|
116 |
static bool qt_write_dibv5(QDataStream &s, QImage image); |
|
117 |
static bool qt_read_dibv5(QDataStream &s, QImage &image); |
|
118 |
#endif |
|
119 |
||
120 |
//#define QMIME_DEBUG |
|
121 |
||
122 |
||
123 |
// helpers for using global memory |
|
124 |
||
125 |
static int getCf(const FORMATETC &formatetc) |
|
126 |
{ |
|
127 |
return formatetc.cfFormat; |
|
128 |
} |
|
129 |
||
130 |
static FORMATETC setCf(int cf) |
|
131 |
{ |
|
132 |
FORMATETC formatetc; |
|
133 |
formatetc.cfFormat = cf; |
|
134 |
formatetc.dwAspect = DVASPECT_CONTENT; |
|
135 |
formatetc.lindex = -1; |
|
136 |
formatetc.ptd = NULL; |
|
137 |
formatetc.tymed = TYMED_HGLOBAL; |
|
138 |
return formatetc; |
|
139 |
} |
|
140 |
||
141 |
static bool setData(const QByteArray &data, STGMEDIUM *pmedium) |
|
142 |
{ |
|
143 |
HGLOBAL hData = GlobalAlloc(0, data.size()); |
|
144 |
if (!hData) |
|
145 |
return false; |
|
146 |
||
147 |
void *out = GlobalLock(hData); |
|
148 |
memcpy(out, data.data(), data.size()); |
|
149 |
GlobalUnlock(hData); |
|
150 |
pmedium->tymed = TYMED_HGLOBAL; |
|
151 |
pmedium->hGlobal = hData; |
|
152 |
pmedium->pUnkForRelease = 0; |
|
153 |
return true; |
|
154 |
} |
|
155 |
||
156 |
static QByteArray getData(int cf, IDataObject *pDataObj) |
|
157 |
{ |
|
158 |
QByteArray data; |
|
159 |
FORMATETC formatetc = setCf(cf); |
|
160 |
STGMEDIUM s; |
|
161 |
if (pDataObj->GetData(&formatetc, &s) == S_OK) { |
|
162 |
DWORD * val = (DWORD*)GlobalLock(s.hGlobal); |
|
163 |
data = QByteArray::fromRawData((char*)val, GlobalSize(s.hGlobal)); |
|
164 |
data.detach(); |
|
165 |
GlobalUnlock(s.hGlobal); |
|
166 |
ReleaseStgMedium(&s); |
|
167 |
} else { |
|
168 |
//Try reading IStream data |
|
169 |
formatetc.tymed = TYMED_ISTREAM; |
|
170 |
if (pDataObj->GetData(&formatetc, &s) == S_OK) { |
|
171 |
char szBuffer[4096]; |
|
172 |
ULONG actualRead = 0; |
|
173 |
LARGE_INTEGER pos = {{0, 0}}; |
|
174 |
//Move to front (can fail depending on the data model implemented) |
|
175 |
HRESULT hr = s.pstm->Seek(pos, STREAM_SEEK_SET, NULL); |
|
176 |
while(SUCCEEDED(hr)){ |
|
177 |
hr = s.pstm->Read(szBuffer, sizeof(szBuffer), &actualRead); |
|
178 |
if (SUCCEEDED(hr) && actualRead > 0) { |
|
179 |
data += QByteArray::fromRawData(szBuffer, actualRead); |
|
180 |
} |
|
181 |
if (actualRead != sizeof(szBuffer)) |
|
182 |
break; |
|
183 |
} |
|
184 |
data.detach(); |
|
185 |
ReleaseStgMedium(&s); |
|
186 |
} |
|
187 |
} |
|
188 |
return data; |
|
189 |
} |
|
190 |
||
191 |
static bool canGetData(int cf, IDataObject * pDataObj) |
|
192 |
{ |
|
193 |
FORMATETC formatetc = setCf(cf); |
|
194 |
if (pDataObj->QueryGetData(&formatetc) != S_OK){ |
|
195 |
formatetc.tymed = TYMED_ISTREAM; |
|
196 |
return pDataObj->QueryGetData(&formatetc) == S_OK; |
|
197 |
} |
|
198 |
return true; |
|
199 |
} |
|
200 |
||
201 |
class QWindowsMimeList |
|
202 |
{ |
|
203 |
public: |
|
204 |
QWindowsMimeList(); |
|
205 |
~QWindowsMimeList(); |
|
206 |
void addWindowsMime(QWindowsMime * mime); |
|
207 |
void removeWindowsMime(QWindowsMime * mime); |
|
208 |
QList<QWindowsMime*> windowsMimes(); |
|
209 |
||
210 |
private: |
|
211 |
void init(); |
|
212 |
bool initialized; |
|
213 |
QList<QWindowsMime*> mimes; |
|
214 |
}; |
|
215 |
||
216 |
Q_GLOBAL_STATIC(QWindowsMimeList, theMimeList); |
|
217 |
||
218 |
||
219 |
/*! |
|
220 |
\class QWindowsMime |
|
221 |
\brief The QWindowsMime class maps open-standard MIME to Window Clipboard formats. |
|
222 |
\ingroup draganddrop |
|
223 |
||
224 |
Qt's drag-and-drop and clipboard facilities use the MIME standard. |
|
225 |
On X11, this maps trivially to the Xdnd protocol, but on Windows |
|
226 |
although some applications use MIME types to describe clipboard |
|
227 |
formats, others use arbitrary non-standardized naming conventions, |
|
228 |
or unnamed built-in formats of Windows. |
|
229 |
||
230 |
By instantiating subclasses of QWindowsMime that provide conversions |
|
231 |
between Windows Clipboard and MIME formats, you can convert |
|
232 |
proprietary clipboard formats to MIME formats. |
|
233 |
||
234 |
Qt has predefined support for the following Windows Clipboard formats: |
|
235 |
||
236 |
\table |
|
237 |
\header \o Windows Format \o Equivalent MIME type |
|
238 |
\row \o \c CF_UNICODETEXT \o \c text/plain |
|
239 |
\row \o \c CF_TEXT \o \c text/plain |
|
240 |
\row \o \c CF_DIB \o \c{image/xyz}, where \c xyz is |
|
241 |
a \l{QImageWriter::supportedImageFormats()}{Qt image format} |
|
242 |
\row \o \c CF_HDROP \o \c text/uri-list |
|
243 |
\row \o \c CF_INETURL \o \c text/uri-list |
|
244 |
\row \o \c CF_HTML \o \c text/html |
|
245 |
\endtable |
|
246 |
||
247 |
An example use of this class would be to map the Windows Metafile |
|
248 |
clipboard format (\c CF_METAFILEPICT) to and from the MIME type |
|
249 |
\c{image/x-wmf}. This conversion might simply be adding or removing |
|
250 |
a header, or even just passing on the data. See \l{Drag and Drop} |
|
251 |
for more information on choosing and definition MIME types. |
|
252 |
||
253 |
You can check if a MIME type is convertible using canConvertFromMime() and |
|
254 |
can perform conversions with convertToMime() and convertFromMime(). |
|
255 |
*/ |
|
256 |
||
257 |
/*! |
|
258 |
Constructs a new conversion object, adding it to the globally accessed |
|
259 |
list of available converters. |
|
260 |
*/ |
|
261 |
QWindowsMime::QWindowsMime() |
|
262 |
{ |
|
263 |
theMimeList()->addWindowsMime(this); |
|
264 |
} |
|
265 |
||
266 |
/*! |
|
267 |
Destroys a conversion object, removing it from the global |
|
268 |
list of available converters. |
|
269 |
*/ |
|
270 |
QWindowsMime::~QWindowsMime() |
|
271 |
{ |
|
272 |
theMimeList()->removeWindowsMime(this); |
|
273 |
} |
|
274 |
||
275 |
||
276 |
/*! |
|
277 |
Registers the MIME type \a mime, and returns an ID number |
|
278 |
identifying the format on Windows. |
|
279 |
*/ |
|
280 |
int QWindowsMime::registerMimeType(const QString &mime) |
|
281 |
{ |
|
282 |
int f = RegisterClipboardFormat(reinterpret_cast<const wchar_t *> (mime.utf16())); |
|
283 |
if (!f) |
|
284 |
qErrnoWarning("QWindowsMime::registerMimeType: Failed to register clipboard format"); |
|
285 |
||
286 |
return f; |
|
287 |
} |
|
288 |
||
289 |
||
290 |
/*! |
|
291 |
\fn bool QWindowsMime::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
292 |
||
293 |
Returns true if the converter can convert from the \a mimeData to |
|
294 |
the format specified in \a formatetc. |
|
295 |
||
296 |
All subclasses must reimplement this pure virtual function. |
|
297 |
*/ |
|
298 |
||
299 |
/*! |
|
300 |
\fn bool QWindowsMime::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
301 |
||
302 |
Returns true if the converter can convert to the \a mimeType from |
|
303 |
the available formats in \a pDataObj. |
|
304 |
||
305 |
All subclasses must reimplement this pure virtual function. |
|
306 |
*/ |
|
307 |
||
308 |
/*! |
|
309 |
\fn QString QWindowsMime::mimeForFormat(const FORMATETC &formatetc) const |
|
310 |
||
311 |
Returns the mime type that will be created form the format specified |
|
312 |
in \a formatetc, or an empty string if this converter does not support |
|
313 |
\a formatetc. |
|
314 |
||
315 |
All subclasses must reimplement this pure virtual function. |
|
316 |
*/ |
|
317 |
||
318 |
/*! |
|
319 |
\fn QVector<FORMATETC> QWindowsMime::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
320 |
||
321 |
Returns a QVector of FORMATETC structures representing the different windows clipboard |
|
322 |
formats that can be provided for the \a mimeType from the \a mimeData. |
|
323 |
||
324 |
All subclasses must reimplement this pure virtual function. |
|
325 |
*/ |
|
326 |
||
327 |
/*! |
|
328 |
\fn QVariant QWindowsMime::convertToMime(const QString &mimeType, IDataObject *pDataObj, |
|
329 |
QVariant::Type preferredType) const |
|
330 |
||
331 |
Returns a QVariant containing the converted data for \a mimeType from \a pDataObj. |
|
332 |
If possible the QVariant should be of the \a preferredType to avoid needless conversions. |
|
333 |
||
334 |
All subclasses must reimplement this pure virtual function. |
|
335 |
*/ |
|
336 |
||
337 |
/*! |
|
338 |
\fn bool QWindowsMime::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const |
|
339 |
||
340 |
Convert the \a mimeData to the format specified in \a formatetc. |
|
341 |
The converted data should then be placed in \a pmedium structure. |
|
342 |
||
343 |
Return true if the conversion was successful. |
|
344 |
||
345 |
All subclasses must reimplement this pure virtual function. |
|
346 |
*/ |
|
347 |
||
348 |
||
349 |
QWindowsMime *QWindowsMime::converterFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) |
|
350 |
{ |
|
351 |
QList<QWindowsMime*> mimes = theMimeList()->windowsMimes(); |
|
352 |
for (int i=mimes.size()-1; i>=0; --i) { |
|
353 |
if (mimes.at(i)->canConvertFromMime(formatetc, mimeData)) |
|
354 |
return mimes.at(i); |
|
355 |
} |
|
356 |
return 0; |
|
357 |
} |
|
358 |
||
359 |
QWindowsMime *QWindowsMime::converterToMime(const QString &mimeType, IDataObject *pDataObj) |
|
360 |
{ |
|
361 |
QList<QWindowsMime*> mimes = theMimeList()->windowsMimes(); |
|
362 |
for (int i=mimes.size()-1; i>=0; --i) { |
|
363 |
if (mimes.at(i)->canConvertToMime(mimeType, pDataObj)) |
|
364 |
return mimes.at(i); |
|
365 |
} |
|
366 |
return 0; |
|
367 |
} |
|
368 |
||
369 |
QVector<FORMATETC> QWindowsMime::allFormatsForMime(const QMimeData *mimeData) |
|
370 |
{ |
|
371 |
QList<QWindowsMime*> mimes = theMimeList()->windowsMimes(); |
|
372 |
QVector<FORMATETC> formatics; |
|
373 |
formatics.reserve(20); |
|
374 |
#ifndef QT_NO_DRAGANDDROP |
|
375 |
QStringList formats = QInternalMimeData::formatsHelper(mimeData); |
|
376 |
for (int f=0; f<formats.size(); ++f) { |
|
377 |
for (int i=mimes.size()-1; i>=0; --i) |
|
378 |
formatics += mimes.at(i)->formatsForMime(formats.at(f), mimeData); |
|
379 |
} |
|
380 |
#else |
|
381 |
Q_UNUSED(mimeData); |
|
382 |
#endif //QT_NO_DRAGANDDROP |
|
383 |
return formatics; |
|
384 |
} |
|
385 |
||
386 |
QStringList QWindowsMime::allMimesForFormats(IDataObject *pDataObj) |
|
387 |
{ |
|
388 |
QList<QWindowsMime*> mimes = theMimeList()->windowsMimes(); |
|
389 |
QStringList formats; |
|
390 |
LPENUMFORMATETC FAR fmtenum; |
|
391 |
HRESULT hr = pDataObj->EnumFormatEtc(DATADIR_GET, &fmtenum); |
|
392 |
||
393 |
if (hr == NOERROR) { |
|
394 |
FORMATETC fmtetc; |
|
395 |
while (S_OK == fmtenum->Next(1, &fmtetc, 0)) { |
|
396 |
#if defined(QMIME_DEBUG) && !defined(Q_OS_WINCE) |
|
397 |
qDebug("QWindowsMime::allMimesForFormats()"); |
|
398 |
wchar_t buf[256] = {0}; |
|
399 |
GetClipboardFormatName(fmtetc.cfFormat, buf, 255); |
|
400 |
qDebug("CF = %d : %s", fmtetc.cfFormat, QString::fromWCharArray(buf)); |
|
401 |
#endif |
|
402 |
for (int i=mimes.size()-1; i>=0; --i) { |
|
403 |
QString format = mimes.at(i)->mimeForFormat(fmtetc); |
|
404 |
if (!format.isEmpty() && !formats.contains(format)) { |
|
405 |
formats += format; |
|
406 |
} |
|
407 |
} |
|
408 |
// as documented in MSDN to avoid possible memleak |
|
409 |
if (fmtetc.ptd) |
|
410 |
CoTaskMemFree(fmtetc.ptd); |
|
411 |
} |
|
412 |
fmtenum->Release(); |
|
413 |
} |
|
414 |
||
415 |
return formats; |
|
416 |
} |
|
417 |
||
418 |
||
419 |
class QWindowsMimeText : public QWindowsMime |
|
420 |
{ |
|
421 |
public: |
|
422 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
423 |
QVariant convertToMime(const QString &mime, LPDATAOBJECT pDataObj, QVariant::Type preferredType) const; |
|
424 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
425 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
426 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM *pmedium) const; |
|
427 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
428 |
}; |
|
429 |
||
430 |
bool QWindowsMimeText::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
431 |
{ |
|
432 |
int cf = getCf(formatetc); |
|
433 |
return (cf == CF_UNICODETEXT || cf == CF_TEXT) && mimeData->hasText(); |
|
434 |
} |
|
435 |
||
436 |
/* |
|
437 |
text/plain is defined as using CRLF, but so many programs don't, |
|
438 |
and programmers just look for '\n' in strings. |
|
439 |
Windows really needs CRLF, so we ensure it here. |
|
440 |
*/ |
|
441 |
bool QWindowsMimeText::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM *pmedium) const |
|
442 |
{ |
|
443 |
if (canConvertFromMime(formatetc, mimeData)) { |
|
444 |
QByteArray data; |
|
445 |
int cf = getCf(formatetc); |
|
446 |
if (cf == CF_TEXT) { |
|
447 |
data = mimeData->text().toLocal8Bit(); |
|
448 |
// Anticipate required space for CRLFs at 1/40 |
|
449 |
int maxsize=data.size()+data.size()/40+3; |
|
450 |
QByteArray r(maxsize, '\0'); |
|
451 |
char* o = r.data(); |
|
452 |
const char* d = data.data(); |
|
453 |
const int s = data.size(); |
|
454 |
bool cr=false; |
|
455 |
int j=0; |
|
456 |
for (int i=0; i<s; i++) { |
|
457 |
char c = d[i]; |
|
458 |
if (c=='\r') |
|
459 |
cr=true; |
|
460 |
else { |
|
461 |
if (c=='\n') { |
|
462 |
if (!cr) |
|
463 |
o[j++]='\r'; |
|
464 |
} |
|
465 |
cr=false; |
|
466 |
} |
|
467 |
o[j++]=c; |
|
468 |
if (j+3 >= maxsize) { |
|
469 |
maxsize += maxsize/4; |
|
470 |
r.resize(maxsize); |
|
471 |
o = r.data(); |
|
472 |
} |
|
473 |
} |
|
474 |
o[j]=0; |
|
475 |
return setData(r, pmedium); |
|
476 |
} else if (cf == CF_UNICODETEXT) { |
|
477 |
QString str = mimeData->text(); |
|
478 |
const QChar *u = str.unicode(); |
|
479 |
QString res; |
|
480 |
const int s = str.length(); |
|
481 |
int maxsize = s + s/40 + 3; |
|
482 |
res.resize(maxsize); |
|
483 |
int ri = 0; |
|
484 |
bool cr = false; |
|
485 |
for (int i=0; i < s; ++i) { |
|
486 |
if (*u == QLatin1Char('\r')) |
|
487 |
cr = true; |
|
488 |
else { |
|
489 |
if (*u == QLatin1Char('\n') && !cr) |
|
490 |
res[ri++] = QLatin1Char('\r'); |
|
491 |
cr = false; |
|
492 |
} |
|
493 |
res[ri++] = *u; |
|
494 |
if (ri+3 >= maxsize) { |
|
495 |
maxsize += maxsize/4; |
|
496 |
res.resize(maxsize); |
|
497 |
} |
|
498 |
++u; |
|
499 |
} |
|
500 |
res.truncate(ri); |
|
501 |
const int byteLength = res.length() * sizeof(ushort); |
|
502 |
QByteArray r(byteLength + 2, '\0'); |
|
503 |
memcpy(r.data(), res.unicode(), byteLength); |
|
504 |
r[byteLength] = 0; |
|
505 |
r[byteLength+1] = 0; |
|
506 |
return setData(r, pmedium); |
|
507 |
} |
|
508 |
} |
|
509 |
return false; |
|
510 |
} |
|
511 |
||
512 |
bool QWindowsMimeText::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
513 |
{ |
|
514 |
return mimeType.startsWith(QLatin1String("text/plain")) |
|
515 |
&& (canGetData(CF_UNICODETEXT, pDataObj) |
|
516 |
|| canGetData(CF_TEXT, pDataObj)); |
|
517 |
} |
|
518 |
||
519 |
QString QWindowsMimeText::mimeForFormat(const FORMATETC &formatetc) const |
|
520 |
{ |
|
521 |
int cf = getCf(formatetc); |
|
522 |
if (cf == CF_UNICODETEXT || cf == CF_TEXT) |
|
523 |
return QLatin1String("text/plain"); |
|
524 |
return QString(); |
|
525 |
} |
|
526 |
||
527 |
||
528 |
QVector<FORMATETC> QWindowsMimeText::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
529 |
{ |
|
530 |
QVector<FORMATETC> formatics; |
|
531 |
if (mimeType.startsWith(QLatin1String("text/plain")) && mimeData->hasText()) { |
|
532 |
formatics += setCf(CF_UNICODETEXT); |
|
533 |
formatics += setCf(CF_TEXT); |
|
534 |
} |
|
535 |
return formatics; |
|
536 |
} |
|
537 |
||
538 |
QVariant QWindowsMimeText::convertToMime(const QString &mime, LPDATAOBJECT pDataObj, QVariant::Type preferredType) const |
|
539 |
{ |
|
540 |
QVariant ret; |
|
541 |
||
542 |
if (canConvertToMime(mime, pDataObj)) { |
|
543 |
QString str; |
|
544 |
QByteArray data = getData(CF_UNICODETEXT, pDataObj); |
|
545 |
if (!data.isEmpty()) { |
|
546 |
str = QString::fromWCharArray((const wchar_t *)data.data()); |
|
547 |
str.replace(QLatin1String("\r\n"), QLatin1String("\n")); |
|
548 |
} else { |
|
549 |
data = getData(CF_TEXT, pDataObj); |
|
550 |
if (!data.isEmpty()) { |
|
551 |
const char* d = data.data(); |
|
552 |
const int s = qstrlen(d); |
|
553 |
QByteArray r(data.size()+1, '\0'); |
|
554 |
char* o = r.data(); |
|
555 |
int j=0; |
|
556 |
for (int i=0; i<s; i++) { |
|
557 |
char c = d[i]; |
|
558 |
if (c!='\r') |
|
559 |
o[j++]=c; |
|
560 |
} |
|
561 |
o[j]=0; |
|
562 |
str = QString::fromLocal8Bit(r); |
|
563 |
} |
|
564 |
} |
|
565 |
if (preferredType == QVariant::String) |
|
566 |
ret = str; |
|
567 |
else |
|
568 |
ret = str.toUtf8(); |
|
569 |
} |
|
570 |
||
571 |
return ret; |
|
572 |
} |
|
573 |
||
574 |
class QWindowsMimeURI : public QWindowsMime |
|
575 |
{ |
|
576 |
public: |
|
577 |
QWindowsMimeURI(); |
|
578 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
579 |
QVariant convertToMime(const QString &mime, LPDATAOBJECT pDataObj, QVariant::Type preferredType) const; |
|
580 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
581 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
582 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM *pmedium) const; |
|
583 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
584 |
private: |
|
585 |
int CF_INETURL_W; // wide char version |
|
586 |
int CF_INETURL; |
|
587 |
}; |
|
588 |
||
589 |
QWindowsMimeURI::QWindowsMimeURI() |
|
590 |
{ |
|
591 |
CF_INETURL_W = QWindowsMime::registerMimeType(QLatin1String("UniformResourceLocatorW")); |
|
592 |
CF_INETURL = QWindowsMime::registerMimeType(QLatin1String("UniformResourceLocator")); |
|
593 |
} |
|
594 |
||
595 |
bool QWindowsMimeURI::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
596 |
{ |
|
597 |
if (getCf(formatetc) == CF_HDROP) { |
|
598 |
QList<QUrl> urls = mimeData->urls(); |
|
599 |
for (int i=0; i<urls.size(); i++) { |
|
600 |
if (!urls.at(i).toLocalFile().isEmpty()) |
|
601 |
return true; |
|
602 |
} |
|
603 |
} |
|
604 |
return (getCf(formatetc) == CF_INETURL_W || getCf(formatetc) == CF_INETURL) && mimeData->hasFormat(QLatin1String("text/uri-list")); |
|
605 |
} |
|
606 |
||
607 |
bool QWindowsMimeURI::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM *pmedium) const |
|
608 |
{ |
|
609 |
if (canConvertFromMime(formatetc, mimeData)) { |
|
610 |
if (getCf(formatetc) == CF_HDROP) { |
|
611 |
QList<QUrl> urls = mimeData->urls(); |
|
612 |
QStringList fileNames; |
|
613 |
int size = sizeof(DROPFILES)+2; |
|
614 |
for (int i=0; i<urls.size(); i++) { |
|
615 |
QString fn = QDir::toNativeSeparators(urls.at(i).toLocalFile()); |
|
616 |
if (!fn.isEmpty()) { |
|
617 |
size += sizeof(ushort) * (fn.length() + 1); |
|
618 |
fileNames.append(fn); |
|
619 |
} |
|
620 |
} |
|
621 |
||
622 |
QByteArray result(size, '\0'); |
|
623 |
DROPFILES* d = (DROPFILES*)result.data(); |
|
624 |
d->pFiles = sizeof(DROPFILES); |
|
625 |
GetCursorPos(&d->pt); // try |
|
626 |
d->fNC = true; |
|
627 |
char* files = ((char*)d) + d->pFiles; |
|
628 |
||
629 |
d->fWide = true; |
|
630 |
wchar_t* f = (wchar_t*)files; |
|
631 |
for (int i=0; i<fileNames.size(); i++) { |
|
632 |
int l = fileNames.at(i).length(); |
|
633 |
memcpy(f, fileNames.at(i).utf16(), l * sizeof(ushort)); |
|
634 |
f += l; |
|
635 |
*f++ = 0; |
|
636 |
} |
|
637 |
*f = 0; |
|
638 |
||
639 |
return setData(result, pmedium); |
|
640 |
} else if (getCf(formatetc) == CF_INETURL_W) { |
|
641 |
QList<QUrl> urls = mimeData->urls(); |
|
642 |
QByteArray result; |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
643 |
if (!urls.isEmpty()) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
644 |
QString url = urls.at(0).toString(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
645 |
result = QByteArray((const char *)url.utf16(), url.length() * sizeof(ushort)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
646 |
} |
0 | 647 |
result.append('\0'); |
648 |
result.append('\0'); |
|
649 |
return setData(result, pmedium); |
|
650 |
} else if (getCf(formatetc) == CF_INETURL) { |
|
651 |
QList<QUrl> urls = mimeData->urls(); |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
652 |
QByteArray result; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
653 |
if (!urls.isEmpty()) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
654 |
result = urls.at(0).toString().toLocal8Bit(); |
0 | 655 |
return setData(result, pmedium); |
656 |
} |
|
657 |
} |
|
658 |
||
659 |
return false; |
|
660 |
} |
|
661 |
||
662 |
bool QWindowsMimeURI::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
663 |
{ |
|
664 |
return mimeType == QLatin1String("text/uri-list") |
|
665 |
&& (canGetData(CF_HDROP, pDataObj) || canGetData(CF_INETURL_W, pDataObj) || canGetData(CF_INETURL, pDataObj)); |
|
666 |
} |
|
667 |
||
668 |
QString QWindowsMimeURI::mimeForFormat(const FORMATETC &formatetc) const |
|
669 |
{ |
|
670 |
QString format; |
|
671 |
if (getCf(formatetc) == CF_HDROP || getCf(formatetc) == CF_INETURL_W || getCf(formatetc) == CF_INETURL) |
|
672 |
format = QLatin1String("text/uri-list"); |
|
673 |
return format; |
|
674 |
} |
|
675 |
||
676 |
QVector<FORMATETC> QWindowsMimeURI::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
677 |
{ |
|
678 |
QVector<FORMATETC> formatics; |
|
679 |
if (mimeType == QLatin1String("text/uri-list")) { |
|
680 |
if (canConvertFromMime(setCf(CF_HDROP), mimeData)) |
|
681 |
formatics += setCf(CF_HDROP); |
|
682 |
if (canConvertFromMime(setCf(CF_INETURL_W), mimeData)) |
|
683 |
formatics += setCf(CF_INETURL_W); |
|
684 |
if (canConvertFromMime(setCf(CF_INETURL), mimeData)) |
|
685 |
formatics += setCf(CF_INETURL); |
|
686 |
} |
|
687 |
return formatics; |
|
688 |
} |
|
689 |
||
690 |
QVariant QWindowsMimeURI::convertToMime(const QString &mimeType, LPDATAOBJECT pDataObj, QVariant::Type preferredType) const |
|
691 |
{ |
|
692 |
if (mimeType == QLatin1String("text/uri-list")) { |
|
693 |
if (canGetData(CF_HDROP, pDataObj)) { |
|
694 |
QByteArray texturi; |
|
695 |
QList<QVariant> urls; |
|
696 |
||
697 |
QByteArray data = getData(CF_HDROP, pDataObj); |
|
698 |
if (data.isEmpty()) |
|
699 |
return QVariant(); |
|
700 |
||
701 |
LPDROPFILES hdrop = (LPDROPFILES)data.data(); |
|
702 |
if (hdrop->fWide) { |
|
703 |
const wchar_t* filesw = (const wchar_t *)(data.data() + hdrop->pFiles); |
|
704 |
int i = 0; |
|
705 |
while (filesw[i]) { |
|
706 |
QString fileurl = QString::fromWCharArray(filesw + i); |
|
707 |
urls += QUrl::fromLocalFile(fileurl); |
|
708 |
i += fileurl.length()+1; |
|
709 |
} |
|
710 |
} else { |
|
711 |
const char* files = (const char *)data.data() + hdrop->pFiles; |
|
712 |
int i=0; |
|
713 |
while (files[i]) { |
|
714 |
urls += QUrl::fromLocalFile(QString::fromLocal8Bit(files+i)); |
|
715 |
i += int(strlen(files+i))+1; |
|
716 |
} |
|
717 |
} |
|
718 |
||
719 |
if (preferredType == QVariant::Url && urls.size() == 1) |
|
720 |
return urls.at(0); |
|
721 |
else if (!urls.isEmpty()) |
|
722 |
return urls; |
|
723 |
} else if (canGetData(CF_INETURL_W, pDataObj)) { |
|
724 |
QByteArray data = getData(CF_INETURL_W, pDataObj); |
|
725 |
if (data.isEmpty()) |
|
726 |
return QVariant(); |
|
727 |
return QUrl(QString::fromWCharArray((const wchar_t *)data.constData())); |
|
728 |
} else if (canGetData(CF_INETURL, pDataObj)) { |
|
729 |
QByteArray data = getData(CF_INETURL, pDataObj); |
|
730 |
if (data.isEmpty()) |
|
731 |
return QVariant(); |
|
732 |
return QUrl(QString::fromLocal8Bit(data.constData())); |
|
733 |
} |
|
734 |
} |
|
735 |
return QVariant(); |
|
736 |
} |
|
737 |
||
738 |
class QWindowsMimeHtml : public QWindowsMime |
|
739 |
{ |
|
740 |
public: |
|
741 |
QWindowsMimeHtml(); |
|
742 |
||
743 |
// for converting from Qt |
|
744 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
745 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const; |
|
746 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
747 |
||
748 |
// for converting to Qt |
|
749 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
750 |
QVariant convertToMime(const QString &mime, IDataObject *pDataObj, QVariant::Type preferredType) const; |
|
751 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
752 |
||
753 |
private: |
|
754 |
int CF_HTML; |
|
755 |
}; |
|
756 |
||
757 |
QWindowsMimeHtml::QWindowsMimeHtml() |
|
758 |
{ |
|
759 |
CF_HTML = QWindowsMime::registerMimeType(QLatin1String("HTML Format")); |
|
760 |
} |
|
761 |
||
762 |
QVector<FORMATETC> QWindowsMimeHtml::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
763 |
{ |
|
764 |
QVector<FORMATETC> formatetcs; |
|
765 |
if (mimeType == QLatin1String("text/html") && (!mimeData->html().isEmpty())) |
|
766 |
formatetcs += setCf(CF_HTML); |
|
767 |
return formatetcs; |
|
768 |
} |
|
769 |
||
770 |
QString QWindowsMimeHtml::mimeForFormat(const FORMATETC &formatetc) const |
|
771 |
{ |
|
772 |
if (getCf(formatetc) == CF_HTML) |
|
773 |
return QLatin1String("text/html"); |
|
774 |
return QString(); |
|
775 |
} |
|
776 |
||
777 |
bool QWindowsMimeHtml::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
778 |
{ |
|
779 |
return mimeType == QLatin1String("text/html") && canGetData(CF_HTML, pDataObj); |
|
780 |
} |
|
781 |
||
782 |
||
783 |
bool QWindowsMimeHtml::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
784 |
{ |
|
785 |
return getCf(formatetc) == CF_HTML && (!mimeData->html().isEmpty()); |
|
786 |
} |
|
787 |
||
788 |
/* |
|
789 |
The windows HTML clipboard format is as follows (xxxxxxxxxx is a 10 integer number giving the positions |
|
790 |
in bytes). Charset used is mostly utf8, but can be different, ie. we have to look for the <meta> charset tag |
|
791 |
||
792 |
Version: 1.0 |
|
793 |
StartHTML:xxxxxxxxxx |
|
794 |
EndHTML:xxxxxxxxxx |
|
795 |
StartFragment:xxxxxxxxxx |
|
796 |
EndFragment:xxxxxxxxxx |
|
797 |
...html... |
|
798 |
||
799 |
*/ |
|
800 |
QVariant QWindowsMimeHtml::convertToMime(const QString &mime, IDataObject *pDataObj, QVariant::Type preferredType) const |
|
801 |
{ |
|
802 |
Q_UNUSED(preferredType); |
|
803 |
QVariant result; |
|
804 |
if (canConvertToMime(mime, pDataObj)) { |
|
805 |
QByteArray html = getData(CF_HTML, pDataObj); |
|
806 |
#ifdef QMIME_DEBUG |
|
807 |
qDebug("QWindowsMimeHtml::convertToMime"); |
|
808 |
qDebug("raw :"); |
|
809 |
qDebug(html); |
|
810 |
#endif |
|
811 |
int start = html.indexOf("StartFragment:"); |
|
812 |
int end = html.indexOf("EndFragment:"); |
|
813 |
||
814 |
if (start != -1) { |
|
815 |
int startOffset = start + 14; |
|
816 |
int i = startOffset; |
|
817 |
while (html.at(i) != '\r' && html.at(i) != '\n') |
|
818 |
++i; |
|
819 |
QByteArray bytecount = html.mid(startOffset, i - startOffset); |
|
820 |
start = bytecount.toInt(); |
|
821 |
} |
|
822 |
||
823 |
if (end != -1) { |
|
824 |
int endOffset = end + 12; |
|
825 |
int i = endOffset ; |
|
826 |
while (html.at(i) != '\r' && html.at(i) != '\n') |
|
827 |
++i; |
|
828 |
QByteArray bytecount = html.mid(endOffset , i - endOffset); |
|
829 |
end = bytecount.toInt(); |
|
830 |
} |
|
831 |
||
832 |
if (end > start && start > 0) { |
|
833 |
html = "<!--StartFragment-->" + html.mid(start, end - start); |
|
834 |
html += "<!--EndFragment-->"; |
|
835 |
html.replace('\r', ""); |
|
836 |
result = QString::fromUtf8(html); |
|
837 |
} |
|
838 |
} |
|
839 |
return result; |
|
840 |
} |
|
841 |
||
842 |
bool QWindowsMimeHtml::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const |
|
843 |
{ |
|
844 |
if (canConvertFromMime(formatetc, mimeData)) { |
|
845 |
QByteArray data = mimeData->html().toUtf8(); |
|
846 |
QByteArray result = |
|
847 |
"Version:1.0\r\n" // 0-12 |
|
848 |
"StartHTML:0000000105\r\n" // 13-35 |
|
849 |
"EndHTML:0000000000\r\n" // 36-55 |
|
850 |
"StartFragment:0000000000\r\n" // 58-86 |
|
851 |
"EndFragment:0000000000\r\n\r\n"; // 87-105 |
|
852 |
||
853 |
if (data.indexOf("<!--StartFragment-->") == -1) |
|
854 |
result += "<!--StartFragment-->"; |
|
855 |
result += data; |
|
856 |
if (data.indexOf("<!--EndFragment-->") == -1) |
|
857 |
result += "<!--EndFragment-->"; |
|
858 |
||
859 |
// set the correct number for EndHTML |
|
860 |
QByteArray pos = QString::number(result.size()).toLatin1(); |
|
861 |
memcpy((char *)(result.data() + 53 - pos.length()), pos.constData(), pos.length()); |
|
862 |
||
863 |
// set correct numbers for StartFragment and EndFragment |
|
864 |
pos = QString::number(result.indexOf("<!--StartFragment-->") + 20).toLatin1(); |
|
865 |
memcpy((char *)(result.data() + 79 - pos.length()), pos.constData(), pos.length()); |
|
866 |
pos = QString::number(result.indexOf("<!--EndFragment-->")).toLatin1(); |
|
867 |
memcpy((char *)(result.data() + 103 - pos.length()), pos.constData(), pos.length()); |
|
868 |
||
869 |
return setData(result, pmedium); |
|
870 |
} |
|
871 |
return false; |
|
872 |
} |
|
873 |
||
874 |
||
875 |
#ifndef QT_NO_IMAGEFORMAT_BMP |
|
876 |
class QWindowsMimeImage : public QWindowsMime |
|
877 |
{ |
|
878 |
public: |
|
879 |
QWindowsMimeImage(); |
|
880 |
// for converting from Qt |
|
881 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
882 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const; |
|
883 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
884 |
||
885 |
// for converting to Qt |
|
886 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
887 |
QVariant convertToMime(const QString &mime, IDataObject *pDataObj, QVariant::Type preferredType) const; |
|
888 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
889 |
private: |
|
890 |
bool hasOriginalDIBV5(IDataObject *pDataObj) const; |
|
891 |
UINT CF_PNG; |
|
892 |
}; |
|
893 |
||
894 |
QWindowsMimeImage::QWindowsMimeImage() |
|
895 |
{ |
|
896 |
CF_PNG = RegisterClipboardFormat(L"PNG"); |
|
897 |
} |
|
898 |
||
899 |
QVector<FORMATETC> QWindowsMimeImage::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
900 |
{ |
|
901 |
QVector<FORMATETC> formatetcs; |
|
902 |
if (mimeData->hasImage() && mimeType == QLatin1String("application/x-qt-image")) { |
|
903 |
//add DIBV5 if image has alpha channel |
|
904 |
QImage image = qvariant_cast<QImage>(mimeData->imageData()); |
|
905 |
if (!image.isNull() && image.hasAlphaChannel()) |
|
906 |
formatetcs += setCf(CF_DIBV5); |
|
907 |
formatetcs += setCf(CF_DIB); |
|
908 |
} |
|
909 |
return formatetcs; |
|
910 |
} |
|
911 |
||
912 |
QString QWindowsMimeImage::mimeForFormat(const FORMATETC &formatetc) const |
|
913 |
{ |
|
914 |
int cf = getCf(formatetc); |
|
915 |
if (cf == CF_DIB || cf == CF_DIBV5 || cf == int(CF_PNG)) |
|
916 |
return QLatin1String("application/x-qt-image"); |
|
917 |
return QString(); |
|
918 |
} |
|
919 |
||
920 |
bool QWindowsMimeImage::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
921 |
{ |
|
922 |
if ((mimeType == QLatin1String("application/x-qt-image")) && |
|
923 |
(canGetData(CF_DIB, pDataObj) || canGetData(CF_PNG, pDataObj))) |
|
924 |
return true; |
|
925 |
return false; |
|
926 |
} |
|
927 |
||
928 |
bool QWindowsMimeImage::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
929 |
{ |
|
930 |
int cf = getCf(formatetc); |
|
931 |
if (mimeData->hasImage()) { |
|
932 |
if (cf == CF_DIB) |
|
933 |
return true; |
|
934 |
else if (cf == CF_DIBV5) { |
|
935 |
//support DIBV5 conversion only if the image has alpha channel |
|
936 |
QImage image = qvariant_cast<QImage>(mimeData->imageData()); |
|
937 |
if (!image.isNull() && image.hasAlphaChannel()) |
|
938 |
return true; |
|
939 |
} |
|
940 |
} |
|
941 |
return false; |
|
942 |
} |
|
943 |
||
944 |
bool QWindowsMimeImage::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const |
|
945 |
{ |
|
946 |
int cf = getCf(formatetc); |
|
947 |
if ((cf == CF_DIB || cf == CF_DIBV5) && mimeData->hasImage()) { |
|
948 |
QImage img = qvariant_cast<QImage>(mimeData->imageData()); |
|
949 |
if (img.isNull()) |
|
950 |
return false; |
|
951 |
QByteArray ba; |
|
952 |
QDataStream s(&ba, QIODevice::WriteOnly); |
|
953 |
s.setByteOrder(QDataStream::LittleEndian);// Intel byte order #### |
|
954 |
if (cf == CF_DIB) { |
|
955 |
if (qt_write_dib(s, img)) |
|
956 |
return setData(ba, pmedium); |
|
957 |
} else { |
|
958 |
if (qt_write_dibv5(s, img)) |
|
959 |
return setData(ba, pmedium); |
|
960 |
} |
|
961 |
} |
|
962 |
return false; |
|
963 |
} |
|
964 |
||
965 |
bool QWindowsMimeImage::hasOriginalDIBV5(IDataObject *pDataObj) const |
|
966 |
{ |
|
967 |
bool isSynthesized = true; |
|
968 |
IEnumFORMATETC *pEnum =NULL; |
|
969 |
HRESULT res = pDataObj->EnumFormatEtc(1, &pEnum); |
|
970 |
if (res == S_OK && pEnum) { |
|
971 |
FORMATETC fc; |
|
972 |
while ((res = pEnum->Next(1, &fc, 0)) == S_OK) { |
|
973 |
if (fc.ptd) |
|
974 |
CoTaskMemFree(fc.ptd); |
|
975 |
if (fc.cfFormat == CF_DIB) |
|
976 |
break; |
|
977 |
else if (fc.cfFormat == CF_DIBV5) { |
|
978 |
isSynthesized = false; |
|
979 |
break; |
|
980 |
} |
|
981 |
} |
|
982 |
pEnum->Release(); |
|
983 |
} |
|
984 |
return !isSynthesized; |
|
985 |
} |
|
986 |
||
987 |
QVariant QWindowsMimeImage::convertToMime(const QString &mimeType, IDataObject *pDataObj, QVariant::Type preferredType) const |
|
988 |
{ |
|
989 |
Q_UNUSED(preferredType); |
|
990 |
QVariant result; |
|
991 |
if (mimeType != QLatin1String("application/x-qt-image")) |
|
992 |
return result; |
|
993 |
//Try to convert from a format which has more data |
|
994 |
//DIBV5, use only if its is not synthesized |
|
995 |
if (canGetData(CF_DIBV5, pDataObj) && hasOriginalDIBV5(pDataObj)) { |
|
996 |
QImage img; |
|
997 |
QByteArray data = getData(CF_DIBV5, pDataObj); |
|
998 |
QDataStream s(&data, QIODevice::ReadOnly); |
|
999 |
s.setByteOrder(QDataStream::LittleEndian); |
|
1000 |
if (qt_read_dibv5(s, img)) { // #### supports only 32bit DIBV5 |
|
1001 |
return img; |
|
1002 |
} |
|
1003 |
} |
|
1004 |
//PNG, MS Office place this (undocumented) |
|
1005 |
if (canGetData(CF_PNG, pDataObj)) { |
|
1006 |
QImage img; |
|
1007 |
QByteArray data = getData(CF_PNG, pDataObj); |
|
1008 |
if (img.loadFromData(data, "PNG")) { |
|
1009 |
return img; |
|
1010 |
} |
|
1011 |
} |
|
1012 |
//Fallback to DIB |
|
1013 |
if (canGetData(CF_DIB, pDataObj)) { |
|
1014 |
QImage img; |
|
1015 |
QByteArray data = getData(CF_DIB, pDataObj); |
|
1016 |
QDataStream s(&data, QIODevice::ReadOnly); |
|
1017 |
s.setByteOrder(QDataStream::LittleEndian);// Intel byte order #### |
|
1018 |
if (qt_read_dib(s, img)) { // ##### encaps "-14" |
|
1019 |
return img; |
|
1020 |
} |
|
1021 |
} |
|
1022 |
// Failed |
|
1023 |
return result; |
|
1024 |
} |
|
1025 |
#endif |
|
1026 |
||
1027 |
class QBuiltInMimes : public QWindowsMime |
|
1028 |
{ |
|
1029 |
public: |
|
1030 |
QBuiltInMimes(); |
|
1031 |
||
1032 |
// for converting from Qt |
|
1033 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
1034 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const; |
|
1035 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
1036 |
||
1037 |
// for converting to Qt |
|
1038 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
1039 |
QVariant convertToMime(const QString &mime, IDataObject *pDataObj, QVariant::Type preferredType) const; |
|
1040 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
1041 |
||
1042 |
private: |
|
1043 |
QMap<int, QString> outFormats; |
|
1044 |
QMap<int, QString> inFormats; |
|
1045 |
}; |
|
1046 |
||
1047 |
QBuiltInMimes::QBuiltInMimes() |
|
1048 |
: QWindowsMime() |
|
1049 |
{ |
|
1050 |
outFormats.insert(QWindowsMime::registerMimeType(QLatin1String("application/x-color")), QLatin1String("application/x-color")); |
|
1051 |
inFormats.insert(QWindowsMime::registerMimeType(QLatin1String("application/x-color")), QLatin1String("application/x-color")); |
|
1052 |
} |
|
1053 |
||
1054 |
bool QBuiltInMimes::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
1055 |
{ |
|
1056 |
// really check |
|
1057 |
return formatetc.tymed & TYMED_HGLOBAL |
|
1058 |
&& outFormats.contains(formatetc.cfFormat) |
|
1059 |
&& mimeData->formats().contains(outFormats.value(formatetc.cfFormat)); |
|
1060 |
} |
|
1061 |
||
1062 |
bool QBuiltInMimes::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const |
|
1063 |
{ |
|
1064 |
if (canConvertFromMime(formatetc, mimeData)) { |
|
1065 |
QByteArray data; |
|
1066 |
if (outFormats.value(getCf(formatetc)) == QLatin1String("text/html")) { |
|
1067 |
// text/html is in wide chars on windows (compatible with mozillia) |
|
1068 |
QString html = mimeData->html(); |
|
1069 |
// same code as in the text converter up above |
|
1070 |
const QChar *u = html.unicode(); |
|
1071 |
QString res; |
|
1072 |
const int s = html.length(); |
|
1073 |
int maxsize = s + s/40 + 3; |
|
1074 |
res.resize(maxsize); |
|
1075 |
int ri = 0; |
|
1076 |
bool cr = false; |
|
1077 |
for (int i=0; i < s; ++i) { |
|
1078 |
if (*u == QLatin1Char('\r')) |
|
1079 |
cr = true; |
|
1080 |
else { |
|
1081 |
if (*u == QLatin1Char('\n') && !cr) |
|
1082 |
res[ri++] = QLatin1Char('\r'); |
|
1083 |
cr = false; |
|
1084 |
} |
|
1085 |
res[ri++] = *u; |
|
1086 |
if (ri+3 >= maxsize) { |
|
1087 |
maxsize += maxsize/4; |
|
1088 |
res.resize(maxsize); |
|
1089 |
} |
|
1090 |
++u; |
|
1091 |
} |
|
1092 |
res.truncate(ri); |
|
1093 |
const int byteLength = res.length() * sizeof(ushort); |
|
1094 |
QByteArray r(byteLength + 2, '\0'); |
|
1095 |
memcpy(r.data(), res.unicode(), byteLength); |
|
1096 |
r[byteLength] = 0; |
|
1097 |
r[byteLength+1] = 0; |
|
1098 |
data = r; |
|
1099 |
} else { |
|
1100 |
#ifndef QT_NO_DRAGANDDROP |
|
1101 |
data = QInternalMimeData::renderDataHelper(outFormats.value(getCf(formatetc)), mimeData); |
|
1102 |
#endif //QT_NO_DRAGANDDROP |
|
1103 |
} |
|
1104 |
return setData(data, pmedium); |
|
1105 |
} |
|
1106 |
return false; |
|
1107 |
} |
|
1108 |
||
1109 |
QVector<FORMATETC> QBuiltInMimes::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const |
|
1110 |
{ |
|
1111 |
QVector<FORMATETC> formatetcs; |
|
1112 |
if (!outFormats.keys(mimeType).isEmpty() && mimeData->formats().contains(mimeType)) |
|
1113 |
formatetcs += setCf(outFormats.key(mimeType)); |
|
1114 |
return formatetcs; |
|
1115 |
} |
|
1116 |
||
1117 |
bool QBuiltInMimes::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
1118 |
{ |
|
1119 |
return (!inFormats.keys(mimeType).isEmpty()) |
|
1120 |
&& canGetData(inFormats.key(mimeType), pDataObj); |
|
1121 |
} |
|
1122 |
||
1123 |
QVariant QBuiltInMimes::convertToMime(const QString &mimeType, IDataObject *pDataObj, QVariant::Type preferredType) const |
|
1124 |
{ |
|
1125 |
QVariant val; |
|
1126 |
if (canConvertToMime(mimeType, pDataObj)) { |
|
1127 |
QByteArray data = getData(inFormats.key(mimeType), pDataObj); |
|
1128 |
if (!data.isEmpty()) { |
|
1129 |
#ifdef QMIME_DEBUG |
|
1130 |
qDebug("QBuiltInMimes::convertToMime()"); |
|
1131 |
#endif |
|
1132 |
if (mimeType == QLatin1String("text/html") && preferredType == QVariant::String) { |
|
1133 |
// text/html is in wide chars on windows (compatible with Mozilla) |
|
1134 |
val = QString::fromWCharArray((const wchar_t *)data.data()); |
|
1135 |
} else { |
|
1136 |
val = data; // it should be enough to return the data and let QMimeData do the rest. |
|
1137 |
} |
|
1138 |
} |
|
1139 |
} |
|
1140 |
return val; |
|
1141 |
} |
|
1142 |
||
1143 |
QString QBuiltInMimes::mimeForFormat(const FORMATETC &formatetc) const |
|
1144 |
{ |
|
1145 |
return inFormats.value(getCf(formatetc)); |
|
1146 |
} |
|
1147 |
||
1148 |
||
1149 |
class QLastResortMimes : public QWindowsMime |
|
1150 |
{ |
|
1151 |
public: |
|
1152 |
||
1153 |
QLastResortMimes(); |
|
1154 |
// for converting from Qt |
|
1155 |
bool canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const; |
|
1156 |
bool convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const; |
|
1157 |
QVector<FORMATETC> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const; |
|
1158 |
||
1159 |
// for converting to Qt |
|
1160 |
bool canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const; |
|
1161 |
QVariant convertToMime(const QString &mime, IDataObject *pDataObj, QVariant::Type preferredType) const; |
|
1162 |
QString mimeForFormat(const FORMATETC &formatetc) const; |
|
1163 |
||
1164 |
private: |
|
1165 |
QMap<int, QString> formats; |
|
1166 |
static QStringList ianaTypes; |
|
1167 |
static QStringList excludeList; |
|
1168 |
}; |
|
1169 |
||
1170 |
QStringList QLastResortMimes::ianaTypes; |
|
1171 |
QStringList QLastResortMimes::excludeList; |
|
1172 |
||
1173 |
QLastResortMimes::QLastResortMimes() |
|
1174 |
{ |
|
1175 |
//MIME Media-Types |
|
1176 |
if (!ianaTypes.size()) { |
|
1177 |
ianaTypes.append(QLatin1String("application/")); |
|
1178 |
ianaTypes.append(QLatin1String("audio/")); |
|
1179 |
ianaTypes.append(QLatin1String("example/")); |
|
1180 |
ianaTypes.append(QLatin1String("image/")); |
|
1181 |
ianaTypes.append(QLatin1String("message/")); |
|
1182 |
ianaTypes.append(QLatin1String("model/")); |
|
1183 |
ianaTypes.append(QLatin1String("multipart/")); |
|
1184 |
ianaTypes.append(QLatin1String("text/")); |
|
1185 |
ianaTypes.append(QLatin1String("video/")); |
|
1186 |
} |
|
1187 |
//Types handled by other classes |
|
1188 |
if (!excludeList.size()) { |
|
1189 |
excludeList.append(QLatin1String("HTML Format")); |
|
1190 |
excludeList.append(QLatin1String("UniformResourceLocator")); |
|
1191 |
excludeList.append(QLatin1String("text/html")); |
|
1192 |
excludeList.append(QLatin1String("text/plain")); |
|
1193 |
excludeList.append(QLatin1String("text/uri-list")); |
|
1194 |
excludeList.append(QLatin1String("application/x-qt-image")); |
|
1195 |
excludeList.append(QLatin1String("application/x-color")); |
|
1196 |
} |
|
1197 |
} |
|
1198 |
||
1199 |
bool QLastResortMimes::canConvertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData) const |
|
1200 |
{ |
|
1201 |
// really check |
|
1202 |
#ifndef QT_NO_DRAGANDDROP |
|
1203 |
return formatetc.tymed & TYMED_HGLOBAL |
|
1204 |
&& (formats.contains(formatetc.cfFormat) |
|
1205 |
&& QInternalMimeData::hasFormatHelper(formats.value(formatetc.cfFormat), mimeData)); |
|
1206 |
#else |
|
1207 |
Q_UNUSED(mimeData); |
|
1208 |
Q_UNUSED(formatetc); |
|
1209 |
return formatetc.tymed & TYMED_HGLOBAL |
|
1210 |
&& formats.contains(formatetc.cfFormat); |
|
1211 |
#endif //QT_NO_DRAGANDDROP |
|
1212 |
} |
|
1213 |
||
1214 |
bool QLastResortMimes::convertFromMime(const FORMATETC &formatetc, const QMimeData *mimeData, STGMEDIUM * pmedium) const |
|
1215 |
{ |
|
1216 |
#ifndef QT_NO_DRAGANDDROP |
|
1217 |
return canConvertFromMime(formatetc, mimeData) |
|
1218 |
&& setData(QInternalMimeData::renderDataHelper(formats.value(getCf(formatetc)), mimeData), pmedium); |
|
1219 |
#else |
|
1220 |
Q_UNUSED(mimeData); |
|
1221 |
Q_UNUSED(formatetc); |
|
1222 |
Q_UNUSED(pmedium); |
|
1223 |
return false; |
|
1224 |
#endif //QT_NO_DRAGANDDROP |
|
1225 |
} |
|
1226 |
||
1227 |
QVector<FORMATETC> QLastResortMimes::formatsForMime(const QString &mimeType, const QMimeData * /*mimeData*/) const |
|
1228 |
{ |
|
1229 |
QVector<FORMATETC> formatetcs; |
|
1230 |
if (!formats.keys(mimeType).isEmpty()) { |
|
1231 |
formatetcs += setCf(formats.key(mimeType)); |
|
1232 |
} else if (!excludeList.contains(mimeType, Qt::CaseInsensitive)){ |
|
1233 |
// register any other available formats |
|
1234 |
int cf = QWindowsMime::registerMimeType(mimeType); |
|
1235 |
QLastResortMimes *that = const_cast<QLastResortMimes *>(this); |
|
1236 |
that->formats.insert(cf, mimeType); |
|
1237 |
formatetcs += setCf(cf); |
|
1238 |
} |
|
1239 |
return formatetcs; |
|
1240 |
} |
|
1241 |
static const char x_qt_windows_mime[] = "application/x-qt-windows-mime;value=\""; |
|
1242 |
||
1243 |
static bool isCustomMimeType(const QString &mimeType) |
|
1244 |
{ |
|
1245 |
return mimeType.startsWith(QLatin1String(x_qt_windows_mime), Qt::CaseInsensitive); |
|
1246 |
} |
|
1247 |
||
1248 |
static QString customMimeType(const QString &mimeType) |
|
1249 |
{ |
|
1250 |
int len = sizeof(x_qt_windows_mime) - 1; |
|
1251 |
int n = mimeType.lastIndexOf(QLatin1Char('\"'))-len; |
|
1252 |
return mimeType.mid(len, n); |
|
1253 |
} |
|
1254 |
||
1255 |
bool QLastResortMimes::canConvertToMime(const QString &mimeType, IDataObject *pDataObj) const |
|
1256 |
{ |
|
1257 |
if (isCustomMimeType(mimeType)) { |
|
1258 |
QString clipFormat = customMimeType(mimeType); |
|
1259 |
int cf = RegisterClipboardFormat(reinterpret_cast<const wchar_t *> (clipFormat.utf16())); |
|
1260 |
return canGetData(cf, pDataObj); |
|
1261 |
} else if (formats.keys(mimeType).isEmpty()) { |
|
1262 |
// if it is not in there then register it an see if we can get it |
|
1263 |
int cf = QWindowsMime::registerMimeType(mimeType); |
|
1264 |
return canGetData(cf, pDataObj); |
|
1265 |
} else { |
|
1266 |
return canGetData(formats.key(mimeType), pDataObj); |
|
1267 |
} |
|
1268 |
return false; |
|
1269 |
} |
|
1270 |
||
1271 |
QVariant QLastResortMimes::convertToMime(const QString &mimeType, IDataObject *pDataObj, QVariant::Type preferredType) const |
|
1272 |
{ |
|
1273 |
Q_UNUSED(preferredType); |
|
1274 |
QVariant val; |
|
1275 |
if (canConvertToMime(mimeType, pDataObj)) { |
|
1276 |
QByteArray data; |
|
1277 |
if (isCustomMimeType(mimeType)) { |
|
1278 |
QString clipFormat = customMimeType(mimeType); |
|
1279 |
int cf = RegisterClipboardFormat(reinterpret_cast<const wchar_t *> (clipFormat.utf16())); |
|
1280 |
data = getData(cf, pDataObj); |
|
1281 |
} else if (formats.keys(mimeType).isEmpty()) { |
|
1282 |
int cf = QWindowsMime::registerMimeType(mimeType); |
|
1283 |
data = getData(cf, pDataObj); |
|
1284 |
} else { |
|
1285 |
data = getData(formats.key(mimeType), pDataObj); |
|
1286 |
} |
|
1287 |
if (!data.isEmpty()) |
|
1288 |
val = data; // it should be enough to return the data and let QMimeData do the rest. |
|
1289 |
} |
|
1290 |
return val; |
|
1291 |
} |
|
1292 |
||
1293 |
QString QLastResortMimes::mimeForFormat(const FORMATETC &formatetc) const |
|
1294 |
{ |
|
1295 |
QString format = formats.value(getCf(formatetc)); |
|
1296 |
if (!format.isEmpty()) |
|
1297 |
return format; |
|
1298 |
||
1299 |
wchar_t buffer[256]; |
|
1300 |
int len = GetClipboardFormatName(getCf(formatetc), buffer, 256); |
|
1301 |
||
1302 |
if (len) { |
|
1303 |
QString clipFormat = QString::fromWCharArray(buffer, len); |
|
1304 |
#ifndef QT_NO_DRAGANDDROP |
|
1305 |
if (QInternalMimeData::canReadData(clipFormat)) |
|
1306 |
format = clipFormat; |
|
1307 |
else if((formatetc.cfFormat >= 0xC000)){ |
|
1308 |
//create the mime as custom. not registered. |
|
1309 |
if (!excludeList.contains(clipFormat, Qt::CaseInsensitive)) { |
|
1310 |
//check if this is a mime type |
|
1311 |
bool ianaType = false; |
|
1312 |
int sz = ianaTypes.size(); |
|
1313 |
for (int i = 0; i < sz; i++) { |
|
1314 |
if (clipFormat.startsWith(ianaTypes[i], Qt::CaseInsensitive)) { |
|
1315 |
ianaType = true; |
|
1316 |
break; |
|
1317 |
} |
|
1318 |
} |
|
1319 |
if (!ianaType) |
|
1320 |
format = QLatin1String(x_qt_windows_mime) + clipFormat + QLatin1Char('\"'); |
|
1321 |
else |
|
1322 |
format = clipFormat; |
|
1323 |
} |
|
1324 |
} |
|
1325 |
#endif //QT_NO_DRAGANDDROP |
|
1326 |
} |
|
1327 |
||
1328 |
return format; |
|
1329 |
} |
|
1330 |
||
1331 |
QWindowsMimeList::QWindowsMimeList() |
|
1332 |
: initialized(false) |
|
1333 |
{ |
|
1334 |
} |
|
1335 |
||
1336 |
QWindowsMimeList::~QWindowsMimeList() |
|
1337 |
{ |
|
1338 |
while (mimes.size()) |
|
1339 |
delete mimes.first(); |
|
1340 |
} |
|
1341 |
||
1342 |
||
1343 |
void QWindowsMimeList::init() |
|
1344 |
{ |
|
1345 |
if (!initialized) { |
|
1346 |
initialized = true; |
|
1347 |
#ifndef QT_NO_IMAGEFORMAT_BMP |
|
1348 |
new QWindowsMimeImage; |
|
1349 |
#endif |
|
1350 |
new QLastResortMimes; |
|
1351 |
new QWindowsMimeText; |
|
1352 |
new QWindowsMimeURI; |
|
1353 |
||
1354 |
new QWindowsMimeHtml; |
|
1355 |
new QBuiltInMimes; |
|
1356 |
} |
|
1357 |
} |
|
1358 |
||
1359 |
void QWindowsMimeList::addWindowsMime(QWindowsMime * mime) |
|
1360 |
{ |
|
1361 |
init(); |
|
1362 |
mimes.append(mime); |
|
1363 |
} |
|
1364 |
||
1365 |
void QWindowsMimeList::removeWindowsMime(QWindowsMime * mime) |
|
1366 |
{ |
|
1367 |
init(); |
|
1368 |
mimes.removeAll(mime); |
|
1369 |
} |
|
1370 |
||
1371 |
QList<QWindowsMime*> QWindowsMimeList::windowsMimes() |
|
1372 |
{ |
|
1373 |
init(); |
|
1374 |
return mimes; |
|
1375 |
} |
|
1376 |
||
1377 |
#ifndef QT_NO_IMAGEFORMAT_BMP |
|
1378 |
static bool qt_write_dibv5(QDataStream &s, QImage image) |
|
1379 |
{ |
|
1380 |
QIODevice* d = s.device(); |
|
1381 |
if (!d->isWritable()) |
|
1382 |
return false; |
|
1383 |
||
1384 |
//depth will be always 32 |
|
1385 |
int bpl_bmp = image.width()*4; |
|
1386 |
||
1387 |
BMP_BITMAPV5HEADER bi ={0}; |
|
1388 |
bi.bV5Size = sizeof(BMP_BITMAPV5HEADER); |
|
1389 |
bi.bV5Width = image.width(); |
|
1390 |
bi.bV5Height = image.height(); |
|
1391 |
bi.bV5Planes = 1; |
|
1392 |
bi.bV5BitCount = 32; |
|
1393 |
bi.bV5Compression = BI_BITFIELDS; |
|
1394 |
bi.bV5SizeImage = bpl_bmp*image.height(); |
|
1395 |
bi.bV5XPelsPerMeter = 0; |
|
1396 |
bi.bV5YPelsPerMeter = 0; |
|
1397 |
bi.bV5ClrUsed = 0; |
|
1398 |
bi.bV5ClrImportant = 0; |
|
1399 |
bi.bV5BlueMask = 0x000000ff; |
|
1400 |
bi.bV5GreenMask = 0x0000ff00; |
|
1401 |
bi.bV5RedMask = 0x00ff0000; |
|
1402 |
bi.bV5AlphaMask = 0xff000000; |
|
1403 |
bi.bV5CSType = BMP_LCS_sRGB; //LCS_sRGB |
|
1404 |
bi.bV5Intent = BMP_LCS_GM_IMAGES; //LCS_GM_IMAGES |
|
1405 |
||
1406 |
d->write(reinterpret_cast<const char*>(&bi), bi.bV5Size); |
|
1407 |
if (s.status() != QDataStream::Ok) |
|
1408 |
return false; |
|
1409 |
||
1410 |
DWORD colorSpace[3] = {0x00ff0000,0x0000ff00,0x000000ff}; |
|
1411 |
d->write(reinterpret_cast<const char*>(colorSpace), sizeof(colorSpace)); |
|
1412 |
if (s.status() != QDataStream::Ok) |
|
1413 |
return false; |
|
1414 |
||
1415 |
if (image.format() != QImage::Format_ARGB32) |
|
1416 |
image = image.convertToFormat(QImage::Format_ARGB32); |
|
1417 |
||
1418 |
uchar *buf = new uchar[bpl_bmp]; |
|
1419 |
uchar *b; |
|
1420 |
||
1421 |
memset(buf, 0, bpl_bmp); |
|
1422 |
for (int y=image.height()-1; y>=0; y--) { |
|
1423 |
// write the image bits |
|
1424 |
QRgb *p = (QRgb *)image.scanLine(y); |
|
1425 |
QRgb *end = p + image.width(); |
|
1426 |
b = buf; |
|
1427 |
while (p < end) { |
|
1428 |
int alpha = qAlpha(*p); |
|
1429 |
if (alpha) { |
|
1430 |
*b++ = qBlue(*p); |
|
1431 |
*b++ = qGreen(*p); |
|
1432 |
*b++ = qRed(*p); |
|
1433 |
} else { |
|
1434 |
//white for fully transparent pixels. |
|
1435 |
*b++ = 0xff; |
|
1436 |
*b++ = 0xff; |
|
1437 |
*b++ = 0xff; |
|
1438 |
} |
|
1439 |
*b++ = alpha; |
|
1440 |
p++; |
|
1441 |
} |
|
1442 |
d->write((char*)buf, bpl_bmp); |
|
1443 |
if (s.status() != QDataStream::Ok) { |
|
1444 |
delete[] buf; |
|
1445 |
return false; |
|
1446 |
} |
|
1447 |
} |
|
1448 |
delete[] buf; |
|
1449 |
return true; |
|
1450 |
} |
|
1451 |
||
1452 |
static int calc_shift(int mask) |
|
1453 |
{ |
|
1454 |
int result = 0; |
|
1455 |
while (!(mask & 1)) { |
|
1456 |
result++; |
|
1457 |
mask >>= 1; |
|
1458 |
} |
|
1459 |
return result; |
|
1460 |
} |
|
1461 |
||
1462 |
//Supports only 32 bit DIBV5 |
|
1463 |
static bool qt_read_dibv5(QDataStream &s, QImage &image) |
|
1464 |
{ |
|
1465 |
BMP_BITMAPV5HEADER bi; |
|
1466 |
QIODevice* d = s.device(); |
|
1467 |
if (d->atEnd()) |
|
1468 |
return false; |
|
1469 |
||
1470 |
d->read((char *)&bi, sizeof(bi)); // read BITMAPV5HEADER header |
|
1471 |
if (s.status() != QDataStream::Ok) |
|
1472 |
return false; |
|
1473 |
||
1474 |
int nbits = bi.bV5BitCount; |
|
1475 |
int comp = bi.bV5Compression; |
|
1476 |
if (nbits != 32 || bi.bV5Planes != 1 || comp != BMP_BITFIELDS) |
|
1477 |
return false; //Unsupported DIBV5 format |
|
1478 |
||
1479 |
int w = bi.bV5Width, h = bi.bV5Height; |
|
1480 |
int red_mask = bi.bV5RedMask; |
|
1481 |
int green_mask = bi.bV5GreenMask; |
|
1482 |
int blue_mask = bi.bV5BlueMask; |
|
1483 |
int alpha_mask = bi.bV5AlphaMask; |
|
1484 |
int red_shift = 0; |
|
1485 |
int green_shift = 0; |
|
1486 |
int blue_shift = 0; |
|
1487 |
int alpha_shift = 0; |
|
1488 |
QImage::Format format = QImage::Format_ARGB32; |
|
1489 |
||
1490 |
if (bi.bV5Height < 0) |
|
1491 |
h = -h; // support images with negative height |
|
1492 |
if (image.size() != QSize(w, h) || image.format() != format) { |
|
1493 |
image = QImage(w, h, format); |
|
1494 |
if (image.isNull()) // could not create image |
|
1495 |
return false; |
|
1496 |
} |
|
1497 |
image.setDotsPerMeterX(bi.bV5XPelsPerMeter); |
|
1498 |
image.setDotsPerMeterY(bi.bV5YPelsPerMeter); |
|
1499 |
// read color table |
|
1500 |
DWORD colorSpace[3]; |
|
1501 |
if (d->read((char *)colorSpace, sizeof(colorSpace)) != sizeof(colorSpace)) |
|
1502 |
return false; |
|
1503 |
||
1504 |
red_shift = calc_shift(red_mask); |
|
1505 |
green_shift = calc_shift(green_mask); |
|
1506 |
blue_shift = calc_shift(blue_mask); |
|
1507 |
if (alpha_mask) { |
|
1508 |
alpha_shift = calc_shift(alpha_mask); |
|
1509 |
} |
|
1510 |
||
1511 |
int bpl = image.bytesPerLine(); |
|
1512 |
uchar *data = image.bits(); |
|
1513 |
register QRgb *p; |
|
1514 |
QRgb *end; |
|
1515 |
uchar *buf24 = new uchar[bpl]; |
|
1516 |
int bpl24 = ((w*nbits+31)/32)*4; |
|
1517 |
uchar *b; |
|
1518 |
unsigned int c; |
|
1519 |
||
1520 |
while (--h >= 0) { |
|
1521 |
p = (QRgb *)(data + h*bpl); |
|
1522 |
end = p + w; |
|
1523 |
if (d->read((char *)buf24,bpl24) != bpl24) |
|
1524 |
break; |
|
1525 |
b = buf24; |
|
1526 |
while (p < end) { |
|
1527 |
c = *b | (*(b+1))<<8 | (*(b+2))<<16 | (*(b+3))<<24; |
|
1528 |
*p++ = qRgba(((c & red_mask) >> red_shift) , |
|
1529 |
((c & green_mask) >> green_shift), |
|
1530 |
((c & blue_mask) >> blue_shift), |
|
1531 |
((c & alpha_mask) >> alpha_shift)); |
|
1532 |
b += 4; |
|
1533 |
} |
|
1534 |
} |
|
1535 |
delete[] buf24; |
|
1536 |
||
1537 |
if (bi.bV5Height < 0) { |
|
1538 |
// Flip the image |
|
1539 |
uchar *buf = new uchar[bpl]; |
|
1540 |
h = -bi.bV5Height; |
|
1541 |
for (int y = 0; y < h/2; ++y) { |
|
1542 |
memcpy(buf, data + y*bpl, bpl); |
|
1543 |
memcpy(data + y*bpl, data + (h-y-1)*bpl, bpl); |
|
1544 |
memcpy(data + (h-y-1)*bpl, buf, bpl); |
|
1545 |
} |
|
1546 |
delete [] buf; |
|
1547 |
} |
|
1548 |
||
1549 |
return true; |
|
1550 |
} |
|
1551 |
||
1552 |
#endif |
|
1553 |
||
1554 |
QT_END_NAMESPACE |