Maki
Formally verified low-code orchestration
Loading...
Searching...
No Matches
node.h
1#pragma once
2
3#include <QBrush>
4#include <QDataStream>
5#include <QGraphicsEllipseItem>
6#include <QGraphicsItem>
7#include <memory>
8
9#include "config.h"
10#include "control_widget.h"
11#include "flow.h"
12#include "flow_info.h"
13#include "node_base.h"
14#include "node_info.h"
15#include "port.h"
16#include "transition.h"
17#include "types.h"
18#include "widgets/controls/canvas_control_widget.h"
19
20class CanvasMessage;
21class QGraphicsSceneMouseEvent;
22class QGraphicsSceneHoverEvent;
23
27class NodeItem : public NodeBase
28{
29public:
30 enum
31 {
32 Type = Types::NODE
33 };
34
44 explicit NodeItem(const QString& id, std::shared_ptr<NodeSaveInfo> info, const QPointF& initialPosition, std::shared_ptr<NodeConfig> nodeConfig,
45 QGraphicsItem* parent = nullptr);
46
50 virtual ~NodeItem();
51
57 int type() const override;
58
64 virtual VoidResult start();
65
71 virtual QRectF nodeRect() const override;
72 QRectF sceneNodeRect() const;
73 virtual QRectF sceneAlignRect() const;
74
75 bool hasControl();
76
77 template <typename T>
78 void hideControlOfType()
79 {
80 if (!mControlActive)
81 return;
82
83 if (auto control = qobject_cast<T*>(mControlWidget->controlWidget()); control && !mControlWidget->permanent())
84 hideControl();
85 }
86
87 void highlight(const QColor& color, const QString& message, int durationMs = 1000);
88 void dismissHighlight();
89
90 void showSimulationControls(maki::ControlWidget* controls, const QColor& highlightColor);
91 void showControls(maki::ControlWidget* controls, const ControlProperties& properties);
92 void dismissControl();
93 void hideControl();
94
100 virtual QPainterPath shape() const override;
101
109 virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* style, QWidget* widget) override;
110
116 HelpConfig help() const;
117
123 QString nodeName() const;
124
130 QString nodeType() const;
131
137 QString behaviour() const;
138
144 QVector<ControlsConfig> controls() const;
145
151 QVector<std::shared_ptr<IParameter>> properties() const;
152
158 QVector<PropertyConfig> configurationProperties() const;
159
165 Types::LibraryTypes function() const;
166
173 const PropertyInfo* getProperty(const QString& key) const;
174
181 virtual void setProperty(const QString& key, const maki::Value& value);
182
186 void renameNode();
187
188 PortItem* getPort(Types::Port type, const QString& subFlowId = Constants::MAIN_SUB_FLOW) const;
189
195 NodeItem* parentNode() const;
196
197 QString parentSubFlow() const;
198
204 QVector<NodeItem*> children(const QString& subflow = Constants::MAIN_SUB_FLOW) const;
205
212 QPointF edgePointToward(const QPointF& targetScenePos) const;
213
219 QVector<std::shared_ptr<IParameter>> fields() const;
220
227 PropertyInfo getField(const QString& key) const;
228
236 VoidResult setField(const QString& key, const QJsonObject& property);
237
245 VoidResult setField(const QString& key, std::shared_ptr<PropertyInfo> property);
246
252 void removeField(const QString& key);
253
260 void setEvent(int index, const FlowConfig& event);
261
267 QVector<std::shared_ptr<IFlow>> flowConfigs() const;
268
269 QVector<std::shared_ptr<IFlow>> eventConfigs() const;
270
271 QVector<std::shared_ptr<IFlow>> allEventFlowConfigs() const;
272
278 virtual void addParent(NodeItem* node, const QString& subflow);
279
280 void removeParent();
281
288 virtual void addChild(NodeItem* node, std::shared_ptr<NodeSaveInfo> info);
289
295 virtual void childRemoved(NodeItem* child);
296
302 std::vector<std::shared_ptr<Flow>> flows() const;
303
311 PFlow createFlow(std::shared_ptr<FlowSaveInfo> info);
312
319 PFlow getFlow(const QString& flowId) const;
320
326 void deleteFlow(const QString& flowId);
327
333 qreal baseScale() const;
334
340 QVector<TransitionConfig> configTransitions() const;
341
347 virtual void applySize(const QSizeF& size);
348
354 void updatePosition(const QPointF& position, Config::NodeMove reason = Config::NodeMove::User);
355
356 QPointF centerPosition() const;
357 void setCenterPosition(const QPointF& center);
358
359 // "signals":
360 std::function<void(NodeItem* item)> nodeModified;
361 std::function<void(PFlow flow, NodeItem* item)> flowAdded;
362 std::function<void(NodeItem* item, bool done)> nodeMoved;
363 std::function<void(NodeItem* item, const QRectF& oldRect)> geometryChanged;
364 std::function<void(NodeItem* item, bool enter)> nodeHovered;
365 std::function<void(NodeItem* item, const QString& subflowId, bool collapsed)> subflowCollapsed;
366 std::function<void(NodeItem* node, const QString& nodeId, const QString& flowId, int type)> focusOn;
367 std::function<void(NodeItem* item, const QPointF& scenePos, maki::ControlWidget* control)> nodeControlRequested;
368
369 // "slots":
370 void onProperties();
371
372 // Serialization functions
373
379 NodeSaveInfo saveInfo() const;
387 friend QDataStream& operator<<(QDataStream& out, const NodeItem& config);
388
396 friend QDataStream& operator>>(QDataStream& in, NodeItem& config);
397
398 virtual QRectF childAreaSceneRect(const QString& subflow = Constants::MAIN_SUB_FLOW) const;
399
403 virtual void childPositionUpdated(NodeItem* node);
404
405protected:
406 std::shared_ptr<NodeSaveInfo> mStorage;
407 QSizeF mSize{0, 0};
408
409 struct PortKey
410 {
411 QString subFlowId;
412 Types::Port type;
413
414 bool operator==(const PortKey& other) const
415 {
416 return subFlowId == other.subFlowId && type == other.type;
417 }
418
419 friend size_t qHash(const PortKey& key, size_t seed = 0) noexcept
420 {
421 seed = ::qHash(key.subFlowId, seed);
422 seed = ::qHash(static_cast<int>(key.type), seed);
423
424 return seed;
425 }
426 };
427
428 QHash<PortKey, PortItem*> mPorts;
429
430 virtual void createPorts();
431
432 virtual bool constrainChildren() const;
433 virtual QPointF constrainChildPosition(const NodeItem* child, const QPointF& proposedPosition) const;
434
440 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
441
447 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
448
454 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
455
456 void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
457 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
458
459 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
460
468 virtual QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;
469
470 void paintDefaultNode(QPainter* painter, const QStyleOptionGraphicsItem* style, QWidget* widget);
471
472 bool isHovered() const;
473
477 virtual void updateExtrasPosition(Config::NodeMove reason);
478
479private:
480 std::vector<std::shared_ptr<Flow>> mFlows;
482 QVector<NodeItem*> mChildrenNodes;
484 CanvasControlWidget* mControlWidget = nullptr;
485 bool mControlActive = false;
486 bool mHovered = false;
487
488 qreal mBaseScale;
489 QPointF mDragStartPos{0, 0};
490 QPointF mLastPosition{0, 0};
491
492 bool mIsResizing{false};
493 QPointF mResizeStartMousePos{0, 0};
494 QSizeF mResizeStartSize{0, 0};
495
503 QSizeF clampSize(qreal width, qreal height) const;
504
512 QPointF clampPosInside(const QRectF& inner, const QRectF& childSceneRect) const;
513
519 void fitInsideParent(qreal padding);
520
527 QRectF parentInnerSceneRect(qreal padding) const;
528};
Definition canvas_control_widget.h:36
Definition canvas_message.h:8
Configuration for a flow or process.
Definition config.h:70
Configuration for help information.
Definition config.h:174
virtual QString nodeId() const
Retrieves the identifier of the node within its type.
Definition node_base.cpp:49
std::shared_ptr< NodeConfig > config() const
Retrieves the configuration settings for the node.
Definition node_base.cpp:54
NodeBase(const QString &id, const QString &nodeId, std::shared_ptr< NodeConfig > config, QGraphicsItem *parent)
Constructs a new NodeBase instance.
Definition node_base.cpp:16
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse release events for this item.
Definition node.cpp:727
Types::LibraryTypes function() const
Returns the library type associated with this node's function.
Definition node.cpp:92
int type() const override
Returns the type of this item.
Definition node.cpp:87
std::vector< std::shared_ptr< Flow > > flows() const
Returns a list of flows associated with this node.
Definition node.cpp:906
friend QDataStream & operator<<(QDataStream &out, const NodeItem &config)
Saves the state of this NodeItem to a QDataStream.
virtual void setProperty(const QString &key, const maki::Value &value)
Sets a property with a given key and value.
Definition node.cpp:422
friend QDataStream & operator>>(QDataStream &in, NodeItem &config)
Loads the state of a NodeItem from a QDataStream.
QSizeF mSize
Save information for the node.
Definition node.h:407
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Handles changes to the item's properties.
Definition node.cpp:782
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse press events for this item.
Definition node.cpp:710
void updatePosition(const QPointF &position, Config::NodeMove reason=Config::NodeMove::User)
Updates the position of the node.
Definition node.cpp:797
virtual void updateExtrasPosition(Config::NodeMove reason)
Updates extra positions related to this node.
Definition node.cpp:867
QVector< std::shared_ptr< IParameter > > fields() const
Returns a list of fields associated with this node.
Definition node.cpp:389
bool mIsResizing
Last known position of the node.
Definition node.h:492
CanvasMessage * mHighlightMessage
List of child nodes.
Definition node.h:483
QPointF edgePointToward(const QPointF &targetScenePos) const
Calculates an edge point toward a target scene position.
Definition node.cpp:887
QPointF mDragStartPos
Base scale for the node.
Definition node.h:489
QPointF mResizeStartMousePos
Flag indicating if the node is being resized.
Definition node.h:493
const PropertyInfo * getProperty(const QString &key) const
Gets a property by key.
Definition node.cpp:414
QString nodeName() const
Returns the name of the node.
Definition node.cpp:109
QVector< TransitionConfig > configTransitions() const
Returns a list of configured transitions.
Definition node.cpp:901
QPointF mLastPosition
Position where dragging started.
Definition node.h:490
virtual void applySize(const QSizeF &size)
Applies a new size to the node.
Definition node.cpp:595
void deleteFlow(const QString &flowId)
Deletes a flow by its ID.
Definition node.cpp:935
QPointF clampPosInside(const QRectF &inner, const QRectF &childSceneRect) const
Clamps a position inside an inner rectangle.
Definition node.cpp:616
PFlow getFlow(const QString &flowId) const
Gets a flow by its ID.
Definition node.cpp:926
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse move events for this item.
Definition node.cpp:686
NodeItem * parentNode() const
Returns the parent node of this item.
Definition node.cpp:556
QSizeF clampSize(qreal width, qreal height) const
Size of the node when resizing started.
Definition node.cpp:634
PFlow createFlow(std::shared_ptr< FlowSaveInfo > info)
Creates a new flow with the given name and save information.
Definition node.cpp:911
HelpConfig help() const
Returns help configuration for this node.
Definition node.cpp:571
QVector< std::shared_ptr< IFlow > > flowConfigs() const
Returns a list of events associated with this node.
Definition node.cpp:399
QVector< NodeItem * > children(const QString &subflow=Constants::MAIN_SUB_FLOW) const
Returns a list of child nodes.
Definition node.cpp:510
virtual void childPositionUpdated(NodeItem *node)
Called to tell the parent that the position of this node was updated.
Definition node.cpp:829
QVector< NodeItem * > mChildrenNodes
Parent node of this item, if any.
Definition node.h:482
NodeItem * mParentNode
List of flows associated with this node.
Definition node.h:481
virtual void childRemoved(NodeItem *child)
Handles when a child is removed from this item.
Definition node.cpp:550
virtual void addParent(NodeItem *node, const QString &subflow)
Adds a parent node to this item.
Definition node.cpp:515
QString behaviour() const
Returns the behavior of the node.
Definition node.cpp:566
VoidResult setField(const QString &key, const QJsonObject &property)
Sets a field with a given key and JSON object.
Definition node.cpp:474
virtual void addChild(NodeItem *node, std::shared_ptr< NodeSaveInfo > info)
Adds a child node with the given save information.
Definition node.cpp:541
void removeField(const QString &key)
Removes a field by key.
Definition node.cpp:502
NodeItem(const QString &id, std::shared_ptr< NodeSaveInfo > info, const QPointF &initialPosition, std::shared_ptr< NodeConfig > nodeConfig, QGraphicsItem *parent=nullptr)
Constructs a new NodeItem with the given parameters.
Definition node.cpp:29
qreal baseScale() const
Returns the base scale for this node.
Definition node.cpp:123
QVector< ControlsConfig > controls() const
Returns the controls configuration for this node.
Definition node.cpp:409
QSizeF mResizeStartSize
Mouse position when resizing started.
Definition node.h:494
QString nodeType() const
Returns the type of the node.
Definition node.cpp:117
void setEvent(int index, const FlowConfig &event)
Sets an event at the given index with the provided FlowConfig.
Definition node.cpp:463
QVector< PropertyConfig > configurationProperties() const
Returns the configuration properties of the node.
Definition node.cpp:379
NodeSaveInfo saveInfo() const
Returns the savable information for this node.
Definition node.cpp:882
void fitInsideParent(qreal padding)
Fits this node inside its parent with optional padding.
Definition node.cpp:657
virtual QRectF nodeRect() const override
Returns the bounding rectangle for this item.
Definition node.cpp:135
QRectF parentInnerSceneRect(qreal padding) const
Returns the inner scene rectangle of the parent, optionally padded.
Definition node.cpp:586
QVector< std::shared_ptr< IParameter > > properties() const
Returns the properties of the node.
Definition node.cpp:384
virtual QPainterPath shape() const override
Returns the shape of this item.
Definition node.cpp:369
virtual VoidResult start()
Starts the node.
Definition node.cpp:128
void renameNode()
Renames the node to a new name.
Definition node.cpp:437
PropertyInfo getField(const QString &key) const
Gets a property info by key.
Definition node.cpp:494
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *style, QWidget *widget) override
Paints the item using the given painter.
Definition node.cpp:364
Represents a node that can be saved and loaded.
Definition node_info.h:16
Definition port.h:14
Represents a property with configuration and options.
Definition property_info.h:16
Definition canvas_control_widget.h:25
Current size of the node.
Definition node.h:410