nodeview: make auto-position a manual and specific function, allow different flow directions
The first in many NodeView improvements. Up until now, nodes have been forcibly auto-arranged, which has worked as a reasonable stop-gap just so that all the nodes are visible during testing/development. However this does not make a good user experience. We move the auto-arrange code to a manual function that can be used when the user or program deems it necessary, and also abstract the node positions so that the view can ultimately determine the exact amount of spacing (useful for changing DPIs) or orientation, so users can direct the flow whichever way they want.
This commit is contained in:
@@ -558,6 +558,8 @@ const QPointF &Node::GetPosition()
|
||||
void Node::SetPosition(const QPointF &pos)
|
||||
{
|
||||
position_ = pos;
|
||||
|
||||
emit PositionChanged(position_);
|
||||
}
|
||||
|
||||
void Node::AddInput(NodeInput *input)
|
||||
|
||||
@@ -384,6 +384,11 @@ signals:
|
||||
*/
|
||||
void EdgeRemoved(NodeEdgePtr edge);
|
||||
|
||||
/**
|
||||
* @brief Signal emitted whenever the position is set through SetPosition()
|
||||
*/
|
||||
void PositionChanged(const QPointF& pos);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Add a parameter to this node
|
||||
|
||||
@@ -45,7 +45,7 @@ NodeView::NodeView(QWidget *parent) :
|
||||
connect(&scene_, &QGraphicsScene::selectionChanged, this, &NodeView::SceneSelectionChangedSlot);
|
||||
connect(this, &NodeView::customContextMenuRequested, this, &NodeView::ShowContextMenu);
|
||||
|
||||
SetFlowDirection(NodeViewCommon::kTopToBottom);
|
||||
SetFlowDirection(NodeViewCommon::kLeftToRight);
|
||||
}
|
||||
|
||||
NodeView::~NodeView()
|
||||
|
||||
@@ -43,6 +43,14 @@ public:
|
||||
return Qt::Horizontal;
|
||||
}
|
||||
}
|
||||
|
||||
static bool DirectionsAreOpposing(FlowDirection a, FlowDirection b) {
|
||||
return ((a == NodeViewCommon::kLeftToRight && b == NodeViewCommon::kRightToLeft)
|
||||
|| (a == NodeViewCommon::kRightToLeft && b == NodeViewCommon::kLeftToRight)
|
||||
|| (a == NodeViewCommon::kTopToBottom && b == NodeViewCommon::kBottomToTop)
|
||||
|| (a == NodeViewCommon::kBottomToTop && b == NodeViewCommon::kTopToBottom));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
@@ -68,6 +68,57 @@ NodeViewItem::NodeViewItem(QGraphicsItem *parent) :
|
||||
setRect(title_bar_rect_);
|
||||
}
|
||||
|
||||
QPointF NodeViewItem::GetNodePosition() const
|
||||
{
|
||||
QPointF node_pos;
|
||||
|
||||
qreal adjusted_x = pos().x() / DefaultItemHorizontalPadding();
|
||||
qreal adjusted_y = pos().y() / DefaultItemVerticalPadding();
|
||||
|
||||
switch (flow_dir_) {
|
||||
case NodeViewCommon::kLeftToRight:
|
||||
node_pos.setX(adjusted_x);
|
||||
node_pos.setY(adjusted_y);
|
||||
break;
|
||||
case NodeViewCommon::kRightToLeft:
|
||||
node_pos.setX(-adjusted_x);
|
||||
node_pos.setY(adjusted_y);
|
||||
break;
|
||||
case NodeViewCommon::kTopToBottom:
|
||||
node_pos.setX(adjusted_y);
|
||||
node_pos.setY(adjusted_x);
|
||||
break;
|
||||
case NodeViewCommon::kBottomToTop:
|
||||
node_pos.setX(-adjusted_y);
|
||||
node_pos.setY(adjusted_x);
|
||||
break;
|
||||
}
|
||||
|
||||
return node_pos;
|
||||
}
|
||||
|
||||
void NodeViewItem::SetNodePosition(const QPointF &pos)
|
||||
{
|
||||
switch (flow_dir_) {
|
||||
case NodeViewCommon::kLeftToRight:
|
||||
setPos(pos.x() * DefaultItemHorizontalPadding(),
|
||||
pos.y() * DefaultItemVerticalPadding());
|
||||
break;
|
||||
case NodeViewCommon::kRightToLeft:
|
||||
setPos(-pos.x() * DefaultItemHorizontalPadding(),
|
||||
pos.y() * DefaultItemVerticalPadding());
|
||||
break;
|
||||
case NodeViewCommon::kTopToBottom:
|
||||
setPos(pos.y() * DefaultItemHorizontalPadding(),
|
||||
pos.x() * DefaultItemVerticalPadding());
|
||||
break;
|
||||
case NodeViewCommon::kBottomToTop:
|
||||
setPos(pos.y() * DefaultItemHorizontalPadding(),
|
||||
-pos.x() * DefaultItemVerticalPadding());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int NodeViewItem::DefaultTextPadding()
|
||||
{
|
||||
return QFontMetrics(QFont()).height() / 4;
|
||||
@@ -88,6 +139,24 @@ int NodeViewItem::DefaultItemBorder()
|
||||
return QFontMetrics(QFont()).height() / 12;
|
||||
}
|
||||
|
||||
qreal NodeViewItem::DefaultItemHorizontalPadding() const
|
||||
{
|
||||
if (NodeViewCommon::GetFlowOrientation(flow_dir_) == Qt::Horizontal) {
|
||||
return DefaultItemWidth() * 1.5;
|
||||
} else {
|
||||
return DefaultItemWidth() * 1.25;
|
||||
}
|
||||
}
|
||||
|
||||
qreal NodeViewItem::DefaultItemVerticalPadding() const
|
||||
{
|
||||
if (NodeViewCommon::GetFlowOrientation(flow_dir_) == Qt::Horizontal) {
|
||||
return DefaultItemHeight() * 1.5;
|
||||
} else {
|
||||
return DefaultItemHeight() * 2.0;
|
||||
}
|
||||
}
|
||||
|
||||
void NodeViewItem::SetNode(Node *n)
|
||||
{
|
||||
node_ = n;
|
||||
@@ -241,8 +310,8 @@ void NodeViewItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
drag_source_ = static_cast<NodeViewScene*>(scene())->NodeToUIObject(drag_src_param_->parentNode());
|
||||
|
||||
// Get the opposing parameter's rect center using the line's current coordinates
|
||||
// (we use the current coordinates because a complex formula is used for the line's coords if the opposing
|
||||
// node is collapsed, therefore it's easier to just retrieve it from line itself)
|
||||
// (we use the current coordinates because a complex formula is used for the line's coords if
|
||||
// the opposing node is collapsed, therefore it's easier to just retrieve it from line itself)
|
||||
NodeViewEdge* existing_edge_ui = static_cast<NodeViewScene*>(scene())->EdgeToUIObject(edge);
|
||||
QPainterPath existing_edge_line = existing_edge_ui->path();
|
||||
QPointF edge_start = existing_edge_line.pointAtPercent(0);
|
||||
@@ -422,7 +491,9 @@ void NodeViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
QVariant NodeViewItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
if (change == ItemPositionHasChanged && node_) {
|
||||
node_->SetPosition(value.toPointF());
|
||||
node_->blockSignals(true);
|
||||
node_->SetPosition(GetNodePosition());
|
||||
node_->blockSignals(false);
|
||||
}
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
|
||||
@@ -46,6 +46,9 @@ class NodeViewItem : public QGraphicsRectItem
|
||||
public:
|
||||
NodeViewItem(QGraphicsItem* parent = nullptr);
|
||||
|
||||
QPointF GetNodePosition() const;
|
||||
void SetNodePosition(const QPointF& pos);
|
||||
|
||||
/**
|
||||
* @brief Set the Node to correspond to this widget
|
||||
*/
|
||||
@@ -85,6 +88,10 @@ public:
|
||||
|
||||
static int DefaultItemBorder();
|
||||
|
||||
qreal DefaultItemHorizontalPadding() const;
|
||||
|
||||
qreal DefaultItemVerticalPadding() const;
|
||||
|
||||
protected:
|
||||
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
|
||||
|
||||
|
||||
@@ -37,13 +37,18 @@ void NodeViewScene::SetFlowDirection(NodeViewCommon::FlowDirection direction)
|
||||
direction_ = direction;
|
||||
|
||||
{
|
||||
// Iterate over node items setting direction
|
||||
QHash<Node*, NodeViewItem*>::const_iterator i;
|
||||
for (i=item_map_.constBegin(); i!=item_map_.constEnd(); i++) {
|
||||
i.value()->SetFlowDirection(direction_);
|
||||
|
||||
// Update position too
|
||||
i.value()->SetNodePosition(i.key()->GetPosition());
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Iterate over edge items setting direction
|
||||
QHash<NodeEdge*, NodeViewEdge*>::const_iterator i;
|
||||
for (i=edge_map_.constBegin(); i!=edge_map_.constEnd(); i++) {
|
||||
i.value()->SetFlowDirection(direction_);
|
||||
@@ -173,10 +178,14 @@ void NodeViewScene::AddNode(Node* node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
connect(node, &Node::PositionChanged, this, &NodeViewScene::NodePositionChanged);
|
||||
}
|
||||
|
||||
void NodeViewScene::RemoveNode(Node *node)
|
||||
{
|
||||
disconnect(node, &Node::PositionChanged, this, &NodeViewScene::NodePositionChanged);
|
||||
|
||||
delete item_map_.take(node);
|
||||
}
|
||||
|
||||
@@ -215,59 +224,23 @@ void NodeViewScene::ReorganizeFrom(Node* n)
|
||||
return;
|
||||
}
|
||||
|
||||
NodeViewItem* parent_item = NodeToUIObject(n);
|
||||
QPointF parent_pos = n->GetPosition();
|
||||
|
||||
int item_sz, item_padding, layer_diff, total_top;
|
||||
|
||||
if (GetFlowOrientation() == Qt::Vertical) {
|
||||
item_sz = NodeViewItem::DefaultItemWidth();
|
||||
item_padding = item_sz / 2;
|
||||
layer_diff = NodeViewItem::DefaultItemHeight() * 2;
|
||||
total_top = parent_item->x();
|
||||
} else {
|
||||
item_sz = NodeViewItem::DefaultItemHeight();
|
||||
item_padding = item_sz;
|
||||
layer_diff = NodeViewItem::DefaultItemWidth() * 3 / 2;
|
||||
total_top = parent_item->y();
|
||||
}
|
||||
|
||||
int item_sz_with_padding = item_sz + item_padding;
|
||||
|
||||
int total_sz = item_sz_with_padding * immediates.size() - item_padding;
|
||||
|
||||
total_top -= total_sz / 2;
|
||||
total_top += item_sz / 2;
|
||||
|
||||
int item_layer_pos;
|
||||
|
||||
switch (direction_) {
|
||||
case NodeViewCommon::kTopToBottom:
|
||||
item_layer_pos = parent_item->pos().y() - layer_diff;
|
||||
break;
|
||||
case NodeViewCommon::kLeftToRight:
|
||||
item_layer_pos = parent_item->pos().x() - layer_diff;
|
||||
break;
|
||||
case NodeViewCommon::kBottomToTop:
|
||||
item_layer_pos = parent_item->pos().y() + layer_diff;
|
||||
break;
|
||||
case NodeViewCommon::kRightToLeft:
|
||||
item_layer_pos = parent_item->pos().x() + layer_diff;
|
||||
break;
|
||||
}
|
||||
qreal child_x = parent_pos.x() - 1.0;
|
||||
qreal children_height = immediates.size()-1;
|
||||
qreal children_y = parent_pos.y() - children_height * 0.5;
|
||||
|
||||
for (int i=0;i<immediates.size();i++) {
|
||||
NodeViewItem* item = NodeToUIObject(immediates.at(i));
|
||||
|
||||
if (GetFlowOrientation() == Qt::Vertical) {
|
||||
item->setPos(total_top + item_sz_with_padding * i,
|
||||
item_layer_pos);
|
||||
} else {
|
||||
item->setPos(item_layer_pos,
|
||||
total_top + item_sz_with_padding * i);
|
||||
}
|
||||
immediates.at(i)->SetPosition(QPointF(child_x,
|
||||
children_y + i));
|
||||
|
||||
ReorganizeFrom(immediates.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
void NodeViewScene::NodePositionChanged(const QPointF &pos)
|
||||
{
|
||||
item_map_.value(static_cast<Node*>(sender()))->SetNodePosition(pos);
|
||||
}
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
@@ -121,6 +121,13 @@ private:
|
||||
|
||||
NodeViewCommon::FlowDirection direction_;
|
||||
|
||||
private slots:
|
||||
/**
|
||||
* @brief Receiver for whenever a node position changes
|
||||
*/
|
||||
void NodePositionChanged(const QPointF& pos);
|
||||
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
Reference in New Issue
Block a user