nodeview: implemented ability to select and delete node edges

This commit is contained in:
itsmattkc
2020-04-29 17:25:41 +10:00
parent 3f77c2ce91
commit 4ac5513e3f
5 changed files with 82 additions and 33 deletions
+33 -13
View File
@@ -97,21 +97,33 @@ void NodeView::DeleteSelected()
return;
}
QList<Node*> selected_nodes = scene_.GetSelectedNodes();
QUndoCommand* command = new QUndoCommand();
// Ensure no nodes are "undeletable"
for (int i=0;i<selected_nodes.size();i++) {
if (!selected_nodes.at(i)->CanBeDeleted()) {
selected_nodes.removeAt(i);
i--;
{
QList<NodeEdge*> selected_edges = scene_.GetSelectedEdges();
foreach (NodeEdge* edge, selected_edges) {
new NodeEdgeRemoveCommand(edge->output(), edge->input(), command);
}
}
if (selected_nodes.isEmpty()) {
return;
{
QList<Node*> selected_nodes = scene_.GetSelectedNodes();
// Ensure no nodes are "undeletable"
for (int i=0;i<selected_nodes.size();i++) {
if (!selected_nodes.at(i)->CanBeDeleted()) {
selected_nodes.removeAt(i);
i--;
}
}
if (!selected_nodes.isEmpty()) {
new NodeRemoveCommand(graph_, selected_nodes, command);
}
}
Core::instance()->undo_stack()->push(new NodeRemoveCommand(graph_, selected_nodes));
Core::instance()->undo_stack()->pushIfHasChildren(command);
}
void NodeView::SelectAll()
@@ -269,8 +281,6 @@ void NodeView::mousePressEvent(QMouseEvent *event)
if (HandPress(event)) return;
if (!attached_items_.isEmpty()) {
DetachItemsFromCursor();
if (attached_items_.size() == 1) {
Node* dropping_node = attached_items_.first().item->GetNode();
@@ -292,6 +302,8 @@ void NodeView::mousePressEvent(QMouseEvent *event)
drop_edge_ = nullptr;
}
DetachItemsFromCursor();
}
super::mousePressEvent(event);
@@ -400,19 +412,26 @@ void NodeView::ShowContextMenu(const QPoint &pos)
m.addSeparator();
if (itemAt(pos)) {
QList<NodeViewItem*> selected = scene_.GetSelectedItems();
QList<NodeViewItem*> selected = scene_.GetSelectedItems();
if (itemAt(pos) && !selected.isEmpty()) {
if (selected.size() == 1) {
// Label node action
QAction* label_action = m.addAction(tr("Label"));
connect(label_action, &QAction::triggered, this, &NodeView::ContextMenuLabelNode);
m.addSeparator();
}
// Auto-position action
QAction* autopos = m.addAction(tr("Auto-Position"));
connect(autopos, &QAction::triggered, this, &NodeView::AutoPositionDescendents);
} else {
Menu* direction_menu = new Menu(tr("Direction"), &m);
m.addMenu(direction_menu);
@@ -440,6 +459,7 @@ void NodeView::ShowContextMenu(const QPoint &pos)
add_menu->setTitle(tr("Add"));
connect(add_menu, &Menu::triggered, this, &NodeView::CreateNodeSlot);
m.addMenu(add_menu);
}
m.exec(mapToGlobal(pos));
+28 -16
View File
@@ -23,6 +23,7 @@
#include <QApplication>
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include "common/clamp.h"
#include "common/lerp.h"
@@ -34,10 +35,12 @@ OLIVE_NAMESPACE_ENTER
NodeViewEdge::NodeViewEdge(QGraphicsItem *parent) :
QGraphicsPathItem(parent),
edge_(nullptr),
color_group_(QPalette::Active),
color_role_(QPalette::Text),
connected_(false),
highlighted_(false),
flow_dir_(NodeViewCommon::kLeftToRight)
{
setFlag(QGraphicsItem::ItemIsSelectable);
// Ensures this UI object is drawn behind other objects
setZValue(-1);
@@ -83,24 +86,16 @@ void NodeViewEdge::Adjust()
void NodeViewEdge::SetConnected(bool c)
{
if (c) {
color_group_ = QPalette::Active;
} else {
color_group_ = QPalette::Disabled;
}
connected_ = c;
UpdatePen();
update();
}
void NodeViewEdge::SetHighlighted(bool e)
{
if (e) {
color_role_ = QPalette::Highlight;
} else {
color_role_ = QPalette::Text;
}
highlighted_ = e;
UpdatePen();
update();
}
void NodeViewEdge::SetPoints(const QPointF &start, const QPointF &end, bool input_is_expanded)
@@ -137,9 +132,26 @@ void NodeViewEdge::SetFlowDirection(NodeViewCommon::FlowDirection dir)
Adjust();
}
void NodeViewEdge::UpdatePen()
void NodeViewEdge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
setPen(QPen(qApp->palette().color(color_group_, color_role_), edge_width_));
QPalette::ColorGroup group;
QPalette::ColorRole role;
if (connected_) {
group = QPalette::Active;
} else {
group = QPalette::Disabled;
}
if (highlighted_ != bool(option->state & QStyle::State_Selected)) {
role = QPalette::Highlight;
} else {
role = QPalette::Text;
}
painter->setPen(QPen(qApp->palette().color(group, role), edge_width_));
painter->setBrush(Qt::NoBrush);
painter->drawPath(path());
}
OLIVE_NAMESPACE_EXIT
+5 -4
View File
@@ -89,16 +89,17 @@ public:
*/
void SetFlowDirection(NodeViewCommon::FlowDirection dir);
private:
void UpdatePen();
protected:
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
private:
NodeEdgePtr edge_;
int edge_width_;
QPalette::ColorGroup color_group_;
bool connected_;
QPalette::ColorRole color_role_;
bool highlighted_;
NodeViewCommon::FlowDirection flow_dir_;
+15
View File
@@ -145,6 +145,21 @@ QList<NodeViewItem *> NodeViewScene::GetSelectedItems() const
return selected;
}
QList<NodeEdge*> NodeViewScene::GetSelectedEdges() const
{
QList<NodeEdge*> edges;
QHash<NodeEdge*, NodeViewEdge*>::const_iterator i;
for (i=edge_map_.constBegin(); i!=edge_map_.constEnd(); i++) {
if (i.value()->isSelected()) {
edges.append(i.key());
}
}
return edges;
}
const QHash<Node *, NodeViewItem *> &NodeViewScene::item_map() const
{
return item_map_;
+1
View File
@@ -65,6 +65,7 @@ public:
QList<Node*> GetSelectedNodes() const;
QList<NodeViewItem*> GetSelectedItems() const;
QList<NodeEdge *> GetSelectedEdges() const;
const QHash<Node*, NodeViewItem*>& item_map() const;
const QHash<NodeEdge*, NodeViewEdge*>& edge_map() const;