2011-07-05 21:13:22 +02:00
|
|
|
/*
|
2012-05-04 14:01:29 +02:00
|
|
|
Copyright (C) 2010 by BetterInbox <contact@betterinbox.com>
|
|
|
|
Original author: Gregory Schlomoff <greg@betterinbox.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
2011-07-05 21:13:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DeclarativeMimeData.h"
|
|
|
|
|
|
|
|
/*!
|
2012-05-04 14:01:29 +02:00
|
|
|
\qmlclass MimeData DeclarativeMimeData
|
2011-07-05 21:13:22 +02:00
|
|
|
|
2012-05-04 14:01:29 +02:00
|
|
|
This is a wrapper class around QMimeData, with a few extensions to provide better support for in-qml drag & drops.
|
2011-07-05 21:13:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2012-05-04 14:01:29 +02:00
|
|
|
\internal
|
|
|
|
\class DeclarativeMimeData
|
2011-07-05 21:13:22 +02:00
|
|
|
|
2012-05-04 14:01:29 +02:00
|
|
|
Creates a new DeclarativeMimeData by cloning the QMimeData passed as parameter.
|
|
|
|
This is useful for two reasons :
|
|
|
|
- In DragArea, we want to clone our "working copy" of the DeclarativeMimeData instance, as Qt will automatically
|
|
|
|
delete it after the drag and drop operation.
|
|
|
|
- In the drop events, the QMimeData is const, and we have troubles passing const to QML. So we clone it to
|
|
|
|
remove the "constness"
|
2011-07-05 21:13:22 +02:00
|
|
|
|
2012-05-04 14:01:29 +02:00
|
|
|
This method will try to cast the QMimeData to DeclarativeMimeData, and will clone our extensions to QMimeData as well
|
2011-07-05 21:13:22 +02:00
|
|
|
*/
|
|
|
|
DeclarativeMimeData::DeclarativeMimeData(const QMimeData* copy)
|
2012-05-04 14:01:29 +02:00
|
|
|
: QMimeData(),
|
|
|
|
m_source(0)
|
2011-07-05 21:13:22 +02:00
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
// Copy the standard MIME data
|
|
|
|
foreach(QString format, copy->formats()) {
|
|
|
|
this->setData(format, copy->data(format));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the object we are copying actually is a DeclarativeMimeData, copy our extended properties as well
|
|
|
|
const DeclarativeMimeData* declarativeMimeData = qobject_cast<const DeclarativeMimeData*>(copy);
|
|
|
|
if (declarativeMimeData) {
|
|
|
|
this->setSource(declarativeMimeData->source());
|
|
|
|
}
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
2012-05-04 14:01:29 +02:00
|
|
|
\qmlproperty url MimeData::url
|
2011-07-05 21:13:22 +02:00
|
|
|
|
2012-05-04 14:01:29 +02:00
|
|
|
Returns the first URL from the urls property of QMimeData
|
|
|
|
TODO: We should use QDeclarativeListProperty<QUrls> to return the whole list instead of only the first element.
|
2011-07-05 21:13:22 +02:00
|
|
|
*/
|
|
|
|
QUrl DeclarativeMimeData::url() const
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
if ( this->hasUrls() && !this->urls().isEmpty()) {
|
|
|
|
return QMimeData::urls().first();
|
|
|
|
}
|
|
|
|
return QUrl();
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
void DeclarativeMimeData::setUrl(const QUrl &url)
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
if (this->url() == url)
|
|
|
|
return;
|
2011-07-05 21:13:22 +02:00
|
|
|
|
2012-05-04 14:01:29 +02:00
|
|
|
QList<QUrl> urlList;
|
|
|
|
urlList.append(url);
|
|
|
|
QMimeData::setUrls(urlList);
|
|
|
|
emit urlChanged();
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
|
2011-12-15 19:51:38 +01:00
|
|
|
QVariantList DeclarativeMimeData::urls() const
|
|
|
|
{
|
|
|
|
QVariantList varUrls;
|
|
|
|
foreach (const QUrl &url, QMimeData::urls()) {
|
|
|
|
varUrls << url;
|
|
|
|
}
|
|
|
|
return varUrls;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeclarativeMimeData::setUrls(const QVariantList &urls)
|
|
|
|
{
|
|
|
|
QList<QUrl> urlList;
|
|
|
|
foreach (const QVariant &varUrl, urls) {
|
|
|
|
urlList << varUrl.value<QUrl>();
|
|
|
|
}
|
|
|
|
QMimeData::setUrls(urlList);
|
|
|
|
emit urlsChanged();
|
|
|
|
}
|
|
|
|
|
2011-07-05 21:13:22 +02:00
|
|
|
// color
|
|
|
|
QColor DeclarativeMimeData::color() const
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
if ( this->hasColor()) {
|
|
|
|
return qvariant_cast<QColor>(this->colorData());
|
|
|
|
}
|
|
|
|
return QColor();
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
void DeclarativeMimeData::setColor(const QColor &color)
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
if (this->color() != color) {
|
|
|
|
this->setColorData(color);
|
|
|
|
emit colorChanged();
|
|
|
|
}
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
|
2011-12-26 16:28:58 +01:00
|
|
|
void DeclarativeMimeData::setData(const QString &mimeType, const QString &data)
|
|
|
|
{
|
|
|
|
QMimeData::setData(mimeType, data.toLatin1());
|
|
|
|
}
|
|
|
|
|
2011-07-05 21:13:22 +02:00
|
|
|
/*!
|
|
|
|
\qmlproperty item MimeData::source
|
|
|
|
|
|
|
|
Setting source to any existing qml item will enable the receiver of the drag and drop operation to know in which item
|
|
|
|
the operation originated.
|
|
|
|
|
|
|
|
In the case of inter-application drag and drop operations, the source will not be available, and will be 0.
|
|
|
|
Be sure to test it in your QML code, before using it, or it will generate errors in the console.
|
|
|
|
*/
|
|
|
|
QDeclarativeItem* DeclarativeMimeData::source() const
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
return m_source;
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|
|
|
|
void DeclarativeMimeData::setSource(QDeclarativeItem* source)
|
|
|
|
{
|
2012-05-04 14:01:29 +02:00
|
|
|
if (m_source != source) {
|
|
|
|
m_source = source;
|
|
|
|
emit sourceChanged();
|
|
|
|
}
|
2011-07-05 21:13:22 +02:00
|
|
|
}
|