File indexing completed on 2024-05-19 15:09:22

0001 /*
0002     SPDX-FileCopyrightText: 2010 BetterInbox <contact@betterinbox.com>
0003     SPDX-FileContributor: Gregory Schlomoff <greg@betterinbox.com>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #include "DeclarativeMimeData.h"
0009 
0010 /*!
0011     \qmlclass MimeData DeclarativeMimeData
0012 
0013     This is a wrapper class around QMimeData, with a few extensions to provide better support for in-qml drag & drops.
0014 */
0015 
0016 DeclarativeMimeData::DeclarativeMimeData()
0017     : QMimeData()
0018     , m_source(nullptr)
0019 {
0020 }
0021 
0022 /*!
0023     \internal
0024     \class DeclarativeMimeData
0025 
0026     Creates a new DeclarativeMimeData by cloning the QMimeData passed as parameter.
0027     This is useful for two reasons :
0028         - In DragArea, we want to clone our "working copy" of the DeclarativeMimeData instance, as Qt will automatically
0029         delete it after the drag and drop operation.
0030         - In the drop events, the QMimeData is const, and we have troubles passing const to QML. So we clone it to
0031         remove the "constness"
0032 
0033     This method will try to cast the QMimeData to DeclarativeMimeData, and will clone our extensions to QMimeData as well
0034 */
0035 DeclarativeMimeData::DeclarativeMimeData(const QMimeData *copy)
0036     : QMimeData()
0037     , m_source(nullptr)
0038 {
0039     // Copy the standard MIME data
0040     const auto formats = copy->formats();
0041     for (const QString &format : formats) {
0042         QMimeData::setData(format, copy->data(format));
0043     }
0044 
0045     // If the object we are copying actually is a DeclarativeMimeData, copy our extended properties as well
0046     const DeclarativeMimeData *declarativeMimeData = qobject_cast<const DeclarativeMimeData *>(copy);
0047     if (declarativeMimeData) {
0048         this->setSource(declarativeMimeData->source());
0049     }
0050 }
0051 
0052 /*!
0053     \qmlproperty url MimeData::url
0054 
0055     Returns the first URL from the urls property of QMimeData
0056     TODO: We should use QDeclarativeListProperty<QUrls> to return the whole list instead of only the first element.
0057 */
0058 QUrl DeclarativeMimeData::url() const
0059 {
0060     if (this->hasUrls() && !this->urls().isEmpty()) {
0061         return QMimeData::urls().constFirst();
0062     }
0063     return QUrl();
0064 }
0065 void DeclarativeMimeData::setUrl(const QUrl &url)
0066 {
0067     if (this->url() == url) {
0068         return;
0069     }
0070 
0071     QList<QUrl> urlList;
0072     urlList.append(url);
0073     QMimeData::setUrls(urlList);
0074     Q_EMIT urlChanged();
0075 }
0076 
0077 QJsonArray DeclarativeMimeData::urls() const
0078 {
0079     QJsonArray varUrls;
0080     const auto lstUrls = QMimeData::urls();
0081     for (const QUrl &url : lstUrls) {
0082         varUrls.append(url.toString());
0083     }
0084     return varUrls;
0085 }
0086 
0087 void DeclarativeMimeData::setUrls(const QJsonArray &urls)
0088 {
0089     QList<QUrl> urlList;
0090     urlList.reserve(urls.size());
0091     for (const auto &varUrl : urls) {
0092         urlList << QUrl(varUrl.toString());
0093     }
0094     QMimeData::setUrls(urlList);
0095     Q_EMIT urlsChanged();
0096 }
0097 
0098 // color
0099 QColor DeclarativeMimeData::color() const
0100 {
0101     if (this->hasColor()) {
0102         return qvariant_cast<QColor>(this->colorData());
0103     }
0104     return QColor();
0105 }
0106 
0107 bool DeclarativeMimeData::hasColor() const
0108 {
0109     // qDebug() << " hasColor " << (QMimeData::hasColor() ? color().name() : "false");
0110     return QMimeData::hasColor();
0111 }
0112 
0113 void DeclarativeMimeData::setColor(const QColor &color)
0114 {
0115     if (this->color() != color) {
0116         this->setColorData(color);
0117         Q_EMIT colorChanged();
0118     }
0119 }
0120 
0121 void DeclarativeMimeData::setData(const QString &mimeType, const QVariant &data)
0122 {
0123     if (data.type() == QVariant::ByteArray) {
0124         QMimeData::setData(mimeType, data.toByteArray());
0125     } else if (data.canConvert(QVariant::String)) {
0126         QMimeData::setData(mimeType, data.toString().toLatin1());
0127     }
0128 }
0129 
0130 /*!
0131   \qmlproperty item MimeData::source
0132 
0133   Setting source to any existing qml item will enable the receiver of the drag and drop operation to know in which item
0134   the operation originated.
0135 
0136   In the case of inter-application drag and drop operations, the source will not be available, and will be 0.
0137   Be sure to test it in your QML code, before using it, or it will generate errors in the console.
0138 */
0139 QQuickItem *DeclarativeMimeData::source() const
0140 {
0141     return m_source;
0142 }
0143 void DeclarativeMimeData::setSource(QQuickItem *source)
0144 {
0145     if (m_source != source) {
0146         m_source = source;
0147         Q_EMIT sourceChanged();
0148     }
0149 }
0150 
0151 QByteArray DeclarativeMimeData::getDataAsByteArray(const QString &format)
0152 {
0153     return data(format);
0154 }
0155 
0156 #include "moc_DeclarativeMimeData.cpp"