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.
This commit is contained in:
Sebastian Kügler 2013-03-07 00:26:39 +01:00
parent a77ae27de4
commit 37e3ef99f0
3 changed files with 38 additions and 30 deletions

View File

@ -43,6 +43,7 @@ DeclarativeDragArea::DeclarativeDragArea(QQuickItem *parent)
m_source(0), m_source(0),
m_target(0), m_target(0),
m_enabled(true), m_enabled(true),
m_draggingJustStarted(false),
m_supportedActions(Qt::MoveAction), m_supportedActions(Qt::MoveAction),
m_defaultAction(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. 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) void DeclarativeDragArea::mousePressEvent(QMouseEvent* event)
{ {
m_buttonDownPos = event->screenPos(); m_buttonDownPos = event->screenPos();
m_draggingJustStarted = true;
} }
@ -205,8 +207,9 @@ void DeclarativeDragArea::mouseMoveEvent(QMouseEvent *event)
< m_startDragDistance) { < m_startDragDistance) {
return; return;
} }
if (m_draggingJustStarted) {
emit dragStarted(); emit dragStarted();
m_draggingJustStarted = false;
QDrag *drag = new QDrag(parent()); QDrag *drag = new QDrag(parent());
DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it. DeclarativeMimeData* dataCopy = new DeclarativeMimeData(m_data); //Qt will take ownership of this copy and delete it.
@ -216,31 +219,32 @@ void DeclarativeDragArea::mouseMoveEvent(QMouseEvent *event)
drag->setPixmap(QPixmap::fromImage(m_delegateImage)); drag->setPixmap(QPixmap::fromImage(m_delegateImage));
} else if (m_delegate) { } else if (m_delegate) {
// Render the delegate to a Pixmap // Render the delegate to a Pixmap
// QQuickItem* item = qobject_cast<QQuickItem *>(m_delegate->create(m_delegate->creationContext())); // QQuickItem* item = qobject_cast<QQuickItem *>(m_delegate->create(m_delegate->creationContext()));
// QGraphicsScene scene; // QGraphicsScene scene;
// scene.addItem(item); // scene.addItem(item);
// QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height()); // QPixmap pixmap(scene.sceneRect().width(), scene.sceneRect().height());
// pixmap.fill(Qt::transparent); // pixmap.fill(Qt::transparent);
// //
// QPainter painter(&pixmap); // QPainter painter(&pixmap);
// painter.setRenderHint(QPainter::Antialiasing); // painter.setRenderHint(QPainter::Antialiasing);
// painter.setRenderHint(QPainter::SmoothPixmapTransform); // painter.setRenderHint(QPainter::SmoothPixmapTransform);
// scene.render(&painter); // scene.render(&painter);
// painter.end(); // painter.end();
// delete item; // delete item;
// //
// drag->setPixmap(pixmap); // drag->setPixmap(pixmap);
drag->setPixmap(QIcon::fromTheme("plasma").pixmap(64,64)); 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 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 //setCursor(Qt::OpenHandCursor); //TODO? Make a property for the cursor
Qt::DropAction action = drag->exec(m_supportedActions, m_defaultAction); Qt::DropAction action = drag->exec(m_supportedActions, m_defaultAction);
emit drop(action); emit drop(action);
}
} }
bool DeclarativeDragArea::childMouseEventFilter(QQuickItem *item, QEvent *event) bool DeclarativeDragArea::childMouseEventFilter(QQuickItem *item, QEvent *event)

View File

@ -142,6 +142,7 @@ private:
QQuickItem* m_source; QQuickItem* m_source;
QQuickItem* m_target; QQuickItem* m_target;
bool m_enabled; bool m_enabled;
bool m_draggingJustStarted;
Qt::DropActions m_supportedActions; Qt::DropActions m_supportedActions;
Qt::DropAction m_defaultAction; Qt::DropAction m_defaultAction;
DeclarativeMimeData* const m_data; DeclarativeMimeData* const m_data;

View File

@ -39,9 +39,11 @@ DeclarativeDropArea::DeclarativeDropArea(QQuickItem *parent)
} }
void DeclarativeDropArea::dragEnterEvent(QDragEnterEvent *event) { void DeclarativeDropArea::dragEnterEvent(QDragEnterEvent *event)
{
DeclarativeDragDropEvent dde(event, this); DeclarativeDragDropEvent dde(event, this);
qDebug() << "enter."; qDebug() << "enter.";
event->accept();
emit dragEnter(&dde); emit dragEnter(&dde);
} }
@ -56,6 +58,7 @@ void DeclarativeDropArea::dragMoveEvent(QDragMoveEvent *event)
{ {
DeclarativeDragDropEvent dde(event, this); DeclarativeDragDropEvent dde(event, this);
qDebug() << "move."; qDebug() << "move.";
event->accept();
emit dragMove(&dde); emit dragMove(&dde);
} }