| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef NODESMODEL_H |
| 5 | #define NODESMODEL_H |
| 6 | |
| 7 | #include <QObject> |
| 8 | #include <QAbstractListModel> |
| 9 | #include <QtQml/qqmlregistration.h> |
| 10 | #include "uniformmodel.h" |
| 11 | |
| 12 | class NodesModel : public QAbstractListModel |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged) |
| 16 | |
| 17 | public: |
| 18 | enum NodeType { |
| 19 | SourceNode = 0, |
| 20 | DestinationNode, |
| 21 | CustomNode |
| 22 | }; |
| 23 | struct Node { |
| 24 | int type = CustomNode; |
| 25 | // Id of the model, must be unique! |
| 26 | int nodeId = -1; |
| 27 | float x = 0; |
| 28 | float y = 0; |
| 29 | float width = 0; |
| 30 | float height = 0; |
| 31 | QString name; |
| 32 | bool selected = false; |
| 33 | bool disabled = false; |
| 34 | // These are helper positions when moving starts |
| 35 | float startX = 0; |
| 36 | float startY = 0; |
| 37 | // This points to next connected node. -1 when not connected. |
| 38 | int nextNodeId = -1; |
| 39 | |
| 40 | // Unforms from the node JSON |
| 41 | // Note: These are not updated when the uniforms/properties change, |
| 42 | // so don't use this to other things than checking JSON uniforms. |
| 43 | QList<UniformModel::Uniform> jsonUniforms = {}; |
| 44 | QString fragmentCode = {}; |
| 45 | QString vertexCode = {}; |
| 46 | QString qmlCode = {}; |
| 47 | QString description = {}; |
| 48 | bool operator==(const Node& rhs) const noexcept |
| 49 | { |
| 50 | return this->nodeId == rhs.nodeId; |
| 51 | } |
| 52 | bool operator!=(const Node& rhs) const noexcept |
| 53 | { |
| 54 | return !operator==(rhs); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | enum NodesModelRoles { |
| 59 | Type = Qt::UserRole + 1, |
| 60 | Name, |
| 61 | X, |
| 62 | Y, |
| 63 | Width, |
| 64 | Height, |
| 65 | Selected, |
| 66 | NodeId, |
| 67 | Description, |
| 68 | Disabled |
| 69 | }; |
| 70 | |
| 71 | explicit NodesModel(QObject *parent = nullptr); |
| 72 | |
| 73 | // Override from QAbstractItemModel |
| 74 | int rowCount(const QModelIndex & = QModelIndex()) const final; |
| 75 | QVariant data(const QModelIndex &index, int role) const final; |
| 76 | bool setData(const QModelIndex &index, const QVariant &value, int role) final; |
| 77 | QHash<int, QByteArray> roleNames() const final; |
| 78 | |
| 79 | Node *getNodeWithId(int id); |
| 80 | void setSelectedNode(Node *node); |
| 81 | |
| 82 | signals: |
| 83 | void selectedNodeChanged(); |
| 84 | void rowCountChanged(); |
| 85 | |
| 86 | private: |
| 87 | friend class NodeView; |
| 88 | friend class EffectManager; |
| 89 | |
| 90 | QList<Node> m_nodesList; |
| 91 | Node m_emptyNode; |
| 92 | Node *m_selectedNode = nullptr; |
| 93 | }; |
| 94 | |
| 95 | #endif // NODESMODEL_H |
| 96 | |