Skip to content

Commit

Permalink
Add ConnectionsCanvas as a separate QML file
Browse files Browse the repository at this point in the history
The existing logic for managing and painting connections has been abstracted into a new QML file, ConnectionsCanvas.qml. The Main.qml file has been updated to use this new component. Additionally, unnecessary whitespaces and
  • Loading branch information
xorza committed Jun 8, 2024
1 parent 25b4777 commit 5c994fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
44 changes: 44 additions & 0 deletions ScenariumEditor.QML/qml/ConnectionsCanvas.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import QtQuick
import com.cssodessa.AppController
import com.cssodessa.NodeController
import com.cssodessa.ConnectionController


Canvas {
id: canvas

property AppController appController

function repaintConnections() {
canvas.requestPaint()
}

anchors.fill: parent
anchors.margins: 10
contextType: "2d"

onPaint: {
context.save()
context.clearRect(0, 0, width, height)

context.strokeStyle = "orange"
context.lineWidth = 3

for (let i = 0; i < appController.connections.length; i++) {
const connection = appController.connections[i];
const output = connection.source.outputs[connection.outputIdx];
const input = connection.target.inputs[connection.inputIdx];

context.beginPath()
context.moveTo(output.viewPos.x, output.viewPos.y)
context.bezierCurveTo(
output.viewPos.x + 40, output.viewPos.y,
input.viewPos.x - 40, input.viewPos.y,
input.viewPos.x, input.viewPos.y
)
context.stroke()
}

context.restore()
}
}
36 changes: 5 additions & 31 deletions ScenariumEditor.QML/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,15 @@ Window {

onAfterSynchronizing: {
AppController.afterSynchronizing()
connectionCanvas.requestPaint()
connectionCanvas.repaintConnections()
}


Canvas {
ConnectionsCanvas {
id: connectionCanvas

anchors.fill: parent
anchors.margins: 10
contextType: "2d"

onPaint: {
context.save()
context.clearRect(0, 0, width, height)

context.strokeStyle = "green"
context.lineWidth = 3


for (var i = 0; i < AppController.connections.length; i++) {
var connection = AppController.connections[i]
var output = connection.source.outputs[connection.outputIdx]
var input = connection.target.inputs[connection.inputIdx]

context.beginPath()
context.moveTo(output.viewPos.x, output.viewPos.y)
context.bezierCurveTo(
output.viewPos.x + 40, output.viewPos.y,
input.viewPos.x - 40, input.viewPos.y,
input.viewPos.x, input.viewPos.y
)
context.stroke()
}

context.restore()
}
appController: AppController
}

Item {
Expand All @@ -61,7 +34,8 @@ Window {
nodeController: modelData

onViewPosChanged: {
connectionCanvas.requestPaint()
// connectionCanvas.requestPaint()
connectionCanvas.repaintConnections()
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions ScenariumEditor.QML/qml/Node.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.cssodessa.NodeController

Rectangle {
property NodeController nodeController

signal viewPosChanged()

id: root
Expand Down Expand Up @@ -62,11 +63,6 @@ Rectangle {
viewPosChanged()
}
}
onPressed: {
}
onReleased: {

}
}
}

Expand Down

0 comments on commit 5c994fc

Please sign in to comment.