loom

Форк
0
294 строки · 6.6 Кб
1
#include "SimpleGraphModel.hpp"
2

3
SimpleGraphModel::SimpleGraphModel()
4
    : _nextNodeId{0}
5
{}
6

7
SimpleGraphModel::~SimpleGraphModel()
8
{
9
    //
10
}
11

12
std::unordered_set<NodeId> SimpleGraphModel::allNodeIds() const
13
{
14
    return _nodeIds;
15
}
16

17
std::unordered_set<ConnectionId> SimpleGraphModel::allConnectionIds(NodeId const nodeId) const
18
{
19
    std::unordered_set<ConnectionId> result;
20

21
    std::copy_if(_connectivity.begin(),
22
                 _connectivity.end(),
23
                 std::inserter(result, std::end(result)),
24
                 [&nodeId](ConnectionId const &cid) {
25
                     return cid.inNodeId == nodeId || cid.outNodeId == nodeId;
26
                 });
27

28
    return result;
29
}
30

31
std::unordered_set<ConnectionId> SimpleGraphModel::connections(NodeId nodeId,
32
                                                               PortType portType,
33
                                                               PortIndex portIndex) const
34
{
35
    std::unordered_set<ConnectionId> result;
36

37
    std::copy_if(_connectivity.begin(),
38
                 _connectivity.end(),
39
                 std::inserter(result, std::end(result)),
40
                 [&portType, &portIndex, &nodeId](ConnectionId const &cid) {
41
                     return (getNodeId(portType, cid) == nodeId
42
                             && getPortIndex(portType, cid) == portIndex);
43
                 });
44

45
    return result;
46
}
47

48
bool SimpleGraphModel::connectionExists(ConnectionId const connectionId) const
49
{
50
    return (_connectivity.find(connectionId) != _connectivity.end());
51
}
52

53
NodeId SimpleGraphModel::addNode(QString const )
54
{
55
    NodeId newId = newNodeId();
56
    // Create new node.
57
    _nodeIds.insert(newId);
58

59
    Q_EMIT nodeCreated(newId);
60

61
    return newId;
62
}
63

64
bool SimpleGraphModel::connectionPossible(ConnectionId const connectionId) const
65
{
66
    return _connectivity.find(connectionId) == _connectivity.end();
67
}
68

69
void SimpleGraphModel::addConnection(ConnectionId const connectionId)
70
{
71
    _connectivity.insert(connectionId);
72

73
    Q_EMIT connectionCreated(connectionId);
74
}
75

76
bool SimpleGraphModel::nodeExists(NodeId const nodeId) const
77
{
78
    return (_nodeIds.find(nodeId) != _nodeIds.end());
79
}
80

81
QVariant SimpleGraphModel::nodeData(NodeId nodeId, NodeRole role) const
82
{
83
    Q_UNUSED(nodeId);
84

85
    QVariant result;
86

87
    switch (role) {
88
    case NodeRole::Type:
89
        result = QString("Default Node Type");
90
        break;
91

92
    case NodeRole::Position:
93
        result = _nodeGeometryData[nodeId].pos;
94
        break;
95

96
    case NodeRole::Size:
97
        result = _nodeGeometryData[nodeId].size;
98
        break;
99

100
    case NodeRole::CaptionVisible:
101
        result = true;
102
        break;
103

104
    case NodeRole::Caption:
105
        result = QString("Node");
106
        break;
107

108
    case NodeRole::Style: {
109
        auto style = StyleCollection::nodeStyle();
110
        result = style.toJson().toVariantMap();
111
    } break;
112

113
    case NodeRole::InternalData:
114
        break;
115

116
    case NodeRole::InPortCount:
117
        result = 1u;
118
        break;
119

120
    case NodeRole::OutPortCount:
121
        result = 1u;
122
        break;
123

124
    case NodeRole::Widget:
125
        result = QVariant();
126
        break;
127
    }
128

129
    return result;
130
}
131

132
bool SimpleGraphModel::setNodeData(NodeId nodeId, NodeRole role, QVariant value)
133
{
134
    bool result = false;
135

136
    switch (role) {
137
    case NodeRole::Type:
138
        break;
139
    case NodeRole::Position: {
140
        _nodeGeometryData[nodeId].pos = value.value<QPointF>();
141

142
        Q_EMIT nodePositionUpdated(nodeId);
143

144
        result = true;
145
    } break;
146

147
    case NodeRole::Size: {
148
        _nodeGeometryData[nodeId].size = value.value<QSize>();
149
        result = true;
150
    } break;
151

152
    case NodeRole::CaptionVisible:
153
        break;
154

155
    case NodeRole::Caption:
156
        break;
157

158
    case NodeRole::Style:
159
        break;
160

161
    case NodeRole::InternalData:
162
        break;
163

164
    case NodeRole::InPortCount:
165
        break;
166

167
    case NodeRole::OutPortCount:
168
        break;
169

170
    case NodeRole::Widget:
171
        break;
172
    }
173

174
    return result;
175
}
176

177
QVariant SimpleGraphModel::portData(NodeId ,
178
                                    PortType portType,
179
                                    PortIndex ,
180
                                    PortRole role) const
181
{
182
    switch (role) {
183
    case PortRole::Data:
184
        return QVariant();
185
        break;
186

187
    case PortRole::DataType:
188
        return QVariant();
189
        break;
190

191
    case PortRole::ConnectionPolicyRole:
192
        return QVariant::fromValue(ConnectionPolicy::One);
193
        break;
194

195
    case PortRole::CaptionVisible:
196
        return true;
197
        break;
198

199
    case PortRole::Caption:
200
        if (portType == PortType::In)
201
            return QString::fromUtf8("Port In");
202
        else
203
            return QString::fromUtf8("Port Out");
204

205
        break;
206
    }
207

208
    return QVariant();
209
}
210

211
bool SimpleGraphModel::setPortData(
212
    NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value, PortRole role)
213
{
214
    Q_UNUSED(nodeId);
215
    Q_UNUSED(portType);
216
    Q_UNUSED(portIndex);
217
    Q_UNUSED(value);
218
    Q_UNUSED(role);
219

220
    return false;
221
}
222

223
bool SimpleGraphModel::deleteConnection(ConnectionId const connectionId)
224
{
225
    bool disconnected = false;
226

227
    auto it = _connectivity.find(connectionId);
228

229
    if (it != _connectivity.end()) {
230
        disconnected = true;
231

232
        _connectivity.erase(it);
233
    }
234

235
    if (disconnected)
236
        Q_EMIT connectionDeleted(connectionId);
237

238
    return disconnected;
239
}
240

241
bool SimpleGraphModel::deleteNode(NodeId const nodeId)
242
{
243
    // Delete connections to this node first.
244
    auto connectionIds = allConnectionIds(nodeId);
245

246
    for (auto &cId : connectionIds) {
247
        deleteConnection(cId);
248
    }
249

250
    _nodeIds.erase(nodeId);
251
    _nodeGeometryData.erase(nodeId);
252

253
    Q_EMIT nodeDeleted(nodeId);
254

255
    return true;
256
}
257

258
QJsonObject SimpleGraphModel::saveNode(NodeId const nodeId) const
259
{
260
    QJsonObject nodeJson;
261

262
    nodeJson["id"] = static_cast<qint64>(nodeId);
263

264
    {
265
        QPointF const pos = nodeData(nodeId, NodeRole::Position).value<QPointF>();
266

267
        QJsonObject posJson;
268
        posJson["x"] = pos.x();
269
        posJson["y"] = pos.y();
270
        nodeJson["position"] = posJson;
271
    }
272

273
    return nodeJson;
274
}
275

276
void SimpleGraphModel::loadNode(QJsonObject const &nodeJson)
277
{
278
    NodeId restoredNodeId = static_cast<NodeId>(nodeJson["id"].toInt());
279

280
    // Next NodeId must be larger that any id existing in the graph
281
    _nextNodeId = std::max(_nextNodeId, restoredNodeId + 1);
282

283
    // Create new node.
284
    _nodeIds.insert(restoredNodeId);
285

286
    Q_EMIT nodeCreated(restoredNodeId);
287

288
    {
289
        QJsonObject posJson = nodeJson["position"].toObject();
290
        QPointF const pos(posJson["x"].toDouble(), posJson["y"].toDouble());
291

292
        setNodeData(restoredNodeId, NodeRole::Position, pos);
293
    }
294
}
295

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.