nodeview: allow expanded views of nodes again

Re-implementing expanding nodes to see the connections (but nicer this time).
This commit is contained in:
itsmattkc
2020-04-29 03:56:50 +10:00
parent d8df7cd5a7
commit f04fa12c06
4 changed files with 113 additions and 60 deletions
+19 -9
View File
@@ -76,8 +76,9 @@ void NodeViewEdge::Adjust()
}
// Draw a line between the two
SetPoints(output->GetParamPoint(edge_->output()),
input->GetParamPoint(edge_->input()));
SetPoints(output->GetParamPoint(edge_->output(), output->pos()),
input->GetParamPoint(edge_->input(), output->pos()),
input->IsExpanded());
}
void NodeViewEdge::SetConnected(bool c)
@@ -102,19 +103,30 @@ void NodeViewEdge::SetHighlighted(bool e)
UpdatePen();
}
void NodeViewEdge::SetPoints(const QPointF &start, const QPointF &end)
void NodeViewEdge::SetPoints(const QPointF &start, const QPointF &end, bool input_is_expanded)
{
QPainterPath path;
path.moveTo(start);
double half_x = lerp(start.x(), end.x(), 0.5);
double half_y = lerp(start.y(), end.y(), 0.5);
QPointF cp1, cp2;
if (NodeViewCommon::GetFlowOrientation(flow_dir_) == Qt::Horizontal) {
double half_x = lerp(start.x(), end.x(), 0.5);
path.cubicTo(QPointF(half_x, start.y()), QPointF(half_x, end.y()), end);
cp1 = QPointF(half_x, start.y());
} else {
double half_y = lerp(start.y(), end.y(), 0.5);
path.cubicTo(QPointF(start.x(), half_y), QPointF(end.x(), half_y), end);
cp1 = QPointF(start.x(), half_y);
}
if (NodeViewCommon::GetFlowOrientation(flow_dir_) == Qt::Horizontal || input_is_expanded) {
cp2 = QPointF(half_x, end.y());
} else {
cp2 = QPointF(end.x(), half_y);
}
path.cubicTo(cp1, cp2, end);
setPath(path);
}
@@ -128,8 +140,6 @@ void NodeViewEdge::SetFlowDirection(NodeViewCommon::FlowDirection dir)
void NodeViewEdge::UpdatePen()
{
setPen(QPen(qApp->palette().color(color_group_, color_role_), edge_width_));
//update();
}
OLIVE_NAMESPACE_EXIT
+1 -1
View File
@@ -82,7 +82,7 @@ public:
/**
* @brief Set points to create curve from
*/
void SetPoints(const QPointF& start, const QPointF& end);
void SetPoints(const QPointF& start, const QPointF& end, bool input_is_expanded);
/**
* @brief Sets the direction nodes are flowing
+87 -47
View File
@@ -44,6 +44,7 @@ NodeViewItem::NodeViewItem(QGraphicsItem *parent) :
cached_drop_item_(nullptr),
cached_drop_item_expanded_(false),
expanded_(false),
hide_titlebar_(false),
standard_click_(false),
highlighted_index_(-1),
node_edge_change_command_(nullptr),
@@ -192,18 +193,26 @@ bool NodeViewItem::IsExpanded() const
return expanded_;
}
void NodeViewItem::SetExpanded(bool e)
void NodeViewItem::SetExpanded(bool e, bool hide_titlebar)
{
if (expanded_ == e) {
if (node_inputs_.isEmpty()
|| (expanded_ == e && hide_titlebar_ == hide_titlebar)) {
return;
}
expanded_ = e;
hide_titlebar_ = hide_titlebar;
if (expanded_ && !node_inputs_.isEmpty()) {
// Create new rect
QRectF new_rect = title_bar_rect_;
new_rect.setHeight(new_rect.height() * node_inputs_.size());
if (hide_titlebar_) {
new_rect.setHeight(new_rect.height() * node_inputs_.size());
} else {
new_rect.setHeight(new_rect.height() * (node_inputs_.size() + 1));
}
setRect(new_rect);
} else {
setRect(title_bar_rect_);
@@ -223,33 +232,14 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
// don't want here)
QPalette app_pal = Core::instance()->main_window()->palette();
{
QPen border_pen;
border_pen.setWidth(node_border_width_);
QBrush bkg_color;
if (option->state & QStyle::State_Selected) {
border_pen.setColor(app_pal.color(QPalette::Highlight));
} else {
border_pen.setColor(css_proxy_.BorderColor());
}
if (IsExpanded()) {
bkg_color = app_pal.color(QPalette::Window);
} else {
bkg_color = css_proxy_.TitleBarColor();
}
painter->setPen(border_pen);
painter->setBrush(bkg_color);
// Draw background rect if expanded
if (IsExpanded()) {
painter->setPen(Qt::NoPen);
painter->setBrush(app_pal.color(QPalette::Window));
painter->drawRect(rect());
}
painter->setPen(app_pal.color(QPalette::Text));
if (IsExpanded()) {
painter->setPen(app_pal.color(QPalette::Text));
for (int i=0;i<node_inputs_.size();i++) {
QRectF input_rect = GetInputRect(i);
@@ -260,13 +250,37 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
painter->drawText(input_rect, Qt::AlignCenter, node_inputs_.at(i)->name());
}
}
} else if (node_) {
// Draw the titlebar
if (!hide_titlebar_ && node_) {
painter->setPen(Qt::black);
painter->setBrush(css_proxy_.TitleBarColor());
painter->drawRect(title_bar_rect_);
painter->setPen(app_pal.color(QPalette::Text));
// Draw the text in a rect (the rect is sized around text already in the constructor)
painter->drawText(title_bar_rect_, Qt::AlignCenter, node_->Name());
}
// Draw final border
QPen border_pen;
border_pen.setWidth(node_border_width_);
if (option->state & QStyle::State_Selected) {
border_pen.setColor(app_pal.color(QPalette::Highlight));
} else {
border_pen.setColor(css_proxy_.BorderColor());
}
painter->setPen(border_pen);
painter->setBrush(Qt::NoBrush);
painter->drawRect(rect());
}
void NodeViewItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
@@ -294,7 +308,7 @@ void NodeViewItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
drag_src_param_ = param;
// Set the starting position to the current param's connector
dragging_edge_start_ = GetParamPoint(param);
dragging_edge_start_ = GetParamPoint(param, QPointF());
} else if (param->type() == NodeParam::kInput) {
// For an input param, we default to moving an existing edge
@@ -369,6 +383,8 @@ void NodeViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
cached_drop_item_->SetExpanded(false);
}
cached_drop_item_->SetHighlightedIndex(-1);
cached_drop_item_->setZValue(0);
cached_drop_item_ = nullptr;
}
@@ -384,7 +400,7 @@ void NodeViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
cached_drop_item_expanded_ = !cached_drop_item_->IsExpanded();
if (cached_drop_item_expanded_) {
cached_drop_item_->SetExpanded(true);
cached_drop_item_->SetExpanded(true, true);
}
cached_drop_item_->setZValue(1);
@@ -412,7 +428,7 @@ void NodeViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
drag_dest_param_ = comp_param;
highlight_their_index = i;
QPointF end_point_local = cached_drop_item_->GetInputPoint(i);
QPointF end_point_local = cached_drop_item_->GetInputPoint(i, pos());
end_point = cached_drop_item_->mapToScene(end_point_local);
}
@@ -425,7 +441,9 @@ void NodeViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
dragging_edge_->SetConnected(drag_dest_param_);
dragging_edge_->SetPoints(dragging_edge_start_, end_point);
dragging_edge_->SetPoints(dragging_edge_start_,
end_point,
cached_drop_item_ ? cached_drop_item_->IsExpanded() : false);
return;
}
@@ -446,8 +464,14 @@ void NodeViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
scene()->removeItem(dragging_edge_);
// If we expanded an item in the drag, re-collapse it now
if (cached_drop_item_ != nullptr) {
cached_drop_item_->SetExpanded(false);
if (cached_drop_item_) {
if (cached_drop_item_expanded_) {
cached_drop_item_->SetExpanded(false);
}
cached_drop_item_->SetHighlightedIndex(-1);
cached_drop_item_->setZValue(0);
cached_drop_item_ = nullptr;
}
@@ -488,6 +512,13 @@ void NodeViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
}
}
void NodeViewItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsRectItem::mouseDoubleClickEvent(event);
SetExpanded(!IsExpanded());
}
QVariant NodeViewItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionHasChanged && node_) {
@@ -514,6 +545,10 @@ QRectF NodeViewItem::GetInputRect(int index) const
{
QRectF r = title_bar_rect_;
if (!hide_titlebar_) {
index++;
}
if (IsExpanded()) {
r.translate(0, r.height() * index);
}
@@ -521,7 +556,7 @@ QRectF NodeViewItem::GetInputRect(int index) const
return r;
}
QPointF NodeViewItem::GetParamPoint(NodeParam *param) const
QPointF NodeViewItem::GetParamPoint(NodeParam *param, const QPointF& source_pos) const
{
if (param->type() == NodeParam::kOutput) {
@@ -545,7 +580,7 @@ QPointF NodeViewItem::GetParamPoint(NodeParam *param) const
input = static_cast<NodeInput*>(input->parent());
}
return pos() + GetInputPoint(node_inputs_.indexOf(input));
return pos() + GetInputPoint(node_inputs_.indexOf(input), source_pos);
}
}
@@ -554,20 +589,25 @@ void NodeViewItem::SetFlowDirection(NodeViewCommon::FlowDirection dir)
flow_dir_ = dir;
}
QPointF NodeViewItem::GetInputPoint(int index) const
QPointF NodeViewItem::GetInputPoint(int index, const QPointF& source_pos) const
{
QRectF input_rect = GetInputRect(index);
switch (flow_dir_) {
case NodeViewCommon::kLeftToRight:
default:
return QPointF(input_rect.left(), input_rect.center().y());
case NodeViewCommon::kRightToLeft:
return QPointF(input_rect.right(), input_rect.center().y());
case NodeViewCommon::kTopToBottom:
return QPointF(input_rect.center().x(), input_rect.top());
case NodeViewCommon::kBottomToTop:
return QPointF(input_rect.center().x(), input_rect.bottom());
Qt::Orientation flow_orientation = NodeViewCommon::GetFlowOrientation(flow_dir_);
if (flow_orientation == Qt::Horizontal || IsExpanded()) {
if (flow_dir_ == NodeViewCommon::kLeftToRight
|| (flow_orientation == Qt::Vertical && source_pos.x() < pos().x())) {
return QPointF(input_rect.left(), input_rect.center().y());
} else {
return QPointF(input_rect.right(), input_rect.center().y());
}
} else {
if (flow_dir_ == NodeViewCommon::kTopToBottom) {
return QPointF(input_rect.center().x(), input_rect.top());
} else {
return QPointF(input_rect.center().x(), input_rect.bottom());
}
}
}
+6 -3
View File
@@ -67,13 +67,13 @@ public:
/**
* @brief Set expanded state
*/
void SetExpanded(bool e);
void SetExpanded(bool e, bool hide_titlebar = false);
void ToggleExpanded();
/**
* @brief Returns GLOBAL point that edges should connect to for any NodeParam member of this object
*/
QPointF GetParamPoint(NodeParam* param) const;
QPointF GetParamPoint(NodeParam* param, const QPointF &source_pos) const;
/**
* @brief Sets the direction nodes are flowing
@@ -98,6 +98,7 @@ protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
@@ -115,7 +116,7 @@ private:
/**
* @brief Returns local point that edges should connect to for a NodeInput in array node_inputs_[index]
*/
QPointF GetInputPoint(int index) const;
QPointF GetInputPoint(int index, const QPointF &source_pos) const;
/**
* @brief Reference to attached Node
@@ -157,6 +158,8 @@ private:
*/
bool expanded_;
bool hide_titlebar_;
/**
* @brief Current click mode
*