qtdeclarative/examples/quick/shapes/interactive.qml

229 lines
7.1 KiB
QML
Raw Normal View History

// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
import QtQuick.Layouts
import QtQuick.Shapes
pragma ComponentBehavior: Bound
Rectangle {
id: root
width: 1024
height: 768
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
color: palette.window
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
RowLayout {
id: topRow
x: 20
y: 10
spacing: 20
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
ButtonGroup {
id: toolButtons
buttons: drawingTools.children
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
RowLayout {
id: drawingTools
ToolButton {
text: qsTr("Line")
checkable: true
checked: true
property Component shapeType: Component {
ShapePath {
id: lineShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: "transparent"
PathLine {
id: pathSegment
x: lineShapePath.startX + 1
y: lineShapePath.startY + 1
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
}
}
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
ToolButton {
text: qsTr("Quadratic")
checkable: true
property Component shapeType: Component {
ShapePath {
id: quadShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: fillSwitch.checked ? "green" : "transparent"
PathQuad {
id: pathSegment
x: quadShapePath.startX + 1
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
y: quadShapePath.startY + 1
controlX: quadShapePath.startX + 50
controlY: quadShapePath.startY + 50
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
pointDragHandle.createObject(canvas, {
idleColor: "blue",
target: pathSegment, xprop: "controlX", yprop: "controlY"
});
}
}
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
ToolButton {
text: qsTr("Cubic")
checkable: true
property Component shapeType: Component {
ShapePath {
id: cubicShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: fillSwitch.checked ? "green" : "transparent"
PathCubic {
id: pathSegment
x: cubicShapePath.startX + 1
y: cubicShapePath.startY + 1
control1X: cubicShapePath.startX + 50;
control1Y: cubicShapePath.startY + 50;
control2X: cubicShapePath.startX + 150;
control2Y: cubicShapePath.startY + 50;
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
pointDragHandle.createObject(canvas, {
idleColor: "blue",
target: pathSegment, xprop: "control1X", yprop: "control1Y"
});
pointDragHandle.createObject(canvas, {
idleColor: "lightBlue",
target: pathSegment, xprop: "control2X", yprop: "control2Y"
});
}
}
}
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Label {
text: qsTr("Width")
}
Slider {
id: widthSlider
from: 1
to: 60
value: 4
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Switch {
id: showHandlesSwitch
text: qsTr("Handles")
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Switch {
id: fillSwitch
text: qsTr("Fill")
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Component {
id: pointDragHandle
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Rectangle {
id: rect
property variant target
property string xprop
property string yprop
property color idleColor: "red"
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
width: 20
height: width
visible: showHandlesSwitch.checked
color: hh.hovered ? "yellow" : idleColor
border.color: "grey"
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
property real halfWidth: width / 2
property bool complete: false
Binding {
target: rect.target
property: rect.xprop
value: rect.x + rect.halfWidth
when: rect.complete
}
Binding {
target: rect.target
property: rect.yprop
value: rect.y + rect.halfWidth
when: rect.complete
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
DragHandler { }
HoverHandler {
id: hh
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Component.onCompleted: {
x = target[xprop] - halfWidth;
y = target[yprop] - halfWidth;
complete = true;
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
function createStartEndHandles(shapePath, pathSegment) {
pointDragHandle.createObject(canvas, {
idleColor: "red",
target: shapePath, xprop: "startX", yprop: "startY"
});
pointDragHandle.createObject(canvas, {
idleColor: "red",
target: pathSegment, xprop: "x", yprop: "y"
});
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
Rectangle {
id: canvas
color: palette.base
width: root.width - 40
height: root.height - y - 20
x: 20
anchors.top: topRow.bottom
anchors.topMargin: 20
DragHandler {
target: null
grabPermissions: DragHandler.TakeOverForbidden
property ShapePath activePath: null
onActiveChanged: {
const tool = toolButtons.checkedButton;
if (active) {
activePath = tool.shapeType.createObject(root, {
startX: centroid.position.x, startY: centroid.position.y
});
shape.data.push(activePath);
} else {
activePath.finishCreation();
activePath = null;
}
}
Shapes interactive example: use Controls canonically; architecture As long as we had the funcs array, it would have been nice to be able to directly get an index from whatever "tool" is selected. For example, TabBar has currentIndex; but TabBar is otherwise not a great fit here. But the funcs array wasn't so elegant anyway. So that prompts switching to the known OO design pattern in which drawing tools are objects containing functions that "do" the drawing, so that there's no conditional dispatch: just ask the tool to handle each mouse state change. I.e. here the DragHandler on the canvas uses the tool's shapeType Component to create the shape, but the DragHandler doesn't have to care which shape it is. Handles for moving the shapes' control points are also shape-agnostic: - a handle is a sort of template taking the path x and y properties to read and write - the handle initializes its position from those properties - the DragHandler inside gets to do what it does best: just drag the handle, nothing else (instead of needing to script the movement of both the handle and the control point, we don't script either one) - declarative bindings on the handle's position update the path properties whenever the handle moves And some styling: - Switch looks better than Button for toggling - use palette colors so that it looks good in dark mode too - fix handle colors getting stuck on yellow - borders on handles (yellow on white makes it hard to see the edge otherwise) - reorder curve types by mathematical order (line, quadratic, cubic) Pick-to: 6.2 Change-Id: Iefd980f428601840deb55370aad9256748855f07 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
2021-09-01 14:04:20 +00:00
onCentroidChanged: if (activePath) {
var pathObj = activePath.pathElements[0];
pathObj.x = centroid.position.x;
pathObj.y = centroid.position.y;
}
}
Shape {
id: shape
anchors.fill: parent
}
}
}