Maki
Formally verfied 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 "node_base.h"
12#include "save_info.h"
13#include "transition.h"
14#include "types.h"
15#include "widgets/controls/canvas_control_widget.h"
16
17class Flow;
18class CanvasMessage;
19class QGraphicsSceneMouseEvent;
20class QGraphicsSceneHoverEvent;
21
25class NodeItem : public NodeBase
26{
27public:
28 enum
29 {
30 Type = Types::NODE
31 };
32
42 NodeItem(const QString& id, std::shared_ptr<NodeSaveInfo> info, const QPointF& initialPosition, std::shared_ptr<NodeConfig> nodeConfig,
43 QGraphicsItem* parent = nullptr);
44
48 virtual ~NodeItem();
49
55 int type() const override;
56
62 VoidResult start() override;
63
69 QRectF nodeRect() const override;
70 QRectF sceneNodeRect() const;
71
72 bool hasControl();
73
74 template <typename T>
75 bool hasControl()
76 {
77 return mControlActive && mControlWidget && qobject_cast<T*>(mControlWidget->controlWidget()) != nullptr;
78 }
79
80 void highlight(const QColor& color, const QString& message, int durationMs = 1000);
81 void dismissHighlight();
82
83 void showSimulationControls(maki::ControlWidget* controls, const QColor& highlightColor);
84 void showControls(maki::ControlWidget* controls, const ControlProperties& properties);
85 void dismissControl();
86 void hideControl();
87
93 QPainterPath shape() const override;
94
102 void paint(QPainter* painter, const QStyleOptionGraphicsItem* style, QWidget* widget) override;
103
109 HelpConfig help() const;
110
116 QString nodeName() const;
117
123 QString nodeType() const;
124
130 QString behaviour() const;
131
137 QVector<ControlsConfig> controls() const;
138
144 QVector<std::shared_ptr<IParameter>> properties() const;
145
151 QVector<PropertyConfig> configurationProperties() const;
152
158 Types::LibraryTypes function() const;
159
166 const PropertyInfo* getProperty(const QString& key) const;
167
174 void setProperty(const QString& key, const maki::Value& value);
175
181 void renameNode(const QString& name);
182
188 NodeItem* parentNode() const;
189
195 QVector<NodeItem*> children() const;
196
203 QPointF edgePointToward(const QPointF& targetScenePos) const;
204
210 QVector<std::shared_ptr<IParameter>> fields() const;
211
218 PropertyInfo getField(const QString& key) const;
219
227 VoidResult setField(const QString& key, const QJsonObject& property);
228
236 VoidResult setField(const QString& key, std::shared_ptr<PropertyInfo> property);
237
243 void removeField(const QString& key);
244
251 void setEvent(int index, const FlowConfig& event);
252
258 QVector<std::shared_ptr<IFlow>> flowConfigs() const;
259
260 QVector<std::shared_ptr<IFlow>> eventConfigs() const;
261
262 QVector<std::shared_ptr<IFlow>> allEventFlowConfigs() const;
263
269 void addParent(NodeItem* node);
270
277 void addChild(NodeItem* node, std::shared_ptr<NodeSaveInfo> info);
278
284 void childRemoved(NodeItem* child);
285
291 QVector<Flow*> flows() const;
292
300 Flow* createFlow(const QString& flowName, std::shared_ptr<FlowSaveInfo> info);
301
308 Flow* getFlow(const QString& flowId) const;
309
315 void deleteFlow(const QString& flowId);
316
320 void updateFlow();
321
327 qreal baseScale() const;
328
334 QVector<TransitionConfig> configTransitions() const;
335
341 void applySize(const QSizeF& size);
342
348 void updatePosition(const QPointF& position);
349
350 // "signals":
351 std::function<void(NodeItem* item)> nodeModified;
352 std::function<void(Flow* flow, NodeItem* item)> flowAdded;
353 std::function<void(NodeItem* item)> nodeMoved;
354 std::function<void(NodeItem* item, bool enter)> nodeHovered;
355
356 // "slots":
357 void onProperties();
358
359 // Serialization functions
360
366 NodeSaveInfo saveInfo() const;
374 friend QDataStream& operator<<(QDataStream& out, const NodeItem& config);
375
383 friend QDataStream& operator>>(QDataStream& in, NodeItem& config);
384
385protected:
391 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
392
398 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
399
405 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
406
407 void hoverEnterEvent(QGraphicsSceneHoverEvent* event) override;
408 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
409
417 QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;
418
419private:
420 std::shared_ptr<NodeSaveInfo> mStorage;
421
422 QVector<Flow*> mFlows;
424 QVector<NodeItem*> mChildrenNodes;
426 CanvasControlWidget* mControlWidget = nullptr;
427 bool mControlActive = false;
428
429 qreal mBaseScale;
430 QSizeF mSize{0, 0};
431 QPointF mDragStartPos{0, 0};
432 QPointF mLastPosition{0, 0};
433
434 bool mIsResizing{false};
435 QPointF mResizeStartMousePos{0, 0};
436 QSizeF mResizeStartSize{0, 0};
437
442
450 QSizeF clampSize(qreal width, qreal height) const;
451
459 QPointF clampPosInside(const QRectF& inner, const QRectF& childSceneRect) const;
460
466 void fitInsideParent(qreal padding);
467
474 QRectF parentInnerSceneRect(qreal padding) const;
475
476 void ensureMainFlowExists();
477};
Definition canvas_control_widget.h:35
Definition canvas_message.h:8
Configuration for a flow or process.
Definition config.h:70
Represents a flow in the system.
Definition flow.h:17
Configuration for help information.
Definition config.h:147
virtual std::shared_ptr< NodeConfig > config() const
Retrieves the configuration settings for the node.
Definition node_base.cpp:57
NodeBase(const QString &id, const QString &nodeId, std::shared_ptr< NodeConfig > config, QGraphicsItem *parent)
Constructs a new NodeBase instance.
Definition node_base.cpp:19
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse release events for this item.
Definition node.cpp:624
Types::LibraryTypes function() const
Returns the library type associated with this node's function.
Definition node.cpp:90
int type() const override
Returns the type of this item.
Definition node.cpp:85
Flow * getFlow(const QString &flowId) const
Gets a flow by its ID.
Definition node.cpp:789
friend QDataStream & operator<<(QDataStream &out, const NodeItem &config)
Saves the state of this NodeItem to a QDataStream.
void updateExtrasPosition()
Size of the node when resizing started.
Definition node.cpp:710
void setProperty(const QString &key, const maki::Value &value)
Sets a property with a given key and value.
Definition node.cpp:372
friend QDataStream & operator>>(QDataStream &in, NodeItem &config)
Loads the state of a NodeItem from a QDataStream.
QSizeF mSize
Base scale for the node.
Definition node.h:430
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Handles changes to the item's properties.
Definition node.cpp:659
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse press events for this item.
Definition node.cpp:607
QVector< std::shared_ptr< IParameter > > fields() const
Returns a list of fields associated with this node.
Definition node.cpp:339
bool mIsResizing
Last known position of the node.
Definition node.h:434
CanvasMessage * mHighlightMessage
List of child nodes.
Definition node.h:425
VoidResult start() override
Starts the node.
Definition node.cpp:114
QPointF edgePointToward(const QPointF &targetScenePos) const
Calculates an edge point toward a target scene position.
Definition node.cpp:727
QPointF mDragStartPos
Current size of the node.
Definition node.h:431
QPointF mResizeStartMousePos
Flag indicating if the node is being resized.
Definition node.h:435
void addParent(NodeItem *node)
Adds a parent node to this item.
Definition node.cpp:441
const PropertyInfo * getProperty(const QString &key) const
Gets a property by key.
Definition node.cpp:364
QString nodeName() const
Returns the name of the node.
Definition node.cpp:95
QVector< TransitionConfig > configTransitions() const
Returns a list of configured transitions.
Definition node.cpp:741
QPointF mLastPosition
Position where dragging started.
Definition node.h:432
void applySize(const QSizeF &size)
Applies a new size to the node.
Definition node.cpp:493
void deleteFlow(const QString &flowId)
Deletes a flow by its ID.
Definition node.cpp:798
QPointF clampPosInside(const QRectF &inner, const QRectF &childSceneRect) const
Clamps a position inside an inner rectangle.
Definition node.cpp:514
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse move events for this item.
Definition node.cpp:581
NodeItem * parentNode() const
Returns the parent node of this item.
Definition node.cpp:468
QSizeF clampSize(qreal width, qreal height) const
Clamps a size within valid limits.
Definition node.cpp:532
HelpConfig help() const
Returns help configuration for this node.
Definition node.cpp:478
QVector< std::shared_ptr< IFlow > > flowConfigs() const
Returns a list of events associated with this node.
Definition node.cpp:349
QVector< NodeItem * > mChildrenNodes
Parent node of this item, if any.
Definition node.h:424
NodeItem * mParentNode
List of flows associated with this node.
Definition node.h:423
void childRemoved(NodeItem *child)
Handles when a child is removed from this item.
Definition node.cpp:462
QString behaviour() const
Returns the behavior of the node.
Definition node.cpp:473
void renameNode(const QString &name)
Renames the node to a new name.
VoidResult setField(const QString &key, const QJsonObject &property)
Sets a field with a given key and JSON object.
Definition node.cpp:400
void addChild(NodeItem *node, std::shared_ptr< NodeSaveInfo > info)
Adds a child node with the given save information.
Definition node.cpp:453
void removeField(const QString &key)
Removes a field by key.
Definition node.cpp:428
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:26
qreal baseScale() const
Returns the base scale for this node.
Definition node.cpp:109
QVector< ControlsConfig > controls() const
Returns the controls configuration for this node.
Definition node.cpp:359
QSizeF mResizeStartSize
Mouse position when resizing started.
Definition node.h:436
QVector< NodeItem * > children() const
Returns a list of child nodes.
Definition node.cpp:436
QString nodeType() const
Returns the type of the node.
Definition node.cpp:103
void setEvent(int index, const FlowConfig &event)
Sets an event at the given index with the provided FlowConfig.
Definition node.cpp:389
QVector< PropertyConfig > configurationProperties() const
Returns the configuration properties of the node.
Definition node.cpp:329
NodeSaveInfo saveInfo() const
Returns the savable information for this node.
Definition node.cpp:722
void fitInsideParent(qreal padding)
Fits this node inside its parent with optional padding.
Definition node.cpp:555
QRectF nodeRect() const override
Returns the bounding rectangle for this item.
Definition node.cpp:122
QVector< Flow * > mFlows
Save information for the node.
Definition node.h:422
QRectF parentInnerSceneRect(qreal padding) const
Returns the inner scene rectangle of the parent, optionally padded.
Definition node.cpp:483
QVector< std::shared_ptr< IParameter > > properties() const
Returns the properties of the node.
Definition node.cpp:334
QPainterPath shape() const override
Returns the shape of this item.
Definition node.cpp:324
void updateFlow()
Updates all flows associated with this node.
Definition node.cpp:804
QVector< Flow * > flows() const
Returns a list of flows associated with this node.
Definition node.cpp:746
PropertyInfo getField(const QString &key) const
Gets a property info by key.
Definition node.cpp:420
void paint(QPainter *painter, const QStyleOptionGraphicsItem *style, QWidget *widget) override
Paints the item using the given painter.
Definition node.cpp:315
void updatePosition(const QPointF &position)
Updates the position of the node.
Definition node.cpp:694
Flow * createFlow(const QString &flowName, std::shared_ptr< FlowSaveInfo > info)
Creates a new flow with the given name and save information.
Definition node.cpp:751
Represents a node that can be saved and loaded.
Definition node_info.h:17
Represents a property with configuration and options.
Definition property_info.h:16
Definition canvas_control_widget.h:25