From 37e3ef99f0bd2e8c5f36686e6760de7debe87dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCgler?= Date: Thu, 7 Mar 2013 00:26:39 +0100 Subject: [PATCH] Make mouse interaction work - events need to be accepted, otherwise they don't have effect on the drag, and it doesn't get released - slightly more intelligent starting of drag, saves quite some cycles and is way more correct this way The "attach pixmap of dragged item" is still disabled. This probably needs to be rethought entirely. One option would be to ditch this class in favour of QtQuick 2's Drag, DragEvent and DropArea. Food for API discussion. --- .../draganddrop/DeclarativeDragArea.cpp | 62 ++++++++++--------- .../draganddrop/DeclarativeDragArea.h | 1 + .../draganddrop/DeclarativeDropArea.cpp | 5 +- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/declarativeimports/draganddrop/DeclarativeDragArea.cpp b/src/declarativeimports/draganddrop/DeclarativeDragArea.cpp index 3df56e3bd..ec9fc6dbf 100644 --- a/src/declarativeimports/draganddrop/DeclarativeDragArea.cpp +++ b/src/declarativeimports/draganddrop/DeclarativeDragArea.cpp @@ -43,6 +43,7 @@ DeclarativeDragArea::DeclarativeDragArea(QQuickItem *parent) m_source(0), m_target(0), m_enabled(true), + m_draggingJustStarted(false), 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. @@ -195,6 +196,7 @@ void DeclarativeDragArea::setDefaultAction(Qt::DropAction action) void DeclarativeDragArea::mousePressEvent(QMouseEvent* event) { m_buttonDownPos = event->screenPos(); + m_draggingJustStarted = true; } @@ -205,42 +207,44 @@ void DeclarativeDragArea::mouseMoveEvent(QMouseEvent *event) < m_startDragDistance) { return; } + if (m_draggingJustStarted) { + emit dragStarted(); + m_draggingJustStarted = false; - emit dragStarted(); + QDrag *drag = new QDrag(parent()); + DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it. + drag->setMimeData(dataCopy); - QDrag *drag = new QDrag(parent()); - DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it. - drag->setMimeData(dataCopy); + if (!m_delegateImage.isNull()) { + drag->setPixmap(QPixmap::fromImage(m_delegateImage)); + } else if (m_delegate) { + // Render the delegate to a Pixmap + // QQuickItem* item = qobject_cast(m_delegate->create(m_delegate->creationContext())); - if (!m_delegateImage.isNull()) { - drag->setPixmap(QPixmap::fromImage(m_delegateImage)); - } else if (m_delegate) { - // Render the delegate to a Pixmap -// QQuickItem* item = qobject_cast(m_delegate->create(m_delegate->creationContext())); + // QGraphicsScene scene; + // scene.addItem(item); -// QGraphicsScene scene; -// scene.addItem(item); + // QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height()); + // pixmap.fill(Qt::transparent); + // + // QPainter painter(&pixmap); + // painter.setRenderHint(QPainter::Antialiasing); + // painter.setRenderHint(QPainter::SmoothPixmapTransform); + // scene.render(&painter); + // painter.end(); + // delete item; + // + // drag->setPixmap(pixmap); + drag->setPixmap(QIcon::fromTheme("plasma").pixmap(64,64)); + } -// QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height()); -// pixmap.fill(Qt::transparent); -// -// QPainter painter(&pixmap); -// painter.setRenderHint(QPainter::Antialiasing); -// painter.setRenderHint(QPainter::SmoothPixmapTransform); -// scene.render(&painter); -// painter.end(); -// delete item; -// -// drag->setPixmap(pixmap); - drag->setPixmap(QIcon::fromTheme("plasma").pixmap(64,64)); + drag->setHotSpot(QPoint(drag->pixmap().width()/2, drag->pixmap().height()/2)); // 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); } - drag->setHotSpot(QPoint(drag->pixmap().width()/2, drag->pixmap().height()/2)); // 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); } bool DeclarativeDragArea::childMouseEventFilter(QQuickItem *item, QEvent *event) diff --git a/src/declarativeimports/draganddrop/DeclarativeDragArea.h b/src/declarativeimports/draganddrop/DeclarativeDragArea.h index edc908123..cdb620b81 100644 --- a/src/declarativeimports/draganddrop/DeclarativeDragArea.h +++ b/src/declarativeimports/draganddrop/DeclarativeDragArea.h @@ -142,6 +142,7 @@ private: QQuickItem* m_source; QQuickItem* m_target; bool m_enabled; + bool m_draggingJustStarted; Qt::DropActions m_supportedActions; Qt::DropAction m_defaultAction; DeclarativeMimeData* const m_data; diff --git a/src/declarativeimports/draganddrop/DeclarativeDropArea.cpp b/src/declarativeimports/draganddrop/DeclarativeDropArea.cpp index 0050a4a97..ff1c9ef4f 100644 --- a/src/declarativeimports/draganddrop/DeclarativeDropArea.cpp +++ b/src/declarativeimports/draganddrop/DeclarativeDropArea.cpp @@ -39,9 +39,11 @@ DeclarativeDropArea::DeclarativeDropArea(QQuickItem *parent) } -void DeclarativeDropArea::dragEnterEvent(QDragEnterEvent *event) { +void DeclarativeDropArea::dragEnterEvent(QDragEnterEvent *event) +{ DeclarativeDragDropEvent dde(event, this); qDebug() << "enter."; + event->accept(); emit dragEnter(&dde); } @@ -56,6 +58,7 @@ void DeclarativeDropArea::dragMoveEvent(QDragMoveEvent *event) { DeclarativeDragDropEvent dde(event, this); qDebug() << "move."; + event->accept(); emit dragMove(&dde); }