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