KDeclarative

DeclarativeMimeData.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 BetterInbox <[email protected]>
3  SPDX-FileContributor: Gregory Schlomoff <[email protected]>
4 
5  SPDX-License-Identifier: MIT
6 */
7 
8 #include "DeclarativeMimeData.h"
9 
17  : QMimeData(),
18  m_source(nullptr)
19 {}
20 
35  : QMimeData(),
36  m_source(nullptr)
37 {
38  // Copy the standard MIME data
39  const auto formats = copy->formats();
40  for (const QString &format : formats) {
41  QMimeData::setData(format, copy->data(format));
42  }
43 
44  // If the object we are copying actually is a DeclarativeMimeData, copy our extended properties as well
45  const DeclarativeMimeData* declarativeMimeData = qobject_cast<const DeclarativeMimeData*>(copy);
46  if (declarativeMimeData) {
47  this->setSource(declarativeMimeData->source());
48  }
49 }
50 
58 {
59  if ( this->hasUrls() && !this->urls().isEmpty()) {
60  return QMimeData::urls().first();
61  }
62  return QUrl();
63 }
64 void DeclarativeMimeData::setUrl(const QUrl &url)
65 {
66  if (this->url() == url)
67  return;
68 
69  QList<QUrl> urlList;
70  urlList.append(url);
71  QMimeData::setUrls(urlList);
72  Q_EMIT urlChanged();
73 }
74 
76 {
77  QJsonArray varUrls;
78  const auto lstUrls = QMimeData::urls();
79  for (const QUrl &url : lstUrls) {
80  varUrls.append(url.toString());
81  }
82  return varUrls;
83 }
84 
85 void DeclarativeMimeData::setUrls(const QJsonArray &urls)
86 {
87  QList<QUrl> urlList;
88  urlList.reserve(urls.size());
89  for (const QVariant &varUrl : urls) {
90  urlList << varUrl.toUrl();
91  }
92  QMimeData::setUrls(urlList);
93  Q_EMIT urlsChanged();
94 }
95 
96 // color
98 {
99  if (this->hasColor()) {
100  return qvariant_cast<QColor>(this->colorData());
101  }
102  return QColor();
103 }
104 
105 bool DeclarativeMimeData::hasColor() const
106 {
107  //qDebug() << " hasColor " << (QMimeData::hasColor() ? color().name() : "false");
108  return QMimeData::hasColor();
109 }
110 
111 void DeclarativeMimeData::setColor(const QColor &color)
112 {
113  if (this->color() != color) {
114  this->setColorData(color);
115  Q_EMIT colorChanged();
116  }
117 }
118 
119 void DeclarativeMimeData::setData(const QString &mimeType, const QVariant &data)
120 {
121  if (data.type() == QVariant::ByteArray) {
122  QMimeData::setData(mimeType, data.toByteArray());
123  } else if (data.canConvert(QVariant::String)) {
124  QMimeData::setData(mimeType, data.toString().toLatin1());
125  }
126 }
127 
138 {
139  return m_source;
140 }
141 void DeclarativeMimeData::setSource(QQuickItem* source)
142 {
143  if (m_source != source) {
144  m_source = source;
145  Q_EMIT sourceChanged();
146  }
147 }
148 
149 QByteArray DeclarativeMimeData::getDataAsByteArray(const QString& format)
150 {
151  return data(format);
152 }
bool canConvert(int targetTypeId) const const
QByteArray toByteArray() const const
QByteArray data(const QString &mimeType) const const
int size() const const
void reserve(int alloc)
QQuickItem source
The graphical item on the scene that started the drag event.
void setColorData(const QVariant &color)
QString toString(QUrl::FormattingOptions options) const const
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...
void append(const T &value)
QVariant colorData() const const
void append(const QJsonValue &value)
QJsonArray urls
A list of URLs contained within the MIME data object.
QByteArray toLatin1() const const
bool hasUrls() const const
QList< QUrl > urls() const const
virtual QStringList formats() const const
void setData(const QString &mimeType, const QByteArray &data)
QVariant::Type type() const const
T qobject_cast(QObject *object)
QString toString() const const
Q_EMITQ_EMIT
void setUrls(const QList< QUrl > &urls)
bool hasColor() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.