nodes: added ability to label nodes

This commit is contained in:
itsmattkc
2020-04-29 15:35:05 +10:00
parent 13dc79d0cd
commit bf68a4d631
11 changed files with 133 additions and 5 deletions
+34 -1
View File
@@ -20,6 +20,7 @@
#include "nodeview.h"
#include <QInputDialog>
#include <QMouseEvent>
#include "core.h"
@@ -208,7 +209,6 @@ void NodeView::Duplicate()
Node* copy = n->copy();
Node::CopyInputs(n, copy, false);
copy->SetPosition(n->GetPosition());
duplicated_nodes.append(copy);
@@ -401,6 +401,15 @@ void NodeView::ShowContextMenu(const QPoint &pos)
m.addSeparator();
if (itemAt(pos)) {
QList<NodeViewItem*> selected = scene_.GetSelectedItems();
if (selected.size() == 1) {
QAction* label_action = m.addAction(tr("Label"));
connect(label_action, &QAction::triggered, this, &NodeView::ContextMenuLabelNode);
m.addSeparator();
}
QAction* autopos = m.addAction(tr("Auto-Position"));
connect(autopos, &QAction::triggered, this, &NodeView::AutoPositionDescendents);
} else {
@@ -462,6 +471,30 @@ void NodeView::AutoPositionDescendents()
}
}
void NodeView::ContextMenuLabelNode()
{
QList<Node*> nodes = scene_.GetSelectedNodes();
if (nodes.isEmpty()) {
return;
}
Node* n = nodes.first();
bool ok;
QString s = QInputDialog::getText(this,
tr("Label Node"),
tr("Set node label"),
QLineEdit::Normal,
n->GetLabel(),
&ok);
if (ok) {
n->SetLabel(s);
}
}
void NodeView::PlaceNode(NodeViewItem *n, const QPointF &pos)
{
QRectF destination_rect = n->rect();
+5
View File
@@ -141,6 +141,11 @@ private slots:
*/
void AutoPositionDescendents();
/**
* @brief Receiver for labelling a node from the context menu
*/
void ContextMenuLabelNode();
};
OLIVE_NAMESPACE_EXIT
+34 -1
View File
@@ -135,6 +135,11 @@ int NodeViewItem::DefaultItemWidth()
return QFontMetricsWidth(QFontMetrics(QFont()), "HHHHHHHHHH");;
}
int NodeViewItem::DefaultMaximumTextWidth()
{
return QFontMetricsWidth(QFontMetrics(QFont()), "HHHHHHHH");;
}
int NodeViewItem::DefaultItemBorder()
{
return QFontMetrics(QFont()).height() / 12;
@@ -262,8 +267,36 @@ void NodeViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
painter->setPen(app_pal.color(QPalette::Text));
QString node_label;
if (node_->GetLabel().isEmpty()) {
node_label = node_->ShortName();
} else {
node_label = node_->GetLabel();
}
{
QFont f;
QFontMetrics fm(f);
int max_text_width = DefaultMaximumTextWidth();
if (QFontMetricsWidth(fm, node_label) > max_text_width) {
QString concatenated;
do {
node_label.chop(1);
concatenated = QCoreApplication::translate("NodeViewItem", "%1...").arg(node_label);
} while (QFontMetricsWidth(fm, concatenated) > max_text_width);
node_label = concatenated;
}
}
// Draw the text in a rect (the rect is sized around text already in the constructor)
painter->drawText(title_bar_rect_, Qt::AlignCenter, node_->ShortName());
painter->drawText(title_bar_rect_,
Qt::AlignCenter,
node_label);
}
+2
View File
@@ -86,6 +86,8 @@ public:
static int DefaultItemWidth();
static int DefaultMaximumTextWidth();
static int DefaultItemBorder();
qreal DefaultItemHorizontalPadding() const;
+7
View File
@@ -180,10 +180,12 @@ void NodeViewScene::AddNode(Node* node)
}
connect(node, &Node::PositionChanged, this, &NodeViewScene::NodePositionChanged);
connect(node, &Node::LabelChanged, this, &NodeViewScene::NodeLabelChanged);
}
void NodeViewScene::RemoveNode(Node *node)
{
disconnect(node, &Node::LabelChanged, this, &NodeViewScene::NodeLabelChanged);
disconnect(node, &Node::PositionChanged, this, &NodeViewScene::NodePositionChanged);
delete item_map_.take(node);
@@ -243,4 +245,9 @@ void NodeViewScene::NodePositionChanged(const QPointF &pos)
item_map_.value(static_cast<Node*>(sender()))->SetNodePosition(pos);
}
void NodeViewScene::NodeLabelChanged()
{
item_map_.value(static_cast<Node*>(sender()))->update();
}
OLIVE_NAMESPACE_EXIT
+4
View File
@@ -127,6 +127,10 @@ private slots:
*/
void NodePositionChanged(const QPointF& pos);
/**
* @brief Receiver for when a node's label has changed
*/
void NodeLabelChanged();
};