loom
59 строк · 1.7 Кб
1#include <QtNodes/BasicGraphicsScene>
2#include <QtNodes/ConnectionStyle>
3#include <QtNodes/GraphicsView>
4#include <QtNodes/StyleCollection>
5
6#include <QAction>
7#include <QScreen>
8#include <QtWidgets/QApplication>
9
10#include "SimpleGraphModel.hpp"
11
12using QtNodes::BasicGraphicsScene;
13using QtNodes::ConnectionStyle;
14using QtNodes::GraphicsView;
15using QtNodes::NodeRole;
16using QtNodes::StyleCollection;
17
18int main(int argc, char *argv[])
19{
20QApplication app(argc, argv);
21
22SimpleGraphModel graphModel;
23
24// Initialize and connect two nodes.
25{
26NodeId id1 = graphModel.addNode();
27graphModel.setNodeData(id1, NodeRole::Position, QPointF(0, 0));
28
29NodeId id2 = graphModel.addNode();
30graphModel.setNodeData(id2, NodeRole::Position, QPointF(300, 300));
31
32graphModel.addConnection(ConnectionId{id1, 0, id2, 0});
33}
34
35auto scene = new BasicGraphicsScene(graphModel);
36
37GraphicsView view(scene);
38
39// Setup context menu for creating new nodes.
40view.setContextMenuPolicy(Qt::ActionsContextMenu);
41QAction createNodeAction(QStringLiteral("Create Node"), &view);
42QObject::connect(&createNodeAction, &QAction::triggered, [&]() {
43// Mouse position in scene coordinates.
44QPointF posView = view.mapToScene(view.mapFromGlobal(QCursor::pos()));
45
46NodeId const newId = graphModel.addNode();
47graphModel.setNodeData(newId, NodeRole::Position, posView);
48});
49view.insertAction(view.actions().front(), &createNodeAction);
50
51view.setWindowTitle("Simple Node Graph");
52view.resize(800, 600);
53
54// Center window.
55view.move(QApplication::primaryScreen()->availableGeometry().center() - view.rect().center());
56view.showNormal();
57
58return app.exec();
59}
60