/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
***/
#include "nodeviewedge.h"
#include
#include "common/clamp.h"
#include "common/lerp.h"
#include "nodeview.h"
NodeViewEdge::NodeViewEdge(QGraphicsItem *parent) :
QGraphicsLineItem(parent),
edge_(nullptr)
{
// FIXME: This should probably be set to the text color in order to work on light themes
setPen(QPen(Qt::white, 2));
// Ensures this UI object is drawn behind other objects
setZValue(-1);
}
void NodeViewEdge::SetEdge(NodeEdgePtr edge)
{
// Set the new edge pointer
edge_ = edge;
// Re-adjust the line positioning for this new edge
Adjust();
}
void NodeViewEdge::Adjust()
{
if (edge_ == nullptr || scene() == nullptr) {
return;
}
// Get the UI objects of both nodes that this edge connects
NodeViewItem* output = NodeView::NodeToUIObject(scene(), edge_->output()->parent());
NodeViewItem* input = NodeView::NodeToUIObject(scene(), edge_->input()->parent());
// Create initial values
QPointF output_point = QPointF(output->pos().x() + output->rect().width(), 0);
QPointF input_point = QPointF(input->pos().x(), 0);
// Calculate output/input points
output_point.setY(CalculateEdgeYPoint(output, edge_->output()->index(), input));
input_point.setY(CalculateEdgeYPoint(input, edge_->input()->index(), output));
// Draw a line between the two
setLine(QLineF(
output_point,
input_point
));
}
qreal NodeViewEdge::CalculateEdgeYPoint(NodeViewItem *item, int param_index, NodeViewItem *opposing)
{
if (item->IsExpanded()) {
return item->pos().y() + item->GetParameterConnectorRect(param_index).center().y();
} else {
qreal max_height = qMax(opposing->rect().height(), item->rect().height());
// Calculate the Y distance between the two nodes and create a 0.0-1.0 range for lerping
qreal input_value = clamp(0.5 + (opposing->pos().y() - item->pos().y()) / max_height / 4, 0.0, 1.0);
// Use a lerp function to draw the line between the two corners
qreal input_y = item->pos().y() + lerp(0.0, item->rect().height(), input_value);
// Set Y values according to calculations
return input_y;
}
}