QmlNodeEditor
ConnectionComponent.qml
Go to the documentation of this file.
1 import QtQuick 2.15
2 import QtQuick.Controls 2.15
3 
4 Item {
5  id: connectionComponent
6  anchors.fill: parent
7 
8  required property Item from
9  required property Item to
10  required property Component connectionShape
11  required property QtObject style
12  required property bool removable
13 
14  property Item fromNodeRoot
15  property Item toNodeRoot
16 
17  Loader {
18  id: connectionShapeLoader
19  anchors.fill: parent
20 
21  property real startX: 0
22  property real startY: 0
23  property real endX: 0
24  property real endY: 0
25  property QtObject style: parent.style
26  property string state: parent.state // "focused", "hovered"
27 
28  property point fromPortPosition // Port side on the Node, fromPortPosition.x === -1 if port is on the left side, 1 on the right
29  property point toPortPosition // To Port side on the Node, toPortPosition.x === -1 if port is on the left side, 1 on the right
30 
31  sourceComponent: connectionShape
32 
33  Binding on fromPortPosition {
34  value: if (from) from.relativeConnectorX < 0 ? "-1, 0" : "1, 0"
35  when: from
36  }
37 
38  Binding on toPortPosition {
39  value: if (to) to.relativeConnectorX < 0 ? "-1, 0" : "1, 0"
40  when: to
41  }
42  }
43 
44  Binding on fromNodeRoot {
45  value: if (from) from.getNodeRoot()
46  when: from
47  }
48 
49  Binding on toNodeRoot {
50  value: if (to) to.getNodeRoot()
51  when: to
52  }
53 
54  containmentMask: connectionShapeLoader.item
55  focus: true
56  activeFocusOnTab: true
57 
58  states: [
59  State {
60  when: activeFocus
61  name: "focused"
62  },
63  State {
64  when: hover.hovered
65  name: "hovered"
66  }
67  ]
68 
69  HoverHandler {
70  id: hover
71  enabled: connectionComponent.state !== "focused"
72  }
73 
74  TapHandler {
75  enabled: connectionComponent.state !== "focused"
76  acceptedButtons: Qt.LeftButton
77  onTapped: forceActiveFocus();
78  }
79 
80  Keys.onPressed: (event) => {
81  if (connectionComponent.removable && event.key === nodeView.deleteKey) {
82  showConnectionDeleteDialog();
83  event.accepted = true;
84  }
85  }
86 
87  Loader {
88  id: connectionDeleteConfirmationDialogLoader
89  property alias from: connectionComponent.from
90  property alias to: connectionComponent.to
91  sourceComponent: nodeView.connectionDeleteConfirmationDialog
92  }
93  Connections {
94  target: connectionDeleteConfirmationDialogLoader.item
95  function onAccepted() {
96  connectionDeleteConfirmationDialogLoader.active = false;
97  nodeView.connectionRemoved(connectionComponent.from.nodeId, connectionComponent.from.portId,
98  connectionComponent.to.nodeId, connectionComponent.to.portId);
99  }
100  }
101  function showConnectionDeleteDialog() {
102  connectionDeleteConfirmationDialogLoader.item.open();
103  }
104 
105  function updateFromX() {
106  connectionShapeLoader.startX = fromNodeRoot.x + from.relativeConnectorX;
107  }
108  function updateFromY() {
109  connectionShapeLoader.startY = fromNodeRoot.y + from.relativeConnectorY;
110  }
111  function updateToX() {
112  connectionShapeLoader.endX = toNodeRoot.x + to.relativeConnectorX;
113  }
114  function updateToY() {
115  connectionShapeLoader.endY = toNodeRoot.y + to.relativeConnectorY;
116  }
117  function updateAll() {
118  updateFromX();
119  updateFromY();
120  updateToX();
121  updateToY();
122  }
123 
124  Connections {
125  target: fromNodeRoot
126 
127  function onXChanged() {
128  updateFromX();
129  }
130 
131  function onYChanged() {
132  updateFromY();
133  }
134  }
135 
136  Connections {
137  target: toNodeRoot
138 
139  function onXChanged() {
140  updateToX();
141  }
142 
143  function onYChanged() {
144  updateToY();
145  }
146  }
147 
148  Connections {
149  target: from
150 
151  function onRelativeConnectorXChanged() {
152  updateFromX();
153  }
154 
155  function onRelativeConnectorYChanged() {
156  updateFromY();
157  }
158  }
159 
160  Connections {
161  target: to
162 
163  function onRelativeConnectorXChanged() {
164  updateToX();
165  }
166 
167  function onRelativeConnectorYChanged() {
168  updateToY();
169  }
170  }
171 
172  Component.onCompleted: {
173  updateAll();
174  }
175 }