qdnd.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qdnd.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtGui module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qplatformdefs.h"-
41-
42#include "qbitmap.h"-
43#include "qdrag.h"-
44#include "qpixmap.h"-
45#include "qevent.h"-
46#include "qfile.h"-
47#include "qtextcodec.h"-
48#include "qguiapplication.h"-
49#include "qpoint.h"-
50#include "qbuffer.h"-
51#include "qimage.h"-
52#include "qpainter.h"-
53#include "qregexp.h"-
54#include "qdir.h"-
55#include "qdnd_p.h"-
56#include "qimagereader.h"-
57#include "qimagewriter.h"-
58#include "qdebug.h"-
59#include <ctype.h>-
60#include <qpa/qplatformintegration.h>-
61#include <qpa/qplatformdrag.h>-
62-
63#include <private/qguiapplication_p.h>-
64-
65#ifndef QT_NO_DRAGANDDROP-
66-
67QT_BEGIN_NAMESPACE-
68-
69// the universe's only drag manager-
70QDragManager *QDragManager::m_instance = 0;-
71-
72-
73QDragManager::QDragManager()-
74 : QObject(qApp), m_platformDropData(0), m_currentDropTarget(0),-
75 m_platformDrag(QGuiApplicationPrivate::platformIntegration()->drag()),-
76 m_object(0)-
77{-
78 Q_ASSERT(!m_instance);-
79-
80 if (m_platformDrag)
m_platformDragDescription
TRUEnever evaluated
FALSEnever evaluated
0
81 m_platformDropData = m_platformDrag->platformDropData();
never executed: m_platformDropData = m_platformDrag->platformDropData();
0
82}
never executed: end of block
0
83-
84-
85QDragManager::~QDragManager()-
86{-
87 m_instance = 0;-
88}
never executed: end of block
0
89-
90QDragManager *QDragManager::self()-
91{-
92 if (!m_instance && !QGuiApplication::closingDown())
!m_instanceDescription
TRUEnever evaluated
FALSEnever evaluated
!QGuiApplicati...:closingDown()Description
TRUEnever evaluated
FALSEnever evaluated
0
93 m_instance = new QDragManager;
never executed: m_instance = new QDragManager;
0
94 return m_instance;
never executed: return m_instance;
0
95}-
96-
97QObject *QDragManager::source() const-
98{-
99 if (m_object)
m_objectDescription
TRUEnever evaluated
FALSEnever evaluated
0
100 return m_object->source();
never executed: return m_object->source();
0
101 return 0;
never executed: return 0;
0
102}-
103-
104void QDragManager::setCurrentTarget(QObject *target, bool dropped)-
105{-
106 if (m_currentDropTarget == target)
m_currentDropTarget == targetDescription
TRUEnever evaluated
FALSEnever evaluated
0
107 return;
never executed: return;
0
108-
109 m_currentDropTarget = target;-
110 if (!dropped && m_object) {
!droppedDescription
TRUEnever evaluated
FALSEnever evaluated
m_objectDescription
TRUEnever evaluated
FALSEnever evaluated
0
111 m_object->d_func()->target = target;-
112 emit m_object->targetChanged(target);-
113 }
never executed: end of block
0
114}
never executed: end of block
0
115-
116QObject *QDragManager::currentTarget() const-
117{-
118 return m_currentDropTarget;
never executed: return m_currentDropTarget;
0
119}-
120-
121Qt::DropAction QDragManager::drag(QDrag *o)-
122{-
123 if (!o || m_object == o)
!oDescription
TRUEnever evaluated
FALSEnever evaluated
m_object == oDescription
TRUEnever evaluated
FALSEnever evaluated
0
124 return Qt::IgnoreAction;
never executed: return Qt::IgnoreAction;
0
125-
126 if (!m_platformDrag || !o->source()) {
!m_platformDragDescription
TRUEnever evaluated
FALSEnever evaluated
!o->source()Description
TRUEnever evaluated
FALSEnever evaluated
0
127 o->deleteLater();-
128 return Qt::IgnoreAction;
never executed: return Qt::IgnoreAction;
0
129 }-
130-
131 if (m_object) {
m_objectDescription
TRUEnever evaluated
FALSEnever evaluated
0
132 qWarning("QDragManager::drag in possibly invalid state");-
133 return Qt::IgnoreAction;
never executed: return Qt::IgnoreAction;
0
134 }-
135-
136 m_object = o;-
137-
138 m_object->d_func()->target = 0;-
139-
140 QGuiApplicationPrivate::instance()->notifyDragStarted(o);-
141 const Qt::DropAction result = m_platformDrag->drag(m_object);-
142 m_object = 0;-
143 if (!m_platformDrag->ownsDragObject())
!m_platformDra...nsDragObject()Description
TRUEnever evaluated
FALSEnever evaluated
0
144 o->deleteLater();
never executed: o->deleteLater();
0
145 return result;
never executed: return result;
0
146}-
147-
148#endif // QT_NO_DRAGANDDROP-
149-
150#if !(defined(QT_NO_DRAGANDDROP) && defined(QT_NO_CLIPBOARD))-
151-
152static QStringList imageReadMimeFormats()-
153{-
154 QStringList formats;-
155 QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();-
156 const int numImageFormats = imageFormats.size();-
157 formats.reserve(numImageFormats);-
158 for (int i = 0; i < numImageFormats; ++i) {
i < numImageFormatsDescription
TRUEnever evaluated
FALSEnever evaluated
0
159 QString format = QLatin1String("image/");-
160 format += QString::fromLatin1(imageFormats.at(i).toLower());-
161 formats.append(format);-
162 }
never executed: end of block
0
163-
164 //put png at the front because it is best-
165 int pngIndex = formats.indexOf(QLatin1String("image/png"));-
166 if (pngIndex != -1 && pngIndex != 0)
pngIndex != -1Description
TRUEnever evaluated
FALSEnever evaluated
pngIndex != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
167 formats.move(pngIndex, 0);
never executed: formats.move(pngIndex, 0);
0
168-
169 return formats;
never executed: return formats;
0
170}-
171-
172-
173static QStringList imageWriteMimeFormats()-
174{-
175 QStringList formats;-
176 QList<QByteArray> imageFormats = QImageWriter::supportedImageFormats();-
177 const int numImageFormats = imageFormats.size();-
178 formats.reserve(numImageFormats);-
179 for (int i = 0; i < numImageFormats; ++i) {
i < numImageFormatsDescription
TRUEnever evaluated
FALSEnever evaluated
0
180 QString format = QLatin1String("image/");-
181 format += QString::fromLatin1(imageFormats.at(i).toLower());-
182 formats.append(format);-
183 }
never executed: end of block
0
184-
185 //put png at the front because it is best-
186 int pngIndex = formats.indexOf(QLatin1String("image/png"));-
187 if (pngIndex != -1 && pngIndex != 0)
pngIndex != -1Description
TRUEnever evaluated
FALSEnever evaluated
pngIndex != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
188 formats.move(pngIndex, 0);
never executed: formats.move(pngIndex, 0);
0
189-
190 return formats;
never executed: return formats;
0
191}-
192-
193QInternalMimeData::QInternalMimeData()-
194 : QMimeData()-
195{-
196}
never executed: end of block
0
197-
198QInternalMimeData::~QInternalMimeData()-
199{-
200}-
201-
202bool QInternalMimeData::hasFormat(const QString &mimeType) const-
203{-
204 bool foundFormat = hasFormat_sys(mimeType);-
205 if (!foundFormat && mimeType == QLatin1String("application/x-qt-image")) {
!foundFormatDescription
TRUEnever evaluated
FALSEnever evaluated
mimeType == QL...n/x-qt-image")Description
TRUEnever evaluated
FALSEnever evaluated
0
206 QStringList imageFormats = imageReadMimeFormats();-
207 for (int i = 0; i < imageFormats.size(); ++i) {
i < imageFormats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
208 if ((foundFormat = hasFormat_sys(imageFormats.at(i))))
(foundFormat =...ormats.at(i)))Description
TRUEnever evaluated
FALSEnever evaluated
0
209 break;
never executed: break;
0
210 }
never executed: end of block
0
211 }
never executed: end of block
0
212 return foundFormat;
never executed: return foundFormat;
0
213}-
214-
215QStringList QInternalMimeData::formats() const-
216{-
217 QStringList realFormats = formats_sys();-
218 if (!realFormats.contains(QLatin1String("application/x-qt-image"))) {
!realFormats.c.../x-qt-image"))Description
TRUEnever evaluated
FALSEnever evaluated
0
219 QStringList imageFormats = imageReadMimeFormats();-
220 for (int i = 0; i < imageFormats.size(); ++i) {
i < imageFormats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
221 if (realFormats.contains(imageFormats.at(i))) {
realFormats.co...Formats.at(i))Description
TRUEnever evaluated
FALSEnever evaluated
0
222 realFormats += QLatin1String("application/x-qt-image");-
223 break;
never executed: break;
0
224 }-
225 }
never executed: end of block
0
226 }
never executed: end of block
0
227 return realFormats;
never executed: return realFormats;
0
228}-
229-
230QVariant QInternalMimeData::retrieveData(const QString &mimeType, QVariant::Type type) const-
231{-
232 QVariant data = retrieveData_sys(mimeType, type);-
233 if (mimeType == QLatin1String("application/x-qt-image")) {
mimeType == QL...n/x-qt-image")Description
TRUEnever evaluated
FALSEnever evaluated
0
234 if (data.isNull() || (data.type() == QVariant::ByteArray && data.toByteArray().isEmpty())) {
data.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
data.toByteArray().isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
235 // try to find an image-
236 QStringList imageFormats = imageReadMimeFormats();-
237 for (int i = 0; i < imageFormats.size(); ++i) {
i < imageFormats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
238 data = retrieveData_sys(imageFormats.at(i), type);-
239 if (data.isNull() || (data.type() == QVariant::ByteArray && data.toByteArray().isEmpty()))
data.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
data.toByteArray().isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
240 continue;
never executed: continue;
0
241 break;
never executed: break;
0
242 }-
243 }
never executed: end of block
0
244 // we wanted some image type, but all we got was a byte array. Convert it to an image.-
245 if (data.type() == QVariant::ByteArray
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
0
246 && (type == QVariant::Image || type == QVariant::Pixmap || type == QVariant::Bitmap))
type == QVariant::ImageDescription
TRUEnever evaluated
FALSEnever evaluated
type == QVariant::PixmapDescription
TRUEnever evaluated
FALSEnever evaluated
type == QVariant::BitmapDescription
TRUEnever evaluated
FALSEnever evaluated
0
247 data = QImage::fromData(data.toByteArray());
never executed: data = QImage::fromData(data.toByteArray());
0
248-
249 } else if (mimeType == QLatin1String("application/x-color") && data.type() == QVariant::ByteArray) {
never executed: end of block
mimeType == QL...tion/x-color")Description
TRUEnever evaluated
FALSEnever evaluated
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
0
250 QColor c;-
251 QByteArray ba = data.toByteArray();-
252 if (ba.size() == 8) {
ba.size() == 8Description
TRUEnever evaluated
FALSEnever evaluated
0
253 ushort * colBuf = (ushort *)ba.data();-
254 c.setRgbF(qreal(colBuf[0]) / qreal(0xFFFF),-
255 qreal(colBuf[1]) / qreal(0xFFFF),-
256 qreal(colBuf[2]) / qreal(0xFFFF),-
257 qreal(colBuf[3]) / qreal(0xFFFF));-
258 data = c;-
259 } else {
never executed: end of block
0
260 qWarning("Qt: Invalid color format");-
261 }
never executed: end of block
0
262 } else if (data.type() != type && data.type() == QVariant::ByteArray) {
data.type() != typeDescription
TRUEnever evaluated
FALSEnever evaluated
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
0
263 // try to use mime data's internal conversion stuf.-
264 QInternalMimeData *that = const_cast<QInternalMimeData *>(this);-
265 that->setData(mimeType, data.toByteArray());-
266 data = QMimeData::retrieveData(mimeType, type);-
267 that->clear();-
268 }
never executed: end of block
0
269 return data;
never executed: return data;
0
270}-
271-
272bool QInternalMimeData::canReadData(const QString &mimeType)-
273{-
274 return imageReadMimeFormats().contains(mimeType);
never executed: return imageReadMimeFormats().contains(mimeType);
0
275}-
276-
277// helper functions for rendering mimedata to the system, this is needed because QMimeData is in core.-
278QStringList QInternalMimeData::formatsHelper(const QMimeData *data)-
279{-
280 QStringList realFormats = data->formats();-
281 if (realFormats.contains(QLatin1String("application/x-qt-image"))) {
realFormats.co.../x-qt-image"))Description
TRUEnever evaluated
FALSEnever evaluated
0
282 // add all supported image formats-
283 QStringList imageFormats = imageWriteMimeFormats();-
284 for (int i = 0; i < imageFormats.size(); ++i) {
i < imageFormats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
285 if (!realFormats.contains(imageFormats.at(i)))
!realFormats.c...Formats.at(i))Description
TRUEnever evaluated
FALSEnever evaluated
0
286 realFormats.append(imageFormats.at(i));
never executed: realFormats.append(imageFormats.at(i));
0
287 }
never executed: end of block
0
288 }
never executed: end of block
0
289 return realFormats;
never executed: return realFormats;
0
290}-
291-
292bool QInternalMimeData::hasFormatHelper(const QString &mimeType, const QMimeData *data)-
293{-
294-
295 bool foundFormat = data->hasFormat(mimeType);-
296 if (!foundFormat) {
!foundFormatDescription
TRUEnever evaluated
FALSEnever evaluated
0
297 if (mimeType == QLatin1String("application/x-qt-image")) {
mimeType == QL...n/x-qt-image")Description
TRUEnever evaluated
FALSEnever evaluated
0
298 // check all supported image formats-
299 QStringList imageFormats = imageWriteMimeFormats();-
300 for (int i = 0; i < imageFormats.size(); ++i) {
i < imageFormats.size()Description
TRUEnever evaluated
FALSEnever evaluated
0
301 if ((foundFormat = data->hasFormat(imageFormats.at(i))))
(foundFormat =...ormats.at(i)))Description
TRUEnever evaluated
FALSEnever evaluated
0
302 break;
never executed: break;
0
303 }
never executed: end of block
0
304 } else if (mimeType.startsWith(QLatin1String("image/"))) {
never executed: end of block
mimeType.start...ing("image/"))Description
TRUEnever evaluated
FALSEnever evaluated
0
305 return data->hasImage() && imageWriteMimeFormats().contains(mimeType);
never executed: return data->hasImage() && imageWriteMimeFormats().contains(mimeType);
0
306 }-
307 }
never executed: end of block
0
308 return foundFormat;
never executed: return foundFormat;
0
309}-
310-
311QByteArray QInternalMimeData::renderDataHelper(const QString &mimeType, const QMimeData *data)-
312{-
313 QByteArray ba;-
314 if (mimeType == QLatin1String("application/x-color")) {
mimeType == QL...tion/x-color")Description
TRUEnever evaluated
FALSEnever evaluated
0
315 /* QMimeData can only provide colors as QColor or the name-
316 of a color as a QByteArray or a QString. So we need to do-
317 the conversion to application/x-color here.-
318 The application/x-color format is :-
319 type: application/x-color-
320 format: 16-
321 data[0]: red-
322 data[1]: green-
323 data[2]: blue-
324 data[3]: opacity-
325 */-
326 ba.resize(8);-
327 ushort * colBuf = (ushort *)ba.data();-
328 QColor c = qvariant_cast<QColor>(data->colorData());-
329 colBuf[0] = ushort(c.redF() * 0xFFFF);-
330 colBuf[1] = ushort(c.greenF() * 0xFFFF);-
331 colBuf[2] = ushort(c.blueF() * 0xFFFF);-
332 colBuf[3] = ushort(c.alphaF() * 0xFFFF);-
333 } else {
never executed: end of block
0
334 ba = data->data(mimeType);-
335 if (ba.isEmpty()) {
ba.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
0
336 if (mimeType == QLatin1String("application/x-qt-image") && data->hasImage()) {
mimeType == QL...n/x-qt-image")Description
TRUEnever evaluated
FALSEnever evaluated
data->hasImage()Description
TRUEnever evaluated
FALSEnever evaluated
0
337 QImage image = qvariant_cast<QImage>(data->imageData());-
338 QBuffer buf(&ba);-
339 buf.open(QBuffer::WriteOnly);-
340 // would there not be PNG ??-
341 image.save(&buf, "PNG");-
342 } else if (mimeType.startsWith(QLatin1String("image/")) && data->hasImage()) {
never executed: end of block
mimeType.start...ing("image/"))Description
TRUEnever evaluated
FALSEnever evaluated
data->hasImage()Description
TRUEnever evaluated
FALSEnever evaluated
0
343 QImage image = qvariant_cast<QImage>(data->imageData());-
344 QBuffer buf(&ba);-
345 buf.open(QBuffer::WriteOnly);-
346 image.save(&buf, mimeType.mid(mimeType.indexOf(QLatin1Char('/')) + 1).toLatin1().toUpper());-
347 }
never executed: end of block
0
348 }
never executed: end of block
0
349 }
never executed: end of block
0
350 return ba;
never executed: return ba;
0
351}-
352-
353#endif // QT_NO_DRAGANDDROP && QT_NO_CLIPBOARD-
354-
355QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9