KDeclarative

DeclarativeMimeData.cpp
1/*
2 SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com>
3 SPDX-FileContributor: Gregory Schlomoff <greg@betterinbox.com>
4
5 SPDX-License-Identifier: MIT
6*/
7
8#include "DeclarativeMimeData.h"
9
10/*!
11 \qmlclass MimeData DeclarativeMimeData
12
13 This is a wrapper class around QMimeData, with a few extensions to provide better support for in-qml drag & drops.
14*/
15
17 : QMimeData()
18 , m_source(nullptr)
19{
20}
21
22/*!
23 \internal
24 \class DeclarativeMimeData
25
26 Creates a new DeclarativeMimeData by cloning the QMimeData passed as parameter.
27 This is useful for two reasons :
28 - In DragArea, we want to clone our "working copy" of the DeclarativeMimeData instance, as Qt will automatically
29 delete it after the drag and drop operation.
30 - In the drop events, the QMimeData is const, and we have troubles passing const to QML. So we clone it to
31 remove the "constness"
32
33 This method will try to cast the QMimeData to DeclarativeMimeData, and will clone our extensions to QMimeData as well
34*/
36 : QMimeData()
37 , m_source(nullptr)
38{
39 // Copy the standard MIME data
40 const auto formats = copy->formats();
41 for (const QString &format : formats) {
42 QMimeData::setData(format, copy->data(format));
43 }
44
45 // If the object we are copying actually is a DeclarativeMimeData, copy our extended properties as well
46 const DeclarativeMimeData *declarativeMimeData = qobject_cast<const DeclarativeMimeData *>(copy);
47 if (declarativeMimeData) {
48 this->setSource(declarativeMimeData->source());
49 }
50}
51
52/*!
53 \qmlproperty url MimeData::url
54
55 Returns the first URL from the urls property of QMimeData
56 TODO: We should use QDeclarativeListProperty<QUrls> to return the whole list instead of only the first element.
57*/
59{
60 if (this->hasUrls() && !this->urls().isEmpty()) {
61 return QMimeData::urls().constFirst();
62 }
63 return QUrl();
64}
65void DeclarativeMimeData::setUrl(const QUrl &url)
66{
67 if (this->url() == url) {
68 return;
69 }
70
71 QList<QUrl> urlList;
72 urlList.append(url);
73 QMimeData::setUrls(urlList);
74 Q_EMIT urlChanged();
75}
76
78{
79 QJsonArray varUrls;
80 const auto lstUrls = QMimeData::urls();
81 for (const QUrl &url : lstUrls) {
82 varUrls.append(url.toString());
83 }
84 return varUrls;
85}
86
87void DeclarativeMimeData::setUrls(const QJsonArray &urls)
88{
89 QList<QUrl> urlList;
90 urlList.reserve(urls.size());
91 for (const auto &varUrl : urls) {
92 urlList << QUrl(varUrl.toString());
93 }
94 QMimeData::setUrls(urlList);
95 Q_EMIT urlsChanged();
96}
97
98// color
100{
101 if (this->hasColor()) {
102 return qvariant_cast<QColor>(this->colorData());
103 }
104 return QColor();
105}
106
107bool DeclarativeMimeData::hasColor() const
108{
109 // qDebug() << " hasColor " << (QMimeData::hasColor() ? color().name() : "false");
110 return QMimeData::hasColor();
111}
112
113void DeclarativeMimeData::setColor(const QColor &color)
114{
115 if (this->color() != color) {
116 this->setColorData(color);
117 Q_EMIT colorChanged();
118 }
119}
120
121void DeclarativeMimeData::setData(const QString &mimeType, const QVariant &data)
122{
123 if (data.userType() == QMetaType::QByteArray) {
124 QMimeData::setData(mimeType, data.toByteArray());
125 } else if (data.canConvert<QString>()) {
126 QMimeData::setData(mimeType, data.toString().toLatin1());
127 }
128}
129
130/*!
131 \qmlproperty item MimeData::source
132
133 Setting source to any existing qml item will enable the receiver of the drag and drop operation to know in which item
134 the operation originated.
135
136 In the case of inter-application drag and drop operations, the source will not be available, and will be 0.
137 Be sure to test it in your QML code, before using it, or it will generate errors in the console.
138*/
140{
141 return m_source;
142}
143void DeclarativeMimeData::setSource(QQuickItem *source)
144{
145 if (m_source != source) {
146 m_source = source;
147 Q_EMIT sourceChanged();
148 }
149}
150
151QByteArray DeclarativeMimeData::getDataAsByteArray(const QString &format)
152{
153 return data(format);
154}
155
156#include "moc_DeclarativeMimeData.cpp"
QJsonArray urls
A list of URLs contained within the MIME data object.
QUrl url
Url contained in the mimedata.
QColor color
A color if the data stored in the object represents a color (MIME type application/x-color); otherwis...
QQuickItem * source
The graphical item on the scene that started the drag event.
void append(const QJsonValue &value)
qsizetype size() const const
void append(QList< T > &&value)
void reserve(qsizetype size)
QVariant colorData() const const
QByteArray data(const QString &mimeType) const const
bool hasColor() const const
void setColorData(const QVariant &color)
void setData(const QString &mimeType, const QByteArray &data)
void setUrls(const QList< QUrl > &urls)
QList< QUrl > urls() const const
Q_EMITQ_EMIT
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.