Skip to content

Commit

Permalink
#185 Add qan::NodeItem::dragOrientation support.
Browse files Browse the repository at this point in the history
Signed-off-by: cneben <benoit@destrat.io>
  • Loading branch information
cneben committed Dec 26, 2022
1 parent b0351e6 commit a8012b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 20221226 prev2.3.0:
- #185: Add horizontal or vertical constrain on node/group dragging, see new property `qan::NodeItem::dragOrientation`.

## 20221225 prev2.3.0:
- #185: Add support for "snap to grid" when moving node or groups.
- Add |qan::Graph`, `snapToGrid` and `snapToGridSize` properties.
Expand Down
30 changes: 17 additions & 13 deletions src/qanDraggableCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,32 +242,36 @@ void DraggableCtrl::dragMove(const QPointF& sceneDragPos, bool dragSelection)
// 2.2. If target position is "centered" on grid
// or mouse delta > grid
// 2.2.1 Compute snapped position, apply it
const auto targetDragOrientation = _targetItem->getDragOrientation();
const auto dragHorizontally = (targetDragOrientation == qan::NodeItem::DragOrientation::All) ||
(targetDragOrientation == qan::NodeItem::DragOrientation::Horizontal);
const auto dragVertically = (targetDragOrientation == qan::NodeItem::DragOrientation::All) ||
(targetDragOrientation == qan::NodeItem::DragOrientation::Vertical);
if (getGraph()->getSnapToGrid()) {
const auto& gridSize = getGraph()->getSnapToGridSize();
bool applyX = std::fabs(delta.x()) > (gridSize.width() / 2.001);
bool applyY = std::fabs(delta.y()) > (gridSize.height() / 2.001);

//qWarning() << "--------";
//qWarning() << "targetUnsnapPos=" << targetUnsnapPos;
if (!applyX) {
bool applyX = dragHorizontally &&
std::fabs(delta.x()) > (gridSize.width() / 2.001);
bool applyY = dragVertically &&
std::fabs(delta.y()) > (gridSize.height() / 2.001);
if (!applyX && dragHorizontally) {
const auto posModGridX = fmod(targetUnsnapPos.x(), gridSize.width());
//qWarning() << "posModGridX=" << posModGridX;
applyX = qFuzzyIsNull(posModGridX);
}
if (!applyY) {
if (!applyY && dragVertically) {
const auto posModGridY = fmod(targetUnsnapPos.y(), gridSize.height());
//qWarning() << "posModGridY=" << posModGridY;
applyY = qFuzzyIsNull(posModGridY);
}
//qWarning() << "applyX=" << applyX << " applyY=" << applyY;
if (applyX || applyY) {
const auto targetSnapPosX = gridSize.width() * std::round(targetUnsnapPos.x() / gridSize.width());
const auto targetSnapPosY = gridSize.height() * std::round(targetUnsnapPos.y() / gridSize.height());
const auto targetSnapPosX = dragHorizontally ? gridSize.width() * std::round(targetUnsnapPos.x() / gridSize.width()) :
_initialTargetPos.x();
const auto targetSnapPosY = dragVertically ? gridSize.height() * std::round(targetUnsnapPos.y() / gridSize.height()) :
_initialTargetPos.y();
_targetItem->setPosition(QPointF{targetSnapPosX,
targetSnapPosY});
}
} else { // Do not snap to grid
_targetItem->setPosition(targetUnsnapPos);
_targetItem->setPosition(QPointF{dragHorizontally ? targetUnsnapPos.x() : _initialTargetPos.x(),
dragVertically ? targetUnsnapPos.y() : _initialTargetPos.y()});
}
// FIXME #185 Selection move does not works...
if (dragSelection) {
Expand Down
10 changes: 10 additions & 0 deletions src/qanNodeItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ void NodeItem::setConnectable(Connectable connectable) noexcept
//-----------------------------------------------------------------------------

/* Draggable Management *///---------------------------------------------------
bool NodeItem::setDragOrientation(DragOrientation dragOrientation) noexcept
{
if (dragOrientation != _dragOrientation) {
_dragOrientation = dragOrientation;
emit dragOrientationChanged();
return true;
}
return false;
}

void NodeItem::dragEnterEvent(QDragEnterEvent* event)
{
if (getNode() != nullptr &&
Expand Down
32 changes: 27 additions & 5 deletions src/qanNodeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ class NodeItem : public QQuickItem,
NodeItem& operator=(const NodeItem&) = delete;
NodeItem(NodeItem&&) = delete;
NodeItem& operator=(NodeItem&&) = delete;

public:
qan::AbstractDraggableCtrl& draggableCtrl();
protected:
std::unique_ptr<qan::AbstractDraggableCtrl> _draggableCtrl;
//@}
//-------------------------------------------------------------------------

Expand Down Expand Up @@ -260,6 +255,10 @@ protected slots:

/*! \name Draggable Management *///----------------------------------------
//@{
public:
qan::AbstractDraggableCtrl& draggableCtrl();
protected:
std::unique_ptr<qan::AbstractDraggableCtrl> _draggableCtrl;
public:
//! \copydoc qan::Draggable::_draggable
Q_PROPERTY(bool draggable READ getDraggable WRITE setDraggable NOTIFY draggableChanged FINAL)
Expand All @@ -269,17 +268,40 @@ protected slots:
Q_PROPERTY(bool droppable READ getDroppable WRITE setDroppable NOTIFY droppableChanged FINAL)
//! \copydoc qan::Draggable::_acceptDrops
Q_PROPERTY(bool acceptDrops READ getAcceptDrops WRITE setAcceptDrops NOTIFY acceptDropsChanged FINAL)

protected:
virtual void emitDraggableChanged() override { emit draggableChanged(); }
virtual void emitDraggedChanged() override { emit draggedChanged(); }
virtual void emitAcceptDropsChanged() override { emit acceptDropsChanged(); }
virtual void emitDroppableChanged() override { emit droppableChanged(); }

signals:
void draggableChanged();
void draggedChanged();
void droppableChanged();
void acceptDropsChanged();

public:
//! Define an orientation contrain on node dragging.
enum class DragOrientation: unsigned int {
All = 0, //! All is no constrain on dragging: drag in all directions
Vertical = 1, //! Drag only horizontally
Horizontal = 2 //! Drag only vertically
};
Q_ENUM(DragOrientation)
//! \copydoc getDragOrientation()
Q_PROPERTY(DragOrientation dragOrientation READ getDragOrientation WRITE setDragOrientation NOTIFY dragOrientationChanged FINAL)
//! \copydoc getDragOrientation()
bool setDragOrientation(DragOrientation dragOrientation) noexcept;
//! Define a constrain on node/group dragging, default to no constrain, set to horizontal or vertical to constrain dragging on one orientation.
DragOrientation getDragOrientation() const noexcept { return _dragOrientation; }
protected:
//! \copydoc getDragOrientation()
DragOrientation _dragOrientation = DragOrientation::All;
signals:
//! \copydoc getDragOrientation()
void dragOrientationChanged();

protected:
//! Internally used to manage drag and drop over nodes, override with caution, and call base class implementation.
virtual void dragEnterEvent(QDragEnterEvent* event) override;
Expand Down

0 comments on commit a8012b1

Please sign in to comment.