diff --git a/examples/styles/models.hpp b/examples/styles/models.hpp index 1352e4bfc..199f80bee 100644 --- a/examples/styles/models.hpp +++ b/examples/styles/models.hpp @@ -36,6 +36,8 @@ class MyDataModel : public NodeDelegateModel QString name() const override { return QString("MyDataModel"); } + bool labelEditable() const override { return true; } + public: QJsonObject save() const override { diff --git a/include/QtNodes/internal/AbstractNodeGeometry.hpp b/include/QtNodes/internal/AbstractNodeGeometry.hpp index acc60def5..c3be96301 100644 --- a/include/QtNodes/internal/AbstractNodeGeometry.hpp +++ b/include/QtNodes/internal/AbstractNodeGeometry.hpp @@ -36,8 +36,7 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry /// Port position in node's coordinate system. virtual QPointF portPosition(NodeId const nodeId, PortType const portType, - PortIndex const index) const - = 0; + PortIndex const index) const = 0; /// A convenience function using the `portPosition` and a given transformation. virtual QPointF portScenePosition(NodeId const nodeId, @@ -48,8 +47,7 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry /// Defines where to draw port label. The point corresponds to a font baseline. virtual QPointF portTextPosition(NodeId const nodeId, PortType const portType, - PortIndex const portIndex) const - = 0; + PortIndex const portIndex) const = 0; /** * Defines where to start drawing the caption. The point corresponds to a font @@ -60,6 +58,15 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry /// Caption rect is needed for estimating the total node size. virtual QRectF captionRect(NodeId const nodeId) const = 0; + /** + * Defines where to start drawing the label. The point corresponds to a font + * baseline. + */ + virtual QPointF labelPosition(NodeId const nodeId) const = 0; + + /// Caption rect is needed for estimating the total node size. + virtual QRectF labelRect(NodeId const nodeId) const = 0; + /// Position for an embedded widget. Return any value if you don't embed. virtual QPointF widgetPosition(NodeId const nodeId) const = 0; @@ -69,6 +76,8 @@ class NODE_EDITOR_PUBLIC AbstractNodeGeometry virtual QRect resizeHandleRect(NodeId const nodeId) const = 0; + virtual int getPortSpacing() = 0; + protected: AbstractGraphModel &_graphModel; }; diff --git a/include/QtNodes/internal/DataFlowGraphModel.hpp b/include/QtNodes/internal/DataFlowGraphModel.hpp index ff93c6eb4..fcde1482c 100644 --- a/include/QtNodes/internal/DataFlowGraphModel.hpp +++ b/include/QtNodes/internal/DataFlowGraphModel.hpp @@ -11,6 +11,8 @@ #include #include +#include +#include namespace QtNodes { @@ -137,6 +139,9 @@ private Q_SLOTS: std::unordered_set _connectivity; mutable std::unordered_map _nodeGeometryData; + + std::unordered_map _labels; + std::unordered_map _labelsVisible; }; } // namespace QtNodes diff --git a/include/QtNodes/internal/DefaultHorizontalNodeGeometry.hpp b/include/QtNodes/internal/DefaultHorizontalNodeGeometry.hpp index 33367e109..b26555282 100644 --- a/include/QtNodes/internal/DefaultHorizontalNodeGeometry.hpp +++ b/include/QtNodes/internal/DefaultHorizontalNodeGeometry.hpp @@ -28,14 +28,21 @@ class NODE_EDITOR_PUBLIC DefaultHorizontalNodeGeometry : public AbstractNodeGeom QPointF portTextPosition(NodeId const nodeId, PortType const portType, PortIndex const PortIndex) const override; + QPointF captionPosition(NodeId const nodeId) const override; QRectF captionRect(NodeId const nodeId) const override; + QPointF labelPosition(const NodeId nodeId) const override; + + QRectF labelRect(NodeId const nodeId) const override; + QPointF widgetPosition(NodeId const nodeId) const override; QRect resizeHandleRect(NodeId const nodeId) const override; + int getPortSpacing() override { return _portSpacing; } + private: QRectF portTextRect(NodeId const nodeId, PortType const portType, @@ -52,7 +59,7 @@ class NODE_EDITOR_PUBLIC DefaultHorizontalNodeGeometry : public AbstractNodeGeom // constness of the Node. mutable unsigned int _portSize; - unsigned int _portSpasing; + unsigned int _portSpacing; mutable QFontMetrics _fontMetrics; mutable QFontMetrics _boldFontMetrics; }; diff --git a/include/QtNodes/internal/DefaultNodePainter.hpp b/include/QtNodes/internal/DefaultNodePainter.hpp index 953faa065..86f236426 100644 --- a/include/QtNodes/internal/DefaultNodePainter.hpp +++ b/include/QtNodes/internal/DefaultNodePainter.hpp @@ -1,10 +1,9 @@ #pragma once -#include -#include - #include "AbstractNodePainter.hpp" #include "Definitions.hpp" +#include +#include namespace QtNodes { @@ -28,6 +27,8 @@ class NODE_EDITOR_PUBLIC DefaultNodePainter : public AbstractNodePainter void drawNodeCaption(QPainter *painter, NodeGraphicsObject &ngo) const; + void drawNodeLabel(QPainter *painter, NodeGraphicsObject &ngo) const; + void drawEntryLabels(QPainter *painter, NodeGraphicsObject &ngo) const; void drawResizeRect(QPainter *painter, NodeGraphicsObject &ngo) const; diff --git a/include/QtNodes/internal/DefaultVerticalNodeGeometry.hpp b/include/QtNodes/internal/DefaultVerticalNodeGeometry.hpp index ce4dd9f17..d20636567 100644 --- a/include/QtNodes/internal/DefaultVerticalNodeGeometry.hpp +++ b/include/QtNodes/internal/DefaultVerticalNodeGeometry.hpp @@ -33,10 +33,16 @@ class NODE_EDITOR_PUBLIC DefaultVerticalNodeGeometry : public AbstractNodeGeomet QRectF captionRect(NodeId const nodeId) const override; + QPointF labelPosition(const NodeId nodeId) const override; + + QRectF labelRect(NodeId const nodeId) const override; + QPointF widgetPosition(NodeId const nodeId) const override; QRect resizeHandleRect(NodeId const nodeId) const override; + int getPortSpacing() override { return _portSpacing; } + private: QRectF portTextRect(NodeId const nodeId, PortType const portType, @@ -54,7 +60,7 @@ class NODE_EDITOR_PUBLIC DefaultVerticalNodeGeometry : public AbstractNodeGeomet // constness of the Node. mutable unsigned int _portSize; - unsigned int _portSpasing; + unsigned int _portSpacing; mutable QFontMetrics _fontMetrics; mutable QFontMetrics _boldFontMetrics; }; diff --git a/include/QtNodes/internal/Definitions.hpp b/include/QtNodes/internal/Definitions.hpp index 8c01475f9..de3336342 100644 --- a/include/QtNodes/internal/Definitions.hpp +++ b/include/QtNodes/internal/Definitions.hpp @@ -18,23 +18,26 @@ NODE_EDITOR_PUBLIC Q_NAMESPACE Q_NAMESPACE_EXPORT(NODE_EDITOR_PUBLIC) #endif -/** - * Constants used for fetching QVariant data from GraphModel. - */ -enum class NodeRole { - Type = 0, ///< Type of the current node, usually a string. - Position = 1, ///< `QPointF` positon of the node on the scene. - Size = 2, ///< `QSize` for resizable nodes. - CaptionVisible = 3, ///< `bool` for caption visibility. - Caption = 4, ///< `QString` for node caption. - Style = 5, ///< Custom NodeStyle as QJsonDocument - InternalData = 6, ///< Node-stecific user data as QJsonObject - InPortCount = 7, ///< `unsigned int` - OutPortCount = 9, ///< `unsigned int` - Widget = 10, ///< Optional `QWidget*` or `nullptr` - ValidationState = 11, ///< Enum NodeValidationState of the node - ProcessingStatus = 12 ///< Enum NodeProcessingStatus of the node -}; + /** + * Constants used for fetching QVariant data from GraphModel. + */ + enum class NodeRole { + Type = 0, ///< Type of the current node, usually a string. + Position = 1, ///< `QPointF` positon of the node on the scene. + Size = 2, ///< `QSize` for resizable nodes. + CaptionVisible = 3, ///< `bool` for caption visibility. + Caption = 4, ///< `QString` for node caption. + Style = 5, ///< Custom NodeStyle as QJsonDocument + InternalData = 6, ///< Node-stecific user data as QJsonObject + InPortCount = 7, ///< `unsigned int` + OutPortCount = 9, ///< `unsigned int` + Widget = 10, ///< Optional `QWidget*` or `nullptr` + ValidationState = 11, ///< Enum NodeValidationState of the node + LabelVisible = 12, ///< `bool` for label visibility. + ProcessingStatus = 13, ///< Enum NodeProcessingStatus of the node + Label = 14, ///< `QString` for node label. + LabelEditable = 15, ///< `bool` to indicate label editing support. + }; Q_ENUM_NS(NodeRole) /** diff --git a/include/QtNodes/internal/GraphicsView.hpp b/include/QtNodes/internal/GraphicsView.hpp index f21c58799..f22acf2c2 100644 --- a/include/QtNodes/internal/GraphicsView.hpp +++ b/include/QtNodes/internal/GraphicsView.hpp @@ -2,8 +2,11 @@ #include +#include "Definitions.hpp" #include "Export.hpp" +class QLineEdit; + namespace QtNodes { class BasicGraphicsScene; @@ -93,5 +96,8 @@ public Q_SLOTS: QPointF _clickPos; ScaleRange _scaleRange; + + QLineEdit *_labelEdit = nullptr; + NodeId _editingNodeId = InvalidNodeId; }; } // namespace QtNodes diff --git a/include/QtNodes/internal/NodeDelegateModel.hpp b/include/QtNodes/internal/NodeDelegateModel.hpp index 04cc78a12..fba9b0fcf 100644 --- a/include/QtNodes/internal/NodeDelegateModel.hpp +++ b/include/QtNodes/internal/NodeDelegateModel.hpp @@ -1,17 +1,15 @@ #pragma once -#include - -#include -#include -#include -#include - #include "Definitions.hpp" #include "Export.hpp" #include "NodeData.hpp" #include "NodeStyle.hpp" #include "Serializable.hpp" +#include +#include +#include +#include +#include namespace QtNodes { @@ -80,6 +78,15 @@ class NODE_EDITOR_PUBLIC NodeDelegateModel /// It is possible to hide port caption in GUI virtual bool portCaptionVisible(PortType, PortIndex) const { return false; } + /// Nicknames can be assigned to nodes and shown in GUI + virtual QString label() const { return QString(); } + + /// It is possible to hide the nickname in GUI + virtual bool labelVisible() const { return true; } + + /// Controls whether the label can be edited or not + virtual bool labelEditable() const { return false; } + /// Validation State will default to Valid, but you can manipulate it by overriding in an inherited class virtual NodeValidationState validationState() const { return _nodeValidationState; } diff --git a/include/QtNodes/internal/UndoCommands.hpp b/include/QtNodes/internal/UndoCommands.hpp index 7aed4d60b..870478618 100644 --- a/include/QtNodes/internal/UndoCommands.hpp +++ b/include/QtNodes/internal/UndoCommands.hpp @@ -3,9 +3,9 @@ #include "Definitions.hpp" #include "Export.hpp" +#include #include #include -#include #include diff --git a/resources/resources.qrc b/resources/resources.qrc index f9da26fcd..42bf08877 100644 --- a/resources/resources.qrc +++ b/resources/resources.qrc @@ -7,6 +7,6 @@ status_icons/partial.svg status_icons/pending.svg status_icons/processing.svg - status_icons/updated.svg + status_icons/updated.svg diff --git a/resources/status_icons/empty.svg b/resources/status_icons/empty.svg index 21d5e820b..92fc4c10c 100644 --- a/resources/status_icons/empty.svg +++ b/resources/status_icons/empty.svg @@ -9,4 +9,4 @@ - \ No newline at end of file + diff --git a/resources/status_icons/failed.svg b/resources/status_icons/failed.svg index 90f53d66c..04a9ff218 100644 --- a/resources/status_icons/failed.svg +++ b/resources/status_icons/failed.svg @@ -11,4 +11,4 @@ - \ No newline at end of file + diff --git a/resources/status_icons/partial.svg b/resources/status_icons/partial.svg index 78522eca3..ab9e8a1fc 100644 --- a/resources/status_icons/partial.svg +++ b/resources/status_icons/partial.svg @@ -39,4 +39,4 @@ - \ No newline at end of file + diff --git a/resources/status_icons/pending.svg b/resources/status_icons/pending.svg index 7e74e3fb0..dc377f0cb 100644 --- a/resources/status_icons/pending.svg +++ b/resources/status_icons/pending.svg @@ -19,4 +19,4 @@ - \ No newline at end of file + diff --git a/resources/status_icons/processing.svg b/resources/status_icons/processing.svg index 03ac710ac..4c05d18fc 100644 --- a/resources/status_icons/processing.svg +++ b/resources/status_icons/processing.svg @@ -17,4 +17,4 @@ - \ No newline at end of file + diff --git a/resources/status_icons/updated.svg b/resources/status_icons/updated.svg index 5c1075e2b..38a8291f8 100644 --- a/resources/status_icons/updated.svg +++ b/resources/status_icons/updated.svg @@ -11,4 +11,4 @@ - \ No newline at end of file + diff --git a/src/DataFlowGraphModel.cpp b/src/DataFlowGraphModel.cpp index fa5655cde..f3f55a8a9 100644 --- a/src/DataFlowGraphModel.cpp +++ b/src/DataFlowGraphModel.cpp @@ -102,6 +102,9 @@ NodeId DataFlowGraphModel::addNode(QString const nodeType) _models[newId] = std::move(model); + _labels[newId] = _models[newId]->label(); + _labelsVisible[newId] = _models[newId]->labelVisible(); + Q_EMIT nodeCreated(newId); return newId; @@ -299,6 +302,18 @@ QVariant DataFlowGraphModel::nodeData(NodeId nodeId, NodeRole role) const result = QVariant::fromValue(validationState); } break; + case NodeRole::LabelVisible: + result = _labelsVisible.at(nodeId); + break; + + case NodeRole::Label: + result = _labels.at(nodeId); + break; + + case NodeRole::LabelEditable: + result = model->labelEditable(); + break; + case NodeRole::ProcessingStatus: { auto processingStatus = model->processingStatus(); result = QVariant::fromValue(processingStatus); @@ -382,6 +397,21 @@ bool DataFlowGraphModel::setNodeData(NodeId nodeId, NodeRole role, QVariant valu } Q_EMIT nodeUpdated(nodeId); } break; + + case NodeRole::LabelVisible: { + _labelsVisible[nodeId] = value.toBool(); + Q_EMIT nodeUpdated(nodeId); + result = true; + } break; + + case NodeRole::Label: { + _labels[nodeId] = value.toString(); + Q_EMIT nodeUpdated(nodeId); + result = true; + } break; + + case NodeRole::LabelEditable: + break; } return result; @@ -489,6 +519,8 @@ bool DataFlowGraphModel::deleteNode(NodeId const nodeId) } _nodeGeometryData.erase(nodeId); + _labels.erase(nodeId); + _labelsVisible.erase(nodeId); _models.erase(nodeId); Q_EMIT nodeDeleted(nodeId); @@ -504,6 +536,9 @@ QJsonObject DataFlowGraphModel::saveNode(NodeId const nodeId) const nodeJson["internal-data"] = _models.at(nodeId)->save(); + nodeJson["label"] = _labels.at(nodeId); + nodeJson["labelVisible"] = _labelsVisible.at(nodeId); + { QPointF const pos = nodeData(nodeId, NodeRole::Position).value(); @@ -599,7 +634,13 @@ void DataFlowGraphModel::loadNode(QJsonObject const &nodeJson) setNodeData(restoredNodeId, NodeRole::Position, pos); - _models[restoredNodeId]->load(internalDataJson); + auto *restoredModel = _models[restoredNodeId].get(); + _labels[restoredNodeId] = nodeJson["label"].toString(restoredModel->label()); + _labelsVisible[restoredNodeId] = nodeJson.contains("labelVisible") + ? nodeJson["labelVisible"].toBool() + : restoredModel->labelVisible(); + + restoredModel->load(internalDataJson); } else { throw std::logic_error(std::string("No registered model with name ") + delegateModelName.toLocal8Bit().data()); diff --git a/src/DefaultHorizontalNodeGeometry.cpp b/src/DefaultHorizontalNodeGeometry.cpp index a30121d2a..de7223de1 100644 --- a/src/DefaultHorizontalNodeGeometry.cpp +++ b/src/DefaultHorizontalNodeGeometry.cpp @@ -11,7 +11,7 @@ namespace QtNodes { DefaultHorizontalNodeGeometry::DefaultHorizontalNodeGeometry(AbstractGraphModel &graphModel) : AbstractNodeGeometry(graphModel) , _portSize(20) - , _portSpasing(10) + , _portSpacing(10) , _fontMetrics(QFont()) , _boldFontMetrics(QFont()) { @@ -26,7 +26,7 @@ QRectF DefaultHorizontalNodeGeometry::boundingRect(NodeId const nodeId) const { QSize s = size(nodeId); - qreal marginSize = 2.0 * _portSpasing; + qreal marginSize = 2.0 * _portSpacing; QMargins margins(marginSize, marginSize, marginSize, marginSize); QRectF r(QPointF(0, 0), s); @@ -48,13 +48,19 @@ void DefaultHorizontalNodeGeometry::recomputeSize(NodeId const nodeId) const } QRectF const capRect = captionRect(nodeId); + QRectF const lblRect = labelRect(nodeId); height += capRect.height(); + if (!lblRect.isNull()) { + height += lblRect.height(); + height += _portSpacing / 2; + } - height += _portSpasing; // space above caption - height += _portSpasing; // space below caption + height += _portSpacing; // space above caption + height += _portSpacing; // space below caption QVariant var = _graphModel.nodeData(nodeId, NodeRole::ProcessingStatus); + auto processingStatusValue = var.value(); if (processingStatusValue != 0) @@ -63,13 +69,17 @@ void DefaultHorizontalNodeGeometry::recomputeSize(NodeId const nodeId) const unsigned int inPortWidth = maxPortsTextAdvance(nodeId, PortType::In); unsigned int outPortWidth = maxPortsTextAdvance(nodeId, PortType::Out); - unsigned int width = inPortWidth + outPortWidth + 4 * _portSpasing; + unsigned int width = inPortWidth + outPortWidth + 4 * _portSpacing; if (auto w = _graphModel.nodeData(nodeId, NodeRole::Widget)) { width += w->width(); } - width = std::max(width, static_cast(capRect.width()) + 2 * _portSpasing); + unsigned int textWidth = static_cast(capRect.width()); + if (!lblRect.isNull()) + textWidth = std::max(textWidth, static_cast(lblRect.width())); + + width = std::max(width, textWidth + 2 * _portSpacing); QSize size(width, height); @@ -80,14 +90,20 @@ QPointF DefaultHorizontalNodeGeometry::portPosition(NodeId const nodeId, PortType const portType, PortIndex const portIndex) const { - unsigned int const step = _portSize + _portSpasing; + unsigned int const step = _portSize + _portSpacing; QPointF result; double totalHeight = 0.0; totalHeight += captionRect(nodeId).height(); - totalHeight += _portSpasing; + + if (_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) { + totalHeight += labelRect(nodeId).height(); + totalHeight += _portSpacing / 2.0; + } + + totalHeight += _portSpacing; totalHeight += step * portIndex; totalHeight += step / 2.0; @@ -130,11 +146,11 @@ QPointF DefaultHorizontalNodeGeometry::portTextPosition(NodeId const nodeId, switch (portType) { case PortType::In: - p.setX(_portSpasing); + p.setX(_portSpacing); break; case PortType::Out: - p.setX(size.width() - _portSpasing - rect.width()); + p.setX(size.width() - _portSpacing - rect.width()); break; default: @@ -157,8 +173,48 @@ QRectF DefaultHorizontalNodeGeometry::captionRect(NodeId const nodeId) const QPointF DefaultHorizontalNodeGeometry::captionPosition(NodeId const nodeId) const { QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); - return QPointF(0.5 * (size.width() - captionRect(nodeId).width()), - 0.5 * _portSpasing + captionRect(nodeId).height()); + + QRectF cap = captionRect(nodeId); + QRectF lbl = labelRect(nodeId); + + double y = 0.5 * _portSpacing + cap.height(); + y += _portSpacing / 2.0 + lbl.height(); + + return QPointF(0.5 * (size.width() - captionRect(nodeId).width()), y); +} + +QRectF DefaultHorizontalNodeGeometry::labelRect(NodeId const nodeId) const +{ + if (!_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) + return QRect(); + + QString nickname = _graphModel.nodeData(nodeId, NodeRole::Label); + + QRectF nickRect = _boldFontMetrics.boundingRect(nickname); + + nickRect.setWidth(nickRect.width() * 0.5); + nickRect.setHeight(nickRect.height() * 0.5); + + return nickRect; +} + +QPointF DefaultHorizontalNodeGeometry::labelPosition(NodeId const nodeId) const +{ + QRectF cap = captionRect(nodeId); + QRectF lbl = labelRect(nodeId); + + double y = 0.5 * _portSpacing + cap.height(); + y += _portSpacing / 2.0 + lbl.height(); + + if (!_graphModel.nodeData(nodeId, NodeRole::CaptionVisible)) { + return QPointF(captionPosition(nodeId).x() + + 0.5 * (captionRect(nodeId).width() - 2 * labelRect(nodeId).width()), + y); + } + + return QPointF(captionPosition(nodeId).x() + + 0.5 * (captionRect(nodeId).width() - 2 * labelRect(nodeId).width()), + 0.5 * _portSpacing + captionRect(nodeId).height()); } QPointF DefaultHorizontalNodeGeometry::widgetPosition(NodeId const nodeId) const @@ -166,15 +222,17 @@ QPointF DefaultHorizontalNodeGeometry::widgetPosition(NodeId const nodeId) const QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); unsigned int captionHeight = captionRect(nodeId).height(); + if (_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) + captionHeight += labelRect(nodeId).height() + _portSpacing / 2; if (auto w = _graphModel.nodeData(nodeId, NodeRole::Widget)) { // If the widget wants to use as much vertical space as possible, // place it immediately after the caption. if (w->sizePolicy().verticalPolicy() & QSizePolicy::ExpandFlag) { - return QPointF(2.0 * _portSpasing + maxPortsTextAdvance(nodeId, PortType::In), - _portSpasing + captionHeight); + return QPointF(2.0 * _portSpacing + maxPortsTextAdvance(nodeId, PortType::In), + _portSpacing + captionHeight); } else { - return QPointF(2.0 * _portSpasing + maxPortsTextAdvance(nodeId, PortType::In), + return QPointF(2.0 * _portSpacing + maxPortsTextAdvance(nodeId, PortType::In), (captionHeight + size.height() - w->height()) / 2.0); } } @@ -187,7 +245,7 @@ QRect DefaultHorizontalNodeGeometry::resizeHandleRect(NodeId const nodeId) const unsigned int rectSize = 7; - return QRect(size.width() - _portSpasing, size.height() - _portSpasing, rectSize, rectSize); + return QRect(size.width() - _portSpacing, size.height() - _portSpacing, rectSize, rectSize); } QRectF DefaultHorizontalNodeGeometry::portTextRect(NodeId const nodeId, @@ -213,7 +271,7 @@ unsigned int DefaultHorizontalNodeGeometry::maxVerticalPortsExtent(NodeId const PortCount nOutPorts = _graphModel.nodeData(nodeId, NodeRole::OutPortCount); unsigned int maxNumOfEntries = std::max(nInPorts, nOutPorts); - unsigned int step = _portSize + _portSpasing; + unsigned int step = _portSize + _portSpacing; return step * maxNumOfEntries; } diff --git a/src/DefaultNodePainter.cpp b/src/DefaultNodePainter.cpp index d313d3a82..21d1e23de 100644 --- a/src/DefaultNodePainter.cpp +++ b/src/DefaultNodePainter.cpp @@ -37,6 +37,8 @@ void DefaultNodePainter::paint(QPainter *painter, NodeGraphicsObject &ngo) const drawResizeRect(painter, ngo); + drawNodeLabel(painter, ngo); + drawValidationIcon(painter, ngo); } @@ -222,12 +224,21 @@ void DefaultNodePainter::drawNodeCaption(QPainter *painter, NodeGraphicsObject & if (!model.nodeData(nodeId, NodeRole::CaptionVisible).toBool()) return; + QString const nickname = model.nodeData(nodeId, NodeRole::Label).toString(); QString const name = model.nodeData(nodeId, NodeRole::Caption).toString(); QFont f = painter->font(); - f.setBold(true); + f.setBold(nickname.isEmpty()); + f.setItalic(!nickname.isEmpty()); + + QFontMetricsF metrics(f); + + QRectF bounding = metrics.boundingRect(name); + QRectF capRect = geometry.captionRect(nodeId); + QPointF capPos = geometry.captionPosition(nodeId); + double centerX = capPos.x() + capRect.width() / 2.0; - QPointF position = geometry.captionPosition(nodeId); + QPointF position(centerX - bounding.width() / 2.0, capPos.y()); QJsonDocument json = QJsonDocument::fromVariant(model.nodeData(nodeId, NodeRole::Style)); NodeStyle nodeStyle(json.object()); @@ -236,6 +247,45 @@ void DefaultNodePainter::drawNodeCaption(QPainter *painter, NodeGraphicsObject & painter->setPen(nodeStyle.FontColor); painter->drawText(position, name); + f.setBold(false); + f.setItalic(false); + painter->setFont(f); +} + +void DefaultNodePainter::drawNodeLabel(QPainter *painter, NodeGraphicsObject &ngo) const +{ + AbstractGraphModel &model = ngo.graphModel(); + NodeId const nodeId = ngo.nodeId(); + AbstractNodeGeometry &geometry = ngo.nodeScene()->nodeGeometry(); + + if (!model.nodeData(nodeId, NodeRole::LabelVisible).toBool()) + return; + + QString const nickname = model.nodeData(nodeId, NodeRole::Label).toString(); + + QFont f = painter->font(); + f.setBold(true); + f.setItalic(false); + + QFontMetricsF metrics(f); + + QRectF bounding = metrics.boundingRect(nickname); + QRectF capRect = geometry.captionRect(nodeId); + QPointF capPos = geometry.captionPosition(nodeId); + double centerX = capPos.x() + capRect.width() / 2.0; + + double textHeight = metrics.height(); + double y = capPos.y() - textHeight - 2.0; + + QPointF position(centerX - bounding.width() / 2.0, y); + + QJsonDocument json = QJsonDocument::fromVariant(model.nodeData(nodeId, NodeRole::Style)); + NodeStyle nodeStyle(json.object()); + + painter->setFont(f); + painter->setPen(nodeStyle.FontColor); + painter->drawText(position, nickname); + f.setBold(false); painter->setFont(f); } diff --git a/src/DefaultVerticalNodeGeometry.cpp b/src/DefaultVerticalNodeGeometry.cpp index f20617f25..589f5cdcb 100644 --- a/src/DefaultVerticalNodeGeometry.cpp +++ b/src/DefaultVerticalNodeGeometry.cpp @@ -12,7 +12,7 @@ namespace QtNodes { DefaultVerticalNodeGeometry::DefaultVerticalNodeGeometry(AbstractGraphModel &graphModel) : AbstractNodeGeometry(graphModel) , _portSize(20) - , _portSpasing(10) + , _portSpacing(10) , _fontMetrics(QFont()) , _boldFontMetrics(QFont()) { @@ -27,7 +27,7 @@ QRectF DefaultVerticalNodeGeometry::boundingRect(NodeId const nodeId) const { QSize s = size(nodeId); - qreal marginSize = 2.0 * _portSpasing; + qreal marginSize = 2.0 * _portSpacing; QMargins margins(marginSize, marginSize, marginSize, marginSize); QRectF r(QPointF(0, 0), s); @@ -42,18 +42,23 @@ QSize DefaultVerticalNodeGeometry::size(NodeId const nodeId) const void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const { - unsigned int height = _portSpasing; // maxHorizontalPortsExtent(nodeId); + unsigned int height = _portSpacing; // maxHorizontalPortsExtent(nodeId); if (auto w = _graphModel.nodeData(nodeId, NodeRole::Widget)) { height = std::max(height, static_cast(w->height())); } QRectF const capRect = captionRect(nodeId); + QRectF const lblRect = labelRect(nodeId); height += capRect.height(); + if (!lblRect.isNull()) { + height += lblRect.height(); + height += _portSpacing / 2; + } - height += _portSpasing; - height += _portSpasing; + height += _portSpacing; + height += _portSpacing; PortCount nInPorts = _graphModel.nodeData(nodeId, NodeRole::InPortCount); PortCount nOutPorts = _graphModel.nodeData(nodeId, NodeRole::OutPortCount); @@ -67,11 +72,11 @@ void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const unsigned int outPortWidth = maxPortsTextAdvance(nodeId, PortType::Out); unsigned int totalInPortsWidth = nInPorts > 0 - ? inPortWidth * nInPorts + _portSpasing * (nInPorts - 1) + ? inPortWidth * nInPorts + _portSpacing * (nInPorts - 1) : 0; unsigned int totalOutPortsWidth = nOutPorts > 0 ? outPortWidth * nOutPorts - + _portSpasing * (nOutPorts - 1) + + _portSpacing * (nOutPorts - 1) : 0; unsigned int width = std::max(totalInPortsWidth, totalOutPortsWidth); @@ -80,10 +85,14 @@ void DefaultVerticalNodeGeometry::recomputeSize(NodeId const nodeId) const width = std::max(width, static_cast(w->width())); } - width = std::max(width, static_cast(capRect.width())); + unsigned int textWidth = static_cast(capRect.width()); + if (!lblRect.isNull()) + textWidth = std::max(textWidth, static_cast(lblRect.width())); + + width = std::max(width, textWidth); - width += _portSpasing; - width += _portSpasing; + width += _portSpacing; + width += _portSpacing; QSize size(width, height); @@ -100,7 +109,7 @@ QPointF DefaultVerticalNodeGeometry::portPosition(NodeId const nodeId, switch (portType) { case PortType::In: { - unsigned int inPortWidth = maxPortsTextAdvance(nodeId, PortType::In) + _portSpasing; + unsigned int inPortWidth = maxPortsTextAdvance(nodeId, PortType::In) + _portSpacing; PortCount nInPorts = _graphModel.nodeData(nodeId, NodeRole::InPortCount); @@ -114,7 +123,7 @@ QPointF DefaultVerticalNodeGeometry::portPosition(NodeId const nodeId, } case PortType::Out: { - unsigned int outPortWidth = maxPortsTextAdvance(nodeId, PortType::Out) + _portSpasing; + unsigned int outPortWidth = maxPortsTextAdvance(nodeId, PortType::Out) + _portSpacing; PortCount nOutPorts = _graphModel.nodeData(nodeId, NodeRole::OutPortCount); double x = (size.width() - (nOutPorts - 1) * outPortWidth) / 2.0 + portIndex * outPortWidth; @@ -176,26 +185,56 @@ QPointF DefaultVerticalNodeGeometry::captionPosition(NodeId const nodeId) const QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); unsigned int step = portCaptionsHeight(nodeId, PortType::In); - step += _portSpasing; + step += _portSpacing; auto rect = captionRect(nodeId); return QPointF(0.5 * (size.width() - rect.width()), step + rect.height()); } +QPointF DefaultVerticalNodeGeometry::labelPosition(const NodeId nodeId) const +{ + QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); + + QRectF rect = labelRect(nodeId); + + unsigned int step = portCaptionsHeight(nodeId, PortType::In); + step += _portSpacing; + step += captionRect(nodeId).height(); + step += _portSpacing / 2; + + return QPointF(0.5 * (size.width() - rect.width()), step + rect.height()); +} + +QRectF DefaultVerticalNodeGeometry::labelRect(NodeId const nodeId) const +{ + if (!_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) + return QRectF(); + + QString nickname = _graphModel.nodeData(nodeId, NodeRole::Label); + + QRectF rect = _boldFontMetrics.boundingRect(nickname); + rect.setWidth(rect.width() * 0.5); + rect.setHeight(rect.height() * 0.5); + + return rect; +} + QPointF DefaultVerticalNodeGeometry::widgetPosition(NodeId const nodeId) const { QSize size = _graphModel.nodeData(nodeId, NodeRole::Size); unsigned int captionHeight = captionRect(nodeId).height(); + if (_graphModel.nodeData(nodeId, NodeRole::LabelVisible)) + captionHeight += labelRect(nodeId).height() + _portSpacing / 2; if (auto w = _graphModel.nodeData(nodeId, NodeRole::Widget)) { // If the widget wants to use as much vertical space as possible, // place it immediately after the caption. if (w->sizePolicy().verticalPolicy() & QSizePolicy::ExpandFlag) { - return QPointF(_portSpasing + maxPortsTextAdvance(nodeId, PortType::In), captionHeight); + return QPointF(_portSpacing + maxPortsTextAdvance(nodeId, PortType::In), captionHeight); } else { - return QPointF(_portSpasing + maxPortsTextAdvance(nodeId, PortType::In), + return QPointF(_portSpacing + maxPortsTextAdvance(nodeId, PortType::In), (captionHeight + size.height() - w->height()) / 2.0); } } @@ -234,7 +273,7 @@ unsigned int DefaultVerticalNodeGeometry::maxHorizontalPortsExtent(NodeId const PortCount nOutPorts = _graphModel.nodeData(nodeId, NodeRole::OutPortCount); unsigned int maxNumOfEntries = std::max(nInPorts, nOutPorts); - unsigned int step = _portSize + _portSpasing; + unsigned int step = _portSize + _portSpacing; return step * maxNumOfEntries; } @@ -284,7 +323,7 @@ unsigned int DefaultVerticalNodeGeometry::portCaptionsHeight(NodeId const nodeId PortCount nInPorts = _graphModel.nodeData(nodeId, NodeRole::InPortCount); for (PortIndex i = 0; i < nInPorts; ++i) { if (_graphModel.portData(nodeId, PortType::In, i, PortRole::CaptionVisible)) { - h += _portSpasing; + h += _portSpacing; break; } } @@ -295,7 +334,7 @@ unsigned int DefaultVerticalNodeGeometry::portCaptionsHeight(NodeId const nodeId PortCount nOutPorts = _graphModel.nodeData(nodeId, NodeRole::OutPortCount); for (PortIndex i = 0; i < nOutPorts; ++i) { if (_graphModel.portData(nodeId, PortType::Out, i, PortRole::CaptionVisible)) { - h += _portSpasing; + h += _portSpacing; break; } } diff --git a/src/GraphicsView.cpp b/src/GraphicsView.cpp index c587f081c..09e575afb 100644 --- a/src/GraphicsView.cpp +++ b/src/GraphicsView.cpp @@ -2,10 +2,13 @@ #include "BasicGraphicsScene.hpp" #include "ConnectionGraphicsObject.hpp" +#include "Definitions.hpp" #include "NodeGraphicsObject.hpp" #include "StyleCollection.hpp" #include "UndoCommands.hpp" +#include + #include #include @@ -324,6 +327,73 @@ void GraphicsView::onPasteObjects() void GraphicsView::keyPressEvent(QKeyEvent *event) { switch (event->key()) { + case Qt::Key_F2: { + BasicGraphicsScene *sc = nodeScene(); + + if (sc) { + QList items = sc->selectedItems(); + NodeGraphicsObject *ngo = nullptr; + for (QGraphicsItem *it : items) { + ngo = qgraphicsitem_cast(it); + + if (ngo) + break; + } + + if (ngo) { + bool const labelEditable + = sc->graphModel().nodeData(ngo->nodeId(), NodeRole::LabelEditable).toBool(); + + if (!labelEditable) + break; + + if (!_labelEdit) { + _labelEdit = new QLineEdit(this); + _labelEdit->setMaxLength(32); + + connect(_labelEdit, &QLineEdit::editingFinished, [this]() { + if (_editingNodeId != InvalidNodeId) { + nodeScene()->graphModel().setNodeData(_editingNodeId, + NodeRole::LabelVisible, + true); + nodeScene()->graphModel().setNodeData(_editingNodeId, + NodeRole::Label, + _labelEdit->text()); + } + + _labelEdit->hide(); + _editingNodeId = InvalidNodeId; + }); + } + + _editingNodeId = ngo->nodeId(); + + sc->graphModel().setNodeData(_editingNodeId, NodeRole::LabelVisible, true); + + AbstractNodeGeometry &geom = sc->nodeGeometry(); + QPointF labelPos = geom.labelPosition(_editingNodeId); + QPointF scenePos = ngo->mapToScene(labelPos); + QSize sz = _labelEdit->sizeHint(); + QPoint viewPos = mapFromScene(scenePos); + _labelEdit->move(viewPos.x() - sz.width() / 2, viewPos.y() - sz.height() / 2); + bool visible + = sc->graphModel().nodeData(_editingNodeId, NodeRole::LabelVisible).toBool(); + QString current + = sc->graphModel().nodeData(_editingNodeId, NodeRole::Label).toString(); + + if (!visible && current.isEmpty()) + _labelEdit->clear(); + else + _labelEdit->setText(current); + _labelEdit->resize(sz); + _labelEdit->show(); + _labelEdit->setFocus(); + return; + } + } + } + + break; case Qt::Key_Shift: setDragMode(QGraphicsView::RubberBandDrag); break; diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index d7b1bbe4b..8adabea2e 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -382,5 +382,4 @@ void NodeGraphicsObject::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) { Q_EMIT nodeScene()->nodeContextMenu(_nodeId, mapToScene(event->pos())); } - } // namespace QtNodes