configurable start drag distance

This commit is contained in:
Marco Martin 2012-03-02 14:14:21 +01:00
parent 1471d27cfa
commit a299c18ddc
2 changed files with 29 additions and 2 deletions

View File

@ -45,6 +45,7 @@ DeclarativeDragArea::DeclarativeDragArea(QDeclarativeItem *parent)
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_startDragDistance = QApplication::startDragDistance();
setAcceptedMouseButtons(Qt::LeftButton);
setFiltersChildEvents(true);
}
@ -109,6 +110,22 @@ DeclarativeMimeData* DeclarativeDragArea::mimeData() const
return m_data;
}
// startDragDistance
int DeclarativeDragArea::startDragDistance() const
{
return m_startDragDistance;
}
void DeclarativeDragArea::setStartDragDistance(int distance)
{
if (distance == m_startDragDistance) {
return;
}
m_startDragDistance = distance;
emit startDragDistanceChanged();
}
// enabled
bool DeclarativeDragArea::isEnabled() const
{
@ -152,7 +169,7 @@ void DeclarativeDragArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if ( !m_enabled
|| QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton)).length()
< QApplication::startDragDistance()) {
< m_startDragDistance) {
return;
}
@ -175,6 +192,8 @@ void DeclarativeDragArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
painter.end();
delete item;
drag->setPixmap(pixmap);
drag->setHotSpot(QPoint(0, 0)); // TODO: Make a property for that
@ -186,7 +205,6 @@ void DeclarativeDragArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
emit drop(action);
}
bool DeclarativeDragArea::sceneEventFilter(QGraphicsItem *item, QEvent *event)
{
if (!isEnabled()) {

View File

@ -75,6 +75,10 @@ class DeclarativeDragArea : public QDeclarativeItem
*/
Q_PROPERTY(Qt::DropAction defaultAction READ defaultAction WRITE setDefaultAction NOTIFY defaultActionChanged)
/**
* distance in pixel after which a drag event will get started
*/
Q_PROPERTY(int startDragDistance READ startDragDistance WRITE setStartDragDistance NOTIFY startDragDistanceChanged)
public:
DeclarativeDragArea(QDeclarativeItem *parent=0);
@ -92,6 +96,9 @@ public:
bool isEnabled() const;
void setEnabled(bool enabled);
int startDragDistance() const;
void setStartDragDistance(int distance);
//supported actions
Qt::DropActions supportedActions() const;
void setSupportedActions(Qt::DropActions actions);
@ -111,6 +118,7 @@ signals:
void drop(int action);
void supportedActionsChanged();
void defaultActionChanged();
void startDragDistanceChanged();
protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
@ -126,6 +134,7 @@ private:
Qt::DropActions m_supportedActions;
Qt::DropAction m_defaultAction;
DeclarativeMimeData* const m_data;
int m_startDragDistance;
};
#endif // DECLARATIVEDRAGAREA_H