Skip to content

Commit

Permalink
#185 Fix qan::EdgeDraggableCtrl for new snap to grid support.
Browse files Browse the repository at this point in the history
Partial FIX.
WIP.

Signed-off-by: cneben <[email protected]>
  • Loading branch information
cneben committed Dec 26, 2022
1 parent c7fd793 commit 7cefb8d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/qanDraggableCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ bool DraggableCtrl::handleMouseMoveEvent(QMouseEvent* event)
const auto sceneDragPos = rootItem->mapFromGlobal(event->globalPos());
if (!_targetItem->getDragged()) {
// Project in scene rect (for example is a node is part of a group)
//const auto sceneTargetPos = rootItem->mapFromItem(_targetItem, QPointF{0,0});
//_initialTargetPos = sceneTargetPos;
beginDragMove(sceneDragPos, _targetItem->getSelected());
return true;
} else {
Expand Down
25 changes: 13 additions & 12 deletions src/qanEdgeDraggableCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ bool EdgeDraggableCtrl::handleMouseMoveEvent(QMouseEvent* event)
const auto rootItem = graph->getContainerItem();
if (rootItem != nullptr && // Root item exist, left button is pressed and the target item
event->buttons().testFlag(Qt::LeftButton)) { // is draggable and not collapsed
const auto globalPos = rootItem->mapFromGlobal(event->globalPos());
const auto sceneDragPos = rootItem->mapFromGlobal(event->globalPos());
if (!_targetItem->getDragged()) {
beginDragMove(globalPos, false); // false = no selection (no edge selection support)
beginDragMove(sceneDragPos, false); // false = no selection (no edge selection support)
return true;
} else {
const auto delta = globalPos - _dragLastPos;
_dragLastPos = globalPos;
dragMove(delta, false); // false = no selection (no edge selection support)
dragMove(sceneDragPos, false); // false = no selection (no edge selection support)
return true;
}
}
Expand All @@ -110,14 +108,17 @@ void EdgeDraggableCtrl::handleMouseReleaseEvent(QMouseEvent* event)
endDragMove();
}

void EdgeDraggableCtrl::beginDragMove(const QPointF& dragInitialMousePos, bool dragSelection)
void EdgeDraggableCtrl::beginDragMove(const QPointF& sceneDragPos, bool dragSelection)
{
Q_UNUSED(dragSelection)
if (!_targetItem)
return;

_targetItem->setDragged(true);
_dragLastPos = dragInitialMousePos;
_initialDragPos = sceneDragPos;
const auto rootItem = getGraph()->getContainerItem();
if (rootItem != nullptr) // Project in scene rect (for example is a node is part of a group)
_initialTargetPos = rootItem->mapFromItem(_targetItem, QPointF{0,0});

// Get target edge adjacent nodes
auto src = _targetItem->getSourceItem() != nullptr &&
Expand All @@ -127,12 +128,12 @@ void EdgeDraggableCtrl::beginDragMove(const QPointF& dragInitialMousePos, boo
_targetItem->getDestinationItem()->getNode() != nullptr ? _targetItem->getDestinationItem()->getNode()->getItem() :
nullptr;
if (src != nullptr)
src->draggableCtrl().beginDragMove(dragInitialMousePos, false);
src->draggableCtrl().beginDragMove(sceneDragPos, false);
if (dst != nullptr)
dst->draggableCtrl().beginDragMove(dragInitialMousePos, false);
dst->draggableCtrl().beginDragMove(sceneDragPos, false);
}

void EdgeDraggableCtrl::dragMove(const QPointF& delta, bool dragSelection)
void EdgeDraggableCtrl::dragMove(const QPointF& sceneDragPos, bool dragSelection)
{
Q_UNUSED(dragSelection)
// PRECONDITIONS:
Expand All @@ -147,9 +148,9 @@ void EdgeDraggableCtrl::dragMove(const QPointF& delta, bool dragSelection)
_targetItem->getDestinationItem()->getNode() != nullptr ? _targetItem->getDestinationItem()->getNode()->getItem() :
nullptr;
if (src != nullptr)
src->draggableCtrl().dragMove(delta, false);
src->draggableCtrl().dragMove(sceneDragPos, false);
if (dst != nullptr)
dst->draggableCtrl().dragMove(delta, false);
dst->draggableCtrl().dragMove(sceneDragPos, false);
}

void EdgeDraggableCtrl::endDragMove(bool dragSelection)
Expand Down
12 changes: 7 additions & 5 deletions src/qanEdgeDraggableCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EdgeDraggableCtrl : public qan::AbstractDraggableCtrl
inline auto getTargetItem() const noexcept -> const qan::EdgeItem* { return _targetItem.data(); }
inline auto setTargetItem(qan::EdgeItem* targetItem) noexcept { _targetItem = targetItem; }
private:
QPointer<qan::EdgeItem> _targetItem{nullptr};
QPointer<qan::EdgeItem> _targetItem;

protected:
//! Utility to return a safe _targetItem->getGraph().
Expand All @@ -90,14 +90,16 @@ class EdgeDraggableCtrl : public qan::AbstractDraggableCtrl

public:
//! \c dragInitialMousePos in window coordinate system.
virtual void beginDragMove(const QPointF& dragInitialMousePos, bool dragSelection = true) override;
virtual void beginDragMove(const QPointF& sceneDragPos, bool dragSelection = true) override;
//! \c delta in scene coordinate system.
virtual void dragMove(const QPointF& delta, bool dragSelection = true) override;
virtual void dragMove(const QPointF& sceneDragPos, bool dragSelection = true) override;
virtual void endDragMove(bool dragSelection = true) override;

private:
//! Internal position cache.
QPointF _dragLastPos{0., 0.};
//! Internal (mouse) initial dragging position.
QPointF _initialDragPos{0., 0.};
//! Internal (target) initial dragging position.
QPointF _initialTargetPos{0., 0.};
//@}
//-------------------------------------------------------------------------
};
Expand Down
2 changes: 1 addition & 1 deletion src/qanEdgeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private slots:
*
* Default to false.
*/
bool _draggable = false;
bool _draggable = true;

public:
//! \copydoc qan::Draggable::_dragged
Expand Down
2 changes: 1 addition & 1 deletion src/qanNodeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ protected slots:
DragOrientation getDragOrientation() const noexcept { return _dragOrientation; }
protected:
//! \copydoc getDragOrientation()
DragOrientation _dragOrientation = DragOrientation::All;
DragOrientation _dragOrientation = DragOrientation::DragAll;
signals:
//! \copydoc getDragOrientation()
void dragOrientationChanged();
Expand Down

0 comments on commit 7cefb8d

Please sign in to comment.