From 93bfcfe8fc758299f19ec3fa820205e5739a3e4b Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 5 Jul 2011 21:13:22 +0200 Subject: [PATCH] drag and drop plugin by Gregory Schlomoff CCMAIL: gregory.schlomoff@gmail.com --- declarativeimports/draganddrop/CMakeLists.txt | 27 +++ .../draganddrop/DeclarativeDragArea.cpp | 186 ++++++++++++++++++ .../draganddrop/DeclarativeDragArea.h | 95 +++++++++ .../draganddrop/DeclarativeDragDropEvent.cpp | 42 ++++ .../draganddrop/DeclarativeDragDropEvent.h | 66 +++++++ .../draganddrop/DeclarativeDropArea.cpp | 68 +++++++ .../draganddrop/DeclarativeDropArea.h | 57 ++++++ .../draganddrop/DeclarativeMimeData.cpp | 121 ++++++++++++ .../draganddrop/DeclarativeMimeData.h | 75 +++++++ .../draganddrop/draganddropplugin.cpp | 43 ++++ .../draganddrop/draganddropplugin.h | 36 ++++ declarativeimports/draganddrop/qmldir | 2 + 12 files changed, 818 insertions(+) create mode 100644 declarativeimports/draganddrop/CMakeLists.txt create mode 100644 declarativeimports/draganddrop/DeclarativeDragArea.cpp create mode 100644 declarativeimports/draganddrop/DeclarativeDragArea.h create mode 100644 declarativeimports/draganddrop/DeclarativeDragDropEvent.cpp create mode 100644 declarativeimports/draganddrop/DeclarativeDragDropEvent.h create mode 100644 declarativeimports/draganddrop/DeclarativeDropArea.cpp create mode 100644 declarativeimports/draganddrop/DeclarativeDropArea.h create mode 100644 declarativeimports/draganddrop/DeclarativeMimeData.cpp create mode 100644 declarativeimports/draganddrop/DeclarativeMimeData.h create mode 100644 declarativeimports/draganddrop/draganddropplugin.cpp create mode 100644 declarativeimports/draganddrop/draganddropplugin.h create mode 100644 declarativeimports/draganddrop/qmldir diff --git a/declarativeimports/draganddrop/CMakeLists.txt b/declarativeimports/draganddrop/CMakeLists.txt new file mode 100644 index 000000000..5450873c7 --- /dev/null +++ b/declarativeimports/draganddrop/CMakeLists.txt @@ -0,0 +1,27 @@ +project(draganddrop) + +include(KDE4Defaults) + +set(declarativedragdrop_SRCS + draganddropplugin.cpp + DeclarativeDragArea.cpp + DeclarativeDragDropEvent.cpp + DeclarativeDropArea.cpp + DeclarativeMimeData.cpp + ) + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR} + ${CMAKE_BINARY_DIR} + ${KDE4_INCLUDES} +) + +qt4_automoc(${declarativedragdrop_SRCS}) + + +kde4_add_library(dragdropplugin SHARED ${declarativedragdrop_SRCS}) +target_link_libraries(dragdropplugin ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTDECLARATIVE_LIBRARY}) + +install(TARGETS dragdropplugin DESTINATION ${IMPORTS_INSTALL_DIR}/org/kde/draganddrop) + +install(FILES qmldir DESTINATION ${IMPORTS_INSTALL_DIR}/org/kde/draganddrop) diff --git a/declarativeimports/draganddrop/DeclarativeDragArea.cpp b/declarativeimports/draganddrop/DeclarativeDragArea.cpp new file mode 100644 index 000000000..93943e609 --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDragArea.cpp @@ -0,0 +1,186 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#include "DeclarativeDragArea.h" +#include "DeclarativeMimeData.h" + +#include +#include +#include +#include +#include +#include + +/*! + A DragArea is used to make an item draggable. +*/ + +DeclarativeDragArea::DeclarativeDragArea(QDeclarativeItem *parent) + : QDeclarativeItem(parent), + m_delegate(0), + m_source(0), + m_target(0), + m_enabled(true), + m_supportedActions(Qt::MoveAction), + m_defaultAction(Qt::MoveAction), + m_data(new DeclarativeMimeData()) // m_data is owned by us, and we shouldn't pass it to Qt directly as it will automatically delete it after the drag and drop. +{ + setAcceptedMouseButtons(Qt::LeftButton); +} + +DeclarativeDragArea::~DeclarativeDragArea() +{ + if (m_data) { + delete m_data; + } +} + +/*! + The delegate is the item that will be displayed next to the mouse cursor during the drag and drop operation. + It usually consists of a large, semi-transparent icon representing the data being dragged. +*/ +QDeclarativeComponent* DeclarativeDragArea::delegate() const +{ + return m_delegate; +} +void DeclarativeDragArea::setDelegate(QDeclarativeComponent *delegate) +{ + if (m_delegate != delegate) { + m_delegate = delegate; + emit delegateChanged(); + } +} +void DeclarativeDragArea::resetDelegate() +{ + setDelegate(0); +} + +/*! + The QML element that is the source of this drag and drop operation. This can be defined to any item, and will + be available to the DropArea as event.data.source +*/ +QDeclarativeItem* DeclarativeDragArea::source() const +{ + return m_source; +} +void DeclarativeDragArea::setSource(QDeclarativeItem* source) +{ + if (m_source != source) { + m_source = source; + emit sourceChanged(); + } +} +void DeclarativeDragArea::resetSource() +{ + setSource(0); +} + +// target +QDeclarativeItem* DeclarativeDragArea::target() const +{ + //TODO: implement me + return 0; +} + +// data +DeclarativeMimeData* DeclarativeDragArea::data() const +{ + return m_data; +} + +// enabled +bool DeclarativeDragArea::isEnabled() const +{ + return m_enabled; +} +void DeclarativeDragArea::setEnabled(bool enabled) +{ + if (enabled != m_enabled) { + m_enabled = enabled; + emit enabledChanged(); + } +} + +// supported actions +Qt::DropActions DeclarativeDragArea::supportedActions() const +{ + return m_supportedActions; +} +void DeclarativeDragArea::setSupportedActions(Qt::DropActions actions) +{ + if (actions != m_supportedActions) { + m_supportedActions = actions; + emit supportedActionsChanged(); + } +} + +// default action +Qt::DropAction DeclarativeDragArea::defaultAction() const +{ + return m_defaultAction; +} +void DeclarativeDragArea::setDefaultAction(Qt::DropAction action) +{ + if (action != m_defaultAction) { + m_defaultAction = action; + emit defaultActionChanged(); + } +} + +void DeclarativeDragArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if ( !m_enabled + || QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length() + < QApplication::startDragDistance()) { + return; + } + + QDrag *drag = new QDrag(event->widget()); + DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it. + drag->setMimeData(dataCopy); + + if (m_delegate) { + + // Render the delegate to a Pixmap + + QDeclarativeItem* item = qobject_cast(m_delegate->create()); + + QGraphicsScene scene; + scene.addItem(item); + + QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height()); + pixmap.fill(Qt::transparent); + + QPainter painter(&pixmap); + painter.setRenderHint(QPainter::Antialiasing); + scene.render(&painter); + + drag->setPixmap(pixmap); + drag->setHotSpot(QPoint(0, 0)); // TODO: Make a property for that + } + + //setCursor(Qt::OpenHandCursor); //TODO? Make a property for the cursor + + Qt::DropAction action = drag->exec(m_supportedActions, m_defaultAction); + emit drop(action); +} diff --git a/declarativeimports/draganddrop/DeclarativeDragArea.h b/declarativeimports/draganddrop/DeclarativeDragArea.h new file mode 100644 index 000000000..e81b3b07d --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDragArea.h @@ -0,0 +1,95 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#ifndef DECLARATIVEDRAGAREA_H +#define DECLARATIVEDRAGAREA_H + +#include + +class QDeclarativeComponent; +class DeclarativeMimeData; + +class DeclarativeDragArea : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QDeclarativeComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged RESET resetDelegate) + Q_PROPERTY(QDeclarativeItem* source READ source WRITE setSource NOTIFY sourceChanged RESET resetSource) + Q_PROPERTY(QDeclarativeItem* target READ source NOTIFY targetChanged) + Q_PROPERTY(DeclarativeMimeData* data READ data CONSTANT) + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) //TODO: Should call setAcceptDrops() + Q_PROPERTY(Qt::DropActions supportedActions READ supportedActions WRITE setSupportedActions NOTIFY supportedActionsChanged) + Q_PROPERTY(Qt::DropAction defaultAction READ defaultAction WRITE setDefaultAction NOTIFY defaultActionChanged) + + +public: + DeclarativeDragArea(QDeclarativeItem *parent=0); + ~DeclarativeDragArea(); + + QDeclarativeComponent *delegate() const; + void setDelegate(QDeclarativeComponent* delegate); + void resetDelegate(); + + QDeclarativeItem* target() const; + QDeclarativeItem* source() const; + void setSource(QDeclarativeItem* source); + void resetSource(); + + bool isEnabled() const; + void setEnabled(bool enabled); + + //supported actions + Qt::DropActions supportedActions() const; + void setSupportedActions(Qt::DropActions actions); + + //default action + Qt::DropAction defaultAction() const; + void setDefaultAction(Qt::DropAction action); + + DeclarativeMimeData* data() const; + +signals: + void delegateChanged(); + void sourceChanged(); + void targetChanged(); + void dataChanged(); + void enabledChanged(); + void drop(int action); + void supportedActionsChanged(); + void defaultActionChanged(); + +protected: + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mousePressEvent(QGraphicsSceneMouseEvent *) {} + void mouseReleaseEvent(QGraphicsSceneMouseEvent *) {} + +private: + QDeclarativeComponent* m_delegate; + QDeclarativeItem* m_source; + QDeclarativeItem* m_target; + DeclarativeMimeData* const m_data; + bool m_enabled; + Qt::DropActions m_supportedActions; + Qt::DropAction m_defaultAction; +}; + +#endif // DECLARATIVEDRAGAREA_H diff --git a/declarativeimports/draganddrop/DeclarativeDragDropEvent.cpp b/declarativeimports/draganddrop/DeclarativeDragDropEvent.cpp new file mode 100644 index 000000000..cd5fed70b --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDragDropEvent.cpp @@ -0,0 +1,42 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#include "DeclarativeDragDropEvent.h" + +DeclarativeDragDropEvent::DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* e, QObject* parent) : + QObject(parent), + m_event(e), + m_x(e->pos().x()), + m_y(e->pos().y()), + m_buttons(e->buttons()), + m_modifiers(e->modifiers()), + m_data(e->mimeData()) +{ + +} + +void DeclarativeDragDropEvent::accept(int action) +{ + m_event->setDropAction( (Qt::DropAction) action ); + m_event->accept(); +} diff --git a/declarativeimports/draganddrop/DeclarativeDragDropEvent.h b/declarativeimports/draganddrop/DeclarativeDragDropEvent.h new file mode 100644 index 000000000..824650541 --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDragDropEvent.h @@ -0,0 +1,66 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#ifndef DECLARATIVEDRAGDROPEVENT_H +#define DECLARATIVEDRAGDROPEVENT_H + +#include +#include +#include "DeclarativeMimeData.h" + +class DeclarativeDragDropEvent : public QObject +{ + Q_OBJECT + Q_PROPERTY(int x READ x) + Q_PROPERTY(int y READ y) + Q_PROPERTY(int buttons READ buttons) + Q_PROPERTY(int modifiers READ modifiers) + Q_PROPERTY(DeclarativeMimeData* data READ data) + Q_PROPERTY(Qt::DropActions possibleActions READ possibleActions) + Q_PROPERTY(Qt::DropAction proposedAction READ proposedAction) + +public: + + DeclarativeDragDropEvent(QGraphicsSceneDragDropEvent* e, QObject* parent = 0); + + int x() const { return m_x; } + int y() const { return m_y; } + int buttons() const { return m_buttons; } + int modifiers() const { return m_modifiers; } + DeclarativeMimeData* data() { return &m_data; } + Qt::DropAction proposedAction() const { return m_event->proposedAction(); } + Qt::DropActions possibleActions() const { return m_event->possibleActions(); } + +public slots: + void accept(int action); + +private: + int m_x; + int m_y; + Qt::MouseButtons m_buttons; + Qt::KeyboardModifiers m_modifiers; + DeclarativeMimeData m_data; + QGraphicsSceneDragDropEvent* m_event; +}; + +#endif // DECLARATIVEDRAGDROPEVENT_H diff --git a/declarativeimports/draganddrop/DeclarativeDropArea.cpp b/declarativeimports/draganddrop/DeclarativeDropArea.cpp new file mode 100644 index 000000000..3b394ab0b --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDropArea.cpp @@ -0,0 +1,68 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#include "DeclarativeDropArea.h" +#include "DeclarativeDragDropEvent.h" + +#include +#include + +DeclarativeDropArea::DeclarativeDropArea(QDeclarativeItem *parent) + : QDeclarativeItem(parent), + m_enabled(true) +{ + setAcceptDrops(m_enabled); +} + +void DeclarativeDropArea::dragEnterEvent(QGraphicsSceneDragDropEvent *event) { + DeclarativeDragDropEvent dde(event, this); + emit dragEnter(&dde); +} + +void DeclarativeDropArea::dragLeaveEvent(QGraphicsSceneDragDropEvent *event) +{ + DeclarativeDragDropEvent dde(event, this); + emit dragLeave(&dde); +} + +void DeclarativeDropArea::dropEvent(QGraphicsSceneDragDropEvent *event) +{ + DeclarativeDragDropEvent dde(event, this); + emit drop(&dde); +} + +bool DeclarativeDropArea::isEnabled() const +{ + return m_enabled; +} + +void DeclarativeDropArea::setEnabled(bool enabled) +{ + if (enabled == m_enabled) { + return; + } + + m_enabled = enabled; + setAcceptDrops(m_enabled); + emit enabledChanged(); +} diff --git a/declarativeimports/draganddrop/DeclarativeDropArea.h b/declarativeimports/draganddrop/DeclarativeDropArea.h new file mode 100644 index 000000000..0394ecf6b --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeDropArea.h @@ -0,0 +1,57 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#ifndef DECLARATIVEDROPAREA_H +#define DECLARATIVEDROPAREA_H + +#include + +class DeclarativeDragDropEvent; + +class DeclarativeDropArea : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) + +public: + DeclarativeDropArea(QDeclarativeItem *parent=0); + bool isEnabled() const; + void setEnabled(bool enabled); + +signals: + void dragEnter(DeclarativeDragDropEvent* event); + void dragLeave(DeclarativeDragDropEvent* event); + void drop(DeclarativeDragDropEvent* event); + void enabledChanged(); + +protected: + void dragEnterEvent(QGraphicsSceneDragDropEvent *event); + void dragLeaveEvent(QGraphicsSceneDragDropEvent *event); + void dropEvent(QGraphicsSceneDragDropEvent *event); + +private: + bool m_enabled; +}; + +#endif + diff --git a/declarativeimports/draganddrop/DeclarativeMimeData.cpp b/declarativeimports/draganddrop/DeclarativeMimeData.cpp new file mode 100644 index 000000000..09dbef02d --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeMimeData.cpp @@ -0,0 +1,121 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#include "DeclarativeMimeData.h" + +/*! + \qmlclass MimeData DeclarativeMimeData + + This is a wrapper class around QMimeData, with a few extensions to provide better support for in-qml drag & drops. +*/ + +/*! + \internal + \class DeclarativeMimeData + + 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" + + This method will try to cast the QMimeData to DeclarativeMimeData, and will clone our extensions to QMimeData as well +*/ +DeclarativeMimeData::DeclarativeMimeData(const QMimeData* copy) + : QMimeData(), + m_source(0) +{ + // 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(copy); + if (declarativeMimeData) { + this->setSource(declarativeMimeData->source()); + } +} + + +/*! + \qmlproperty url MimeData::url + + Returns the first URL from the urls property of QMimeData + TODO: We should use QDeclarativeListProperty to return the whole list instead of only the first element. +*/ +QUrl DeclarativeMimeData::url() const +{ + if ( this->hasUrls() && !this->urls().isEmpty()) { + return urls().first(); + } + return QUrl(); +} +void DeclarativeMimeData::setUrl(const QUrl &url) +{ + if (this->url() == url) + return; + + QList urlList; + urlList.append(url); + setUrls(urlList); + emit urlChanged(); +} + +// color +QColor DeclarativeMimeData::color() const +{ + if ( this->hasColor()) { + return qvariant_cast(this->colorData()); + } + return QColor(); +} +void DeclarativeMimeData::setColor(const QColor &color) +{ + if (this->color() != color) { + this->setColorData(color); + emit colorChanged(); + } +} + +/*! + \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 +{ + return m_source; +} +void DeclarativeMimeData::setSource(QDeclarativeItem* source) +{ + if (m_source != source) { + m_source = source; + emit sourceChanged(); + } +} diff --git a/declarativeimports/draganddrop/DeclarativeMimeData.h b/declarativeimports/draganddrop/DeclarativeMimeData.h new file mode 100644 index 000000000..1c9b1cd5b --- /dev/null +++ b/declarativeimports/draganddrop/DeclarativeMimeData.h @@ -0,0 +1,75 @@ +/* + Copyright (C) 2010 by BetterInbox + Original author: Gregory Schlomoff + + 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. +*/ + +#ifndef DECLARATIVEMIMEDATA_H +#define DECLARATIVEMIMEDATA_H + +#include +#include +#include +#include + +class DeclarativeMimeData : public QMimeData +{ + Q_OBJECT + + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) + Q_PROPERTY(QString html READ html WRITE setHtml NOTIFY htmlChanged) + Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY urlChanged) //TODO: use QDeclarativeListProperty to return the whole list instead of only the first url + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) + Q_PROPERTY(QDeclarativeItem* source READ source WRITE setSource NOTIFY sourceChanged) + //TODO: Image property + +public: + DeclarativeMimeData() : QMimeData() {} + DeclarativeMimeData(const QMimeData* copy); + + QUrl url() const; + void setUrl(const QUrl &url); + + QColor color() const; + void setColor(const QColor &color); + + QDeclarativeItem* source() const; + void setSource(QDeclarativeItem* source); + + /* + QString text() const; //TODO: Reimplement this to issue the onChanged signals + void setText(const QString &text); + QString html() const; + void setHtml(const QString &html); + */ + +signals: + void textChanged(); //FIXME not being used + void htmlChanged(); //FIXME not being used + void urlChanged(); + void colorChanged(); + void sourceChanged(); + +private: + QDeclarativeItem* m_source; + +}; + +#endif // DECLARATIVEMIMEDATA_H diff --git a/declarativeimports/draganddrop/draganddropplugin.cpp b/declarativeimports/draganddrop/draganddropplugin.cpp new file mode 100644 index 000000000..228ef79d4 --- /dev/null +++ b/declarativeimports/draganddrop/draganddropplugin.cpp @@ -0,0 +1,43 @@ +/* + Copyright 2011 by Marco Martin + + 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. +*/ + +#include "draganddropplugin.h" + +#include + +#include "DeclarativeDragArea.h" +#include "DeclarativeDragDropEvent.h" +#include "DeclarativeDropArea.h" +#include "DeclarativeMimeData.h" + +void DragAndDropPlugin::registerTypes(const char *uri) +{ + Q_ASSERT(uri == QLatin1String("org.kde.draganddrop")); + + qmlRegisterType(uri, 1, 0, "DropArea"); + qmlRegisterType(uri, 1, 0, "DragArea"); + qmlRegisterUncreatableType(uri, 1, 0, "MimeData", "MimeData cannot be created from QML."); + qmlRegisterUncreatableType(uri, 1, 0, "DragDropEvent", "DragDropEvent cannot be created from QML."); +} + +Q_EXPORT_PLUGIN2(draganddropplugin, DragAndDropPlugin) + diff --git a/declarativeimports/draganddrop/draganddropplugin.h b/declarativeimports/draganddrop/draganddropplugin.h new file mode 100644 index 000000000..ce7aba576 --- /dev/null +++ b/declarativeimports/draganddrop/draganddropplugin.h @@ -0,0 +1,36 @@ +/* + Copyright 2011 by Marco Martin + + 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. +*/ + +#ifndef DRAGANDDROPPLUGIN_H +#define DRAGANDDROPPLUGIN_H + +#include + +class DragAndDropPlugin : public QDeclarativeExtensionPlugin +{ + Q_OBJECT + +public: + void registerTypes(const char *uri); +}; + +#endif diff --git a/declarativeimports/draganddrop/qmldir b/declarativeimports/draganddrop/qmldir new file mode 100644 index 000000000..92ed88537 --- /dev/null +++ b/declarativeimports/draganddrop/qmldir @@ -0,0 +1,2 @@ +plugin draganddropplugin +