began node viewer for dag insight
This commit is contained in:
@@ -20,5 +20,7 @@ set(OLIVE_SOURCES
|
||||
widget/nodeview/nodeview.cpp
|
||||
widget/nodeview/nodeviewitem.h
|
||||
widget/nodeview/nodeviewitem.cpp
|
||||
widget/nodeview/nodeviewedge.h
|
||||
widget/nodeview/nodeviewedge.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
@@ -20,22 +20,81 @@
|
||||
|
||||
#include "nodeview.h"
|
||||
|
||||
#include <QGraphicsRectItem>
|
||||
|
||||
#include "nodeviewitem.h"
|
||||
|
||||
NodeView::NodeView(QWidget *parent) :
|
||||
QGraphicsView(parent),
|
||||
graph_(nullptr)
|
||||
{
|
||||
setScene(&scene_);
|
||||
|
||||
NodeViewItem* item = new NodeViewItem();
|
||||
|
||||
scene_.addItem(item);
|
||||
connect(&scene_, SIGNAL(changed(const QList<QRectF>&)), this, SLOT(ItemsChanged()));
|
||||
}
|
||||
|
||||
void NodeView::SetGraph(NodeGraph *graph)
|
||||
{
|
||||
// Clear the scene of all UI objects
|
||||
scene_.clear();
|
||||
edges_.clear();
|
||||
|
||||
// Set reference to the graph
|
||||
graph_ = graph;
|
||||
|
||||
// If the graph is valid, add UI objects for each of its Nodes
|
||||
if (graph_ != nullptr) {
|
||||
QList<Node*> graph_nodes = graph_->nodes();
|
||||
|
||||
foreach (Node* node, graph_nodes) {
|
||||
NodeViewItem* item = new NodeViewItem();
|
||||
|
||||
item->SetNode(node);
|
||||
|
||||
scene_.addItem(item);
|
||||
|
||||
// Add a NodeViewEdge for each connection
|
||||
QList<NodeParam*> node_params = node->parameters();
|
||||
|
||||
foreach (NodeParam* param, node_params) {
|
||||
|
||||
// We only bother working with outputs since eventually this will cover all inputs too
|
||||
// (covering both would lead to duplicates since every edge connects to one input and one output)
|
||||
if (param->type() == NodeParam::kOutput) {
|
||||
const QVector<NodeEdgePtr>& edges = param->edges();
|
||||
|
||||
foreach(NodeEdgePtr edge, edges) {
|
||||
NodeViewEdge* edge_ui = new NodeViewEdge();
|
||||
|
||||
edge_ui->SetEdge(edge);
|
||||
|
||||
scene_.addItem(edge_ui);
|
||||
|
||||
// Keep track of edge widgets so they can be quickly updated whenever nodes move
|
||||
edges_.append(edge_ui);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NodeViewItem *NodeView::NodeToUIObject(QGraphicsScene *scene, Node *n)
|
||||
{
|
||||
QList<QGraphicsItem*> graphics_items = scene->items();
|
||||
|
||||
for (int i=0;i<graphics_items.size();i++) {
|
||||
NodeViewItem* item = dynamic_cast<NodeViewItem*>(graphics_items.at(i));
|
||||
|
||||
if (item != nullptr) {
|
||||
if (item->node() == n) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void NodeView::ItemsChanged()
|
||||
{
|
||||
foreach (NodeViewEdge* edge, edges_) {
|
||||
edge->Adjust();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
#include <QGraphicsView>
|
||||
|
||||
#include "node/graph.h"
|
||||
#include "widget/nodeview/nodeviewedge.h"
|
||||
#include "widget/nodeview/nodeviewitem.h"
|
||||
|
||||
class NodeView : public QGraphicsView
|
||||
{
|
||||
@@ -33,10 +35,18 @@ public:
|
||||
|
||||
void SetGraph(NodeGraph* graph);
|
||||
|
||||
static NodeViewItem* NodeToUIObject(QGraphicsScene* scene, Node* n);
|
||||
|
||||
private:
|
||||
NodeGraph* graph_;
|
||||
|
||||
QGraphicsScene scene_;
|
||||
|
||||
QList<NodeViewEdge*> edges_;
|
||||
|
||||
private slots:
|
||||
void ItemsChanged();
|
||||
|
||||
};
|
||||
|
||||
#endif // NODEVIEW_H
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/***
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
***/
|
||||
|
||||
#include "nodeviewedge.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "clamp.h"
|
||||
#include "lerp.h"
|
||||
#include "nodeview.h"
|
||||
#include "nodeviewitem.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());
|
||||
|
||||
// Calculate output/input points
|
||||
qreal value = clamp(0.5 + (output->pos().y() - input->pos().y()) / (input->rect().height()) / 4, 0.0, 1.0);
|
||||
|
||||
// Use a lerp function to draw the line between the two corners
|
||||
qreal output_point = output->pos().y() + lerp(0.0, output->rect().height(), 1.0 - value);
|
||||
qreal input_point = input->pos().y() + lerp(0.0, input->rect().height(), value);
|
||||
|
||||
// Draw a line between the two
|
||||
setLine(QLineF(
|
||||
QPointF(output->pos().x() + output->rect().width(), output_point),
|
||||
QPointF(input->pos().x(), input_point)
|
||||
));
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/***
|
||||
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
***/
|
||||
|
||||
#ifndef NODEEDGEITEM_H
|
||||
#define NODEEDGEITEM_H
|
||||
|
||||
#include <QGraphicsLineItem>
|
||||
|
||||
#include "node/edge.h"
|
||||
|
||||
class NodeViewEdge : public QGraphicsLineItem
|
||||
{
|
||||
public:
|
||||
NodeViewEdge(QGraphicsItem* parent = nullptr);
|
||||
|
||||
void SetEdge(NodeEdgePtr edge);
|
||||
|
||||
void Adjust();
|
||||
|
||||
private:
|
||||
NodeEdgePtr edge_;
|
||||
};
|
||||
|
||||
#endif // NODEEDGEITEM_H
|
||||
@@ -20,41 +20,66 @@
|
||||
|
||||
#include "nodeviewitem.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QBrush>
|
||||
#include <QFontMetrics>
|
||||
#include <QPainter>
|
||||
#include <QPen>
|
||||
#include <QStyleOptionGraphicsItem>
|
||||
|
||||
#include "ui/icons/icons.h"
|
||||
|
||||
const int kNodeViewItemBorderWidth = 2;
|
||||
const int kNodeViewItemWidth = 250;
|
||||
const int kNodeViewItemPadding = 5;
|
||||
const int kNodeViewItemTextPadding = 4;
|
||||
const int kNodeViewItemIconPadding = 12;
|
||||
|
||||
NodeViewItem::NodeViewItem(QGraphicsItem *parent) :
|
||||
QGraphicsRectItem(parent),
|
||||
node_(nullptr)
|
||||
{
|
||||
// Set flags for this widget
|
||||
setFlag(QGraphicsItem::ItemIsMovable);
|
||||
setFlag(QGraphicsItem::ItemIsSelectable);
|
||||
|
||||
// Use the current default font height to size this widget
|
||||
QFont f;
|
||||
QFontMetrics fm(f);
|
||||
|
||||
setRect(0, 0, kNodeViewItemWidth, fm.height() + kNodeViewItemPadding * 2);
|
||||
setRect(0, 0, kNodeViewItemWidth, fm.height() + kNodeViewItemTextPadding * 2);
|
||||
|
||||
// FIXME: Magic "number"/magic "color" - allow this to be editable by the user
|
||||
SetColor(QColor(32, 32, 128));
|
||||
}
|
||||
|
||||
void NodeViewItem::SetColor(const QColor &color)
|
||||
{
|
||||
color_ = color;
|
||||
|
||||
// Create a light gradient based on this color
|
||||
UpdateGradient();
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void NodeViewItem::SetNode(Node *n)
|
||||
{
|
||||
node_ = n;
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
Node *NodeViewItem::node()
|
||||
{
|
||||
return node_;
|
||||
}
|
||||
|
||||
void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||
{
|
||||
Q_UNUSED(widget)
|
||||
|
||||
// Set up border, which will change color if selected
|
||||
QPen pen;
|
||||
|
||||
pen.setWidth(kNodeViewItemBorderWidth);
|
||||
|
||||
if (option->state & QStyle::State_Selected) {
|
||||
pen.setColor(qApp->palette().highlight().color());
|
||||
pen.setColor(widget->palette().highlight().color());
|
||||
} else {
|
||||
// FIXME: Not configurable?
|
||||
pen.setColor(Qt::black);
|
||||
@@ -62,16 +87,37 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
|
||||
|
||||
// Draw rect
|
||||
painter->setPen(pen);
|
||||
painter->setBrush(qApp->palette().window());
|
||||
painter->setBrush(brush());
|
||||
painter->drawRect(rect());
|
||||
|
||||
|
||||
// Draw text
|
||||
if (node_ != nullptr) {
|
||||
painter->setPen(qApp->palette().text().color());
|
||||
// FIXME: The text is always drawn white assuming the color will be dark - the intention is to provide preset
|
||||
// colors that will always be dark for the user to choose, so this value can stay white.
|
||||
painter->setPen(Qt::white);
|
||||
|
||||
// Draw the expand icon
|
||||
QRectF icon_rect = rect();
|
||||
icon_rect.adjust(kNodeViewItemIconPadding,
|
||||
kNodeViewItemIconPadding,
|
||||
-kNodeViewItemIconPadding,
|
||||
-kNodeViewItemIconPadding);
|
||||
olive::icon::TriRight.paint(painter, icon_rect.toRect(), Qt::AlignLeft | Qt::AlignVCenter);
|
||||
|
||||
// Draw the text in a rect (the rect is sized around text already in the constructor)
|
||||
QRectF text_rect = rect();
|
||||
text_rect.adjust(kNodeViewItemPadding, kNodeViewItemPadding, -kNodeViewItemPadding, -kNodeViewItemPadding);
|
||||
text_rect.adjust(kNodeViewItemIconPadding + icon_rect.height() + kNodeViewItemTextPadding,
|
||||
kNodeViewItemTextPadding,
|
||||
-kNodeViewItemTextPadding,
|
||||
-kNodeViewItemTextPadding);
|
||||
painter->drawText(text_rect, Qt::AlignTop | Qt::AlignLeft, node_->Name());
|
||||
}
|
||||
}
|
||||
|
||||
void NodeViewItem::UpdateGradient()
|
||||
{
|
||||
QLinearGradient grad(QPointF(0, rect().top()), QPointF(0, rect().bottom()));
|
||||
grad.setColorAt(0, color_.lighter(175));
|
||||
grad.setColorAt(1, color_);
|
||||
setBrush(grad);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#define NODEVIEWITEM_H
|
||||
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QLinearGradient>
|
||||
|
||||
#include "node/node.h"
|
||||
|
||||
@@ -30,11 +31,20 @@ class NodeViewItem : public QGraphicsRectItem
|
||||
public:
|
||||
NodeViewItem(QGraphicsItem* parent = nullptr);
|
||||
|
||||
void SetColor(const QColor& color);
|
||||
|
||||
void SetNode(Node* n);
|
||||
Node* node();
|
||||
|
||||
protected:
|
||||
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
|
||||
|
||||
private:
|
||||
void UpdateGradient();
|
||||
|
||||
Node* node_;
|
||||
|
||||
QColor color_;
|
||||
};
|
||||
|
||||
#endif // NODEVIEWITEM_H
|
||||
|
||||
Reference in New Issue
Block a user